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