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