def main(argv): input_stream = FileStream(argv[1]) lexer = JavaScriptLexer(input_stream) stream = CommonTokenStream(lexer) parser = JavaScriptParser(stream) tree = parser.program() visitor = AstVisitor() visitor.visit(tree=tree) print(visitor.tree)
def parse_js_deep(file): Command = "ctags -f - --kinds-javascript=* --fields=neK " + file global delimiter delimiter = "\r\0?\r?\0\r" functionInstanceList = [] try: astString = subprocess.check_output(Command, stderr=subprocess.STDOUT, shell=True).decode() except subprocess.CalledProcessError as e: print("Parser Error:", e) astString = "" f = open(file, 'r') lines = f.readlines() functionList = astString.split('\n') func = re.compile(r'(function)') method = re.compile(r'(method)') number = re.compile(r'(\d+)') new_line = re.compile(r'(\n)') funcB = pcre.compile(r'function[^{]+({(?:[^{}]+|(?-1))*+})') string = " " funcId = 1 lines_count = 0 for i in functionList: elemList = re.sub(r'[\t\s ]{2,}', '', i) elemList = elemList.split("\t") functionInstance = function(file) functionInstance.funcBody = '' if i != '' and len(elemList) >= 5 and (func.fullmatch(elemList[3]) or method.fullmatch(elemList[3])): functionInstance.name = elemList[0] functionInstance.parentFile = elemList[1] functionInstance.parentNumLoc = len(lines) string = " " string = string.join( lines[int(number.search(elemList[4]).group(0)) - 1:]) if funcB.search(string): functionInstance.funcBody = functionInstance.funcBody + funcB.search( string).group(1)[1:-1] else: functionInstance.funcBody = " " functionInstance.lines = ( int(number.search(elemList[4]).group(0)), int(number.search(elemList[4]).group(0)) + functionInstance.funcBody.count("\n")) functionInstance.funcId = funcId funcId += 1 #For function parameters text = "" text = text.join(lines[functionInstance.lines[0] - 1:functionInstance.lines[1]]) input = InputStream(text) lexer = JavaScriptLexer(input) tokens = CommonTokenStream(lexer) parser = JavaScriptParser(tokens) tree = parser.functionBody() listener = JavaScriptParserListener() listener.variables = [] listener.identifiers = [] listener.parameters = [] walker = ParseTreeWalker() walker.walk(listener, tree) for i in listener.identifiers: funccall = re.compile("" + str(i) + "\(") if funccall.search(text): functionInstance.funcCalleeList.append(i) functionInstance.parameterList = listener.parameters functionInstance.variableList = listener.variables functionInstanceList.append(functionInstance) return functionInstanceList
def displayRecognitionError(self, tokens, e): self.lex_errors__.append(e) JavaScriptLexer.displayRecognitionError(self, tokens, e)
def __init__(self, tokens): JavaScriptLexer.__init__(self, tokens) self.lex_errors__ = []