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