diff --git a/sources/zweic/Position.scala b/sources/zweic/Position.scala new file mode 100755 index 0000000..49a074b --- /dev/null +++ b/sources/zweic/Position.scala @@ -0,0 +1,46 @@ +/* zweic -- a compiler for zwei + * + * Stephane Micheloud & LAMP + * + * $Id$ + */ + +package zweic; + + +/** This class provides methods for encoding a location in + * a source file (line/column) into a single integer. + */ +object Position { + + /** number of bits reserved for the column. + */ + val columnBits = 12; + + val columnMask = (1 << columnBits) - 1; + + /** undefined position + */ + val UNDEFINED = 0; + + /** first position in a source file + */ + val FIRST = (1 << columnBits) | 1; + + /** encode a line and column number into a single int + */ + def encode(line: Int, col: Int): Int = + (line << columnBits) | (col & columnMask); + + /** get the line number out of an encoded position + */ + def line(pos: Int): Int = + pos >>> columnBits; + + /** return the column number of an encoded position + */ + def column(pos: Int): Int = + pos & columnMask; + + def toString(pos: Int) = line(pos).toString() + ":" + column(pos); +}