Skip to content

Instantly share code, notes, and snippets.

@Rogach
Created January 8, 2012 06:17
Show Gist options
  • Save Rogach/1577445 to your computer and use it in GitHub Desktop.
Save Rogach/1577445 to your computer and use it in GitHub Desktop.
Trying to get simple syntax tree out of scala compiler
import scala.tools.nsc.interactive.{Global, RefinedBuildManager}
import scala.tools.nsc.Settings
import scala.tools.nsc.reporters.StoreReporter
import scala.tools.nsc.io._
import scala.tools.nsc.util._
import scala.tools.nsc.interactive.Response
import scala.tools.nsc.util.{Position, OffsetPosition}
import scala.reflect.generic.Trees
val settings = new Settings
val cmp = new Global(settings, new StoreReporter())
val src = new BatchSourceFile("<source>", "package scct { class A { val i = 1}}")
val r = new Response[cmp.Tree]()
cmp.askLoadedTyped(src, r)
r.get match {
case Left(data) =>
def pT(t:cmp.Tree):List[String] = {
t match {
// case Ident(name) => println("Got identifier: %s" format name)
case _ =>
}
List(
("class = %s" format t.getClass),
("pos = %s" format t.pos),
("pos.getClass = %s" format t.pos.getClass),
("tpe = %s" format t.tpe),
("symbol = %s" format t.symbol),
""
) ::: t.children.filter(_.pos != NoPosition).flatMap(pT(_)).map(" " + _)
}
pT(data).foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment