我在哪里可以学习如何构建 Scala 的宏生成的 AST?
Scaladoc 没有我想的那么有用.例如:
abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.TreeApply 节点的工厂方法.但是我如何确定 Apply 节点是什么?我在哪里可以找到 AST 中的节点类型列表,以及它们如何组合在一起?
解决方案没有很多关于编译器内部的文档可用,但是可用的东西应该足够开始了.
,其中包含 AST.
如果需要了解特定源代码片段在内部是如何表示的,可以使用 reify:
scala>导入反射.runtime.universe._导入反射.runtime.universe._斯卡拉>showRaw(reify{val i = 0}.tree)res8: String = Block(List(ValDef(Modifiers(), newTermName("i"), TypeTree(),文字(常量(0)))),文字(常量(())))Where I can learn how to construct the AST's that Scala's macros generate?
The Scaladoc isn't as helpful as I'd like. For example:
abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.Tree A factory method for Apply nodes.But how do I figure out what an Apply node is? Where can I find a list of the node types in AST's, and how they fit together?
解决方案There isn't a lot of documentation for the internals of the compiler available, but the things that are available should be enough to get started.
Mirko Stocker, has written his Master Thesis about Scala Refactoring. In Appendix D (p. 95) he describes the architecture of the AST. It includes also a graphical overview:
Another way to find information about the AST is to look directly into the sources of reflect.internal.Trees, which contains the AST.
If one needs to find out how a specific source code snippet is represented internally there is reify:
scala> import reflect.runtime.universe._ import reflect.runtime.universe._ scala> showRaw(reify{val i = 0}.tree) res8: String = Block(List(ValDef(Modifiers(), newTermName("i"), TypeTree(), Literal(Constant(0)))), Literal(Constant(())))
更多推荐
我在哪里可以了解如何为 Scala 宏构建 AST?
发布评论