diff options
author | Martin Odersky <odersky@gmail.com> | 2013-05-10 16:48:47 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2013-05-10 16:52:43 +0200 |
commit | aad2d3a71f65e1b542bbfafbf387394331a270ee (patch) | |
tree | 20b71bc654557cd02d3a965d87afdf1a76a2cfa1 /src/dotty/tools/dotc | |
parent | 5ec2199c4966404d462eb866533dc0589fe7f239 (diff) | |
download | dotty-aad2d3a71f65e1b542bbfafbf387394331a270ee.tar.gz dotty-aad2d3a71f65e1b542bbfafbf387394331a270ee.tar.bz2 dotty-aad2d3a71f65e1b542bbfafbf387394331a270ee.zip |
Refactored and augmented source positions.
Also Implemented ConsoleReporter formatting.
Diffstat (limited to 'src/dotty/tools/dotc')
-rw-r--r-- | src/dotty/tools/dotc/parsing/Parsers.scala | 8 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/ConsoleReporter.scala | 30 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/Reporter.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/StoreReporter.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala | 3 | ||||
-rw-r--r-- | src/dotty/tools/dotc/util/Positions.scala | 11 | ||||
-rw-r--r-- | src/dotty/tools/dotc/util/SourceFile.scala | 21 | ||||
-rw-r--r-- | src/dotty/tools/dotc/util/SourcePosition.scala | 19 |
8 files changed, 60 insertions, 36 deletions
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index 29efa7227..c7b52fc63 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -4,7 +4,7 @@ package parsing import scala.collection.mutable.ListBuffer import scala.collection.immutable.BitSet -import util.{ SourceFile, FreshNameCreator } +import util.{ SourceFile, FreshNameCreator, SourcePosition } import Tokens._ import Scanners._ import MarkupParsers._ @@ -212,7 +212,11 @@ object Parsers { */ def syntaxErrorOrIncomplete(msg: String) = if (in.token == EOF) incompleteInputError(msg) - else { syntaxError(msg); skip(); lastErrorOffset = in.offset } + else { + syntaxError(msg+", found: "+in) + skip() + lastErrorOffset = in.offset + } // DEBUG private def expectedMsg(token: Int): String = showToken(token) + " expected but " + showToken(in.token) + " found." diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index 2372485f4..9f191afac 100644 --- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -3,7 +3,7 @@ package dotc package reporting import scala.collection.mutable -import util.Positions.SourcePosition +import util.SourcePosition import core.Contexts._ import Reporter.Severity.{Value => Severity, _} import java.io.{ BufferedReader, IOException, PrintWriter } @@ -24,36 +24,28 @@ class ConsoleReporter( /** maximal number of error messages to be printed */ protected def ErrorLimit = 100 - def formatMessage(msg: String, pos: SourcePosition)(implicit ctx: Context) = msg // for now + def printSourceLine(pos: SourcePosition) = + printMessage(pos.lineContents.stripLineEnd) + + def printColumnMarker(pos: SourcePosition) = + if (pos.exists) { writer.print(" " * (pos.column - 1) + "^ ") } /** Prints the message. */ def printMessage(msg: String) { writer.print(msg + "\n"); writer.flush() } /** Prints the message with the given position indication. */ def printMessage(msg: String, pos: SourcePosition)(implicit ctx: Context) { - printMessage(formatMessage(msg, pos)) + if (pos.exists) { + printSourceLine(pos) + printColumnMarker(pos) + } + printMessage(msg) } def printMessage(msg: String, severity: Severity, pos: SourcePosition)(implicit ctx: Context) { printMessage(label(severity) + msg, pos) } - /** - * @param pos ... - - def printSourceLine(pos: SourcePosition) { - printMessage(pos.lineContent.stripLineEnd) - printColumnMarker(pos) - } - - /** Prints the column marker of the given position. - * - * @param pos ... - */ - def printColumnMarker(pos: SourcePosition) = - if (pos.isDefined) { printMessage(" " * (pos.column - 1) + "^") } -*/ - /** Prints the number of errors and warnings if their are non-zero. */ def printSummary() { if (count(WARNING) > 0) printMessage(countString(WARNING) + " found") diff --git a/src/dotty/tools/dotc/reporting/Reporter.scala b/src/dotty/tools/dotc/reporting/Reporter.scala index 86dfb96d8..15dd288c3 100644 --- a/src/dotty/tools/dotc/reporting/Reporter.scala +++ b/src/dotty/tools/dotc/reporting/Reporter.scala @@ -3,7 +3,7 @@ package dotc package reporting import core.Contexts._ -import util.Positions._ +import util.{SourcePosition, NoSourcePosition} import util.{SourceFile, NoSource} import core.Decorators.PhaseListDecorator import collection.mutable diff --git a/src/dotty/tools/dotc/reporting/StoreReporter.scala b/src/dotty/tools/dotc/reporting/StoreReporter.scala index 31df36e6f..c5c8580c2 100644 --- a/src/dotty/tools/dotc/reporting/StoreReporter.scala +++ b/src/dotty/tools/dotc/reporting/StoreReporter.scala @@ -4,7 +4,7 @@ package reporting import core.Contexts.Context import scala.collection.mutable -import util.Positions.SourcePosition +import util.SourcePosition import Reporter.Severity.{Value => Severity} /** diff --git a/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala b/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala index 704317f23..78c39ac7c 100644 --- a/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala +++ b/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala @@ -3,8 +3,7 @@ package dotc package reporting import scala.collection.mutable -import util.Positions.SourcePosition -import util.SourceFile +import util.{SourcePosition, SourceFile} import Reporter.Severity.{Value => Severity} import core.Contexts.Context diff --git a/src/dotty/tools/dotc/util/Positions.scala b/src/dotty/tools/dotc/util/Positions.scala index b04ccc523..5d4ea5996 100644 --- a/src/dotty/tools/dotc/util/Positions.scala +++ b/src/dotty/tools/dotc/util/Positions.scala @@ -109,17 +109,6 @@ object Positions { /** A sentinal for a non-existing position */ val NoPosition = Position(1, 0) - /** A source position is comprised of a position in a source file */ - case class SourcePosition(source: SourceFile, pos: Position) { - def point: Int = pos.point - def start: Int = pos.start - def end: Int = pos.end - def exists = pos.exists - } - - /** A sentinel for a non-existing source position */ - val NoSourcePosition = SourcePosition(NoSource, NoPosition) - /** The coordinate of a symbol. This is either an index or * a zero-range position. */ diff --git a/src/dotty/tools/dotc/util/SourceFile.scala b/src/dotty/tools/dotc/util/SourceFile.scala index b67eefdfa..91b296962 100644 --- a/src/dotty/tools/dotc/util/SourceFile.scala +++ b/src/dotty/tools/dotc/util/SourceFile.scala @@ -38,6 +38,9 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) { def this(sourceName: String, cs: Seq[Char]) = this(new VirtualFile(sourceName), cs.toArray) def this(file: AbstractFile, cs: Seq[Char]) = this(file, cs.toArray) + /** Tab increment; can be overridden */ + def tabInc = 8 + override def equals(that : Any) = that match { case that : SourceFile => file.path == that.file.path && start == that.start case _ => false @@ -103,6 +106,24 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) { lastLine = findLine(0, lines.length, lastLine) lastLine } + + def startOfLine(offset: Int): Int = lineToOffset(offsetToLine(offset)) + + def nextLine(offset: Int): Int = + lineToOffset(offsetToLine(offset) + 1 min lineIndices.length - 1) + + def lineContents(offset: Int): String = + content.slice(startOfLine(offset), nextLine(offset)).mkString + + def column(offset: Int): Int = { + var idx = startOfLine(offset) + var col = 0 + while (idx != offset) { + col += (if (content(idx) == '\t') tabInc - col % tabInc else 1) + idx += 1 + } + col + 1 + } } object NoSource extends SourceFile("<no source>", Nil) { diff --git a/src/dotty/tools/dotc/util/SourcePosition.scala b/src/dotty/tools/dotc/util/SourcePosition.scala new file mode 100644 index 000000000..769aa87c4 --- /dev/null +++ b/src/dotty/tools/dotc/util/SourcePosition.scala @@ -0,0 +1,19 @@ +package dotty.tools.dotc.util + +import Positions.{Position, NoPosition} + +/** A source position is comprised of a position in a source file */ +case class SourcePosition(source: SourceFile, pos: Position) { + def point: Int = pos.point + def start: Int = pos.start + def end: Int = pos.end + def exists = pos.exists + + def lineContents: String = source.lineContents(point) + def line: Int = source.offsetToLine(point) + def column: Int = source.column(point) +} + +/** A sentinel for a non-existing source position */ +object NoSourcePosition extends SourcePosition(NoSource, NoPosition) + |