Scala 中的 Scala AST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1788627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala AST in Scala
提问by AWhitford
Is there a Scala library that parses Scala and creates an Abstract Syntax Tree(AST)?
是否有解析 Scala 并创建抽象语法树(AST)的 Scala 库?
Ideally I am interested in a Scala library. Plan B would be a Java library.
理想情况下,我对 Scala 库感兴趣。计划 B 将是一个 Java 库。
(I know I could leverage the EBNF from the Scala Syntax Summary.)
(我知道我可以利用Scala Syntax Summary 中的 EBNF 。)
采纳答案by Mitch Blevins
I would think the best way to access the AST is with a compiler plugin. You should read a soft introductionbefore diving in deep.
回答by Matt R
A few existing parsers:
一些现有的解析器:
- The offical Scala compiler.
- The IntelliJ IDEA Scala plugin has a parserwritten in Scala against IntelliJ's
PsiBuilderAPI. - The Scala Netbeans plugin used a parserimplemented in Rats! (which generates Java code), but "replaced these parser and analyzer by Scala's native compiler".
- The Scala-rulesproject, written in Scala.
- 该官方Scala编译器。
- IntelliJ IDEA Scala 插件有一个用 Scala 编写的针对 IntelliJ API的解析器
PsiBuilder。 - Scala Netbeans 插件使用了在 Rats 中实现的解析器!(生成 Java 代码),但“用 Scala 的本机编译器替换了这些解析器和分析器”。
- 在斯卡拉规则项目,用Scala编写。
Be cautious if using the EBNF from the spec, there are apparently:
如果使用规范中的 EBNF,请小心,显然有:
"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.
“附录和内联语法之间的不匹配,以及 scalac 编译的语言(并在 Scala 源中使用)与语法声明的语言之间的不匹配”——Scala Trac 错误 #1826。
回答by Daniel C. Sobral
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.
您不能仅从语法中为 Scala 构建 AST。有隐式需要考虑,为了考虑它们,还有类型推断器需要考虑。
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.
但是,您可以调用编译器本身——毕竟它只是一个 jar 文件。特别是 Scala 2.8,它有很多钩子供其他程序使用——Miles Sabin 的工作,他正是这样做的,以便 Scala 的 Eclipse 插件可以以这种方式利用编译器。
I suggest you go to the Scala Tools mailing list, and get in contact with people there.
我建议您访问 Scala Tools 邮件列表,并与那里的人取得联系。
回答by Flora Liu
If you want to generate AST of a piece of code. You could use scala reflection:
如果你想生成一段代码的AST。您可以使用 Scala 反射:
showRaw(reify{
//your code here like:
print(2)
})
The code above will generate an AST:
上面的代码将生成一个 AST:
Expr(Apply(Select(Ident(scala.Predef), TermName("print")), List(Literal(Constant(2)))))
Reference:
参考:
http://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html
http://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html
回答by Mohamed Bana
Here is a project by one of the compiler committers http://github.com/paulp/scala-lang-combinators
这是编译器提交者之一的项目http://github.com/paulp/scala-lang-combinator

