abstract-syntax-tree

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

不羁的心 提交于 2019-11-30 21:23:04
问题 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')

How to programmatically rename a method using JDT

你。 提交于 2019-11-30 20:37:52
My aim is to programmatically call the Refactor >> Rename Eclipse command for a method inside a Java Source File. Renaming a method as such should also apply the change to all the instances where this method is being used/referred. I believe that JDT has a Refactoring API, but not able to find any documents or tutorials for the same. Can somebody point me in the right direction. Edit: The change is not needed at runtime. I think your most promising approach is to go to the eclipse source code. Download the release you want, with source code. In particular, you want the source for the JDT

Converting Data.Reify explicit sharing graph to AST with de Bruijn indices

丶灬走出姿态 提交于 2019-11-30 19:47:08
I'm trying to recover sharing (in the Type-Safe Observable Sharing in Haskell sense) for a simple AST, using Data.Reify : {-# LANGUAGE DeriveFoldable, DeriveFunctor, DeriveTraversable, TypeFamilies #-} module Sharing where import Data.Foldable import Data.Reify import Data.Traversable -- Original AST, without sharing. Expressed as a functor for ease of -- use with Data.Reify. data AstF f = LitF Int | AddF f f deriving (Foldable, Functor, Show, Traversable) newtype Fix f = In { out :: f (Fix f) } instance Traversable a => MuRef (Fix a) where type DeRef (Fix a) = a mapDeRef f = traverse f . out

Easy way to convert python source code to an AST with comments intact

核能气质少年 提交于 2019-11-30 19:27:23
I have done fair bit of searching around how to capture python ASTs with comments preserved. The suggested way includes using ast and tokenize libraries to get the job done. I have had fair bit of success in utilizing these libraries as per my requirement but I feel there has to be a better way. This thought stems from the fact that lib2to3 converts python2 code to python3 code with comments preserved. Also the process is stated to be Source-Code-in-Python2 -> AST -> Source-Code-in-Python3 (to be put forth in a simplified manner). My question is how do I capture the in-between AST? I have

What is an Expr in Python AST?

早过忘川 提交于 2019-11-30 17:28:01
问题 I'm working on dynamically generating code in Python. To aid with this, I wrote a helper method which takes in a string of Python code and dumps out the AST. Here's that method: # I want print treated as a function, not a statement. import __future__ pfcf = __future__.print_function.compiler_flag from ast import dump, PyCF_ONLY_AST def d(s): print(dump(compile(s, '<String>', 'exec', pfcf|PyCF_ONLY_AST)) When I run this function on a simple Hello World, it spits out the following (formatted

Best way to parse Java in Java

烈酒焚心 提交于 2019-11-30 15:12:50
问题 As the title says, I want to parse some Java source code in Java. I'm pretty sure there are other java libraries that already perform this, but I couldn't find any. 回答1: Janino can parse, compile and execute Java code. 回答2: Antlr has a grammar file for Java. See this. 回答3: You may be looking for something like ANTLR: http://www.antlr.org/ 回答4: Eclipse exposes the Syntax Tree of it's own Java compiler. You can simply access the elements. See here. 回答5: JavaCC has a Java 1.5 grammar including

Typed abstract syntax tree with function application

怎甘沉沦 提交于 2019-11-30 15:03:33
I am trying to write a typed abstract syntax tree datatype that can represent function application. So far I have type Expr<'a> = | Constant of 'a | Application of Expr<'b -> 'a> * Expr<'b> // error: The type parameter 'b' is not defined I don't think there is a way in F# to write something like 'for all b' on that last line - am I approaching this problem wrongly? In general, the F# type system is not expressive enough to (directly) define a typed abstract syntax tree as the one in your example. This can be done using generalized algebraic data types (GADTs) which are not supported in F#

Best way to parse Java in Java

会有一股神秘感。 提交于 2019-11-30 14:19:46
As the title says, I want to parse some Java source code in Java. I'm pretty sure there are other java libraries that already perform this, but I couldn't find any. Janino can parse, compile and execute Java code. Antlr has a grammar file for Java. See this . You may be looking for something like ANTLR: http://www.antlr.org/ Eclipse exposes the Syntax Tree of it's own Java compiler. You can simply access the elements. See here . JavaCC has a Java 1.5 grammar including generics. the tokens are probably more fine-grained then what I want Your first requirement is to get an accurate parse, and

string to abstract syntax tree

让人想犯罪 __ 提交于 2019-11-30 13:18:35
问题 I would like to convert a string containing a valid Erlang expression to its abstract syntax tree representation, without any success so far. Below is an example of what I would like to do. After compiling, alling z:z(). generates module zed , which by calling zed:zed(). returns the result of applying lists:reverse on the given list. -module(z). -export([z/0]). z() -> ModuleAST = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom("zed")]), ExportAST = erl_syntax:attribute(erl

How to implement JJTree on grammar

China☆狼群 提交于 2019-11-30 12:07:33
问题 I have an assignment to use JavaCC to make a Top-Down Parser with Semantic Analysis for a language supplied by the lecturer. I have the production rules written out and no errors. I'm completely stuck on how to use JJTree for my code and my hours of scouring the internet for tutorials hasn't gotten me anywhere. Just wondering could anyone take some time out to explain how to implement JJTree in the code? Or if there's a hidden step-by-step tutorial out there somewhere that would be a great