编译器代码库非常庞大,我无法立刻解决这个问题:)
目前,我只想在“typer”阶段之后获得AST。有办法吗?
我按以下方式运行编译器:
val settings = new Settings
settings.classpath.value = ...
val compiler = new Global(settings, new ConsoleReporter(settings))
new compiler.Run() {
override def stopPhase(name: String) = name == "superaccessors"
} compileSources files
答案 0 :(得分:6)
使用-Xprint:typer
(在typer之后转储树)和-Yshow-trees-compact
(以原始AST格式转储树)。如果你也使用-Yshow-trees-stringified
,AST将被另外转储为伪Scala代码(注意:最后两个选项需要2.10.0)。
C:\Projects\Kepler\sandbox @ ticket/6356>cat Test.scala
class C {
def x = 2
}
C:\Projects\Kepler\sandbox @ ticket/6356>scalac -Xprint:typer -Yshow-trees-compact -Yshow-trees-stringified Test.scala
[[syntax trees at end of typer]]// Scala source: Test.scala
package <empty> {
class C extends scala.AnyRef {
def <init>(): C = {
C.super.<init>();
()
};
def x: Int = 2
}
}
PackageDef(
Ident(<empty>),
List(
ClassDef(Modifiers(), newTypeName("C"), List(),
Template(List(Select(Ident(scala), newTypeName("AnyRef"))), emptyValDef,
List(
DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(newTypeName("C")), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))),
DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), Literal(Constant(2))))))))
答案 1 :(得分:1)
编译器代码库非常庞大,我无法立刻解决这个问题:)
除了所有重要的typer之外,Scala编译器的大多数阶段都在以下详细描述:
http://lampwww.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/