From 7be585a0a29ba70845c2a5ed2f52d50db06a8189 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sat, 24 Dec 2011 16:57:43 +0100 Subject: Upgraded the implementation of showRaw Now showRaw no longer significantly lags behind -Yreify-copypaste. Didn't put very much effort into it, since quite soon LiftCode will become a macro and we'll be able to reuse it in RawTreePrinter. --- src/library/scala/reflect/api/TreePrinters.scala | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/library/scala/reflect/api/TreePrinters.scala b/src/library/scala/reflect/api/TreePrinters.scala index 15fba0e418..88ef450ed9 100644 --- a/src/library/scala/reflect/api/TreePrinters.scala +++ b/src/library/scala/reflect/api/TreePrinters.scala @@ -28,7 +28,21 @@ trait TreePrinters { self: Universe => */ def newTreePrinter(out: PrintWriter): TreePrinter + // emits more or less verbatim representation of the provided tree + // todo. when LiftCode becomes a macro, throw this code away and use that macro class RawTreePrinter(out: PrintWriter) extends TreePrinter { + import scala.reflect.api.Modifier + import scala.reflect.api.Modifier._ + + def copypasteModifier(mod: Modifier.Value): String = mod match { + case mod @ ( + `protected` | `private` | `override` | + `abstract` | `final` | `sealed` | + `implicit` | `lazy` | `macro` | + `case` | `trait`) => "`" + mod.toString + "`" + case mod => mod.toString + } + def print(args: Any*): Unit = args foreach { case EmptyTree => print("EmptyTree") @@ -48,10 +62,35 @@ trait TreePrinters { self: Universe => case arg => print(arg) } - print(if (it.hasNext) ", " else ")") + print(if (it.hasNext) ", " else "") } + print(")") if (typesPrinted) print(".setType(", tree.tpe, ")") + case list: List[_] => + print("List(") + val it = list.iterator + while (it.hasNext) { + print(it.next()) + print(if (it.hasNext) ", " else "") + } + print(")") + case mods: Modifiers => + val parts = collection.mutable.ListBuffer[String]() + parts += "Set(" + mods.allModifiers.map{copypasteModifier}.mkString(", ") + ")" + parts += "newTypeName(\"" + mods.privateWithin.toString + "\")" + parts += "List(" + mods.annotations.map{showRaw}.mkString(", ") + ")" + + var keep = 3 + if (keep == 3 && mods.annotations.isEmpty) keep -= 1 + if (keep == 2 && mods.privateWithin == EmptyTypeName) keep -= 1 + if (keep == 1 && mods.allModifiers.isEmpty) keep -= 1 + + print("Modifiers(", parts.take(keep).mkString(", "), ")") + case name: Name => + if (name.isTermName) print("newTermName(\"") else print("newTypeName(\"") + print(name.toString) + print("\")") case arg => out.print(arg) } -- cgit v1.2.3