001: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
002:
003: /*
004: This file was auto generated, but has been modified since then. If we
005: need to regenerate it for some reason we should be careful to look at
006: the notes below.
007:
008: All BeanShell modifications are demarcated by "Begin BeanShell
009: Modification - ... " and "End BeanShell Modification - ..."
010:
011: Note: Please leave the ^M carriage return in the above auto-generated line
012: or JavaCC will complain about version on Win systems.
013:
014: BeanShell Modification to generated file
015: ----------------------------------------
016:
017: - Added sourceFile attribute
018: setErrorSourceFile()
019: getErrorSourceFile()
020: - Modified getMessage() to print more tersely except on debug
021: (removed "Was expecting one of...)
022: - Added sourceFile info to getMessage()
023: - Made ParseException extend EvalError
024: - Modified constructors to use EvalError
025: - Override
026: getErrorLineNumber()
027: getErrorText()
028: - Add
029: toString()
030:
031: */
032:
033: package bsh;
034:
035: /**
036: * This exception is thrown when parse errors are encountered.
037: * You can explicitly create objects of this exception type by
038: * calling the method generateParseException in the generated
039: * parser.
040: *
041: * You can modify this class to customize your error reporting
042: * mechanisms so long as you retain the public fields.
043: */
044:
045: // Begin BeanShell Modification - public, extend EvalError
046: public class ParseException extends EvalError {
047: // End BeanShell Modification - public, extend EvalError
048:
049: // Begin BeanShell Modification - sourceFile
050:
051: String sourceFile = "<unknown>";
052:
053: /**
054: Used to add source file info to exception
055: */
056: public void setErrorSourceFile(String file) {
057: this .sourceFile = file;
058: }
059:
060: public String getErrorSourceFile() {
061: return sourceFile;
062: }
063:
064: // End BeanShell Modification - sourceFile
065:
066: /**
067: * This constructor is used by the method "generateParseException"
068: * in the generated parser. Calling this constructor generates
069: * a new object of this type with the fields "currentToken",
070: * "expectedTokenSequences", and "tokenImage" set. The boolean
071: * flag "specialConstructor" is also set to true to indicate that
072: * this constructor was used to create this object.
073: * This constructor calls its super class with the empty string
074: * to force the "toString" method of parent class "Throwable" to
075: * print the error message in the form:
076: * ParseException: <result of getMessage>
077: */
078: public ParseException(Token currentTokenVal,
079: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
080: // Begin BeanShell Modification - constructor
081: this ();
082: // End BeanShell Modification - constructor
083: specialConstructor = true;
084: currentToken = currentTokenVal;
085: expectedTokenSequences = expectedTokenSequencesVal;
086: tokenImage = tokenImageVal;
087: }
088:
089: /**
090: * The following constructors are for use by you for whatever
091: * purpose you can think of. Constructing the exception in this
092: * manner makes the exception behave in the normal way - i.e., as
093: * documented in the class "Throwable". The fields "errorToken",
094: * "expectedTokenSequences", and "tokenImage" do not contain
095: * relevant information. The JavaCC generated code does not use
096: * these constructors.
097: */
098:
099: public ParseException() {
100: // Begin BeanShell Modification - constructor
101: this ("");
102: // End BeanShell Modification - constructor
103: specialConstructor = false;
104: }
105:
106: public ParseException(String message) {
107: // Begin BeanShell Modification - super constructor args
108: // null node, null callstack, ParseException knows where the error is.
109: super (message, null, null);
110: // End BeanShell Modification - super constructor args
111: specialConstructor = false;
112: }
113:
114: /**
115: * This variable determines which constructor was used to create
116: * this object and thereby affects the semantics of the
117: * "getMessage" method (see below).
118: */
119: protected boolean specialConstructor;
120:
121: /**
122: * This is the last token that has been consumed successfully. If
123: * this object has been created due to a parse error, the token
124: * followng this token will (therefore) be the first error token.
125: */
126: public Token currentToken;
127:
128: /**
129: * Each entry in this array is an array of integers. Each array
130: * of integers represents a sequence of tokens (by their ordinal
131: * values) that is expected at this point of the parse.
132: */
133: public int[][] expectedTokenSequences;
134:
135: /**
136: * This is a reference to the "tokenImage" array of the generated
137: * parser within which the parse error occurred. This array is
138: * defined in the generated ...Constants interface.
139: */
140: public String[] tokenImage;
141:
142: // Begin BeanShell Modification - moved body to overloaded getMessage()
143: public String getMessage() {
144: return getMessage(false);
145: }
146:
147: // End BeanShell Modification - moved body to overloaded getMessage()
148:
149: /**
150: * This method has the standard behavior when this object has been
151: * created using the standard constructors. Otherwise, it uses
152: * "currentToken" and "expectedTokenSequences" to generate a parse
153: * error message and returns it. If this object has been created
154: * due to a parse error, and you do not catch it (it gets thrown
155: * from the parser), then this method is called during the printing
156: * of the final stack trace, and hence the correct error message
157: * gets displayed.
158: */
159: // Begin BeanShell Modification - added debug param
160: public String getMessage(boolean debug) {
161: // End BeanShell Modification - added debug param
162: if (!specialConstructor) {
163: return super .getMessage();
164: }
165: String expected = "";
166: int maxSize = 0;
167: for (int i = 0; i < expectedTokenSequences.length; i++) {
168: if (maxSize < expectedTokenSequences[i].length) {
169: maxSize = expectedTokenSequences[i].length;
170: }
171: for (int j = 0; j < expectedTokenSequences[i].length; j++) {
172: expected += tokenImage[expectedTokenSequences[i][j]]
173: + " ";
174: }
175: if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
176: expected += "...";
177: }
178: expected += eol + " ";
179: }
180: // Begin BeanShell Modification - added sourceFile info
181: String retval = "In file: " + sourceFile + " Encountered \"";
182: // End BeanShell Modification - added sourceFile info
183: Token tok = currentToken.next;
184: for (int i = 0; i < maxSize; i++) {
185: if (i != 0)
186: retval += " ";
187: if (tok.kind == 0) {
188: retval += tokenImage[0];
189: break;
190: }
191: retval += add_escapes(tok.image);
192: tok = tok.next;
193: }
194: retval += "\" at line " + currentToken.next.beginLine
195: + ", column " + currentToken.next.beginColumn + "."
196: + eol;
197:
198: // Begin BeanShell Modification - made conditional on debug
199: if (debug) {
200: if (expectedTokenSequences.length == 1) {
201: retval += "Was expecting:" + eol + " ";
202: } else {
203: retval += "Was expecting one of:" + eol + " ";
204: }
205:
206: retval += expected;
207: }
208: // End BeanShell Modification - made conditional on debug
209:
210: return retval;
211: }
212:
213: /**
214: * The end of line string for this machine.
215: */
216: protected String eol = System.getProperty("line.separator", "\n");
217:
218: /**
219: * Used to convert raw characters to their escaped version
220: * when these raw version cannot be used as part of an ASCII
221: * string literal.
222: */
223: protected String add_escapes(String str) {
224: StringBuffer retval = new StringBuffer();
225: char ch;
226: for (int i = 0; i < str.length(); i++) {
227: switch (str.charAt(i)) {
228: case 0:
229: continue;
230: case '\b':
231: retval.append("\\b");
232: continue;
233: case '\t':
234: retval.append("\\t");
235: continue;
236: case '\n':
237: retval.append("\\n");
238: continue;
239: case '\f':
240: retval.append("\\f");
241: continue;
242: case '\r':
243: retval.append("\\r");
244: continue;
245: case '\"':
246: retval.append("\\\"");
247: continue;
248: case '\'':
249: retval.append("\\\'");
250: continue;
251: case '\\':
252: retval.append("\\\\");
253: continue;
254: default:
255: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
256: String s = "0000" + Integer.toString(ch, 16);
257: retval.append("\\u"
258: + s.substring(s.length() - 4, s.length()));
259: } else {
260: retval.append(ch);
261: }
262: continue;
263: }
264: }
265: return retval.toString();
266: }
267:
268: // Begin BeanShell Modification - override error methods and toString
269:
270: public int getErrorLineNumber() {
271: return currentToken.next.beginLine;
272: }
273:
274: public String getErrorText() {
275: // copied from generated getMessage()
276: int maxSize = 0;
277: for (int i = 0; i < expectedTokenSequences.length; i++) {
278: if (maxSize < expectedTokenSequences[i].length)
279: maxSize = expectedTokenSequences[i].length;
280: }
281:
282: String retval = "";
283: Token tok = currentToken.next;
284: for (int i = 0; i < maxSize; i++) {
285: if (i != 0)
286: retval += " ";
287: if (tok.kind == 0) {
288: retval += tokenImage[0];
289: break;
290: }
291: retval += add_escapes(tok.image);
292: tok = tok.next;
293: }
294:
295: return retval;
296: }
297:
298: public String toString() {
299: return getMessage();
300: }
301:
302: // End BeanShell Modification - override error methods and toString
303:
304: }
|