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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:45:22  来源:igfitidea点击:

Scala AST in Scala

scalaabstract-syntax-tree

提问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.

我认为访问 AST 的最佳方式是使用编译器插件。在深入研究之前,您应该阅读一个简单的介绍

回答by Matt R

A few existing parsers:

一些现有的解析器:

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

回答by Steve Lianoglou

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

不确定纯 Scala 解决方案,但如果您发现自己需要实施计划 B,您可以从查看ANTLRRats 开始!