summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/util/Position.scala
blob: e900aa2553d67c06b9bcb5305417f4cba139feca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2004, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
** $Id$
\*                                                                      */

package scala.tools.nsc.util;

/** This position uses offset in character buffer rather than line column relationship.
 *  @author Sean McDirmid
 */
object Position {
  val NOPOS    = -1;
  val FIRSTPOS = 0;
  val NOLINE   = 0;
  val FIRSTLINE = 1;

  def line(source : SourceFile, offset : Int) = (new Position(source, offset)).line;

  def lineToOffset(line : Int) = {
    assert(line >= 1);
    NOPOS - line;
  }
  def offsetToLine(pos : Int) = {
    assert(pos < NOPOS);
    NOPOS - pos;
  }
}


class Position( val source : SourceFile, val offset: Int) {
  import Position._;

  private val tabInc = 8;

  def this(sourceName : String) = this(new SourceFile(sourceName, new Array[Char](0)), Position.NOPOS);
  def this(sourceName : String, _offset : Int) = this(new SourceFile(sourceName, new Array[Char](0)), _offset);

  private def hasOffset = offset > NOPOS;
  private def isLine    = offset < NOPOS;


  def   line: Int =
    if (hasOffset) source.offsetToLine(offset) + FIRSTLINE
    else if (isLine) Position.offsetToLine(offset) else NOLINE;

  // for display purposes only.
  def column: Int = if (hasOffset) {
    var column = 1;

    // find beginning offset for line
    val line    = source.offsetToLine  (offset);
    var coffset = source.  lineToOffset(line);
    var continue = true;
    while (continue) {
      if (coffset == offset) continue = false;
      else if (source.content(coffset) == '\t') column = ((column - 1) / tabInc * tabInc) + tabInc + 1;
      else column = column + 1;
      coffset = coffset + 1;
    }
    column;
  } else 0;


  def dbgString = {
    "source: " + (if (source == null) source else source . path) + " " +
    (if (isLine) "line-" + line;
    else if (!hasOffset) "NOP";
    else if (offset >= source.content.length) "out-of-bounds-" + offset else {
      val ret = "offset=" + offset + " line=" + line;
      var add = "";
      while (offset + add.length() < source.content.length &&
	     add.length() < 10) add = add + source.content(offset + add.length());
      ret + " c[0..9]=\"" + add + "\"";
    })
  }



  def lineContent: String = if (hasOffset) source.lineToString(line - FIRSTLINE) else "NO_LINE";

  /** Returns a string representation of the encoded position. */
  override def toString(): String = {
    val sb = new StringBuffer();
    if (source != null) {
      sb.append(source.file.getPath());
      if (hasOffset) {
	sb.append(line);
	sb.append(':');
	sb.append(column);
      }
    } else sb.append("::" + offset);
    sb.toString();
  }
}