001:/*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019:package org.apache.beehive.netui.script.el.parser;
020:
021:import org.apache.beehive.netui.util.internal.InternalStringBuilder;
022:
023:public class TokenMgrError
024: extends Error {
025:
026: /*
027: * Ordinals for various reasons why an Error of this type can be thrown.
028: */
029:
030: /**
031: * Lexical error occured.
032: */
033: static final int LEXICAL_ERROR = 0;
034:
035: /**
036: * An attempt wass made to create a second instance of a static token manager.
037: */
038: static final int STATIC_LEXER_ERROR = 1;
039:
040: /**
041: * Tried to change to an invalid lexical state.
042: */
043: static final int INVALID_LEXICAL_STATE = 2;
044:
045: /**
046: * Detected (and bailed out of) an infinite loop in the token manager.
047: */
048: static final int LOOP_DETECTED = 3;
049:
050: /**
051: * Indicates the reason why the exception is thrown. It will have
052: * one of the above 4 values.
053: */
054: int errorCode;
055:
056: /**
057: * Replaces unprintable characters by their espaced (or unicode escaped)
058: * equivalents in the given string
059: */
060: protected static final String addEscapes(String str) {
061: InternalStringBuilder retval = new InternalStringBuilder();
062: char ch;
063: for(int i = 0; i < str.length(); i++) {
064: switch(str.charAt(i)) {
065: case 0:
066: continue;
067: case '\b':
068: retval.append("\\b");
069: continue;
070: case '\t':
071: retval.append("\\t");
072: continue;
073: case '\n':
074: retval.append("\\n");
075: continue;
076: case '\f':
077: retval.append("\\f");
078: continue;
079: case '\r':
080: retval.append("\\r");
081: continue;
082: case '\"':
083: retval.append("\\\"");
084: continue;
085: case '\'':
086: retval.append("\\\'");
087: continue;
088: case '\\':
089: retval.append("\\\\");
090: continue;
091: default:
092: if((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
093: String s = "0000" + Integer.toString(ch, 16);
094: retval.append("\\u" + s.substring(s.length() - 4, s.length()));
095: } else {
096: retval.append(ch);
097: }
098: continue;
099: }
100: }
101: return retval.toString();
102: }
103:
104: /**
105: * Returns a detailed message for the Error when it is thrown by the
106: * token manager to indicate a lexical error.
107: * Parameters :
108: * EOFSeen : indicates if EOF caused the lexicl error
109: * curLexState : lexical state in which this error occured
110: * errorLine : line number when the error occured
111: * errorColumn : column number when the error occured
112: * errorAfter : prefix that was seen before this error occured
113: * curchar : the offending character
114: * Note: You can customize the lexical error message by modifying this method.
115: */
116: protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
117: return ("Lexical error at line " +
118: errorLine + ", column " +
119: errorColumn + ". Encountered: " +
120: (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
121: "after : \"" + addEscapes(errorAfter) + "\"");
122: }
123:
124: /**
125: * You can also modify the body of this method to customize your error messages.
126: * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
127: * of end-users concern, so you can return something like :
128: * <p/>
129: * "Internal Error : Please file a bug report .... "
130: * <p/>
131: * from this method for such cases in the release version of your parser.
132: */
133: public String getMessage() {
134: return super .getMessage();
135: }
136:
137: /*
138: * Constructors of various flavors follow.
139: */
140:
141: public TokenMgrError() {
142: }
143:
144: public TokenMgrError(String message, int reason) {
145: super (message);
146: errorCode = reason;
147: }
148:
149: public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
150: this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
151: }
152:}
|