package scala.tools.nsc
package util
object Position {
val tabInc = 8
}
trait Position {
def source: SourceFile = throw new UnsupportedOperationException("Position.source")
def isDefined: Boolean = false
def isTransparent: Boolean = false
def isRange: Boolean = false
def isOpaqueRange: Boolean = false
def makeTransparent: Position = this
def start: Int = throw new UnsupportedOperationException("Position.start")
def startOrPoint: Int = point
def point: Int = throw new UnsupportedOperationException("Position.point")
def pointOrElse(default: Int): Int = default
def end: Int = throw new UnsupportedOperationException("Position.end")
def endOrPoint: Int = point
@deprecated("use point instead", "2.9.0")
def offset: Option[Int] = if (isDefined) Some(point) else None
def withStart(off: Int) = this
def withEnd(off: Int) = this
def withPoint(off: Int) = this
def withSource(source: SourceFile, shift: Int) = this
def union(pos: Position) = this
def focusStart = this
def focus = this
def focusEnd = this
def includes(pos: Position) = false
def properlyIncludes(pos: Position) =
includes(pos) && (start < pos.startOrPoint || pos.endOrPoint < end)
def precedes(pos: Position) =
isDefined && pos.isDefined && endOrPoint <= pos.startOrPoint
def properlyPrecedes(pos: Position) =
isDefined && pos.isDefined && endOrPoint < pos.startOrPoint
def overlaps(pos: Position) =
isRange && pos.isRange &&
((pos.start < end && start < pos.end) || (start < pos.end && pos.start < end))
def sameRange(pos: Position) =
isRange && pos.isRange && start == pos.start && end == pos.end
def line: Int = throw new UnsupportedOperationException("Position.line")
def column: Int = throw new UnsupportedOperationException("Position.column")
def toSingleLine: Position = this
def lineContent: String =
if (isDefined) source.lineToString(line - 1)
else "NO_LINE"
def inUltimateSource(source : SourceFile) =
if (source == null) this else source.positionInUltimateSource(this)
def dbgString = toString
def show: String = "["+toString+"]"
}
case object NoPosition extends Position {
override def dbgString = toString
}
case class FakePos(msg: String) extends Position {
override def toString = msg
}
class OffsetPosition(override val source: SourceFile, override val point: Int) extends Position {
override def isDefined = true
override def pointOrElse(default: Int): Int = point
override def withPoint(off: Int) = new OffsetPosition(source, off)
override def withSource(source: SourceFile, shift: Int) = new OffsetPosition(source, point + shift)
override def line: Int = source.offsetToLine(point) + 1
override def column: Int = {
var idx = source.lineToOffset(source.offsetToLine(point))
var col = 0
while (idx != point) {
col += (if (source.content(idx) == '\t') Position.tabInc - col % Position.tabInc else 1)
idx += 1
}
col + 1
}
override def union(pos: Position) =
if (pos.isRange) pos else this
override def equals(that : Any) = that match {
case that : OffsetPosition => point == that.point && source.file == that.source.file
case that => false
}
override def hashCode = point * 37 + source.file.hashCode
override def toString = {
val pointmsg = if (point > source.length) "out-of-bounds-" else "offset="
"source-%s,line-%s,%s%s".format(source.path, line, pointmsg, point)
}
override def show = "["+point+"]"
}
class RangePosition(source: SourceFile, override val start: Int, point: Int, override val end: Int)
extends OffsetPosition(source, point) {
if (start > end) assert(false, "bad position: "+show)
override def isRange: Boolean = true
override def isOpaqueRange: Boolean = true
override def startOrPoint: Int = start
override def endOrPoint: Int = end
override def withStart(off: Int) = new RangePosition(source, off, point, end)
override def withEnd(off: Int) = new RangePosition(source, start, point, off)
override def withPoint(off: Int) = new RangePosition(source, start, off, end)
override def withSource(source: SourceFile, shift: Int) = new RangePosition(source, start + shift, point + shift, end + shift)
override def focusStart = new OffsetPosition(source, start)
override def focus = {
if (focusCache eq NoPosition) focusCache = new OffsetPosition(source, point)
focusCache
}
override def focusEnd = new OffsetPosition(source, end)
override def makeTransparent = new TransparentPosition(source, start, point, end)
override def includes(pos: Position) = pos.isDefined && start <= pos.startOrPoint && pos.endOrPoint <= end
override def union(pos: Position) =
if (pos.isRange) new RangePosition(source, start min pos.start, point, end max pos.end) else this
override def toSingleLine: Position = source match {
case bs: BatchSourceFile
if end > 0 && bs.offsetToLine(start) < bs.offsetToLine(end - 1) =>
val pointLine = bs.offsetToLine(point)
new RangePosition(source, bs.lineToOffset(pointLine), point, bs.lineToOffset(pointLine + 1))
case _ => this
}
override def toString = "RangePosition("+source+", "+start+", "+point+", "+end+")"
override def show = "["+start+":"+end+"]"
private var focusCache: Position = NoPosition
}
class TransparentPosition(source: SourceFile, start: Int, point: Int, end: Int) extends RangePosition(source, start, point, end) {
override def isOpaqueRange: Boolean = false
override def isTransparent = true
override def makeTransparent = this
override def show = "<"+start+":"+end+">"
}