001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
043: package org.netbeans.modules.debugger.jpda.expr;
044:
045: /**
046: * This exception is thrown when parse errors are encountered.
047: * You can explicitly create objects of this exception type by
048: * calling the method generateParseException in the generated
049: * parser.
050: *
051: * You can modify this class to customize your error reporting
052: * mechanisms so long as you retain the public fields.
053: */
054: public class ParseException extends Exception {
055:
056: /**
057: * This constructor is used by the method "generateParseException"
058: * in the generated parser. Calling this constructor generates
059: * a new object of this type with the fields "currentToken",
060: * "expectedTokenSequences", and "tokenImage" set. The boolean
061: * flag "specialConstructor" is also set to true to indicate that
062: * this constructor was used to create this object.
063: * This constructor calls its super class with the empty string
064: * to force the "toString" method of parent class "Throwable" to
065: * print the error message in the form:
066: * ParseException: <result of getMessage>
067: */
068: public ParseException(Token currentTokenVal,
069: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
070: super ("");
071: specialConstructor = true;
072: currentToken = currentTokenVal;
073: expectedTokenSequences = expectedTokenSequencesVal;
074: tokenImage = tokenImageVal;
075: }
076:
077: /**
078: * The following constructors are for use by you for whatever
079: * purpose you can think of. Constructing the exception in this
080: * manner makes the exception behave in the normal way - i.e., as
081: * documented in the class "Throwable". The fields "errorToken",
082: * "expectedTokenSequences", and "tokenImage" do not contain
083: * relevant information. The JavaCC generated code does not use
084: * these constructors.
085: */
086:
087: public ParseException() {
088: super ();
089: specialConstructor = false;
090: }
091:
092: public ParseException(String message) {
093: super (message);
094: specialConstructor = false;
095: }
096:
097: /**
098: * This variable determines which constructor was used to create
099: * this object and thereby affects the semantics of the
100: * "getMessage" method (see below).
101: */
102: protected boolean specialConstructor;
103:
104: /**
105: * This is the last token that has been consumed successfully. If
106: * this object has been created due to a parse error, the token
107: * followng this token will (therefore) be the first error token.
108: */
109: public Token currentToken;
110:
111: /**
112: * Each entry in this array is an array of integers. Each array
113: * of integers represents a sequence of tokens (by their ordinal
114: * values) that is expected at this point of the parse.
115: */
116: public int[][] expectedTokenSequences;
117:
118: /**
119: * This is a reference to the "tokenImage" array of the generated
120: * parser within which the parse error occurred. This array is
121: * defined in the generated ...Constants interface.
122: */
123: public String[] tokenImage;
124:
125: /**
126: * This method has the standard behavior when this object has been
127: * created using the standard constructors. Otherwise, it uses
128: * "currentToken" and "expectedTokenSequences" to generate a parse
129: * error message and returns it. If this object has been created
130: * due to a parse error, and you do not catch it (it gets thrown
131: * from the parser), then this method is called during the printing
132: * of the final stack trace, and hence the correct error message
133: * gets displayed.
134: */
135: public String getMessage() {
136: if (!specialConstructor) {
137: return super .getMessage();
138: }
139: String expected = "";
140: int maxSize = 0;
141: for (int i = 0; i < expectedTokenSequences.length; i++) {
142: if (maxSize < expectedTokenSequences[i].length) {
143: maxSize = expectedTokenSequences[i].length;
144: }
145: for (int j = 0; j < expectedTokenSequences[i].length; j++) {
146: expected += tokenImage[expectedTokenSequences[i][j]]
147: + " ";
148: }
149: if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
150: expected += "...";
151: }
152: expected += eol + " ";
153: }
154: String retval = "Encountered \"";
155: Token tok = currentToken.next;
156: for (int i = 0; i < maxSize; i++) {
157: if (i != 0)
158: retval += " ";
159: if (tok.kind == 0) {
160: retval += tokenImage[0];
161: break;
162: }
163: retval += add_escapes(tok.image);
164: tok = tok.next;
165: }
166: retval += "\" at line " + currentToken.next.beginLine
167: + ", column " + currentToken.next.beginColumn;
168: retval += "." + eol;
169: if (expectedTokenSequences.length == 1) {
170: retval += "Was expecting:" + eol + " ";
171: } else {
172: retval += "Was expecting one of:" + eol + " ";
173: }
174: retval += expected;
175: return retval;
176: }
177:
178: /**
179: * The end of line string for this machine.
180: */
181: protected String eol = System.getProperty("line.separator", "\n");
182:
183: /**
184: * Used to convert raw characters to their escaped version
185: * when these raw version cannot be used as part of an ASCII
186: * string literal.
187: */
188: protected String add_escapes(String str) {
189: StringBuffer retval = new StringBuffer();
190: char ch;
191: for (int i = 0; i < str.length(); i++) {
192: switch (str.charAt(i)) {
193: case 0:
194: continue;
195: case '\b':
196: retval.append("\\b");
197: continue;
198: case '\t':
199: retval.append("\\t");
200: continue;
201: case '\n':
202: retval.append("\\n");
203: continue;
204: case '\f':
205: retval.append("\\f");
206: continue;
207: case '\r':
208: retval.append("\\r");
209: continue;
210: case '\"':
211: retval.append("\\\"");
212: continue;
213: case '\'':
214: retval.append("\\\'");
215: continue;
216: case '\\':
217: retval.append("\\\\");
218: continue;
219: default:
220: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
221: String s = "0000" + Integer.toString(ch, 16);
222: retval.append("\\u"
223: + s.substring(s.length() - 4, s.length()));
224: } else {
225: retval.append(ch);
226: }
227: continue;
228: }
229: }
230: return retval.toString();
231: }
232:
233: }
|