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