abstract-syntax-tree

Eclipse AST not changing files which are not opened in eclipse

两盒软妹~` 提交于 2019-12-02 05:07:32
I am trying to modify source code using eclipse plugin, JDT and AST (Abstract Syntax Tree). I can read all Java files and make operation on all those file, But when i am saving those changes (Edits) in to files using TextEdit edits = rewriter.rewriteAST(); // apply the text edits to the compilation unit edits.apply(document); iCompilationUnit.getBuffer().setContents(document.get()); It only make changes in file those are open in eclipse in unsaved mode. Rest of files are not affected. Find my code snippet below: CompilationUnit cu = parse(iCompilationUnit); MethodVisitor visitor = new

Haskell labelling items in recursive function

无人久伴 提交于 2019-12-02 03:37:48
问题 I am quite new to Haskell and functional programming in general, so excuse me if the question seems straightforward or silly. I have a parser for a simple language that produces an abstract syntax tree. In order to flatten the AST (turn while and if statements into jumps) I need to put labels in the tree. The problem is that I do not know what the next label should be (I am still thinking imperatively, because if I had state, none of this would be a problem). The function that I have so far

changing ** operator to power function using parsing?

最后都变了- 提交于 2019-12-02 02:28:39
My requirement is to change ** operator to power function For example 1.Input -"B**2" Output - power(B,2) 2."B**2&&T**2*X" Output - power(B,2) I have wrote following regular expression to address that problem rx=r"([a-zA-Z0-9]+)\*\*([a-zA-Z0-9()]+)" result = regex.sub(rx, r"power(\1,\2)", expression, 0, regex.IGNORECASE | regex.MULTILINE) But above code successfully converting expression similar to the example 1 and example 2, but failed to convert expression like (a+1)**2 or ((a+b)*c)**2 . I realized regular expression is not the best way to handle such scenarios. Instead of that parsing will

Haskell labelling items in recursive function

孤者浪人 提交于 2019-12-02 01:36:17
I am quite new to Haskell and functional programming in general, so excuse me if the question seems straightforward or silly. I have a parser for a simple language that produces an abstract syntax tree. In order to flatten the AST (turn while and if statements into jumps) I need to put labels in the tree. The problem is that I do not know what the next label should be (I am still thinking imperatively, because if I had state, none of this would be a problem). The function that I have so far is the following: transform :: Stmt -> FStmt transform (Seq stmt) = FSeq (map transform stmt) transform

How to convert AST to JDT Java model

霸气de小男生 提交于 2019-12-02 00:38:45
I am writing unit tests for my plugin that makes use of IType and IMethod interfaces from JDT. To write unit tests I would need to instantiate such interfaces. Answer to this question shows how to create AST model, but I don't know how to convert it into Java model? My code looks like this: String source = "package com.test\n" + "\n" + "import com.test.something;" + "\n" + "public class Class{\n" + "int sum(int a, int b)\n" + "}\n"; ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); So I

Java / Abstract Syntax Tree into XML representation

折月煮酒 提交于 2019-12-01 08:36:06
问题 do you know of any tool which creates an AST from a Java program or class and creates an XML representation (Collection or single XML document) from the AST? kind regards, Johannes 回答1: Not any tools directly, but http://www.antlr.org/ is the defacto tool for building ASTs from any general language. And there exists several grammar files for Java that you can repurpose for your own programs. So grab ANTLR, use the latest Java grammer, and write out the XML representation you want. 回答2: Our

Recommend C front-end that preserves preprocessor directives

好久不见. 提交于 2019-12-01 05:18:30
I'd like to start a project that involves transforming C code, but I'd like to include the preprocessor directives. I don't want to reinvent the wheel by writing my own C parser, so does anyone know of a front-end that can parse C preprocessor and C code, and produce an AST that can be used to re-generate (or pretty-print) the original source? e.g.,: #define FILENAME "filename" #include <stdio.h> FILE *f=0; ... if (file_is_open) { #ifdef CAN_OPEN_IT f = fopen(FILENAME, "r"); #else printf("Unable to open file.\n"); #endif } The above code should be parsed into some in-memory representation that

Recommend C front-end that preserves preprocessor directives

喜夏-厌秋 提交于 2019-12-01 04:21:42
问题 I'd like to start a project that involves transforming C code, but I'd like to include the preprocessor directives. I don't want to reinvent the wheel by writing my own C parser, so does anyone know of a front-end that can parse C preprocessor and C code, and produce an AST that can be used to re-generate (or pretty-print) the original source? e.g.,: #define FILENAME "filename" #include <stdio.h> FILE *f=0; ... if (file_is_open) { #ifdef CAN_OPEN_IT f = fopen(FILENAME, "r"); #else printf(

How to get lineno of “end-of-statement” in Python ast

一笑奈何 提交于 2019-12-01 02:39:30
I am trying to work on a script that manipulates another script in Python, the script to be modified has structure like: class SomethingRecord(Record): description = 'This records something' author = 'john smith' I use ast to locate the description line number, and I use some code to change the original file with new description string base on the line number. So far so good. Now the only issue is description occasionally is a multi-line string, e.g. description = ('line 1' 'line 2' 'line 3') or description = 'line 1' \ 'line 2' \ 'line 3' and I only have the line number of the first line, not

Parsing “->” assignment operator in R

丶灬走出姿态 提交于 2019-12-01 02:27:09
My question is about parsing expressions in R language. Let me jump right into an example: fun_text <- c(" 0 -> var f1 <- function() { 0 -> sum_var sum_var2 = 0 sum_var3 <- 0 } (function() { 0 -> sum_var sum_var2 = 0 sum_var3 <- 0 })->f2 f3 = function(x) { 0 -> sum_var sum_var2 = 0 sum_var3 <- 0 } ") fun_tree <- parse(text=fun_text) fun_tree fun_tree[[1]] fun_tree[[2]] fun_tree[[3]] fun_tree[[4]] After that, we obtain those results: expression(0 -> var, f1 <- function() { 0 -> sum_var sum_var2 = 0 sum_var3 <- 0 }, (function() { 0 -> sum_var sum_var2 = 0 sum_var3 <- 0 })->f2, f3 = function(x) {