001:/*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004:package com.tc.aspectwerkz.expression.ast;
005:
006:public class TokenMgrError extends Error {
007: /*
008: * Ordinals for various reasons why an Error of this type can be thrown.
009: */
010:
011: /**
012: * Lexical error occured.
013: */
014: static final int LEXICAL_ERROR = 0;
015:
016: /**
017: * An attempt wass made to create a second instance of a static token manager.
018: */
019: static final int STATIC_LEXER_ERROR = 1;
020:
021: /**
022: * Tried to change to an invalid lexical state.
023: */
024: static final int INVALID_LEXICAL_STATE = 2;
025:
026: /**
027: * Detected (and bailed out of) an infinite loop in the token manager.
028: */
029: static final int LOOP_DETECTED = 3;
030:
031: /**
032: * Indicates the reason why the exception is thrown. It will have one of the above 4 values.
033: */
034: int errorCode;
035:
036: /**
037: * Replaces unprintable characters by their espaced (or unicode escaped) equivalents in the given string
038: */
039: protected static final String addEscapes(String str) {
040: StringBuffer retval = new StringBuffer();
041: char ch;
042: for (int i = 0; i < str.length(); i++) {
043: switch (str.charAt(i)) {
044: case 0:
045: continue;
046: case '\b':
047: retval.append("\\b");
048: continue;
049: case '\t':
050: retval.append("\\t");
051: continue;
052: case '\n':
053: retval.append("\\n");
054: continue;
055: case '\f':
056: retval.append("\\f");
057: continue;
058: case '\r':
059: retval.append("\\r");
060: continue;
061: case '\"':
062: retval.append("\\\"");
063: continue;
064: case '\'':
065: retval.append("\\\'");
066: continue;
067: case '\\':
068: retval.append("\\\\");
069: continue;
070: default:
071: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
072: String s = "0000" + Integer.toString(ch, 16);
073: retval.append("\\u" + s.substring(s.length() - 4, s.length()));
074: } else {
075: retval.append(ch);
076: }
077: continue;
078: }
079: }
080: return retval.toString();
081: }
082:
083: /**
084: * Returns a detailed message for the Error when it is thrown by the token manager to indicate a lexical error.
085: * Parameters : EOFSeen : indicates if EOF caused the lexicl error curLexState : lexical state in which this error
086: * occured errorLine : line number when the error occured errorColumn : column number when the error occured
087: * errorAfter : prefix that was seen before this error occured curchar : the offending character Note: You can
088: * customize the lexical error message by modifying this method.
089: */
090: protected static String LexicalError(boolean EOFSeen,
091: int lexState,
092: int errorLine,
093: int errorColumn,
094: String errorAfter,
095: char curChar) {
096: return ("Lexical error at line "
097: + errorLine
098: + ", column "
099: + errorColumn
100: + ". Encountered: "
101: +
102: (EOFSeen ?
103: "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int) curChar + "), ")
104: + "after : \""
105: + addEscapes(errorAfter) + "\"");
106: }
107:
108: /**
109: * You can also modify the body of this method to customize your error messages. For example, cases like
110: * LOOP_DETECTED and INVALID_LEXICAL_STATE are not of end-users concern, so you can return something like :
111: * "Internal Error : Please file a bug report .... " from this method for such cases in the release version of your
112: * parser.
113: */
114: public String getMessage() {
115: return super .getMessage();
116: }
117:
118: /*
119: * Constructors of various flavors follow.
120: */
121:
122: public TokenMgrError() {
123: }
124:
125: public TokenMgrError(String message, int reason) {
126: super (message);
127: errorCode = reason;
128: }
129:
130: public TokenMgrError(boolean EOFSeen,
131: int lexState,
132: int errorLine,
133: int errorColumn,
134: String errorAfter,
135: char curChar,
136: int reason) {
137: this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
138: }
139:}
|