abstract-syntax-tree

Python code reflection and modification

别来无恙 提交于 2021-01-27 14:50:35
问题 Perhaps I'm not using the proper term, but I'm looking to take a block of Python code (in Python), get the token tree for the code, make some kind of modification, and re-assemble it to Python. For instance, consider this block of code: def x(y): b = 2 if y == b: foo(y) I would like to be able to take this and programmatically do this: def x(y): b = 2 if y == b: bar(y) I can't imagine that there's not a library that does something like this. Thanks in advance. EDIT Perhaps I wasn't entirely

How do you get inferred type from a typescript AST?

杀马特。学长 韩版系。学妹 提交于 2020-12-29 13:20:08
问题 I'm using the built-in parser to generate the AST from source code: const ts = require('typescript') //... const ast = ts.createSourceFile(filename, fs.readFileSync(filename).toString(), ts.ScriptTarget.ES6, true) Is there a way to get the inferred type of a variable from the AST? For example, in the code below, bar is of type IBar . The compiler knows about that type--- bar.foo() doesn't compile---how can I programmatically get the type? interface IBar { bar() } const foo : IBar = //...

is there any way to revert back the AST tree from the dump file of the tree?

≯℡__Kan透↙ 提交于 2020-12-13 07:46:11
问题 c is a parsed tree of a python code: c=ast.parse(''' for x in y: print(x) ''') d is the dump file of tree-c d=ast.dump(c) the content of d is the following string: Module( body=[For(target=Name(id='x', ctx=Store()), iter=Name(id='y', ctx=Load()), body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Name(id='x', ctx=Load())], keywords=[]))], orelse=[])]) I've looked around the net to see if I could find a method to use the content of d, to revert back to the tree c. Any suggestions?

Finding all XPaths in a XQuery using Saxon-HE with C#

 ̄綄美尐妖づ 提交于 2020-12-07 04:57:26
问题 Situational Background: XSD with SCH XML Schema (XSD) I have an XML schema definition ("the schema") that includes several other XSDs, all in the same namespace. Some of those import other XSDs from foreign namespaces. All in all, the schema declares several global elements that can be instantiated as XML documents. Let's call them Global_1 , Global_2 and Global_3 . Business Rules (SCH) The schema is augmented by a Schematron file that defines the "business rules". It defines a number of

ANTLR Visitor Class is returning null for Parse tree in C#

时光总嘲笑我的痴心妄想 提交于 2020-07-23 08:24:10
问题 I am trying to build an AST using the visitor pattern on ANTLR Java Grammar (Java Grammar) in C# (.Net Core 3.1). I have created IJavaParserVisitor , JavaParserbaseVisitor , JavaLexer , and JavaParser file for grammar and created parse tree for java source file. But when I am trying to create AST using JavaParserBaseVisitor.Visit() I am getting null as a result. AntlrFileStream stream = new AntlrFileStream(file); ITokenSource lexer = new JavaLexer(stream); ITokenStream tokens = new