Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala code parser (not compiler)

Tags:

parsing

scala

I need to create converter from Scala to another language. I'm looking for scala code parser that converts code to syntax tree without compilation.

like image 413
Mateusz Avatar asked Oct 19 '11 21:10

Mateusz


1 Answers

Let me make it simple: there is no way to generate a Scala program's AST with parser alone. It is absolutely necessary to run the typer, and that means type inference and implicits.

After that, you can do whatever you want. But these first few phases of the compiler (four on most recent versions, counting the typer) are necessary.

Coincidentally, that's the phases ran by the presentation compiler, which is used by the Scala IDE for Eclipse. It seems to me that this might be the perfect interface for you.

ENSIME also uses it, which seems to be the best source of information about it, and you might also want to take a look at the Scala Refactoring tool, since it uses the compiler's AST as well.

Finally, you can try compiling the code with -Ybrowse:typer to see the tree after typer. Use -Xshow-phases to display the existing phases, or -Xprint:typer to print the "source" after typer (or any other phase).

like image 84
Daniel C. Sobral Avatar answered Oct 26 '22 10:10

Daniel C. Sobral