views:

725

answers:

5
+3  Q: 

Scala AST in Scala

Is there a Scala library that parses Scala and creates an Abstract Syntax Tree (AST)?

Ideally I am interested in a Scala library. Plan B would be a Java library.

(I know I could leverage the EBNF from the Scala Syntax Summary.)

+2  A: 

I would think the best way to access the AST is with a compiler plugin. You should read a soft introduction before diving in deep.

Mitch Blevins
The Scala Compiler Plugin looks promising.
AWhitford
A: 

Not sure about the pure scala solutions, but if you find yourself needing to implement plan B, you can start by checking out ANTLR or Rats!

Steve Lianoglou
+2  A: 

You can't build an AST for Scala from the grammar alone. There's implicits to consider, and, to consider them, there is the type inferencer to consider.

You can, however, call the compiler itself -- it is just a jar file, after all. Scala 2.8, in particular, has quite a few hooks for other programs to latch on -- work of Miles Sabin, who is doing this precisely so that the Eclipse plugin for Scala can leverage the compiler in such way.

I suggest you go to the Scala Tools mailing list, and get in contact with people there.

Daniel
You can build an AST for Scala without considering semantic aspects of the language, like types and implicits. It's certainly true that for a great many applications you would want to take them into account, but not necessarily all.
Matt R
I can imagine purposes for wanting an AST for just what the user wrote as well as one reflecting implicits and whatever other factors contribute to the precise AST ultimately used for code generation.
Randall Schulz
Initially, I was thinking that I did not care about a truly semantic interpretation of the scala code, but your point about implicits and type inference is a good one... I think I will definitely require the latter, so looking closer at the Scala compiler seems prudent.
AWhitford
+2  A: 

A few existing parsers:

Be cautious if using the EBNF from the spec, there are apparently:

"mismatches between the appendix and the inline grammar, and mismatches between the language compiled by scalac (and utilized in the scala sources) and the language claimed by the grammar" -- Scala Trac bug #1826.

Matt R
Your tip about the EBNF is useful. This means that JavaCC, ANTLR, etc. would likely be a challenge.The IDE compilers tend to have their own nuances because they are interested in real-time compilation -- which is not something that I need.
AWhitford
+1  A: 

Here is a project by one of the compiler committers http://github.com/paulp/scala-lang-combinators

bruce.banner