abstract-syntax-tree

Ignore missing headers with clang AST parser

六月ゝ 毕业季﹏ 提交于 2019-12-08 20:20:04
问题 I'm on Windows, using MSVC to compile my project, but I need clang for its neat AST parser, which allow me to write a little code generator. Problem is, clang cannot parse MSVC headers (a very-well known and understandable problem). I tried two options : I include MSVC header folder, parsing the built-in headers included in my code will end-up leading to a fatal error at some point, preventing me from parsing the parts I want correctly. What I did before is simply not provide any built-in

Why can't I do a method call after a @Grab declaration in a Groovy script?

醉酒当歌 提交于 2019-12-08 15:48:40
问题 I'm trying to build a DSL and using a Global AST Transform to do it. The script is compiling with groovyc fine, but I'd like to be able to be able to have a user use Grab/Grape to pull the JAR and just have it execute right away as a groovy script. I then found that I couldn't do it correctly because there's a parsing error in the script if there isn't a method declaration or import statement after the @Grab call. Here's an example: @Grab(group='mysql', module='mysql-connector-java', version=

Clang IfStmt with shortcut binary operator in condition

为君一笑 提交于 2019-12-08 12:43:12
问题 I am trying to detect if there is a function call inside an if statement as part of condition; like following: if (cmp(a, b)){ \\do something } I have found I could do this with AST matcher in following manner: Matcher.addMatcher(ifStmt(hasCondition(callExpr().bind("call_expr"))) .bind("call_if_stmt"),&handleMatch); But the problem is condition could have shortcuts like &&, ||; like following: if(a != b && cmp(a,b) || c == 10){ \\ do something } Now this condition has binaryoperator && and ||

How to find/detect if a build-in function is used in Python AST?

只谈情不闲聊 提交于 2019-12-08 11:28:12
问题 The goal is to detect if a builtin function such as eval() is used in some code. def foo(a): eval('a = 2') I have tried the following approach: ex_ast = ast.parse(inspect.getsource(foo)) for node in ast.walk(ex_ast): if isinstance(node, ast.FunctionDef): print(node.name) The function name foo is printed as the output. I know that Builtin functions don't have constructors. They are in the type Module. So 1 approach would be using types.FunctionType in an isinstance call. But since I'm using

How to use ASTRewrite to replace a particular SimpleType with a PrimitiveType?

核能气质少年 提交于 2019-12-08 03:30:52
问题 I need to preprocess some code before compiling for a java based language - Processing. In this language, all instances of type color, need to be replaced with int. For ex, here's a code snippet: color red = 0xffaabbcc; color[][] primary = new color[10][10]; After preprocessing, the above code should look like: int red = 0xffaabbcc; int[][] primary = new int[10][10]; I'm working in a non eclipse environment. I'm using Eclipse JDT ASTParser to do this. I've implemented my ASTVisitor which

extract interface that a class implementing using AST parser

半城伤御伤魂 提交于 2019-12-08 01:56:49
问题 I am compiling a project source using AST parser. In what way i can extract class hierarchy infromation, that is whether it is implementing any interface or extends from another class? 回答1: You can visit the TypeDeclaration node and get the type binding from it. ITypeBinding typeBind = typDec.resolveBinding(); You can then get the super class and implemented interfaces as follows: public boolean visit(TypeDeclaration typeDeclaration) { ITypeBinding typeBind = typeDeclaration.resolveBinding();

How to fold over a discriminated union

六眼飞鱼酱① 提交于 2019-12-07 23:51:28
问题 I'm attempting to implement a fold over a discriminated union. The DU is called Expr, and represents a program expression, and is often recursive. I'm attempting to write a fold that accumulates the result of an operation over the Exprs recursively. Below is my attempt to write the fold. let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's = let children = match expr with | Series s -> s.SerExprs | Lambda l -> [l.LamBody; l.LamPre; l.LamPost] | Attempt a

Haskell: Labeling an AST with type information using Algorithm W

自古美人都是妖i 提交于 2019-12-07 18:41:22
问题 We have an AST definition: data Term a = Lam String a | App a a | Var String deriving(Read,Show,Eq,Functor,Foldable,Traversable) And an F-Algebra for the type inference: type Wrapped m a = Enviroment -> m a type Result m = Wrapped (Type,Substitution) w :: (MonadState Int, MonadError TypeError) => Term (Result m) -> Result m w term env = ... We can get the result of running the inference using cata : infer :: (MonadState Int, MonadError TypeError) => Fix Term -> Result m infer ast = cata hm

Change source code of a module via AST from a WebPack 4 plugin

試著忘記壹切 提交于 2019-12-07 17:28:07
问题 I have the following JS file: // hello-foo.js console.log('foo') I want to replace 'foo' with 'bar' with webpack. I have the following WebPack plugin for that: class MyPlugin { constructor() {} apply(compiler) { compiler .hooks .compilation .tap('MyPlugin', (compilation, {normalModuleFactory}) => { normalModuleFactory .hooks .parser .for('javascript/auto') .tap('MyPlugin', (parser) => { parser .hooks .program .tap('MyPlugin', (ast, comments) => { ast.body[0].expression.arguments[0].value =

In Eclipse JDT Java parser, is it possible to traverse the AST node by node without the using of visitors?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 14:21:19
问题 The standard way to access information on Nodes through the Eclipse JDT API is with Visitor's pattern. For example: unit.accept(new MyVisitorAdapter<Object>() { @Override public void visit(MethodCallExpr node, Object arg) { System.out.println("found method call: " + node.toString()); } }, null); In this case, to visit a node I need to specify what type of node I'm interested ( MethodCallExpr for this case). But, to proceed to access node information in a generic way, I should override all the