abstract-syntax-tree

How to add a code snippet to method body with JDT/AST

独自空忆成欢 提交于 2019-12-06 17:19:58
问题 I'm trying to generate Java source code with JDT/AST. I now have MethodDeclaration and want to add a code snippet (from another source) to the method body. The code snippet can contain any Java code, even syntactically invalid code . I just can't find the way to do this. With JCodeModel you would use JBlock#directStatement(String s) method. Is there a way to do this with JDT/AST? 回答1: Since you have a well-formed tree for the rest of the application, and you want to insert non-well-formed

How to write a Typescript plugin?

ぃ、小莉子 提交于 2019-12-06 15:21:45
Are there any docs / examples of writing a Typescript plugin? For the last time I am very inspired with the idea of bringing Typescript into my projects. However, currently I see this is not possible because of my failed attempts to find any docs about writing a Typescript plugin . I need this plugin for combining classes metadata during compilation and then generating an asset. It was not that easy but I've already written such for babel and now I am interested if it is possible to do the same with Typescript. You can download https://github.com/microsoft/typescript or via npm. npm install

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

浪子不回头ぞ 提交于 2019-12-06 15:18:34
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 visits all SimpleType nodes. Here's the code snippet from the ASTVisitor implementation: public boolean

How to fold over a discriminated union

蓝咒 提交于 2019-12-06 12:27:39
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 -> a.AttemptBody :: List.map (fun ab -> ab.ABBody) a.AttemptBranches | Let l -> l.LetBody :: List

simple criteria expression parser with antlr3

故事扮演 提交于 2019-12-06 10:36:44
I want to create a simple criteria expression parser with antlr3 Updated: separate AND OR expression rules to support AND/OR different hierarchy, but got another problems: if the expression is something like: a = 1 and b = 2 and c = 3 The tree should be as following according to current implement: = = (a = 1)(b = 2)(c = 3) But I want to generate it as follows: = = (a = 1)(b = 2) (c = 3) First "and" should be higher priority than another, because I want to parse all the expression as left exp and right exp. I think I need to re-write the rule in the "subcond" To make a = 1 and b = 2 and c = 3 -

extract interface that a class implementing using AST parser

China☆狼群 提交于 2019-12-06 09:20:28
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? 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(); ITypeBinding superTypeBind = typeBind.getSuperclass(); ITypeBinding[] interfaceBinds = typeBind

How to replace a subtree with some other tree?

久未见 提交于 2019-12-06 08:52:48
问题 In Scala macro, I want to do something like this: I have a Tree (possibly large). Now I want to find a subtree of this tree that has some concrete form, e.g. Apply(_, _) . And now I want to create a new tree that is a copy of the original tree, but the found subtree is replaced by some other tree. With something like this, I could for example replace invocation of some method with invocation of some other method. Is something like this possible? 回答1: I'm very interested in seeing alternative

Haskell: Labeling an AST with type information using Algorithm W

馋奶兔 提交于 2019-12-06 06:21:37
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 ast But now I want the result to be the original AST annotated with type information for each expression,

Adding nodes to Clang's AST

爷,独闯天下 提交于 2019-12-06 05:38:19
问题 I need to insert new nodes to AST. for instance, adding a namespace to a function: Turning this - void foo(); into this - namespace bar { void foo(); } I read How to clone or create an AST Stmt node of clang? but I prefer not using source-to-source compilation Tnx 回答1: The answer can be found here http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html However, the nodes are added to the compiled AST - for instance, in case one wants to inject a namespace to the

How to get abstract syntax tree of a `c` program in `GCC`

五迷三道 提交于 2019-12-06 05:07:01
问题 How can I get the abstract syntax tree of a c program in gcc? I'm trying to automatically insert OpenMP pragmas to the input c program. I need to analyze nested for loops for finding dependencies so that I can insert appropriate OpenMP pragmas. So basically what I want to do is traverse and analyze the abstract syntax tree of the input c program. How do I achieve this? 回答1: You need full dataflow to find 'dependencies'. Then you will need to actually insert the OpenMP calls. What you want is