diff options
author | Sean McDirmid <sean.mcdirmid@gmail.com> | 2005-11-14 16:58:22 +0000 |
---|---|---|
committer | Sean McDirmid <sean.mcdirmid@gmail.com> | 2005-11-14 16:58:22 +0000 |
commit | d11a5ec080a8fd0890047a0f7d91b938bc7bf1b6 (patch) | |
tree | cb67756886c8568b701f509acab69d559fc833ea /sources/scala/tools/nsc/reporters | |
parent | c3a4c7ee6edf7bb442b5beb2d1c97c76b29562d3 (diff) | |
download | scala-d11a5ec080a8fd0890047a0f7d91b938bc7bf1b6.tar.gz scala-d11a5ec080a8fd0890047a0f7d91b938bc7bf1b6.tar.bz2 scala-d11a5ec080a8fd0890047a0f7d91b938bc7bf1b6.zip |
Updated NSC to use offsets instead of line/colu...
Updated NSC to use offsets instead of line/column positions.
Diffstat (limited to 'sources/scala/tools/nsc/reporters')
4 files changed, 256 insertions, 0 deletions
diff --git a/sources/scala/tools/nsc/reporters/AbstractReporter.scala b/sources/scala/tools/nsc/reporters/AbstractReporter.scala new file mode 100644 index 0000000000..b85fde78ca --- /dev/null +++ b/sources/scala/tools/nsc/reporters/AbstractReporter.scala @@ -0,0 +1,56 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package scala.tools.nsc.reporters; +import java.util.HashSet; +import scala.tools.nsc.util.Position; + +/** + * This abstract class implements most aspects of a Reporter, only how + * things are displayed has to be implemented in subclasses. + */ +abstract class AbstractReporter extends Reporter { + private val positions = new HashSet(); + + def displayInfo (pos : Position, msg : String) : Unit; + def displayWarning(pos : Position, msg : String) : Unit; + def displayError (pos : Position, msg : String) : Unit; + def displayPrompt : Unit; + + // XXX: while is pos ignored? + def info(pos : Position, msg : String, force : Boolean) : Unit = + if (force || verbose) displayInfo(pos, msg); + + def warning(pos : Position, msg : String) : Unit = { + val hidden = testAndLog(pos); + if (nowarn) return; + if (!hidden || prompt) displayWarning(pos, msg); + if (!hidden) warningsx = warningsx + 1; + if (prompt) displayPrompt; + } + def error(pos : Position, msg : String) : Unit = { + val hidden = testAndLog(pos); + if (!hidden || prompt) displayError(pos, msg); + if (!hidden) errorsx = errorsx + 1; + if (prompt) displayPrompt; + } + + //######################################################################## + // Private Methods + + /** Logs a position and returns true if it was already logged. */ + private def testAndLog(pos : Position) : Boolean = { + if (pos == null) return false; + if (pos.column == 0) return false; + if (positions.contains(pos)) return true; + positions.add(pos); + return false; + } + + //######################################################################## +} diff --git a/sources/scala/tools/nsc/reporters/ConsoleReporter.scala b/sources/scala/tools/nsc/reporters/ConsoleReporter.scala new file mode 100644 index 0000000000..8582c868bf --- /dev/null +++ b/sources/scala/tools/nsc/reporters/ConsoleReporter.scala @@ -0,0 +1,135 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package scala.tools.nsc.reporters; +import scala.tools.nsc.util.Position; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * This class implements a Reporter that displays messages on a text + * console. + */ +class ConsoleReporter(_reader : BufferedReader, _writer : PrintWriter) extends AbstractReporter { + + //######################################################################## + // Private Fields + + /** The reader to ask for failures on demand */ + private val reader = _reader; + /** The writer to print messages */ + private val writer = _writer; + + + //######################################################################## + // Public Fields + + /** Whether a short file name should be displayed before errors */ + var shortname : Boolean = false; + + //######################################################################## + // Public Constructors + def this() = this( + new BufferedReader(new InputStreamReader(System.in)), + new PrintWriter(System.err, true)); + + //######################################################################## + // Public Methods - Count + + /** Returns the number of errors issued totally as a string */ + def getErrorCountString = getCountString(errorsx, "error"); + + /** Returns the number of warnings issued totally as a string */ + def getWarningCountString = getCountString(warningsx, "warning"); + + /** Returns a string meaning "n elements". */ + def getCountString(n : Int, elements : String) : String = + n match { + case 0 => "no " + elements + "s"; + case 1 => "one " + elements; + case 2 => "two " + elements + "s"; + case 3 => "three " + elements + "s"; + case 4 => "four " + elements + "s"; + "" + n + " " + elements + "s"; + } + + + //######################################################################## + // Public Methods - Print + + /** Prints the message. */ + def printMessage(msg : String) = writer.println(msg); + + /** Prints the message with the given position indication. */ + def printMessage(pos : Position, msg : String) : Unit = { + if (pos != null) { + val buf = new StringBuffer(msg); + buf.insert(0, " "); + if (pos.line != Position.NOLINE) + buf.insert(0, ":" + pos.line); + val file = pos.source.file; + buf.insert(0, if (shortname) file.getName() else file.getPath()); + printMessage(buf.toString()); + printSourceLine(pos); + } else printMessage(msg); + } + + def printWarning(pos : Position, msg : String) = printMessage(pos, "warning: " + msg); + def printError (pos : Position, msg : String) = printMessage(pos, "error: " + msg); + + def printSourceLine(pos : Position) = if (pos != null && pos.offset != Position.NOPOS) { + printMessage(pos.lineContent); + printColumnMarker(pos); + } + /** Prints the column marker of the given position. */ + def printColumnMarker(pos : Position) = if (pos != null) { + val buffer = new StringBuffer(pos.column); + var i = 1; + while (i < pos.column) { + buffer.append(' '); + i = i + 1; + } + if (pos.column > 0) buffer.append('^'); + printMessage(buffer.toString()); + } + + /** Prints the number of errors and warnings if their are non-zero. */ + def printSummary() = { + if (warningsx > 0) printMessage(getWarningCountString + " found"); + if ( errorsx > 0) printMessage(getErrorCountString + " found"); + } + + //######################################################################## + // Public Methods - Display + def displayInfo (pos : Position, msg : String) : Unit = printMessage(pos, msg); + def displayWarning(pos : Position, msg : String) : Unit = printWarning(pos, msg); + def displayError (pos : Position, msg : String) : Unit = printError (pos, msg); + + def displayPrompt : Unit = try { + var continue = true; + while (continue) { + writer.print("r)esume, a)bort: "); + writer.flush(); + var line = reader.readLine(); + if (line != null) { + line = line.toLowerCase(); + if ("abort".startsWith(line)) + throw new Error("user abort"); + if ("resume".startsWith(line)) continue = false; + } + } + } catch { + case ex: IOException => { + ex.printStackTrace(); + throw new Error("input read error"); + } + } +} diff --git a/sources/scala/tools/nsc/reporters/Reporter.scala b/sources/scala/tools/nsc/reporters/Reporter.scala new file mode 100644 index 0000000000..3687ea3b66 --- /dev/null +++ b/sources/scala/tools/nsc/reporters/Reporter.scala @@ -0,0 +1,38 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package scala.tools.nsc.reporters; +import scala.tools.nsc.util.Position; + + +/** + * This interface provides methods to issue information, warning and + * error messages. + */ +abstract class Reporter { + var verbose : Boolean = false; + var nowarn : Boolean = false; + var prompt : Boolean = false; + + def prompt(v : Boolean) : Unit = this.prompt = v; + + def warnings() = warningsx; + def errors() = errorsx; + + protected var warningsx : Int = 0; + protected var errorsx : Int = 0; + def resetCounters() : Unit = { + warningsx = 0; + errorsx = 0; + } + + def info(pos : Position, msg : String, force : Boolean) : Unit; + def warning(pos : Position, msg : String ) : Unit; + def error(pos : Position, msg : String ) : Unit; + +} diff --git a/sources/scala/tools/nsc/reporters/ReporterTimer.scala b/sources/scala/tools/nsc/reporters/ReporterTimer.scala new file mode 100644 index 0000000000..5c0cfd4092 --- /dev/null +++ b/sources/scala/tools/nsc/reporters/ReporterTimer.scala @@ -0,0 +1,27 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package scala.tools.nsc.reporters; +import scala.tools.util.AbstractTimer; + +/** + * This class implements a timer that uses a Reporter to issue + * timings. + */ +class ReporterTimer(_reporter : Reporter) extends AbstractTimer { + //######################################################################## + // Private Fields + private val reporter = _reporter; + //######################################################################## + // Public Methods + + def issue(msg : String, duration : Long) = + reporter.info(null, "[" + msg + " in " + duration + "ms]", false); + + //######################################################################## +} |