Scala演示编译器locateTree方法

时间:2016-03-19 18:25:46

标签: scala macros abstract-syntax-tree scala-macros

我一直在使用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

1 个答案:

答案 0 :(得分:0)

树的输入方式不同,因此您必须使用正确的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是用于检查而不是构建。