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