summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/javac/JavaScanners.scala4
-rw-r--r--src/library/scala/io/Position.scala113
-rw-r--r--src/library/scala/io/Source.scala26
-rwxr-xr-xsrc/library/scala/xml/pull/XMLEventReader.scala3
4 files changed, 67 insertions, 79 deletions
diff --git a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala
index 3de17d6b56..c600f6364b 100644
--- a/src/compiler/scala/tools/nsc/javac/JavaScanners.scala
+++ b/src/compiler/scala/tools/nsc/javac/JavaScanners.scala
@@ -305,14 +305,14 @@ trait JavaScanners extends ast.parser.ScannersCommon {
*/
private def fetchToken() {
if (token == EOF) return
- lastPos = in.cpos - 1 // Position.encode(in.cline, in.ccol)
+ lastPos = in.cpos - 1
//var index = bp
while (true) {
in.ch match {
case ' ' | '\t' | CR | LF | FF =>
in.next
case _ =>
- pos = in.cpos // Position.encode(in.cline, in.ccol)
+ pos = in.cpos
(in.ch: @switch) match {
case 'A' | 'B' | 'C' | 'D' | 'E' |
'F' | 'G' | 'H' | 'I' | 'J' |
diff --git a/src/library/scala/io/Position.scala b/src/library/scala/io/Position.scala
index d08ed2d7e7..3cc7a481f3 100644
--- a/src/library/scala/io/Position.scala
+++ b/src/library/scala/io/Position.scala
@@ -6,97 +6,76 @@
** |/ **
\* */
-
-
package scala.io
-/** <p>
- * The object <code>Position</code> provides convenience methods to encode
- * line and column number in one single integer. The encode line (column)
- * numbers range from 0 to <code>LINE_MASK</code>
- * (<code>COLUMN_MASK</code>), where 0 indicates that the line (column) is
- * the undefined and 1 represents the first line (column). Line (Column)
- * numbers greater than <code>LINE_MASK</code>
- * (<code>COLUMN_MASK</code>) are replaced by <code>LINE_MASK</code>
- * (<code>COLUMN_MASK</code>). Furthermore, if the encoded line number is
- * <code>LINE_MASK</code>, the column number is always set to 0.
- * </p>
- * <p>
- * The following properties hold:
- * </p>
- * <ul>
- * <li>
- * the undefined position is 0: <code>encode(0,0) == 0</code>
- * </li>
- * <li>
- * encodings are non-negative : <code>encode(line,column) >= 0</code>
- * </li>
- * <li>
- * position order is preserved:
- * <code>(line1 &lt; line2) || (line1 == line2 &amp;&amp; column1 &lt; column2)</code>
- * <div>implies</div>
- * <code>encode(line1,column1) &lt;= encode(line2,column2)</code>
- * </li>
- * </ul>
+/** The object Position provides convenience methods to encode
+ * line and column number in one single integer. The encoded line
+ * (column) numbers range from 0 to LINE_MASK (COLUMN_MASK),
+ * where 0 indicates that the line (column) is undefined and 1
+ * represents the first line (column).
+ *
+ * Line (Column) numbers greater than LINE_MASK (COLUMN_MASK) are
+ * replaced by LINE_MASK (COLUMN_MASK). Furthermore, if the encoded
+ * line number is LINE_MASK, the column number is always set to 0.
+ *
+ * The following properties hold:
+ *
+ * the undefined position is 0: encode(0,0) == 0
+ * encodings are non-negative : encode(line,column) >= 0
+ * position order is preserved:
+ *
+ * (line1 <= line2) || (line1 == line2 && column1 <= column2)
+ * implies
+ * encode(line1,column1) <= encode(line2,column2)
*
* @author Burak Emir (translated from work by Matthias Zenger and others)
*/
-object Position {
+abstract class Position {
+ /** Definable behavior for overflow conditions.
+ */
+ def checkInput(line: Int, column: Int): Unit
/** Number of bits used to encode the line number */
final val LINE_BITS = 20
/** Number of bits used to encode the column number */
final val COLUMN_BITS = 31 - LINE_BITS // no negatives => 31
-
/** Mask to decode the line number */
final val LINE_MASK = (1 << LINE_BITS) - 1
/** Mask to decode the column number */
final val COLUMN_MASK = (1 << COLUMN_BITS) - 1
- /** The undefined position */
- final val NOPOS = 0
-
- /** The first position in a source file */
- final val FIRSTPOS = encode(1, 1)
-
/** Encodes a position into a single integer. */
final def encode(line: Int, column: Int): Int = {
- var line1, column1 = 0
- if (line < 0)
- throw new IllegalArgumentException(line + " < 0")
- if ((line == 0) && (column != 0))
- throw new IllegalArgumentException(line + "," + column + " not allowed")
- if (column < 0)
- throw new IllegalArgumentException(line + "," + column + " not allowed")
+ checkInput(line, column)
- if (line >= LINE_MASK) {
- line1 = LINE_MASK
- column1 = 0
- }
- else {
- line1 = line
- if (column > COLUMN_MASK)
- column1 = COLUMN_MASK
- else
- column1 = column
- }
- (line1 << COLUMN_BITS) | column1
+ if (line >= LINE_MASK)
+ LINE_MASK << COLUMN_BITS
+ else
+ (line << COLUMN_BITS) | math.min(LINE_MASK, line)
}
/** Returns the line number of the encoded position. */
- final def line(pos: Int): Int =
- (pos >> COLUMN_BITS) & LINE_MASK
+ final def line(pos: Int): Int = (pos >> COLUMN_BITS) & LINE_MASK
/** Returns the column number of the encoded position. */
- final def column(pos: Int): Int =
- pos & COLUMN_MASK
+ final def column(pos: Int): Int = pos & COLUMN_MASK
/** Returns a string representation of the encoded position. */
- def toString(pos: Int): String = {
- val sb = new StringBuilder
- sb append line(pos)
- sb append ':'
- sb append column(pos)
- sb.toString()
+ def toString(pos: Int): String = line(pos) + ":" + column(pos)
+}
+
+object Position extends Position {
+ /** The undefined position */
+ @deprecated("This will be removed") final val NOPOS = 0
+ /** The first position in a source file */
+ @deprecated("This will be removed") final val FIRSTPOS = encode(1, 1)
+
+ def checkInput(line: Int, column: Int) {
+ if (line < 0)
+ throw new IllegalArgumentException(line + " < 0")
+ if ((line == 0) && (column != 0))
+ throw new IllegalArgumentException(line + "," + column + " not allowed")
+ if (column < 0)
+ throw new IllegalArgumentException(line + "," + column + " not allowed")
}
}
diff --git a/src/library/scala/io/Source.scala b/src/library/scala/io/Source.scala
index 570c48a91a..c81f2bfed0 100644
--- a/src/library/scala/io/Source.scala
+++ b/src/library/scala/io/Source.scala
@@ -186,7 +186,6 @@ abstract class Source extends Iterator[Char] {
/** description of this source, default empty */
var descr: String = ""
-
var nerrors = 0
var nwarnings = 0
@@ -242,7 +241,8 @@ abstract class Source extends Iterator[Char] {
*/
def next: Char = positioner.next
- class Positioner {
+ class Positioner(encoder: Position) {
+ def this() = this(RelaxedPosition)
/** the last character returned by next. */
var ch: Char = _
@@ -258,7 +258,7 @@ abstract class Source extends Iterator[Char] {
def next: Char = {
ch = iter.next
- pos = Position.encode(cline, ccol)
+ pos = encoder.encode(cline, ccol)
ch match {
case '\n' =>
ccol = 1
@@ -271,7 +271,14 @@ abstract class Source extends Iterator[Char] {
ch
}
}
- object NoPositioner extends Positioner {
+ /** A Position implementation which ignores errors in
+ * the positions.
+ */
+ object RelaxedPosition extends Position {
+ def checkInput(line: Int, column: Int): Unit = ()
+ }
+ object RelaxedPositioner extends Positioner(RelaxedPosition) { }
+ object NoPositioner extends Positioner(Position) {
override def next: Char = iter.next
}
def ch = positioner.ch
@@ -321,7 +328,7 @@ abstract class Source extends Iterator[Char] {
private[this] var resetFunction: () => Source = null
private[this] var closeFunction: () => Unit = null
- private[this] var positioner: Positioner = new Positioner
+ private[this] var positioner: Positioner = RelaxedPositioner
def withReset(f: () => Source): this.type = {
resetFunction = f
@@ -335,10 +342,13 @@ abstract class Source extends Iterator[Char] {
descr = text
this
}
- // we'd like to default to no positioning, but for now we break
- // less by defaulting to status quo.
+ /** Change or disable the positioner. */
def withPositioning(on: Boolean): this.type = {
- positioner = if (on) new Positioner else NoPositioner
+ positioner = if (on) RelaxedPositioner else NoPositioner
+ this
+ }
+ def withPositioning(pos: Positioner): this.type = {
+ positioner = pos
this
}
diff --git a/src/library/scala/xml/pull/XMLEventReader.scala b/src/library/scala/xml/pull/XMLEventReader.scala
index 4188f8b084..3902967626 100755
--- a/src/library/scala/xml/pull/XMLEventReader.scala
+++ b/src/library/scala/xml/pull/XMLEventReader.scala
@@ -24,8 +24,7 @@ import scala.xml.parsing.{ ExternalSources, MarkupHandler, MarkupParser }
* @author Burak Emir
* @author Paul Phillips
*/
-class XMLEventReader(src: Source) extends ProducerConsumerIterator[XMLEvent]
-{
+class XMLEventReader(src: Source) extends ProducerConsumerIterator[XMLEvent] {
// We implement a pull parser as an iterator, but since we may be operating on
// a stream (e.g. XML over a network) there may be arbitrarily long periods when
// the queue is empty. Fortunately the ProducerConsumerIterator is ideally