001: package gnu.lists;
002:
003: import java.io.*;
004: import gnu.text.SourceLocator;
005:
006: /** A <code>Pair</code> with the file name and position it was read from. */
007:
008: public class PairWithPosition extends Pair implements
009: gnu.text.SourceLocator {
010: String filename;
011: /** An encoding of lineNumber+(columnNumber<<20).
012: * Note if columnNumber is unspecified (0), then position is lineNumber. */
013: int position;
014:
015: public final void setFile(String filename) {
016: this .filename = filename;
017: }
018:
019: public final void setLine(int lineno, int colno) {
020: if (lineno < 0)
021: lineno = 0;
022: if (colno < 0)
023: colno = 0;
024: position = (lineno << 12) + colno;
025: }
026:
027: public final void setLine(int lineno) {
028: setLine(lineno, 0);
029: }
030:
031: public final String getFileName() {
032: return filename;
033: }
034:
035: public String getPublicId() {
036: return null;
037: }
038:
039: public String getSystemId() {
040: return filename;
041: }
042:
043: /** Get the line number of (the start of) this Expression.
044: * The "first" line is line 1; unknown is -1. */
045: public final int getLineNumber() {
046: int line = position >> 12;
047: return line == 0 ? -1 : line;
048: }
049:
050: public final int getColumnNumber() {
051: int column = position & ((1 << 12) - 1);
052: return column == 0 ? -1 : column;
053: }
054:
055: public boolean isStableSourceLocation() {
056: return true;
057: }
058:
059: /** Only for serialization. */
060: public PairWithPosition() {
061: }
062:
063: public PairWithPosition(SourceLocator where, Object car, Object cdr) {
064: super (car, cdr);
065: filename = where.getFileName();
066: setLine(where.getLineNumber(), where.getColumnNumber());
067: }
068:
069: public PairWithPosition(Object car, Object cdr) {
070: super (car, cdr);
071: }
072:
073: public static PairWithPosition make(Object car, Object cdr,
074: String filename, int line, int column) {
075: PairWithPosition pair = new PairWithPosition(car, cdr);
076: pair.filename = filename;
077: pair.setLine(line, column);
078: return pair;
079: }
080:
081: public static PairWithPosition make(Object car, Object cdr,
082: String filename, int position) {
083: PairWithPosition pair = new PairWithPosition(car, cdr);
084: pair.filename = filename;
085: pair.position = position;
086: return pair;
087: }
088:
089: /**
090: * @serialData Write the car followed by the cdr,
091: * followed by filename (as an Object, so it can be shared),
092: * followed by position (line|(column<<20)).
093: */
094: public void writeExternal(ObjectOutput out) throws IOException {
095: out.writeObject(car);
096: out.writeObject(cdr);
097: out.writeObject(filename);
098: out.writeInt(position);
099: }
100:
101: public void readExternal(ObjectInput in) throws IOException,
102: ClassNotFoundException {
103: car = in.readObject();
104: cdr = in.readObject();
105: filename = (String) in.readObject();
106: position = in.readInt();
107: }
108: }
|