abstract-syntax-tree

How to explain the abstract syntax tree of chained comparison operations?

微笑、不失礼 提交于 2020-02-29 12:47:42
问题 Comparison operators can be chained in python, so that for example x < y < z should give the result of (x < y) and (y < z) , except that y is guaranteed to be evaluated only once. The abstract syntax tree of this operation looks like: >>> ast.dump(ast.parse('0 < 1 < 2'), annotate_fields=0) 'Module([Expr(Compare(Num(0), [Lt(), Lt()], [Num(1), Num(2)]))])' Pretty printed: Module Expr Compare Num Lt Lt Num Num But it seems to parse as something like 0 < < 1 2 and I'm not sure how to reconcile

Find parent of a declaration in Clang AST

时光毁灭记忆、已成空白 提交于 2020-02-27 07:36:09
问题 I'm using clang to do some analysis and I need to find parent of a declaration in AST. For instance, in the following code I have int x and I want to get its parent which should be the function declaration : int main(int x) { return 0 } I know as mentioned in this link http://comments.gmane.org/gmane.comp.compilers.clang.devel/2152 there is a ParentMap class to track parent nodes. However, this just represents a map from Stmt* -> Stmt* and I need to find parent of a declaration. Does anyone

HQL Unexpected AST node: min

谁说胖子不能爱 提交于 2020-02-03 11:00:04
问题 I'm trying to create a HQL Query like so: List<Task> results = session.createQuery("FROM Task where ProcessID = :procId " + "and Role = :role and completed = 0 " + "group by TaskID " + "having min(chronology)") .setParameter("procId", procId) .setParameter("role", role).list(); The following error occurs: <AST>:1:114: unexpected AST node: min at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1943) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker

How can I dynamically execute function in current scope and add it as property of the calling function?

≡放荡痞女 提交于 2020-01-25 11:08:33
问题 I have some code like this: def f1(): <some stuff here> . . . @mylib.codegen def f2(args): f1() <some more stuff here> mylib.py : def codegen(fn): src = inspect.getsource(fn) original_ast = ast.parse(src) new_ast = transform_ast(original_ast) code_obj = compile(new_ast, '<auto-generated>', 'exec') myscope = {} exec code_obj in myscope fn.generated_fn = myscope['name'] # Where name is the binding created by execing code_obj To summarize, mylib.codegen is a decorator which parses code of f,

How can I dynamically execute function in current scope and add it as property of the calling function?

≡放荡痞女 提交于 2020-01-25 11:06:22
问题 I have some code like this: def f1(): <some stuff here> . . . @mylib.codegen def f2(args): f1() <some more stuff here> mylib.py : def codegen(fn): src = inspect.getsource(fn) original_ast = ast.parse(src) new_ast = transform_ast(original_ast) code_obj = compile(new_ast, '<auto-generated>', 'exec') myscope = {} exec code_obj in myscope fn.generated_fn = myscope['name'] # Where name is the binding created by execing code_obj To summarize, mylib.codegen is a decorator which parses code of f,

transform python code programmatically via meta package

那年仲夏 提交于 2020-01-25 02:51:08
问题 I'd like to transform all occurrences of "some_func(a, b)" in a python module to "assert a == b". I tried with meta package: import meta, ast source = ''' assert_equal(a, b) ''' orig = ast.parse(source, '<nofile>', 'exec') assert_ast = ast.parse('assert a == b') assert_ast.body[0].test.left = orig.body[0].value.args[0] assert_ast.body[0].test.comparators[0] = orig.body[0].value.args[1] code = compile(assert_ast, '<nofile>', 'exec') mod2 = meta.decompile(code) source2 = meta.dump_python_source

Clang AST visitor, avoid traversing include files

本小妞迷上赌 提交于 2020-01-23 01:06:26
问题 Hello I am trying to implement an AST Clang visitor and this is my code. class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> { private: ASTContext *astContext; // used for getting additional AST info public: virtual bool VisitVarDecl(VarDecl *var) { numVariables++; string varName = var->getQualifiedNameAsString(); string varType = var->getType().getAsString(); cout << "Found variable declaration: " << varName << " of type " << varType << "\n"; APIs << varType << ", "; return

Generate AST in the form of a dot file

≯℡__Kan透↙ 提交于 2020-01-15 04:30:50
问题 I am working with ANTLR4 to generate AST of a java source code and i had to move to ANTLR3 because i was not getting much help and documentation and it was really tough to proceed.I managed to generate AST but not in a visual format. Then i came across an awesome answer and i was really able to generate AST in a DOT file but there was a slight problem. My code: import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.tree.CommonTree;

Generate AST in the form of a dot file

强颜欢笑 提交于 2020-01-15 04:30:26
问题 I am working with ANTLR4 to generate AST of a java source code and i had to move to ANTLR3 because i was not getting much help and documentation and it was really tough to proceed.I managed to generate AST but not in a visual format. Then i came across an awesome answer and i was really able to generate AST in a DOT file but there was a slight problem. My code: import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.tree.CommonTree;

JS: rename variables for refactor (using an AST, not text)

随声附和 提交于 2020-01-14 22:32:28
问题 I often need to rename variables when refactoring code, which I currently do in a somewhat hacky way using regexs - I end up having to come with silly text workaround workarounds for the lack of actual structure, eg, rename 'req' to 'request' and avoid side effects with similar names like 'require'. Thinking about this stuff: it's kind of like modifying the DOM with regexs: it just doesn't work. I've learnt about ASTs and code structure modification tools like Esprima. Is there a tool to