001 /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 3.0 */
002 /**
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 package org.granite.gravity.selector;
021
022 public class TokenMgrError extends Error
023 {
024 private static final long serialVersionUID = 1L;
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 StringBuffer retval = new StringBuffer();
062 char ch;
063 for (int i = 0; i < str.length(); i++) {
064 switch (str.charAt(i))
065 {
066 case 0 :
067 continue;
068 case '\b':
069 retval.append("\\b");
070 continue;
071 case '\t':
072 retval.append("\\t");
073 continue;
074 case '\n':
075 retval.append("\\n");
076 continue;
077 case '\f':
078 retval.append("\\f");
079 continue;
080 case '\r':
081 retval.append("\\r");
082 continue;
083 case '\"':
084 retval.append("\\\"");
085 continue;
086 case '\'':
087 retval.append("\\\'");
088 continue;
089 case '\\':
090 retval.append("\\\\");
091 continue;
092 default:
093 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
094 String s = "0000" + Integer.toString(ch, 16);
095 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
096 } else {
097 retval.append(ch);
098 }
099 continue;
100 }
101 }
102 return retval.toString();
103 }
104
105 /**
106 * Returns a detailed message for the Error when it is thrown by the
107 * token manager to indicate a lexical error.
108 * Parameters :
109 * EOFSeen : indicates if EOF caused the lexicl error
110 * curLexState : lexical state in which this error occured
111 * errorLine : line number when the error occured
112 * errorColumn : column number when the error occured
113 * errorAfter : prefix that was seen before this error occured
114 * curchar : the offending character
115 * Note: You can customize the lexical error message by modifying this method.
116 */
117 protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
118 return("Lexical error at line " +
119 errorLine + ", column " +
120 errorColumn + ". Encountered: " +
121 (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
122 "after : \"" + addEscapes(errorAfter) + "\"");
123 }
124
125 /**
126 * You can also modify the body of this method to customize your error messages.
127 * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
128 * of end-users concern, so you can return something like :
129 *
130 * "Internal Error : Please file a bug report .... "
131 *
132 * from this method for such cases in the release version of your parser.
133 */
134 @Override
135 public String getMessage() {
136 return super.getMessage();
137 }
138
139 /*
140 * Constructors of various flavors follow.
141 */
142
143 public TokenMgrError() {
144 }
145
146 public TokenMgrError(String message, int reason) {
147 super(message);
148 errorCode = reason;
149 }
150
151 public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
152 this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
153 }
154 }