001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2004-2007 Robert Grimm
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public License
007: * version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.tree;
020:
021: import java.io.IOException;
022:
023: /**
024: * The location in a source file.
025: *
026: * @author Robert Grimm
027: * @version $Revision: 1.12 $
028: */
029: public class Location implements Comparable {
030:
031: /** The file name. */
032: public final String file;
033:
034: /** The line number. */
035: public final int line;
036:
037: /** The column. */
038: public final int column;
039:
040: /**
041: * Create a new location.
042: *
043: * @param file The file name.
044: * @param line The line number.
045: * @param column The column.
046: */
047: public Location(String file, int line, int column) {
048: this .file = file;
049: this .line = line;
050: this .column = column;
051: }
052:
053: public int hashCode() {
054: return file.hashCode() + line * 7 + column;
055: }
056:
057: public boolean equals(Object o) {
058: if (this == o)
059: return true;
060: if (!(o instanceof Location))
061: return false;
062: Location other = (Location) o;
063: if (!file.equals(other.file))
064: return false;
065: if (line != other.line)
066: return false;
067: return column == other.column;
068: }
069:
070: public int compareTo(Object o) {
071: Location other = (Location) o;
072:
073: if (!file.equals(other.file)) {
074: return file.compareTo(other.file);
075: } else if (line != other.line) {
076: return (line < other.line ? -1 : 1);
077: } else {
078: return (column < other.column ? -1
079: : (column == other.column ? 0 : 1));
080: }
081: }
082:
083: /**
084: * Write this location to the specified appenable.
085: *
086: * @param out The appendable.
087: * @throws IOException Signals an I/O error.
088: */
089: public void write(Appendable out) throws IOException {
090: out.append(file);
091: out.append(':');
092: out.append(Integer.toString(line));
093: out.append(':');
094: out.append(Integer.toString(column));
095: }
096:
097: public String toString() {
098: StringBuilder buf = new StringBuilder();
099:
100: buf.append(file);
101: buf.append(':');
102: buf.append(line);
103: buf.append(':');
104: buf.append(column);
105:
106: return buf.toString();
107: }
108:
109: }
|