@@ -70,21 +70,31 @@ object ShowPickled extends Names {
7070 case _ => " ***BAD TAG***(" + tag + " )"
7171 }
7272
73- def printFile (buf : PickleBuffer , out : PrintStream ) {
73+ def printFile (buf : PickleBuffer , out : PrintStream ): Unit = printFile(buf, out, false )
74+ def printFile (buf : PickleBuffer , out : PrintStream , bare : Boolean ) {
7475 out.println(" Version " + buf.readNat() + " ." + buf.readNat())
7576 val index = buf.createIndex
7677
78+ /** A print wrapper which discards everything if bare is true.
79+ */
80+ def p (s : String ) = if (! bare) out print s
81+
7782 def printNameRef () {
7883 val x = buf.readNat()
7984 val savedIndex = buf.readIndex
8085 buf.readIndex = index(x)
8186 val tag = buf.readByte()
8287 val len = buf.readNat()
83- out.print(" " + x + " (" + newTermName(buf.bytes, buf.readIndex, len) + " )" )
88+ val name = newTermName(buf.bytes, buf.readIndex, len)
89+
90+ val toPrint = if (bare) " " + name else " %s(%s)" .format(x, name)
91+ out print toPrint
92+
8493 buf.readIndex = savedIndex
8594 }
8695
87- def printNat () = out.print(" " + buf.readNat())
96+ def printNat () = p(" " + buf.readNat())
97+
8898 def printSymbolRef () = printNat()
8999 def printTypeRef () = printNat()
90100 def printConstantRef () = printNat()
@@ -107,12 +117,12 @@ object ShowPickled extends Names {
107117 */
108118 def printEntry (i : Int ) {
109119 buf.readIndex = index(i)
110- out.print (i + " ," + buf.readIndex + " : " )
120+ p (i + " ," + buf.readIndex + " : " )
111121 val tag = buf.readByte()
112122 out.print(tag2string(tag))
113123 val len = buf.readNat()
114124 val end = len + buf.readIndex
115- out.print (" " + len + " :" )
125+ p (" " + len + " :" )
116126 tag match {
117127 case TERMname =>
118128 out.print(" " )
@@ -196,26 +206,28 @@ object ShowPickled extends Names {
196206 for (i <- 0 until index.length) printEntry(i)
197207 }
198208
199- def fromBytes (what : String , data : => Array [Byte ]): Boolean = {
200- try {
201- val pickle = new PickleBuffer (data, 0 , data.length)
202- Console .println(what + " : " )
203- printFile(pickle, Console .out)
204- true
205- }
206- catch {
207- case _ : Exception => false
208- }
209- }
209+ def fromFile (path : String ) = fromBytes(io.File (path).toByteArray)
210+ def fromName (name : String ) = fromBytes(scalaSigBytesForPath(name) getOrElse Array ())
211+ def fromBytes (data : => Array [Byte ]): Option [PickleBuffer ] =
212+ try Some (new PickleBuffer (data, 0 , data.length))
213+ catch { case _ : Exception => None }
210214
211- def fromFile (path : String ) = fromBytes(path, io.File (path).toByteArray)
212- def fromName (name : String ) = fromBytes(name, scalaSigBytesForPath(name) getOrElse Array ())
215+ def show (what : String , pickle : PickleBuffer , bare : Boolean ) = {
216+ Console .println(what + " : " )
217+ printFile(pickle, Console .out, bare)
218+ }
213219
220+ /** Option --bare suppresses numbers so the output can be diffed.
221+ */
214222 def main (args : Array [String ]) {
215- args foreach { arg =>
216- val res = fromFile(arg) || fromName(arg)
217- if (! res)
218- Console .println(" Cannot read " + arg)
223+ val parsed = new CommandLine (args, List (" --bare" ))
224+ def isBare = parsed isSet " --bare"
225+
226+ parsed.residualArgs foreach { arg =>
227+ (fromFile(arg) orElse fromName(arg)) match {
228+ case Some (pb) => show(arg, pb, isBare)
229+ case _ => Console .println(" Cannot read " + arg)
230+ }
219231 }
220232 }
221233}
0 commit comments