001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
031: package de.intarsys.pdf.postscript;
032:
033: /**
034: * This exception is thrown when parse errors are encountered. You can
035: * explicitly create objects of this exception type by calling the method
036: * generateParseException in the generated parser.
037: *
038: * You can modify this class to customize your error reporting mechanisms so
039: * long as you retain the public fields.
040: */
041: public class ParseException extends Exception {
042: /**
043: * This is the last token that has been consumed successfully. If this
044: * object has been created due to a parse error, the token followng this
045: * token will (therefore) be the first error token.
046: */
047: public Token currentToken;
048:
049: /**
050: * The end of line string for this machine.
051: */
052: protected String eol = System.getProperty("line.separator", "\n");
053:
054: /**
055: * Each entry in this array is an array of integers. Each array of integers
056: * represents a sequence of tokens (by their ordinal values) that is
057: * expected at this point of the parse.
058: */
059: public int[][] expectedTokenSequences;
060:
061: /**
062: * This variable determines which constructor was used to create this object
063: * and thereby affects the semantics of the "getMessage" method (see below).
064: */
065: protected boolean specialConstructor;
066:
067: /**
068: * This is a reference to the "tokenImage" array of the generated parser
069: * within which the parse error occurred. This array is defined in the
070: * generated ...Constants interface.
071: */
072: public String[] tokenImage;
073:
074: /**
075: * The following constructors are for use by you for whatever purpose you
076: * can think of. Constructing the exception in this manner makes the
077: * exception behave in the normal way - i.e., as documented in the class
078: * "Throwable". The fields "errorToken", "expectedTokenSequences", and
079: * "tokenImage" do not contain relevant information. The JavaCC generated
080: * code does not use these constructors.
081: */
082: public ParseException() {
083: super ();
084: specialConstructor = false;
085: }
086:
087: public ParseException(String message) {
088: super (message);
089: specialConstructor = false;
090: }
091:
092: public ParseException(Throwable cause) {
093: super (cause);
094: }
095:
096: /**
097: * This constructor is used by the method "generateParseException" in the
098: * generated parser. Calling this constructor generates a new object of this
099: * type with the fields "currentToken", "expectedTokenSequences", and
100: * "tokenImage" set. The boolean flag "specialConstructor" is also set to
101: * true to indicate that this constructor was used to create this object.
102: * This constructor calls its super class with the empty string to force the
103: * "toString" method of parent class "Throwable" to print the error message
104: * in the form: ParseException: <result of getMessage>
105: */
106: public ParseException(Token currentTokenVal,
107: int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
108: super (""); //$NON-NLS-1$
109: specialConstructor = true;
110: currentToken = currentTokenVal;
111: expectedTokenSequences = expectedTokenSequencesVal;
112: tokenImage = tokenImageVal;
113: }
114:
115: /**
116: * Used to convert raw characters to their escaped version when these raw
117: * version cannot be used as part of an ASCII string literal.
118: */
119: protected String add_escapes(String str) {
120: StringBuilder retval = new StringBuilder();
121: char ch;
122: for (int i = 0; i < str.length(); i++) {
123: switch (str.charAt(i)) {
124: case 0:
125: continue;
126: case '\b':
127: retval.append("\\b"); //$NON-NLS-1$
128: continue;
129: case '\t':
130: retval.append("\\t"); //$NON-NLS-1$
131: continue;
132: case '\n':
133: retval.append("\\n"); //$NON-NLS-1$
134: continue;
135: case '\f':
136: retval.append("\\f"); //$NON-NLS-1$
137: continue;
138: case '\r':
139: retval.append("\\r"); //$NON-NLS-1$
140: continue;
141: case '\"':
142: retval.append("\\\""); //$NON-NLS-1$
143: continue;
144: case '\'':
145: retval.append("\\\'"); //$NON-NLS-1$
146: continue;
147: case '\\':
148: retval.append("\\\\"); //$NON-NLS-1$
149: continue;
150: default:
151: if (((ch = str.charAt(i)) < 0x20) || (ch > 0x7e)) {
152: String s = "0000" + Integer.toString(ch, 16); //$NON-NLS-1$
153: retval.append("\\u" //$NON-NLS-1$
154: + s.substring(s.length() - 4, s.length()));
155: } else {
156: retval.append(ch);
157: }
158: continue;
159: }
160: }
161: return retval.toString();
162: }
163:
164: /**
165: * This method has the standard behavior when this object has been created
166: * using the standard constructors. Otherwise, it uses "currentToken" and
167: * "expectedTokenSequences" to generate a parse error message and returns
168: * it. If this object has been created due to a parse error, and you do not
169: * catch it (it gets thrown from the parser), then this method is called
170: * during the printing of the final stack trace, and hence the correct error
171: * message gets displayed.
172: */
173: public String getMessage() {
174: if (!specialConstructor) {
175: return super .getMessage();
176: }
177: StringBuilder expected = new StringBuilder();
178: int maxSize = 0;
179: for (int i = 0; i < expectedTokenSequences.length; i++) {
180: if (maxSize < expectedTokenSequences[i].length) {
181: maxSize = expectedTokenSequences[i].length;
182: }
183: for (int j = 0; j < expectedTokenSequences[i].length; j++) {
184: expected.append(
185: tokenImage[expectedTokenSequences[i][j]])
186: .append(" ");
187: }
188: if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
189: expected.append("...");
190: }
191: expected.append(eol).append(" ");
192: }
193: String retval = "Encountered \"";
194: Token tok = currentToken.next;
195: for (int i = 0; i < maxSize; i++) {
196: if (i != 0) {
197: retval += " ";
198: }
199: if (tok.kind == 0) {
200: retval += tokenImage[0];
201: break;
202: }
203: retval += add_escapes(tok.image);
204: tok = tok.next;
205: }
206: retval += ("\" at line " + currentToken.next.beginLine
207: + ", column " + currentToken.next.beginColumn);
208: retval += ("." + eol);
209: if (expectedTokenSequences.length == 1) {
210: retval += ("Was expecting:" + eol + " ");
211: } else {
212: retval += ("Was expecting one of:" + eol + " ");
213: }
214: retval += expected.toString();
215: return retval;
216: }
217: }
|