Skip to content

Add color to tasty printer #3529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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 = {
Expand All @@ -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(nameColor(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]").show)
}
def printTree(): Unit = {
newLine()
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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
}