stats是语句的缩写,而孩子是语句的缩写。我运行了这两个方法,结果似乎是一样的。有人能给我指出两者之间更详细的区别吗?
附加一个示例代码,可以在Scastie(https://scastie.scala-lang.org/A6huYabOTGmpZu9HRa6UZw)上运行它
val program = """
object Main {
def main(args: Array[String]): Unit = {
println("Hello Scalameta!")
}
def cats(args: Array[String]): Unit = {
println("Ancient cat here!")
}
def bats(args: Array[String]): Unit = {
println("Ancient bat here!")
}
def dragons = {
bats(List())
cats(List())
}
val ancient = dragons()
}
"""
import scala.meta._
val tree = program.parse[Source].get
println(tree.stats.head.children)
println("-----------------------\n")
tree.children.head.children.tail.head.children.foreach(child => {
println(s"${child.productPrefix} From: ${child.pos.startLine} and End: ${child.pos.endLine}")
println(child)
println("-----------------------\n")
})发布于 2021-03-26 07:00:16
每个Tree都有children: List[Tree],但只有Source (及其继承者)有stats: List[Stat]。
https://www.javadoc.io/doc/org.scalameta/trees_2.13/latest/scala/meta/Tree.html
https://www.javadoc.io/doc/org.scalameta/trees_2.13/latest/scala/meta/Source.html
https://stackoverflow.com/questions/66799588
复制相似问题