diff options
author | Martin Odersky <odersky@gmail.com> | 2011-11-19 18:41:06 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2011-11-19 18:41:06 +0000 |
commit | 943d2cfb07cb978e096eead09dc24581a0974333 (patch) | |
tree | 91e529784cc81733a07e1dcad01024975aa9cec2 /src/library | |
parent | 6d5a16b3821375386b09bfd944be8ec93bd7e82c (diff) | |
download | scala-943d2cfb07cb978e096eead09dc24581a0974333.tar.gz scala-943d2cfb07cb978e096eead09dc24581a0974333.tar.bz2 scala-943d2cfb07cb978e096eead09dc24581a0974333.zip |
Partial cleanup and generalization of tree prin...
Partial cleanup and generalization of tree printing. You can now print
a tree in direct case class form with `showRaw(tree)`. Should make
NodePrinters redundant.
Diffstat (limited to 'src/library')
-rwxr-xr-x | src/library/scala/reflect/api/Symbols.scala | 3 | ||||
-rw-r--r-- | src/library/scala/reflect/api/TreePrinters.scala | 59 | ||||
-rw-r--r-- | src/library/scala/reflect/api/Trees.scala | 4 | ||||
-rwxr-xr-x | src/library/scala/reflect/api/Universe.scala | 1 |
4 files changed, 63 insertions, 4 deletions
diff --git a/src/library/scala/reflect/api/Symbols.scala b/src/library/scala/reflect/api/Symbols.scala index 3c4408aacd..31bcdebe7e 100755 --- a/src/library/scala/reflect/api/Symbols.scala +++ b/src/library/scala/reflect/api/Symbols.scala @@ -36,6 +36,9 @@ trait Symbols { self: Universe => */ def fullName: String + /** An id number which is unique for all symbols in this universe */ + def id: Int + /** * Set when symbol has a modifier of the form private[X], NoSymbol otherwise. * diff --git a/src/library/scala/reflect/api/TreePrinters.scala b/src/library/scala/reflect/api/TreePrinters.scala new file mode 100644 index 0000000000..15fba0e418 --- /dev/null +++ b/src/library/scala/reflect/api/TreePrinters.scala @@ -0,0 +1,59 @@ +package scala.reflect +package api + +import java.io.{ PrintWriter, StringWriter } + +trait TreePrinters { self: Universe => + + trait TreePrinter { + def print(args: Any*) + protected var typesPrinted = false + protected var uniqueIds = false + def withTypesPrinted: this.type = { typesPrinted = true; this } + def withUniqueIds: this.type = { uniqueIds = true; this } + } + + def show(tree: Tree, mkPrinter: PrintWriter => TreePrinter = newTreePrinter): String = { + val buffer = new StringWriter() + val writer = new PrintWriter(buffer) + val printer = mkPrinter(writer) + printer.print(tree) + writer.flush() + buffer.toString + } + + def showRaw(tree: Tree): String = show(tree, new RawTreePrinter(_)) + + /** Hook to define what `show(tree)` means. + */ + def newTreePrinter(out: PrintWriter): TreePrinter + + class RawTreePrinter(out: PrintWriter) extends TreePrinter { + def print(args: Any*): Unit = args foreach { + case EmptyTree => + print("EmptyTree") + case tree @ TypeTree() => + print("TypeTree()") + if (tree.tpe != null) + print(".setType(", tree.tpe, ")") + else if (tree.original != null) + print(".setOriginal(", tree.original, ")") + case tree: Tree => + print(tree.productPrefix+"(") + val it = tree.productIterator + while (it.hasNext) { + it.next() match { + case name: Name if uniqueIds && tree.hasSymbol && tree.symbol != NoSymbol => + print(tree.symbol.name, "#", tree.symbol.id) + case arg => + print(arg) + } + print(if (it.hasNext) ", " else ")") + } + if (typesPrinted) + print(".setType(", tree.tpe, ")") + case arg => + out.print(arg) + } + } +} diff --git a/src/library/scala/reflect/api/Trees.scala b/src/library/scala/reflect/api/Trees.scala index 64793fb303..478c47a210 100644 --- a/src/library/scala/reflect/api/Trees.scala +++ b/src/library/scala/reflect/api/Trees.scala @@ -16,10 +16,6 @@ trait Trees /*extends reflect.generic.Trees*/ { self: Universe => type Modifiers <: AbsModifiers - /** Hook to define what toString means on a tree - */ - def show(tree: Tree): String - abstract class AbsModifiers { def hasModifier(mod: Modifier.Value): Boolean def allModifiers: Set[Modifier.Value] diff --git a/src/library/scala/reflect/api/Universe.scala b/src/library/scala/reflect/api/Universe.scala index ad145b12ac..03acbdda2c 100755 --- a/src/library/scala/reflect/api/Universe.scala +++ b/src/library/scala/reflect/api/Universe.scala @@ -8,6 +8,7 @@ abstract class Universe extends Symbols with Names with Trees with Positions + with TreePrinters with AnnotationInfos with StandardDefinitions { type Position |