Scala 演示编译器的 locateTree 方法

Scala presentation compiler locateTree method

我一直在使用 scala 表示编译器 API,或者更准确地说,它是 locateTree 方法来获取一些源代码的 AST,然后通过 showRaw(ast) 调用,但结果似乎与我预期的不同。例如

val tree = q"final def x = 1"
println(showRaw(tree))

输出 DefDef(Modifiers(FINAL), TermName("x"), List(), List(), TypeTree(), Literal(Constant(1))),而在同一源上调用演示编译器会生成 DefDef(Modifiers(32, , List()), x, List(), List(), TypeTree(), Literal(Constant(1)))(请注意 x 未包含在 TermName 中,并且 Modifiers 参数不同列表)。为什么会发生这种情况以及如何在演示编译器上强制执行类似的行为?

编辑: scala 版本为 2.11.8

树的类型不同,因此您必须使用正确的 show:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> import scala.tools.nsc._
import scala.tools.nsc._

scala> val ss = new Settings(println)
ss: scala.tools.nsc.Settings =
Settings {
  -d = .
}

scala> val g = new interactive.Global(ss, new reporters.ConsoleReporter(ss), "testing")
g: scala.tools.nsc.interactive.Global = scala.tools.nsc.interactive.Global@4c6007fb

scala> val tt = g.newUnitParser("final def x = 1").parseStats
tt: List[g.syntaxAnalyzer.global.Tree] = List(final def x = 1)

scala> reflect.runtime.universe.show(tt.head)
res0: String = final def x = 1

scala> reflect.runtime.universe.showRaw(tt.head)
res1: String = DefDef(Modifiers(32, , List()), x, List(), List(), TypeTree(), Literal(Constant(1)))

scala> g.showRaw(tt)
res2: String = List(DefDef(Modifiers(FINAL), TermName("x"), List(), List(), TypeTree(), Literal(Constant(1))))

res1 没有类型安全,所以你不知道这棵树来自不同的宇宙。

由于位置注释,树在修饰符上略有不同:

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> import scala.tools.nsc._
import scala.tools.nsc._

scala> val ss = new Settings(println)
ss: scala.tools.nsc.Settings =
Settings {
  -d = .
}

scala> new interactive.Global(ss, new reporters.ConsoleReporter(ss), "testing")
res0: scala.tools.nsc.interactive.Global = scala.tools.nsc.interactive.Global@4c6007fb

scala> val cc = res0
cc: scala.tools.nsc.interactive.Global = scala.tools.nsc.interactive.Global@4c6007fb

scala> val tt = cc.newUnitParser("final def x = 1").parseStats
tt: List[cc.syntaxAnalyzer.global.Tree] = List(final def x = 1)

scala> val cc.DefDef(mods, nam, ps, vs, tpt, rhs) = tt.head
mods: cc.Modifiers = Modifiers(final, , Map(32 -> RangePosition(<console>, 0, 0, 4), 72 -> RangePosition(<console>, 6, 6, 8)))
nam: cc.TermName = x
ps: List[cc.TypeDef] = List()
vs: List[List[cc.ValDef]] = List()
tpt: cc.Tree = <type ?>
rhs: cc.Tree = 1

相对于:

scala> import reflect.runtime._, universe._
import reflect.runtime._
import universe._

scala> val DefDef(mods, nam, ps, vs, tpt, rhs) = q"final def x = 1"
mods: reflect.runtime.universe.Modifiers = Modifiers(final, , Map())
nam: reflect.runtime.universe.TermName = x
ps: List[reflect.runtime.universe.TypeDef] = List()
vs: List[List[reflect.runtime.universe.ValDef]] = List()
tpt: reflect.runtime.universe.Tree = <type ?>
rhs: reflect.runtime.universe.Tree = 1

原来annotations只显示显式的:

scala> mods.annotations
res13: List[reflect.runtime.universe.Tree] = List()

值得一提的是 the docshowRaw 是检查而不是施工。