Pretty print for Java - rascal

I am trying to change the content of a .java file
I got the AST thanks to:
ast = createAstFromFile(|project://foo/src/main/Main.java|,true);
ast = doSomeChanges(ast);
Then I want to serialize the AST.
Is there a way to get back the source code from an AST?

Related

Use Generator CodeGen in Tatsu

I have generated Model code and parser from my Grammar but I can't modify model to generated code in python.
My EBNF grammar is a script code like "C" syntax for translate file in XML or ANSI X12.
It's a language specific and I would like to generate Python code from this script with Tatsu.
I parse script but I can't success to use Parser or Model to generated Python source code.... Where i must to save Model or modify parser to generated python code ... I see tools.py ... can I copy the code to build a new code model...
Can you help me ... I start learn python and i must to implement this solution on web site with upload script and download python code.
TatSu is a parser generator. I doesn't have any provisions for generating running code from text parsed by an arbitrary grammar.
You have to write your own code generator (walk thes AST after a parse, and generate the corresponding code).

AST of a project by Clang

I use the Clang python binding to extract the AST of c/c++ files. It works perfectly for a simple program I wrote. The problem is when I want to employ it for a big project like openssl. I can run clang for any single file of the project, but clang seems to miss some headers of the project, and just gives me the AST of a few functions of the file, not all of the functions. I set the include folder by -I, but still getting part of the functions.
This is my code:
import clang.cindex as cl
cl.Config.set_library_path(clang_lib_dir)
index = cl.Index.create()
lib = 'Path to include folder'
args = ['-I{}'.format(lib)]
translation_unit = index.parse(source_file, args=args)
my_get_info(translation_unit.cursor)
I receive too many header files not found errors.
UPDATE
I used Make to compile openssl by clang? I can pass -emit-ast option to clang to dump the ast of each file, but I cannot read it now by the clang python binding.
Any clues how I can save the the serialized representation of the translation units so that I will be able to read it by index.read()?
Thank you!
You would "simply" need to provide the right args. But be aware of two possible issues.
Different files may require different arguments for parsing. The easiest solution is to obtain compilation database and then extract compile commands from it. If you go this way be aware that you would need to filter out the arguments a bit and remove things like -c FooBar.cpp (potentially some others), otherwise you may get something like ASTReadError.
Another issue is that the include paths (-I ...) may be relative to the source directory. I.e., if a file main.cpp compiled from a directory /opt/project/ with -I include/path argument, then before calling index.parse(source_file, args=args) you need to step in (chdir) into the /opt/project, and when you are done you will probably need to go back to the original working directory. So the code may look like this (pseudocode):
cwd = getcwd()
chdir('/opt/project')
translation_unit = index.parse(source_file, args=args)
chdir(cwd)
I hope it helps.

How to create "list of type" for HtmlProvider<list of HTML files> in F#?

I am very new to F# and trying to convert my python script to F# code for my learning.
i want to parse multiple (around 25 files) static html files to extract similar information from each file.
I want to have a list of file handle for all the html files.
I am able to do it for single file as:
type SummaryHtmlType = HtmlProvider< #"C:/MyLocation/Summary_1.html">
I tried something similar to XmlProvider (even not sure if that's correct for XmlProvider), but no success.
type MyType = HtmlProvider<htmlFileList; SampleIsList=true>
Let me know solution even if there is all together different approach to do it.
"C:/MyLocation/Summary_1.html" in type SummaryHtmlType = HtmlProvider< #"C:/MyLocation/Summary_1.html"> is a sample file for HtmlProvider get the basic structure.
To parse file or url, use Load method like SummaryHtmlType.Load(url)
For more information, see http://fsharp.github.io/FSharp.Data/library/HtmlProvider.html

JavaParser: Where is the Java grammar definition?

Where can I get the BNF-style Java 1.8 grammar that JavaParser is actually using to parse Java code?
There's a java_1_8.jj file in JavaParser's codebase automatically generated by javacc, but no sight of the grammar file used to generate this .jj file.
There's a java_1_8.jj file in JavaParser's codebase automatically generated by javacc, but no sight of the grammar file used to generate this .jj file.
java_1_8.jjis the grammar file, while it is not in the BNF style you expected. It is not generated, but it is used to generate the parser java files.

Do anybody know how to generate the rsc file of jdt's parser

Do anybody know how to generate the rsc file of jdt's parser .I mean how to serialization the rule of parser .and where can i find the detail about the rule.
I have import jdt to my code ,and try to learn the rule of the parsr.
But the serialization rule confuse .Then i can easier to learn the rule if i find the code which is used to serialization the rule to rsc file.
http://www.eclipse.org/jdt/core/howto/generate%20parser/generateParser.html
i think i hava found the answer.

Resources