我正在使用Scala编译器API中的工具箱,将代码编译成AST,然后将它们分解/拆分,然后将它们组合成一棵树。出于调试目的,我试图跟踪哪些节点来自哪个源代码。
示例:
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
// obtain toolbox
val tb = runtimeMirror(this.getClass.getClassLoader).mkToolBox()
// get and parse source code from file
val myCode = scala.io.Source.fromFile("MyCode.scala").mkString
val myTree = tb.parse(myCode)
// get and parse dynamically-generated source code
val genCode = com.example.CodeGenerator.gimmeCode
val genTree = tb.parse(genCode)
// get and parse source code from a string literal
val literalCode = """println("to life, the universe, and everything")"""
val literalTree = tb.parse(literalCode)
// an over-simplified combination of the trees
val frankensteinsTree = q"$myTree;$genTree;$literalTree"
// walk the tree an print the source of each element
val traverser = new Traverser() {
override def traverse(tree: Tree): Unit = {
println("This node originated from " + tree.pos.source)
super.traverse(tree)
}
}
// the root element prints "This node originated from <no source file>"
// the rest print "This node originated from <toolbox>"
traverser.traverse(frankensteinsTree)
在上面的示例中,除根节点以外的所有节点都表明源是<toolbox>
。 (根节点说<no source file>
。)在解剖和重组它们之前,是否可以指定tree.pos.source来标识每个节点的实际来源?
答案 0 :(得分:1)
你是说
println("Position is: " + "MyCode" + tree.pos.toString.stripPrefix("source-<toolbox>"))
// Position is: MyCode,line-1,offset=7
?
尝试
val p = tree.pos
val pointMessage = if (p.point > p.source.length) "out-of-bounds-" else "offset="
println(
s"source-${p.source.file.canonicalPath},INSERT WHAT YOU WANT,line-${p.line},$pointMessage${p.point}"
) // source-<toolbox>,INSERT WHAT YOU WANT,line-1,offset=7
答案 1 :(得分:0)
不,目前(截至2020-05-28)无法完成。