export function tokenizeDefinition ( functionDefinition ) { const definitionPattern = /(?:def|class)\s+\w+\s*\(([\s\S]*)\)\s*(->\s*(["']?)[\w\[\], \.]*\3)?:\s*(?:#.*)?$/ ; const match = definitionPattern . exec ( functionDefinition ) ; if ( match == undefined || match [ 1 ] == undefined ) { return [ ] ; } const tokens = tokenizeParameterString ( match [ 1 ] ) ; if ( match [ 2 ] != undefined ) { tokens . push ( match [ 2 ] ) ; } return tokens ; } function tokenizeParameterString ( parameterString ) { const stack = [ ] ; const parameters = [ ] ; let arg = "" ; let position = parameterString . length - 1 ; while ( position >= 0 ) { const top = stack [ stack . length - 1 ] ; const char = parameterString . charAt ( position ) ; switch ( true ) { case char === "," && stack . length === 0 : parameters . unshift ( arg ) ; arg = "" ; position -= 1 ; continue ; case char === '"' && top === '"' : case char === "'" && top === "'" : stack . pop ( ) ; break ; case top === '"' : case top === "'" : break ; case char === '"' : case char === "'" : stack . push ( char ) ; break ; case char === "(" && top === ")" : case char === "[" && top === "]" : case char === "{" && top === "}" : stack . pop ( ) ; break ; case char === "(" : case char === "[" : case char === "{" : break ; case char === ")" : case char === "]" : case char === "}" : stack . push ( char ) ; break ; case char === " " && stack . length === 0 : case char === "\n" && stack . length === 0 : case char === "\t" && stack . length === 0 : position -= 1 ; continue ; } arg = char + arg ; position -= 1 ; } if ( arg . length > 0 ) { parameters . unshift ( arg ) ; } return parameters ; }