001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
012: package com.versant.core.jdo.query;
013:
014: /**
015: * This exception is thrown when parse errors are encountered.
016: * You can explicitly create objects of this exception type by
017: * calling the method generateParseException in the generated
018: * parser.
019: *
020: * You can modify this class to customize your error reporting
021: * mechanisms so long as you retain the public fields.
022: */
023: public class ParseException extends Exception {
024:
025: /**
026: * This constructor is used by the method "generateParseException"
027: * in the generated parser. Calling this constructor generates
028: * a new object of this type with the fields "currentToken",
029: * "expectedTokenSequences", and "tokenImage" set. The boolean
030: * flag "specialConstructor" is also set to true to indicate that
031: * this constructor was used to create this object.
032: * This constructor calls its super class with the empty string
033: * to force the "toString" method of parent class "Throwable" to
034: * print the error message in the form:
035: * ParseException: <result of getMessage>
036: */
037: public ParseException(Token currentTokenVal,
038: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
039: super ("");
040: specialConstructor = true;
041: currentToken = currentTokenVal;
042: expectedTokenSequences = expectedTokenSequencesVal;
043: tokenImage = tokenImageVal;
044: }
045:
046: /**
047: * The following constructors are for use by you for whatever
048: * purpose you can think of. Constructing the exception in this
049: * manner makes the exception behave in the normal way - i.e., as
050: * documented in the class "Throwable". The fields "errorToken",
051: * "expectedTokenSequences", and "tokenImage" do not contain
052: * relevant information. The JavaCC generated code does not use
053: * these constructors.
054: */
055:
056: public ParseException() {
057: super ();
058: specialConstructor = false;
059: }
060:
061: public ParseException(String message) {
062: super (message);
063: specialConstructor = false;
064: }
065:
066: /**
067: * This variable determines which constructor was used to create
068: * this object and thereby affects the semantics of the
069: * "getMessage" method (see below).
070: */
071: protected boolean specialConstructor;
072:
073: /**
074: * This is the last token that has been consumed successfully. If
075: * this object has been created due to a parse error, the token
076: * followng this token will (therefore) be the first error token.
077: */
078: public Token currentToken;
079:
080: /**
081: * Each entry in this array is an array of integers. Each array
082: * of integers represents a sequence of tokens (by their ordinal
083: * values) that is expected at this point of the parse.
084: */
085: public int[][] expectedTokenSequences;
086:
087: /**
088: * This is a reference to the "tokenImage" array of the generated
089: * parser within which the parse error occurred. This array is
090: * defined in the generated ...Constants interface.
091: */
092: public String[] tokenImage;
093:
094: /**
095: * This method has the standard behavior when this object has been
096: * created using the standard constructors. Otherwise, it uses
097: * "currentToken" and "expectedTokenSequences" to generate a parse
098: * error message and returns it. If this object has been created
099: * due to a parse error, and you do not catch it (it gets thrown
100: * from the parser), then this method is called during the printing
101: * of the final stack trace, and hence the correct error message
102: * gets displayed.
103: */
104: public String getMessage() {
105: if (!specialConstructor) {
106: return super .getMessage();
107: }
108: String expected = "";
109: int maxSize = 0;
110: for (int i = 0; i < expectedTokenSequences.length; i++) {
111: if (maxSize < expectedTokenSequences[i].length) {
112: maxSize = expectedTokenSequences[i].length;
113: }
114: for (int j = 0; j < expectedTokenSequences[i].length; j++) {
115: expected += tokenImage[expectedTokenSequences[i][j]]
116: + " ";
117: }
118: if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
119: expected += "...";
120: }
121: expected += eol + " ";
122: }
123: String retval = "Encountered \"";
124: Token tok = currentToken.next;
125: for (int i = 0; i < maxSize; i++) {
126: if (i != 0)
127: retval += " ";
128: if (tok.kind == 0) {
129: retval += tokenImage[0];
130: break;
131: }
132: retval += add_escapes(tok.image);
133: tok = tok.next;
134: }
135: retval += "\" at line " + currentToken.next.beginLine
136: + ", column " + currentToken.next.beginColumn;
137: retval += "." + eol;
138: if (expectedTokenSequences.length == 1) {
139: retval += "Was expecting:" + eol + " ";
140: } else {
141: retval += "Was expecting one of:" + eol + " ";
142: }
143: retval += expected;
144: return retval;
145: }
146:
147: /**
148: * The end of line string for this machine.
149: */
150: protected String eol = System.getProperty("line.separator", "\n");
151:
152: /**
153: * Used to convert raw characters to their escaped version
154: * when these raw version cannot be used as part of an ASCII
155: * string literal.
156: */
157: protected String add_escapes(String str) {
158: StringBuffer retval = new StringBuffer();
159: char ch;
160: for (int i = 0; i < str.length(); i++) {
161: switch (str.charAt(i)) {
162: case 0:
163: continue;
164: case '\b':
165: retval.append("\\b");
166: continue;
167: case '\t':
168: retval.append("\\t");
169: continue;
170: case '\n':
171: retval.append("\\n");
172: continue;
173: case '\f':
174: retval.append("\\f");
175: continue;
176: case '\r':
177: retval.append("\\r");
178: continue;
179: case '\"':
180: retval.append("\\\"");
181: continue;
182: case '\'':
183: retval.append("\\\'");
184: continue;
185: case '\\':
186: retval.append("\\\\");
187: continue;
188: default:
189: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
190: String s = "0000" + Integer.toString(ch, 16);
191: retval.append("\\u"
192: + s.substring(s.length() - 4, s.length()));
193: } else {
194: retval.append(ch);
195: }
196: continue;
197: }
198: }
199: return retval.toString();
200: }
201:
202: }
|