9
9
package scala.util.parsing.input
12
* <code>Position</code> is the base trait for objects describing a
13
* position in a ``document''.
16
* It provides functionality for:
18
* <li> generating a visual representation of this position (`longString');
19
* <li> comparing two positions (`<').
22
* To use this class for a concrete kind of ``document'', implement the
23
* <code>lineContents</code> method.
26
* @author Martin Odersky, Adriaan Moors
11
/** `Position` is the base trait for objects describing a position in a ``document''.
13
* It provides functionality for:
14
* - generating a visual representation of this position (`longString`);
15
* - comparing two positions (`<`).
17
* To use this class for a concrete kind of ``document'', implement the `lineContents` method.
19
* @author Martin Odersky
20
* @author Adriaan Moors
30
/** The line number referred to by the position; line numbers start at 1 */
24
/** The line number referred to by the position; line numbers start at 1. */
33
/** The column number referred to by the position; column numbers start at 1 */
27
/** The column number referred to by the position; column numbers start at 1. */
36
/** The contents of the line numbered `lnum' (must not contain a new-line character).
38
* @param lnum a 1-based integer index into the `document'
39
* @return the line at `lnum' (not including a newline)
30
/** The contents of the line at this position. (must not contain a new-line character).
41
32
protected def lineContents: String
43
/** Returns a string representation of the `Position', of the form `line.column' */
34
/** Returns a string representation of the `Position`, of the form `line.column`. */
44
35
override def toString = ""+line+"."+column
46
37
/** Returns a more ``visual'' representation of this position.
47
* More precisely, the resulting string consists of two lines: <ol>
48
* <li> the line in the document referred to by this position </li>
49
* <li>a caret indicating the column</li></ol>
38
* More precisely, the resulting string consists of two lines:
39
* 1. the line in the document referred to by this position
40
* 2. a caret indicating the column
53
*<pre> List(this, is, a, line, from, the, document)
44
* List(this, is, a, line, from, the, document)
56
48
def longString = lineContents+"\n"+lineContents.take(column-1).map{x => if (x == '\t') x else ' ' } + "^"
58
50
/** Compare this position to another, by first comparing their line numbers,
59
51
* and then -- if necessary -- using the columns to break a tie.
61
* @param `that' a `Position' to compare to this `Position'
62
* @return true if this position's line or (in case of a tie wrt. line numbers)
63
* its column is smaller than the corresponding components of `that'
53
* @param `that` a `Position` to compare to this `Position`
54
* @return true if this position's line number or (in case of equal line numbers)
55
* column is smaller than the corresponding components of `that`
65
57
def <(that: Position) = {
66
58
this.line < that.line ||