From a3f4f0671fc4aa07909d3040c380608fd21515ab Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 22 Nov 2017 17:34:37 +0100 Subject: [PATCH 1/2] Add color to tasty printer Makes clear the distinction between name indices, tree indices and tree lengths. --- .../tools/dotc/core/tasty/TastyPrinter.scala | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index e74e5166062f..3d917e31495a 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -10,6 +10,7 @@ import TastyUnpickler._ import TastyBuffer.{Addr, NameRef} import util.Positions.{Position, offsetToInt} import collection.mutable +import printing.Highlighting._ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { @@ -22,8 +23,8 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { def printNames() = for ((name, idx) <- nameAtRef.contents.zipWithIndex) { - val index = "%4d: ".format(idx) - println(index + nameToString(name)) + val index = nameColor("%4d".format(idx)) + println(index + ": " + nameToString(name)) } def printContents(): Unit = { @@ -41,13 +42,13 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { import reader._ var indent = 0 def newLine() = { - val length = "%5d:".format(index(currentAddr) - index(startAddr)) - print(s"\n $length" + " " * indent) + val length = treeColor("%5d".format(index(currentAddr) - index(startAddr))) + print(s"\n $length:" + " " * indent) } - def printNat() = print(" " + readNat()) + def printNat() = print(Yellow(" " + readNat()).show) def printName() = { val idx = readNat() - print(" ") ;print(idx); print("["); print(nameRefToString(NameRef(idx))); print("]") + print(Magenta(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]").show) } def printTree(): Unit = { newLine() @@ -56,7 +57,7 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { indent += 2 if (tag >= firstLengthTreeTag) { val len = readNat() - print(s"($len)") + print(s"(${lengthColor(len.toString)})") val end = currentAddr + len def printTrees() = until(end)(printTree()) tag match { @@ -83,7 +84,7 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { } else if (tag >= firstNatASTTreeTag) { tag match { - case IDENT | SELECT | TERMREF | TYPEREF | SELFDEF => printName() + case IDENT | IDENTtpt | SELECT | TERMREF | TYPEREF | SELFDEF => printName() case _ => printNat() } printTree() @@ -108,11 +109,18 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { class PositionSectionUnpickler extends SectionUnpickler[Unit]("Positions") { def unpickle(reader: TastyReader, tastyName: NameTable): Unit = { - print(s"${reader.endAddr.index - reader.currentAddr.index}") + print(s" ${reader.endAddr.index - reader.currentAddr.index}") val positions = new PositionUnpickler(reader).positions println(s" position bytes:") val sorted = positions.toSeq.sortBy(_._1.index) - for ((addr, pos) <- sorted) println(s" ${addr.index}: ${offsetToInt(pos.start)} .. ${pos.end}") + for ((addr, pos) <- sorted) { + print(treeColor("%10d".format(addr.index))) + println(s": ${offsetToInt(pos.start)} .. ${pos.end}") + } } } + + private def nameColor(str: String): String = Magenta(str).show + private def treeColor(str: String): String = Yellow(str).show + private def lengthColor(str: String): String = Cyan(str).show } From 4e6e2d122e904850ab38d2d5dfbce2e7a1a44977 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 23 Nov 2017 14:10:59 +0100 Subject: [PATCH 2/2] Add missing nameColor abstraction --- compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index 3d917e31495a..bde686b1522e 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -48,7 +48,7 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { def printNat() = print(Yellow(" " + readNat()).show) def printName() = { val idx = readNat() - print(Magenta(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]").show) + print(nameColor(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]").show) } def printTree(): Unit = { newLine()