001: /**************************************************************************/
002: /* N I C E */
003: /* A high-level object-oriented research language */
004: /* (c) Daniel Bonniot 2003 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package bossa.util;
012:
013: import java.util.*;
014:
015: /**
016: Represents a portion of the input file.
017: Used to report errors to the user.
018:
019: @see Located
020:
021: @author Daniel Bonniot (bonniot@users.sourceforge.net)
022: */
023: public abstract class Location implements Located {
024: public static Location make(java.io.File file, int startLine,
025: int startColumn, int endLine, int endColumn) {
026: return new Source(file, startLine, startColumn, endLine,
027: endColumn);
028: }
029:
030: public static Location make(java.io.File file, int startLine,
031: int startColumn) {
032: return make(file, startLine, startColumn, -1, -1);
033: }
034:
035: public static Location make(int startLine, int startColumn,
036: int endLine, int endColumn) {
037: return make(currentFile, startLine, startColumn, endLine,
038: endColumn);
039: }
040:
041: public static Location make(int startLine, int startColumn) {
042: return make(currentFile, startLine, startColumn, -1, -1);
043: }
044:
045: /**
046: * The file beeing parsed.
047: * Enables to set the file name just once,
048: * not at each Location construction.
049: */
050: public static void setCurrentFile(java.io.File file) {
051: currentFile = file;
052: }
053:
054: private static java.io.File currentFile = null;
055:
056: public static final Location option = new Option();
057:
058: public static Location nowhere() {
059: return new Location.File(currentFile);
060: }
061:
062: public Location location() {
063: return this ;
064: }
065:
066: /**
067: Return an identifier, which is guaranteed to be different for
068: different locations.
069: */
070: public String uniqueIdentifier(String root) {
071: StringBuffer uniq = new StringBuffer(super .toString());
072: // Replace the '@' in bossa.util.Location@12345 with '_'
073: uniq.setCharAt("bossa.util.Location".length(), '_');
074:
075: return root + uniq.toString();
076: }
077:
078: public String toShortString() {
079: return toString();
080: }
081:
082: /****************************************************************
083: * Setting source location in the generated bytecode.
084: ****************************************************************/
085:
086: public void write(gnu.expr.Expression exp) {
087: }
088:
089: public void writeEnd(gnu.expr.Expression exp) {
090: }
091:
092: public void write(gnu.expr.Declaration decl) {
093: }
094:
095: /****************************************************************
096: * Different behaviour wether compiler is invoked by an editor.
097: ****************************************************************/
098:
099: public static boolean editorMode = false;
100:
101: /****************************************************************
102: * File location
103: ****************************************************************/
104:
105: public static class File extends Location {
106: private File(java.io.File file) {
107: this .file = file;
108: }
109:
110: private java.io.File file;
111:
112: public java.io.File getFile() {
113: return file;
114: }
115:
116: public String toString() {
117: if (file == null)
118: return "";
119: else
120: return nice.tools.util.System.prettyPrint(file);
121: }
122:
123: public String toShortString() {
124: return (file == null) ? "" : file.getName();
125: }
126:
127: public void write(gnu.expr.Expression exp) {
128: if (file == null)
129: return;
130:
131: // In the bytecode, the fully qualified name is not wanted.
132: exp.setFile(file.getName());
133: }
134:
135: public void write(gnu.expr.Declaration decl) {
136: if (file == null)
137: return;
138:
139: // In the bytecode, the fully qualified name is not wanted.
140: decl.setFile(file.getName());
141: }
142: }
143:
144: /****************************************************************
145: * Source location
146: ****************************************************************/
147:
148: public static class Source extends File {
149: private Source(java.io.File file, int startLine,
150: int startColumn, int endLine, int endColumn) {
151: super (file);
152: this .startLine = startLine;
153: this .startColumn = startColumn;
154: this .endLine = endLine;
155: this .endColumn = endColumn;
156: }
157:
158: private int startLine, startColumn, endLine, endColumn;
159:
160: public int getLine() {
161: return startLine;
162: }
163:
164: public int getColumn() {
165: return startColumn;
166: }
167:
168: public String toString() {
169: String res = super .toString();
170: if (editorMode)
171: return (res.length() > 0 ? (res + ":") : "")
172: + startLine + ":" + startColumn;
173: else
174: return (res.length() > 0 ? (res + ": ") : "") + "line "
175: + startLine + ", column " + startColumn;
176: }
177:
178: public String toShortString() {
179: return super .toShortString() + ":" + startLine;
180: }
181:
182: public String uniqueIdentifier(String root) {
183: return root + startLine + "_" + startColumn;
184: }
185:
186: public void write(gnu.expr.Expression exp) {
187: super .write(exp);
188: exp.setLine(getLine(), getColumn());
189: }
190:
191: public void writeEnd(gnu.expr.Expression exp) {
192: super .write(exp);
193: exp.setLine(endLine, endColumn);
194: }
195:
196: public void write(gnu.expr.Declaration decl) {
197: super .write(decl);
198: decl.setLine(getLine(), getColumn());
199: }
200: }
201:
202: public static Location make(gnu.expr.Expression expr) {
203: String file = expr.getFile();
204: return new Source(file != null ? new java.io.File(file) : null,
205: expr.getLine(), expr.getColumn(), -1, -1);
206: }
207:
208: /****************************************************************
209: * A compilation option
210: ****************************************************************/
211:
212: public static class Option extends Location {
213: public String toString() {
214: return "Command line";
215: }
216: }
217: }
|