abstract-syntax-tree

Java library for code analysis

我与影子孤独终老i 提交于 2019-12-06 04:23:09
问题 Is there any Java library, that can help in building AST from the specified java source file and vice versa (generate code from the ASTree object)? I need something like this, but with an API, allowing to access the generated tree programmatically. 回答1: Everything's already available within the Eclipse core. Here's a page with a small example of how to use org.eclipse.jdt.core.dom.ASTParser to create your desired AST datastructure. 来源: https://stackoverflow.com/questions/2832996/java-library

Code generating JUnit based on Abstract Syntax tree walk

坚强是说给别人听的谎言 提交于 2019-12-06 04:20:20
Assuming I have the following class and method: package generation; class HelloWorld { public boolean isEven(int val) { if ( (val % 2) == 0) return true; else return false; } } Assume I want to generated the following JUnit test: package generation; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; public class HelloWorldTest { @Test public void testIsEven() { HelloWorld h = new HelloWorld(); assertTrue(h.isEven(2)); assertFalse(h.isEven(1)); } } Given the following method of tree walking a Java Syntax Tree: How can I use the java

How to set the symbol for a raw AST when writing a scala compiler plugin?

邮差的信 提交于 2019-12-06 03:56:17
问题 I'm working with a scala compiler plugin which replaces function calls like f(x) with a block { val newvalue = f(x) newvalue } This block has the same value as f(x) . The compiler plugin works right after compiler phase "typer" and do the transformation above by replacing the AST of f(x) with the AST of the block. Doing this after compiler phase "typer", the newly created block is a raw AST, without symbol and type, as well as its child ASTs. I need to call localTyper to give the raw ASTs

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

陌路散爱 提交于 2019-12-06 03:32:46
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 = 'bar' // ast.body[0].expression.arguments[0].raw = 'bar' // does not make any difference :( }) }) }) } }

What is the easiest way to update an immutable AST?

你。 提交于 2019-12-06 03:29:01
问题 While working with macros, I have reached the point (I have been trying hard to avoid it) where I need to update those nodes in the AST which hold certain condition. For instance, let's say I would like to update each node: Literal(Constant(1)) with the value: Literal(Constant(2)) Those AST nodes could be anywhere in the expression tree, so I cannot use an ad-hoc pattern matcher. Obviously, the last thing I would like to do is to code a full pattern matcher which is able to cover all the

Getting all the nodes from Python AST that correspond to a particular variable with a given name

别来无恙 提交于 2019-12-06 01:52:54
Consider the code below: 1 | x = 20 2 | 3 | def f(): 4 | x = 0 5 | for x in range(10): 6 | x += 10 7 | return x 8 | f() 9 | 10| for x in range(10): 11| pass 12| x += 1 13| print(x) The value of x after execution of the code above is 10 . Now, how can I get all the nodes with class Name whose id s are x and refer to the x that's being used in lines 1, 10, 12 and 13? In other words, the x inside of f is different from the rest of the x s. Is it possible to get their AST nodes, having only the script and script's AST while not executing it? When walking the AST tree, track the context; start with

Modeling an ordered tree with neo4j

南笙酒味 提交于 2019-12-06 01:38:48
问题 I'm just getting started with neo4j, and I understand the principles of the graph and relationships, but I'm having a little bit of trouble with certain structures I want to model. I wanted to use it on a programming language project, and store the AST of a parsed source file. From there, I plan on adding a lot of additional data and relationships to the nodes to help with analysis and tooling, but the fundamental AST is still a little difficult. The naive way of making a tree would be to

Extract the AST from a Ruby block

 ̄綄美尐妖づ 提交于 2019-12-05 22:44:26
问题 Is it possible to grab the AST of a block from Ruby itself? I've had a look at both ParseTree and ruby_parser, but they both seem to have sketchy support (from what I've read) for Ruby 1.9.2. I need something that works well with 1.9.2. 回答1: Ripper is included in MRI 1.9 out of the box. ruby-1.9.2-p180 :004 > require 'ripper' => true ruby-1.9.2-p180 :005 > Ripper.sexp("def a; end") => [:program, [[:def, [:@ident, "a", [1, 4]], [:params, nil, nil, nil, nil, nil], [:bodystmt, [[:void_stmt]],

How to manually construct an AST?

牧云@^-^@ 提交于 2019-12-05 20:45:59
问题 I'm currently learning about parsing but i'm a bit confused as how to generate an AST. I have written a parser that correctly verifies whether an expressions conforms to a grammar (it is silent when the expression conforms and raises an exception when it is not). Where do i go from here to build an AST? I found plenty of information on building my LL(1) parser, but very little on then going on to build the AST. My current code (written in very simple Ruby, and including a lexer and a parser)

eclipse ASTNode to source code line number

霸气de小男生 提交于 2019-12-05 16:06:27
Given an ASTNode in eclipse, is there any way to get the corresponding source code line number? You can get the line number of an ASTNode using the below code int lineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1; the compilation unit can be obtained from the ASTParser using the below code ASTParser parser = ASTParser.newParser(AST.JLS3); // Parse the class as a compilation unit. parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source); // give your java source here as char array parser.setResolveBindings(true); // Return the compiled class as a compilation