Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala AST in Scala [closed]

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.)

like image 303
AWhitford Avatar asked Nov 24 '09 08:11

AWhitford


2 Answers

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.

like image 117
Mitch Blevins Avatar answered Sep 19 '22 23:09

Mitch Blevins


A few existing parsers:

  • The offical Scala compiler.
  • The IntelliJ IDEA Scala plugin has a parser written in Scala against IntelliJ's PsiBuilder API.
  • The Scala Netbeans plugin used a parser implemented in Rats! (which generates Java code), but "replaced these parser and analyzer by Scala's native compiler".
  • The Scala-rules project, written in Scala.

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.

like image 38
Matt R Avatar answered Sep 20 '22 23:09

Matt R