我在哪里可以了解如何为 Scala 宏构建 AST? [英] Where can I learn about constructing AST's for Scala macros?

查看:25
本文介绍了我在哪里可以了解如何为 Scala 宏构建 AST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里可以学习如何构建 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆