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