@@ -11,6 +11,8 @@ import printing.Highlighting._
11
11
12
12
class TastyPrinter (bytes : Array [Byte ])(implicit ctx : Context ) {
13
13
14
+ private [this ] val sb : StringBuilder = new StringBuilder
15
+
14
16
val unpickler : TastyUnpickler = new TastyUnpickler (bytes)
15
17
import unpickler .{nameAtRef , unpickle }
16
18
@@ -21,41 +23,56 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
21
23
def printNames (): Unit =
22
24
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
23
25
val index = nameColor(" %4d" .format(idx))
24
- println (index + " : " + nameToString(name))
26
+ sb.append (index + " : " + nameToString(name) + " \n " )
25
27
}
26
28
27
- def printContents (): Unit = {
28
- println (" Names:" )
29
+ def printContents (): String = {
30
+ sb.append (" Names:\n " )
29
31
printNames()
30
- println()
31
- println(" Trees:" )
32
- unpickle(new TreeSectionUnpickler )
33
- unpickle(new PositionSectionUnpickler )
34
- unpickle(new CommentSectionUnpickler )
32
+ sb.append(" \n " )
33
+ sb.append(" Trees:\n " )
34
+ unpickle(new TreeSectionUnpickler ) match {
35
+ case Some (s) => sb.append(s)
36
+ case _ => Unit
37
+ }
38
+ sb.append(" \n\n " )
39
+ unpickle(new PositionSectionUnpickler ) match {
40
+ case Some (s) => sb.append(s)
41
+ case _ => Unit
42
+ }
43
+ sb.append(" \n\n " )
44
+ unpickle(new CommentSectionUnpickler ) match {
45
+ case Some (s) => sb.append(s)
46
+ case _ => Unit
47
+ }
48
+ sb.result
35
49
}
36
50
37
- class TreeSectionUnpickler extends SectionUnpickler [Unit ](TreePickler .sectionName) {
51
+ class TreeSectionUnpickler extends SectionUnpickler [String ](TreePickler .sectionName) {
38
52
import TastyFormat ._
39
- def unpickle (reader : TastyReader , tastyName : NameTable ): Unit = {
53
+
54
+ private [this ] val sb : StringBuilder = new StringBuilder
55
+
56
+ def unpickle (reader : TastyReader , tastyName : NameTable ): String = {
40
57
import reader ._
41
58
var indent = 0
42
59
def newLine () = {
43
60
val length = treeColor(" %5d" .format(index(currentAddr) - index(startAddr)))
44
- print (s " \n $length: " + " " * indent)
61
+ sb.append (s " \n $length: " + " " * indent)
45
62
}
46
- def printNat () = print (Yellow (" " + readNat()).show)
63
+ def printNat () = sb.append (Yellow (" " + readNat()).show)
47
64
def printName () = {
48
65
val idx = readNat()
49
- print (nameColor(" " + idx + " [" + nameRefToString(NameRef (idx)) + " ]" ))
66
+ sb.append (nameColor(" " + idx + " [" + nameRefToString(NameRef (idx)) + " ]" ))
50
67
}
51
68
def printTree (): Unit = {
52
69
newLine()
53
70
val tag = readByte()
54
- print (" " );print (astTagToString(tag))
71
+ sb.append (" " );sb.append (astTagToString(tag))
55
72
indent += 2
56
73
if (tag >= firstLengthTreeTag) {
57
74
val len = readNat()
58
- print (s " ( ${lengthColor(len.toString)}) " )
75
+ sb.append (s " ( ${lengthColor(len.toString)}) " )
59
76
val end = currentAddr + len
60
77
def printTrees () = until(end)(printTree())
61
78
tag match {
@@ -76,7 +93,7 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
76
93
printTrees()
77
94
}
78
95
if (currentAddr != end) {
79
- println (s " incomplete read, current = $currentAddr, end = $end" )
96
+ sb.append (s " incomplete read, current = $currentAddr, end = $end\n " )
80
97
goto(end)
81
98
}
82
99
}
@@ -96,42 +113,51 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
96
113
}
97
114
indent -= 2
98
115
}
99
- println (i " start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr" )
100
- println (s " ${endAddr.index - startAddr.index} bytes of AST, base = $currentAddr" )
116
+ sb.append (i " start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr\n " )
117
+ sb.append (s " ${endAddr.index - startAddr.index} bytes of AST, base = $currentAddr\n " )
101
118
while (! isAtEnd) {
102
119
printTree()
103
120
newLine()
104
121
}
122
+ sb.result
105
123
}
106
124
}
107
125
108
- class PositionSectionUnpickler extends SectionUnpickler [Unit ](" Positions" ) {
109
- def unpickle (reader : TastyReader , tastyName : NameTable ): Unit = {
110
- print(s " ${reader.endAddr.index - reader.currentAddr.index}" )
126
+ class PositionSectionUnpickler extends SectionUnpickler [String ](" Positions" ) {
127
+
128
+ private [this ] val sb : StringBuilder = new StringBuilder
129
+
130
+ def unpickle (reader : TastyReader , tastyName : NameTable ): String = {
131
+ sb.append(s " ${reader.endAddr.index - reader.currentAddr.index}" )
111
132
val positions = new PositionUnpickler (reader).positions
112
- println (s " position bytes: " )
133
+ sb.append (s " position bytes: \n " )
113
134
val sorted = positions.toSeq.sortBy(_._1.index)
114
135
for ((addr, pos) <- sorted) {
115
- print (treeColor(" %10d" .format(addr.index)))
116
- println (s " : ${offsetToInt(pos.start)} .. ${pos.end}" )
136
+ sb.append (treeColor(" %10d" .format(addr.index)))
137
+ sb.append (s " : ${offsetToInt(pos.start)} .. ${pos.end}\n " )
117
138
}
139
+ sb.result
118
140
}
119
141
}
120
142
121
- class CommentSectionUnpickler extends SectionUnpickler [Unit ](" Comments" ) {
122
- def unpickle (reader : TastyReader , tastyName : NameTable ): Unit = {
123
- print(s " ${reader.endAddr.index - reader.currentAddr.index}" )
143
+ class CommentSectionUnpickler extends SectionUnpickler [String ](" Comments" ) {
144
+
145
+ private [this ] val sb : StringBuilder = new StringBuilder
146
+
147
+ def unpickle (reader : TastyReader , tastyName : NameTable ): String = {
148
+ sb.append(s " ${reader.endAddr.index - reader.currentAddr.index}" )
124
149
val comments = new CommentUnpickler (reader).comments
125
- println (s " comment bytes: " )
150
+ sb.append (s " comment bytes: \n " )
126
151
val sorted = comments.toSeq.sortBy(_._1.index)
127
152
for ((addr, cmt) <- sorted) {
128
- print (treeColor(" %10d" .format(addr.index)))
129
- println (s " : ${cmt.raw} (expanded = ${cmt.isExpanded}) " )
153
+ sb.append (treeColor(" %10d" .format(addr.index)))
154
+ sb.append (s " : ${cmt.raw} (expanded = ${cmt.isExpanded}) \n " )
130
155
}
156
+ sb.result
131
157
}
132
158
}
133
159
134
- private def nameColor (str : String ): String = Magenta (str).show
135
- private def treeColor (str : String ): String = Yellow (str).show
136
- private def lengthColor (str : String ): String = Cyan (str).show
160
+ protected def nameColor (str : String ): String = Magenta (str).show
161
+ protected def treeColor (str : String ): String = Yellow (str).show
162
+ protected def lengthColor (str : String ): String = Cyan (str).show
137
163
}
0 commit comments