001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 2.1 */
028: package fr.ign.cogit.geoxygene.util.conversion;
029:
030: /**
031: * This exception is thrown when parse errors are encountered.
032: * You can explicitly create objects of this exception type by
033: * calling the method generateParseException in the generated
034: * parser.
035: *
036: * You can modify this class to customize your error reporting
037: * mechanisms so long as you retain the public fields.
038: */
039: public class ParseException extends Exception {
040:
041: /**
042: * This constructor is used by the method "generateParseException"
043: * in the generated parser. Calling this constructor generates
044: * a new object of this type with the fields "currentToken",
045: * "expectedTokenSequences", and "tokenImage" set. The boolean
046: * flag "specialConstructor" is also set to true to indicate that
047: * this constructor was used to create this object.
048: * This constructor calls its super class with the empty string
049: * to force the "toString" method of parent class "Throwable" to
050: * print the error message in the form:
051: * ParseException: <result of getMessage>
052: */
053: public ParseException(Token currentTokenVal,
054: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
055: super ("");
056: specialConstructor = true;
057: currentToken = currentTokenVal;
058: expectedTokenSequences = expectedTokenSequencesVal;
059: tokenImage = tokenImageVal;
060: }
061:
062: /**
063: * The following constructors are for use by you for whatever
064: * purpose you can think of. Constructing the exception in this
065: * manner makes the exception behave in the normal way - i.e., as
066: * documented in the class "Throwable". The fields "errorToken",
067: * "expectedTokenSequences", and "tokenImage" do not contain
068: * relevant information. The JavaCC generated code does not use
069: * these constructors.
070: */
071:
072: public ParseException() {
073: super ();
074: specialConstructor = false;
075: }
076:
077: public ParseException(String message) {
078: super (message);
079: specialConstructor = false;
080: }
081:
082: /**
083: * This variable determines which constructor was used to create
084: * this object and thereby affects the semantics of the
085: * "getMessage" method (see below).
086: */
087: protected boolean specialConstructor;
088:
089: /**
090: * This is the last token that has been consumed successfully. If
091: * this object has been created due to a parse error, the token
092: * followng this token will (therefore) be the first error token.
093: */
094: public Token currentToken;
095:
096: /**
097: * Each entry in this array is an array of integers. Each array
098: * of integers represents a sequence of tokens (by their ordinal
099: * values) that is expected at this point of the parse.
100: */
101: public int[][] expectedTokenSequences;
102:
103: /**
104: * This is a reference to the "tokenImage" array of the generated
105: * parser within which the parse error occurred. This array is
106: * defined in the generated ...Constants interface.
107: */
108: public String[] tokenImage;
109:
110: /**
111: * This method has the standard behavior when this object has been
112: * created using the standard constructors. Otherwise, it uses
113: * "currentToken" and "expectedTokenSequences" to generate a parse
114: * error message and returns it. If this object has been created
115: * due to a parse error, and you do not catch it (it gets thrown
116: * from the parser), then this method is called during the printing
117: * of the final stack trace, and hence the correct error message
118: * gets displayed.
119: */
120: public String getMessage() {
121: if (!specialConstructor) {
122: return super .getMessage();
123: }
124: String expected = "";
125: int maxSize = 0;
126: for (int i = 0; i < expectedTokenSequences.length; i++) {
127: if (maxSize < expectedTokenSequences[i].length) {
128: maxSize = expectedTokenSequences[i].length;
129: }
130: for (int j = 0; j < expectedTokenSequences[i].length; j++) {
131: expected += tokenImage[expectedTokenSequences[i][j]]
132: + " ";
133: }
134: if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
135: expected += "...";
136: }
137: expected += eol + " ";
138: }
139: String retval = "Encountered \"";
140: Token tok = currentToken.next;
141: for (int i = 0; i < maxSize; i++) {
142: if (i != 0)
143: retval += " ";
144: if (tok.kind == 0) {
145: retval += tokenImage[0];
146: break;
147: }
148: retval += add_escapes(tok.image);
149: tok = tok.next;
150: }
151: retval += "\" at line " + currentToken.next.beginLine
152: + ", column " + currentToken.next.beginColumn;
153: retval += "." + eol;
154: if (expectedTokenSequences.length == 1) {
155: retval += "Was expecting:" + eol + " ";
156: } else {
157: retval += "Was expecting one of:" + eol + " ";
158: }
159: retval += expected;
160: return retval;
161: }
162:
163: /**
164: * The end of line string for this machine.
165: */
166: protected String eol = System.getProperty("line.separator", "\n");
167:
168: /**
169: * Used to convert raw characters to their escaped version
170: * when these raw version cannot be used as part of an ASCII
171: * string literal.
172: */
173: protected String add_escapes(String str) {
174: StringBuffer retval = new StringBuffer();
175: char ch;
176: for (int i = 0; i < str.length(); i++) {
177: switch (str.charAt(i)) {
178: case 0:
179: continue;
180: case '\b':
181: retval.append("\\b");
182: continue;
183: case '\t':
184: retval.append("\\t");
185: continue;
186: case '\n':
187: retval.append("\\n");
188: continue;
189: case '\f':
190: retval.append("\\f");
191: continue;
192: case '\r':
193: retval.append("\\r");
194: continue;
195: case '\"':
196: retval.append("\\\"");
197: continue;
198: case '\'':
199: retval.append("\\\'");
200: continue;
201: case '\\':
202: retval.append("\\\\");
203: continue;
204: default:
205: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
206: String s = "0000" + Integer.toString(ch, 16);
207: retval.append("\\u"
208: + s.substring(s.length() - 4, s.length()));
209: } else {
210: retval.append(ch);
211: }
212: continue;
213: }
214: }
215: return retval.toString();
216: }
217:
218: }
|