summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2003-06-24 08:57:30 +0000
committerpaltherr <paltherr@epfl.ch>2003-06-24 08:57:30 +0000
commit9753961477f1f648656ec130675ed1b78f4f73b1 (patch)
tree3d0d93f692d5f7ca3a0eedfe1b0b74eab6dd027d
parent3102d7d40fcc65e6174e84d5424cd45d688c0257 (diff)
downloadscala-9753961477f1f648656ec130675ed1b78f4f73b1.tar.gz
scala-9753961477f1f648656ec130675ed1b78f4f73b1.tar.bz2
scala-9753961477f1f648656ec130675ed1b78f4f73b1.zip
- Removed file from encoded positions
-rw-r--r--sources/ch/epfl/lamp/util/Position.java87
-rw-r--r--sources/ch/epfl/lamp/util/SourceFile.java52
-rw-r--r--sources/scala/tools/scalai/EvaluatorException.java2
-rw-r--r--sources/scalac/Unit.java19
-rw-r--r--sources/scalac/ast/parser/Scanner.java12
-rw-r--r--sources/scalac/backend/msil/GenMSIL.java2
-rw-r--r--sources/scalac/backend/msil/TypeCreator.java2
-rw-r--r--sources/scalac/util/Reporter.java10
8 files changed, 74 insertions, 112 deletions
diff --git a/sources/ch/epfl/lamp/util/Position.java b/sources/ch/epfl/lamp/util/Position.java
index 91c9d702a3..486ef7add6 100644
--- a/sources/ch/epfl/lamp/util/Position.java
+++ b/sources/ch/epfl/lamp/util/Position.java
@@ -8,20 +8,43 @@
package ch.epfl.lamp.util;
+/**
+ * This class represents a position in a source file. Such a position
+ * is defined by a source file (mandatory), a line number (optional)
+ * and a column number (optional, may be specified only if the line
+ * number is defined).
+ *
+ * Line (Column) numbers range from 0 to Integer.MAX_VALUE. A value of
+ * 0 indicates that the line (column) is undefined, 1 represents the
+ * first line (column). Negative values are prohibited.
+ *
+ * The class provides also functions to encode a line number and a
+ * column number into one single integer. The encode line (column)
+ * numbers range from 0 to LINE_MASK (COLUMN_MASK), where 0 indicates
+ * that the line (column) is the 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)
+ */
public class Position {
//########################################################################
// Public Constants
- /** Number of bits used to encode the file */
- public static final int FILE_BITS = 6;
/** Number of bits used to encode the line number */
- public static final int LINE_BITS = 16;
+ public static final int LINE_BITS = 20;
/** Number of bits used to encode the column number */
- public static final int COLUMN_BITS = 32 - LINE_BITS - FILE_BITS;
+ public static final int COLUMN_BITS = 31 - LINE_BITS; // no negatives => 31
- /** Mask to decode the file */
- public static final int FILE_MASK = (1 << FILE_BITS) - 1;
/** Mask to decode the line number */
public static final int LINE_MASK = (1 << LINE_BITS) - 1;
/** Mask to decode the column number */
@@ -31,24 +54,18 @@ public class Position {
public static final int NOPOS = 0;
/** The first position in a source file */
- public static final int FIRSTPOS = encode(null, 1, 1);
+ public static final int FIRSTPOS = encode(1, 1);
//########################################################################
// Public Functions
- /** Encodes a position into a single int. */
- public static int encode(SourceFile file, int line, int column) {
- int fileId = file == null ? 0 : file.id() & FILE_MASK;
- if (fileId < 0 || FILE_MASK < fileId) fileId = 0;
- if (line < 0 || LINE_MASK < line) line = 0;
- if (column < 0 || COLUMN_MASK < column) column = 0;
- return (((fileId << LINE_BITS) | line) << COLUMN_BITS) | column;
- }
-
- /** Returns the file of the encoded position. */
- public static SourceFile file(int position) {
- int fileId = (position >> (COLUMN_BITS + LINE_BITS)) & FILE_MASK;
- return SourceFile.fromId(fileId);
+ /** Encodes a position into a single integer. */
+ public static int encode(int line, int column) {
+ assert line >= 0 : line;
+ assert line == 0 ? column == 0 : column >= 0 : line + "," + column;
+ if (line >= LINE_MASK) { line = LINE_MASK; column = 0; }
+ if (column > COLUMN_MASK) column = COLUMN_MASK;
+ return (line << COLUMN_BITS) | column;
}
/** Returns the line number of the encoded position. */
@@ -77,11 +94,6 @@ public class Position {
// Public Constructors
/** Initializes a new instance. */
- public Position(int position) {
- this(file(position), line(position), column(position));
- }
-
- /** Initializes a new instance. */
public Position(String source) {
this(new SourceFile(source, new byte[0]));
}
@@ -92,20 +104,23 @@ public class Position {
}
/** Initializes a new instance. */
+ public Position(SourceFile file, int position) {
+ this(file, line(position), column(position));
+ }
+
+ /** Initializes a new instance. */
public Position(SourceFile file, int line, int column) {
this.file = file;
this.line = line;
this.column = column;
+ assert file != null;
+ assert line >= 0 : line;
+ assert line == 0 ? column == 0 : column >= 0 : line + "," + column;
}
//########################################################################
// Public Methods
- /** Returns an int encoding this position. */
- public int encode() {
- return encode(file, line, column);
- }
-
/** Returns the file of this position. */
public SourceFile file() {
return file;
@@ -121,14 +136,22 @@ public class Position {
return column;
}
+ /** Returns an int encoding the line and column of this position. */
+ public int encodedLineColumn() {
+ return encode(line, column);
+ }
+
/** Returns a string representation of this position. */
public String toString() {
- return file.name() + ":" + line + ":" + column;
+ StringBuffer buffer = new StringBuffer(file.name());
+ if (line > 0) buffer.append(":").append(line);
+ if (line > 0 && column > 0) buffer.append(":").append(column);
+ return buffer.toString();
}
/** Returns the hash code of this position. */
public int hashCode() {
- return encode();
+ return file.hashCode() ^ encodedLineColumn();
}
/** Returns true iff the given object represents the same position. */
diff --git a/sources/ch/epfl/lamp/util/SourceFile.java b/sources/ch/epfl/lamp/util/SourceFile.java
index 44dbf82506..68a6739cab 100644
--- a/sources/ch/epfl/lamp/util/SourceFile.java
+++ b/sources/ch/epfl/lamp/util/SourceFile.java
@@ -30,29 +30,6 @@ public class SourceFile {
public static final byte SU = 0x1A;
//########################################################################
- // Public Functions
-
- /** Returns the source file with the given id. */
- public static SourceFile fromId(int id) {
- if (id <= 0 || files.size() < id) return UNKNOWN;
- return (SourceFile)files.get(id - 1);
- }
-
- /** Releases all source file. */
- public static void releaseAll() {
- files.clear();
- }
-
- //########################################################################
- // Private Variables
-
- /** The unknown source file */
- private static final SourceFile UNKNOWN = new SourceFile();
-
- /** The list containing all source files */
- private static final ArrayList files = new ArrayList();
-
- //########################################################################
// Private Fields
/** The name of this source file */
@@ -61,9 +38,6 @@ public class SourceFile {
/** The content of source this file */
private final byte[] bytes;
- /** The id of this source file */
- private final int id;
-
/** The encoding of this source file or null if unspecified */
private String encoding;
@@ -74,16 +48,6 @@ public class SourceFile {
private int nextIndex = 0;
//########################################################################
- // Private Constructors
-
- /** Initializes a new instance. */
- private SourceFile() {
- this.name = "<<unknown source file>>";
- this.bytes = normalize(new byte[0]);
- this.id = 0;
- }
-
- //########################################################################
// Public Constructors
/** Initializes a new instance. */
@@ -105,8 +69,6 @@ public class SourceFile {
public SourceFile(String name, byte[] bytes) {
this.name = name;
this.bytes = normalize(bytes);
- this.id = files.size() + 1;
- files.add(this);
}
//########################################################################
@@ -122,11 +84,6 @@ public class SourceFile {
return bytes;
}
- /** Returns the id of this source file. */
- public int id() {
- return id;
- }
-
/** Sets the encoding of the file. */
public void setEncoding(String encoding) {
this.encoding = encoding;
@@ -165,14 +122,7 @@ public class SourceFile {
return new Position(this, line, column);
}
- /**
- * Returns the integer encoding the position of the given line and
- * column of this source file.
- */
- public int getEncodedPosition(int line, int column) {
- return Position.encode(this, line, column);
- }
-
+ /** Returns the name of this source file. */
public String toString() {
return name;
}
diff --git a/sources/scala/tools/scalai/EvaluatorException.java b/sources/scala/tools/scalai/EvaluatorException.java
index 22807adec5..8b5eb76e6b 100644
--- a/sources/scala/tools/scalai/EvaluatorException.java
+++ b/sources/scala/tools/scalai/EvaluatorException.java
@@ -54,7 +54,7 @@ public class EvaluatorException extends RuntimeException {
buffer.append('.');
buffer.append(method.nameString());
buffer.append('(');
- buffer.append(Position.file(pos));
+ // !!! buffer.append(Position.file(pos));
buffer.append(':');
buffer.append(Position.line(pos));
buffer.append(")");
diff --git a/sources/scalac/Unit.java b/sources/scalac/Unit.java
index 0a529efbfb..354fc1af55 100644
--- a/sources/scalac/Unit.java
+++ b/sources/scalac/Unit.java
@@ -52,16 +52,22 @@ public class Unit {
this.console = console;
}
+ /** return the position representing the given encoded position
+ */
+ public Position position(int pos) {
+ return new Position(source, pos);
+ }
+
/** issue an error in this compilation unit at a specific location
*/
public void error(int pos, String message) {
- global.reporter.error(decode(pos), message);
+ global.reporter.error(position(pos), message);
}
/** issue a warning in this compilation unit at a specific location
*/
public void warning(int pos, String message) {
- global.reporter.warning(decode(pos), message);
+ global.reporter.warning(position(pos), message);
}
/** return a string representation
@@ -70,13 +76,4 @@ public class Unit {
return source.toString();
}
-
- private Position decode(int pos) {
- Position position = new Position(pos);
- if (position.file().id() == 0)
- if (/* !!! source.id() > Position.FILE_MASK && */ position.line() != 0)
- return source.getPosition(position.line(), position.column());
- return position;
- }
-
}
diff --git a/sources/scalac/ast/parser/Scanner.java b/sources/scalac/ast/parser/Scanner.java
index 3555dcad5e..c2eb82a27e 100644
--- a/sources/scalac/ast/parser/Scanner.java
+++ b/sources/scalac/ast/parser/Scanner.java
@@ -66,10 +66,6 @@ public class Scanner extends TokenData {
public int cline;
public int ccol;
- /** the current sourcefile
- */
- public SourceFile currentSource;
-
/** a buffer for character and string literals
*/
protected byte[] lit = new byte[64];
@@ -84,7 +80,7 @@ public class Scanner extends TokenData {
*/
public Scanner(Unit unit) {
this.unit = unit;
- buf = (currentSource = unit.source).bytes();
+ buf = unit.source.bytes();
cline = 1;
bp = -1;
ccol = 0;
@@ -167,7 +163,7 @@ public class Scanner extends TokenData {
*/
public void fetchToken() {
if (token == EOF) return;
- lastpos = Position.encode(currentSource, cline, ccol);
+ lastpos = Position.encode(cline, ccol);
int index = bp;
while(true) {
switch (ch) {
@@ -194,7 +190,7 @@ public class Scanner extends TokenData {
nextch();
break;
default:
- pos = Position.encode(currentSource, cline, ccol);
+ pos = Position.encode(cline, ccol);
index = bp;
switch (ch) {
case 'A': case 'B': case 'C': case 'D': case 'E':
@@ -574,7 +570,7 @@ public class Scanner extends TokenData {
putch(ch);
break;
default:
- syntaxError(Position.encode(currentSource, cline, ccol) - 1, "invalid escape character");
+ syntaxError(Position.encode(cline, ccol) - 1, "invalid escape character");
putch(ch);
}
nextch();
diff --git a/sources/scalac/backend/msil/GenMSIL.java b/sources/scalac/backend/msil/GenMSIL.java
index 4e2567781d..cd1ce73774 100644
--- a/sources/scalac/backend/msil/GenMSIL.java
+++ b/sources/scalac/backend/msil/GenMSIL.java
@@ -1641,7 +1641,7 @@ public class GenMSIL /*implements Modifiers */ {
}
void logErr(int pos, String message) {
- global.reporter.printMessage(new Position(pos), message);
+ global.reporter.printMessage(currUnit.position(pos), message);
}
void logErr(String message) {
diff --git a/sources/scalac/backend/msil/TypeCreator.java b/sources/scalac/backend/msil/TypeCreator.java
index 4b81269a95..f5b7806174 100644
--- a/sources/scalac/backend/msil/TypeCreator.java
+++ b/sources/scalac/backend/msil/TypeCreator.java
@@ -754,7 +754,7 @@ public final class TypeCreator
}
void logErr(String message) {
- global.reporter.printMessage(new Position(pos), message);
+ global.reporter.printMessage(unit.position(pos), message);
}
} // class TypeCreator
diff --git a/sources/scalac/util/Reporter.java b/sources/scalac/util/Reporter.java
index ccf466ae7d..b7d233213f 100644
--- a/sources/scalac/util/Reporter.java
+++ b/sources/scalac/util/Reporter.java
@@ -148,7 +148,7 @@ public class Reporter {
/** Prints the message with the given position indication. */
public void printMessage(Position position, String message) {
- if (position != null && position.file().id() != 0) {
+ if (position != null) {
message = " " + message;
if (position.line() != 0)
message = position.line() + ":" + message;
@@ -160,8 +160,7 @@ public class Reporter {
/** Prints the error message. */
public void printError(Position position, String message) {
- if (position != null && position.file().id() == 0)
- message = "error: " + message;
+ if (position == null) message = "error: " + message;
printMessage(position, message);
}
@@ -179,8 +178,7 @@ public class Reporter {
/** Prints the source line of the given position. */
public void printSourceLine(Position position) {
- if (position == null || position.file().id() == 0) return;
- if (position.line() == 0) return;
+ if (position == null || position.line() == 0) return;
printMessage(position.file().getLine(position.line()));
printColumnMarker(position);
}
@@ -226,8 +224,6 @@ public class Reporter {
private boolean testAndLog(Position position) {
if (position == null) return false;
if (position.column() == 0) return false;
- if (position.line() == 0) return false;
- if (position.file().id() == 0) return false;
if (positions.contains(position)) return true;
positions.add(position);
return false;