From 9b0529c56f667fb6cec884b4542d88a5b8462380 Mon Sep 17 00:00:00 2001 From: paltherr Date: Thu, 11 Dec 2003 10:20:12 +0000 Subject: - Redesigned the tree printing scheme to enable... - Redesigned the tree printing scheme to enable printing of attributed trees --- sources/scala/tools/scalac/Global.scala | 10 +- .../tools/scalac/ast/printer/HTMLPrinter.scala | 2 +- .../tools/scalac/ast/printer/TextTreePrinter.scala | 31 +++---- sources/scalac/Global.java | 102 ++++++++++----------- sources/scalac/Phase.java | 7 -- sources/scalac/ast/printer/HTMLTreePrinter.java | 5 +- sources/scalac/ast/printer/TextTreePrinter.java | 34 +++---- sources/scalac/ast/printer/TreePrinter.java | 6 +- 8 files changed, 83 insertions(+), 114 deletions(-) (limited to 'sources') diff --git a/sources/scala/tools/scalac/Global.scala b/sources/scala/tools/scalac/Global.scala index cd0cd2d3ec..f0bf5ff96e 100644 --- a/sources/scala/tools/scalac/Global.scala +++ b/sources/scala/tools/scalac/Global.scala @@ -12,7 +12,7 @@ import scalac.ast.printer.TreePrinter; package scala.tools.scalac { import ast.printer._; -import java.io.OutputStream; +import java.io.PrintWriter; /** The global environment of a compiler run * @@ -21,10 +21,10 @@ class Global(args: CompilerCommand, interpret: boolean) extends scalac_Global(ar def this(args: CompilerCommand) = this(args, false); - protected override def newTextTreePrinter(printStream: OutputStream): TreePrinter = - new TextTreePrinter(printStream); - protected override def newHTMLTreePrinter(printStream: OutputStream): TreePrinter = - new HTMLTreePrinter(printStream); + protected override def newTextTreePrinter(writer: PrintWriter): TreePrinter = + new TextTreePrinter(writer); + protected override def newHTMLTreePrinter(writer: PrintWriter): TreePrinter = + new HTMLTreePrinter(writer); } } diff --git a/sources/scala/tools/scalac/ast/printer/HTMLPrinter.scala b/sources/scala/tools/scalac/ast/printer/HTMLPrinter.scala index 9cf4e4cf6b..1d7e07c930 100644 --- a/sources/scala/tools/scalac/ast/printer/HTMLPrinter.scala +++ b/sources/scala/tools/scalac/ast/printer/HTMLPrinter.scala @@ -25,7 +25,7 @@ import java.util.HashMap; * @version 1.0 */ -class HTMLTreePrinter(stream: OutputStream) extends TextTreePrinter(stream) { +class HTMLTreePrinter(writer: PrintWriter) extends TextTreePrinter(writer) { protected var outSectionLevel = 1; protected var started = false; diff --git a/sources/scala/tools/scalac/ast/printer/TextTreePrinter.scala b/sources/scala/tools/scalac/ast/printer/TextTreePrinter.scala index fe74bd5022..2b301cd055 100644 --- a/sources/scala/tools/scalac/ast/printer/TextTreePrinter.scala +++ b/sources/scala/tools/scalac/ast/printer/TextTreePrinter.scala @@ -11,7 +11,7 @@ import scalac.ast.printer._; import scalac.ast._; import scalac.symtab._; import scalac.util.Debug; -import scalac.{Global => scalac_Global}; +import scalac.{Global => scalac_Global, Phase}; import scalac.Unit; import scalac.util.Name; import scalac.util.TypeNames; @@ -26,20 +26,12 @@ import java.io._; * @author Michel Schinz, Matthias Zenger * @version 1.0 */ -class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { +class TextTreePrinter(writer: PrintWriter) with TreePrinter { - val out = _out; - - def this(stream: OutputStream, autoFlush: boolean) = - this(new PrintWriter(stream), autoFlush); - - def this(stream: OutputStream) = this(stream, false); - - def this(stream: Writer, autoFlush: boolean) = - this(new PrintWriter(stream), autoFlush); - - def this(stream: Writer) = this(stream, false); + val out = writer; + def this(writer: Writer) = this(new PrintWriter(writer)); + def this(stream: OutputStream) = this(new PrintWriter(stream)); def this() = this(System.out); protected var indentMargin = 0; @@ -54,13 +46,11 @@ class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { def print(str: String) = { out.print(str); - if (autoFlush) flush(); this } def println() = { out.println(); - if (autoFlush) flush(); this } @@ -79,7 +69,6 @@ class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { protected def printString(str: String) = { out.print(str); - if (autoFlush) flush(); } protected def printNewLine() = { @@ -89,7 +78,6 @@ class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { } if (indentMargin > 0) out.write(INDENT_STRING, 0, indentMargin); - if (autoFlush) flush(); } abstract class Text; @@ -204,6 +192,13 @@ class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { protected final val TXT_BAR_SP = Sequence(Predef.List(Space, TXT_BAR, Space)); + def print(global: scalac_Global): unit = { + val phase: Phase = global.currentPhase; + beginSection(1, "syntax trees at "+phase+" (after "+phase.prev+")"); + for (val i <- Iterator.range(0, global.units.length)) + print(global.units(i)); + } + def print(unit: Unit): unit = { printUnitHeader(unit); if (unit.body != null) { @@ -522,8 +517,6 @@ class TextTreePrinter(_out: PrintWriter, autoFlush: boolean) with TreePrinter { case _ => print(TXT_UNKNOWN); } - if (autoFlush) - flush(); return this; } diff --git a/sources/scalac/Global.java b/sources/scalac/Global.java index 874b718226..6f211a0094 100644 --- a/sources/scalac/Global.java +++ b/sources/scalac/Global.java @@ -8,11 +8,14 @@ package scalac; +import ch.epfl.lamp.util.CodePrinter; import ch.epfl.lamp.util.Position; import ch.epfl.lamp.util.SourceFile; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; import java.io.IOException; import java.io.OutputStream; import java.util.*; @@ -20,6 +23,7 @@ import java.util.*; import scalac.ast.*; import scalac.ast.parser.*; import scalac.ast.printer.*; +import scalac.atree.ATreePrinter; import scalac.backend.Primitives; // !!! <<< Interpreter stuff import scalac.symtab.*; @@ -86,9 +90,8 @@ public class Global { /** the global tree printer */ - public final TreePrinter printer; - public OutputStream printStream; - public final TreePrinter debugPrinter; + public final PrintWriter writer; + public final TreePrinter treePrinter; /** documentation comments of trees */ @@ -135,9 +138,9 @@ public class Global { public static final String TARGET_MSIL; public static final String[] TARGETS = new String[] { - TARGET_INT = "int".intern(), - TARGET_JVM = "jvm".intern(), - TARGET_MSIL = "msil".intern(), + TARGET_INT = "int", + TARGET_JVM = "jvm", + TARGET_MSIL = "msil", }; /** tree printers @@ -146,8 +149,8 @@ public class Global { public static final String PRINTER_HTML; public static final String[] PRINTERS = new String[] { - PRINTER_TEXT = "text".intern(), - PRINTER_HTML = "html".intern(), + PRINTER_TEXT = "text", + PRINTER_HTML = "html", }; /** @@ -161,11 +164,11 @@ public class Global { /** hooks for installing printers */ - protected TreePrinter newTextTreePrinter(OutputStream printStream) { - return new TextTreePrinter(printStream); + protected TreePrinter newTextTreePrinter(PrintWriter writer) { + return new TextTreePrinter(writer); } - protected TreePrinter newHTMLTreePrinter(OutputStream printStream) { - return new HTMLTreePrinter(printStream); + protected TreePrinter newHTMLTreePrinter(PrintWriter writer) { + return new HTMLTreePrinter(writer); } /** @@ -198,20 +201,23 @@ public class Global { args.separate.value.equals("default") && !this.target.equals(TARGET_INT); this.uniqueID = new UniqueID(); String printFile = args.printfile.value; + OutputStream stream; try { - this.printStream = "-".equals(printFile) + stream = "-".equals(printFile) ? System.out : new FileOutputStream(printFile); } catch (FileNotFoundException e) { error("unable to open file " + printFile + ". Printing on console"); - this.printStream = System.out; + stream = System.out; + } + this.writer = new PrintWriter(stream, debug); + if (args.printer.value.equals(PRINTER_HTML)) { + this.treePrinter = newHTMLTreePrinter(writer); + } else { + if (!args.printer.value.equals(PRINTER_TEXT)) + error("unknown printer kind: " + args.printer.value); + this.treePrinter = newTextTreePrinter(writer); } - String printerName = args.printer.value.intern(); - if (printerName == PRINTER_TEXT) - this.printer = newTextTreePrinter(printStream); - else - this.printer = newHTMLTreePrinter(printStream); - this.debugPrinter = new TextTreePrinter(System.err, true); this.freshNameCreator = new FreshNameCreator(); this.make = new DefaultTreeFactory(); this.PHASE = args.phases; @@ -289,7 +295,7 @@ public class Global { /** compile all compilation units */ private void compile() { - printer.begin(); + treePrinter.begin(); currentPhase = firstPhase; // apply successive phases and pray that it works @@ -299,14 +305,7 @@ public class Global { // !!! new scalac.checkers.SymbolChecker(this).check(); currentPhase.apply(units); stop(currentPhase.descriptor.taskDescription()); - if (currentPhase.descriptor.hasPrintFlag()) { - printer.beginSection(1, "Trees after phase " + currentPhase); - // go to next phase to print symbols with their new type - boolean next = currentPhase.next != null; - if (next) currentPhase = currentPhase.next; - (next ? currentPhase.prev : currentPhase).print(this); - if (next) currentPhase = currentPhase.prev; - } + if (currentPhase.descriptor.hasPrintFlag()) print(); if (currentPhase.descriptor.hasGraphFlag()) currentPhase.graph(this); if (currentPhase.descriptor.hasCheckFlag()) @@ -323,33 +322,26 @@ public class Global { } symdata.clear(); compiledNow.clear(); - printer.end(); - } - - /** transform a unit and stop at the current compilation phase - */ - public void transformUnit(Unit unit) { - Phase oldCurrentPhase = currentPhase; - currentPhase = PHASE.ANALYZER.phase().next; // or REFCHECK.next? - while ((currentPhase.id < oldCurrentPhase.id) && (reporter.errors() == 0)) { - start(); - currentPhase.apply(new Unit[] {unit}); // !!! pb with Analyzer - stop(currentPhase.descriptor.taskDescription()); - if (currentPhase.descriptor.hasPrintFlag()) { - printer.beginSection(1, "Trees after phase " + currentPhase); - // go to next phase to print symbols with their new type - boolean next = currentPhase.next != null; - if (next) currentPhase = currentPhase.next; - (next ? currentPhase.prev : currentPhase).print(this); - if (next) currentPhase = currentPhase.prev; - } - if (currentPhase.descriptor.hasGraphFlag()) - currentPhase.graph(this); - if (currentPhase.descriptor.hasCheckFlag()) - currentPhase.check(this); - currentPhase = currentPhase.next; + treePrinter.end(); + } + + private void print() { + if (currentPhase.id == PHASE.MAKEBOXINGEXPLICIT.id()) { + boolean html = args.printer.value.equals(PRINTER_HTML); + if (html) writer.println("
");
+            ATreePrinter printer = new ATreePrinter(new CodePrinter(writer));
+            boolean next = currentPhase.next != null;
+            if (next) currentPhase = currentPhase.next;
+            printer.printGlobal(this);
+            if (next) currentPhase = currentPhase.prev;
+            if (html) writer.println("
"); + } else { + // go to next phase to print symbols with their new type + boolean next = currentPhase.next != null; + if (next) currentPhase = currentPhase.next; + treePrinter.print(this); + if (next) currentPhase = currentPhase.prev; } - currentPhase = oldCurrentPhase; } // !!! <<< Interpreter stuff diff --git a/sources/scalac/Phase.java b/sources/scalac/Phase.java index 33190960bf..8ce7fd5c53 100644 --- a/sources/scalac/Phase.java +++ b/sources/scalac/Phase.java @@ -65,13 +65,6 @@ public abstract class Phase { /** Applies this phase to the given compilation units. */ public abstract void apply(Unit[] units); - /** Prints all compilation units. */ - public void print(Global global) { - TreePrinter printer = global.printer; - for (int i = 0; i < global.units.length; i++) - printer.print(global.units[i]); - } - /** Graphs all compilation units. */ public void graph(Global global) { for (int i = 0; i < global.units.length; i++) graph(global.units[i]); diff --git a/sources/scalac/ast/printer/HTMLTreePrinter.java b/sources/scalac/ast/printer/HTMLTreePrinter.java index 034ae42fb9..a4e6bb4f18 100644 --- a/sources/scalac/ast/printer/HTMLTreePrinter.java +++ b/sources/scalac/ast/printer/HTMLTreePrinter.java @@ -13,7 +13,6 @@ import scalac.Unit; import scalac.symtab.Symbol; import scalac.util.Name; -import java.io.OutputStream; import java.io.PrintWriter; import java.lang.Math; import java.util.HashMap; @@ -29,8 +28,8 @@ public class HTMLTreePrinter extends TextTreePrinter { protected int outSectionLevel = 1; protected boolean started = false; - public HTMLTreePrinter(OutputStream stream) { - super(stream); + public HTMLTreePrinter(PrintWriter writer) { + super(writer); } public void begin() { diff --git a/sources/scalac/ast/printer/TextTreePrinter.java b/sources/scalac/ast/printer/TextTreePrinter.java index 9e71518f23..730c53fe06 100644 --- a/sources/scalac/ast/printer/TextTreePrinter.java +++ b/sources/scalac/ast/printer/TextTreePrinter.java @@ -13,6 +13,7 @@ import scalac.ast.*; import scalac.symtab.*; import scalac.util.Debug; import scalac.Global; +import scalac.Phase; import scalac.Unit; import scalac.util.Name; import scalac.util.TypeNames; @@ -27,8 +28,7 @@ import java.util.*; * @version 1.0 */ public class TextTreePrinter implements TreePrinter { - protected PrintWriter out; - protected final boolean autoFlush; + protected final PrintWriter out; protected int indent = 0; protected final int INDENT_STEP = 2; @@ -36,22 +36,16 @@ public class TextTreePrinter implements TreePrinter { " "; protected final int MAX_INDENT = INDENT_STRING.length(); - public TextTreePrinter(OutputStream stream) { - this(stream, false); - } - - public TextTreePrinter(OutputStream stream, boolean autoFlush) { - this.autoFlush = autoFlush; - this.out = new PrintWriter(stream); + public TextTreePrinter(PrintWriter writer) { + this.out = writer; } - public TextTreePrinter(Writer stream) { - this(stream, false); + public TextTreePrinter(Writer writer) { + this(new PrintWriter(writer)); } - public TextTreePrinter(Writer stream, boolean autoFlush) { - this.autoFlush = autoFlush; - this.out = new PrintWriter(stream); + public TextTreePrinter(OutputStream stream) { + this(new PrintWriter(stream)); } public TextTreePrinter() { @@ -70,13 +64,11 @@ public class TextTreePrinter implements TreePrinter { public TreePrinter print(String str) { out.print(str); - if (autoFlush) flush(); return this; } public TreePrinter println() { out.println(); - if (autoFlush) flush(); return this; } @@ -95,7 +87,6 @@ public class TextTreePrinter implements TreePrinter { protected void printString(String str) { out.print(str); - if (autoFlush) flush(); } protected void printNewLine() { @@ -105,7 +96,6 @@ public class TextTreePrinter implements TreePrinter { } if (indent > 0) out.write(INDENT_STRING, 0, indent); - if (autoFlush) flush(); } public static class SymbolUsage { @@ -226,6 +216,12 @@ public class TextTreePrinter implements TreePrinter { protected static final Text TXT_BAR_SP = Text.Sequence(new Text[]{ Text.Space, TXT_BAR, Text.Space }); + public void print(Global global) { + Phase phase = global.currentPhase; + beginSection(1, "syntax trees at "+phase+" (after "+phase.prev+")"); + for (int i = 0; i < global.units.length; i++) print(global.units[i]); + } + public void print(Unit unit) { printUnitHeader(unit); if (unit.body != null) { @@ -621,8 +617,6 @@ public class TextTreePrinter implements TreePrinter { break; } //print("{" + tree.type + "}");//DEBUG - if (autoFlush) - flush(); return this; } diff --git a/sources/scalac/ast/printer/TreePrinter.java b/sources/scalac/ast/printer/TreePrinter.java index 059b335c72..53ce4bd3cb 100644 --- a/sources/scalac/ast/printer/TreePrinter.java +++ b/sources/scalac/ast/printer/TreePrinter.java @@ -8,8 +8,7 @@ package scalac.ast.printer; -import java.io.OutputStream; - +import scalac.Global; import scalac.Unit; import scalac.ast.Tree; @@ -24,8 +23,7 @@ public interface TreePrinter { public void end(); public void flush(); - public void beginSection(int level, String title); - + public void print(Global global); public void print(Unit unit); public TreePrinter print(Tree tree); -- cgit v1.2.3