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