001    /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 0.7pre2 */
002    
003    /*
004     * Cobertura - http://cobertura.sourceforge.net/
005     *
006     * This file was taken from JavaNCSS
007     * http://www.kclee.com/clemens/java/javancss/
008     * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
009     *
010     * Cobertura is free software; you can redistribute it and/or modify
011     * it under the terms of the GNU General Public License as published
012     * by the Free Software Foundation; either version 2 of the License,
013     * or (at your option) any later version.
014     *
015     * Cobertura is distributed in the hope that it will be useful, but
016     * WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018     * General Public License for more details.
019     *
020     * You should have received a copy of the GNU General Public License
021     * along with Cobertura; if not, write to the Free Software
022     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023     * USA
024     */
025    
026    package net.sourceforge.cobertura.javancss;
027    
028    public class TokenMgrError extends Error
029    {
030       /*
031        * Ordinals for various reasons why an Error of this type can be thrown.
032        */
033    
034       /**
035        * Lexical error occured.
036        */
037       static final int LEXICAL_ERROR = 0;
038    
039       /**
040        * An attempt wass made to create a second instance of a static token manager.
041        */
042       static final int STATIC_LEXER_ERROR = 1;
043    
044       /**
045        * Tried to change to an invalid lexical state.
046        */
047       static final int INVALID_LEXICAL_STATE = 2;
048    
049       /**
050        * Detected (and bailed out of) an infinite loop in the token manager.
051        */
052       static final int LOOP_DETECTED = 3;
053    
054       /**
055        * Indicates the reason why the exception is thrown. It will have
056        * one of the above 4 values.
057        */
058       int errorCode;
059    
060       /**
061        * Replaces unprintable characters by their espaced (or unicode escaped)
062        * equivalents in the given string
063        */
064       protected static final String addEscapes(String str) {
065          StringBuffer retval = new StringBuffer();
066          char ch;
067          for (int i = 0; i < str.length(); i++) {
068            switch (str.charAt(i))
069            {
070               case 0 :
071                  continue;
072               case '\b':
073                  retval.append("\\b");
074                  continue;
075               case '\t':
076                  retval.append("\\t");
077                  continue;
078               case '\n':
079                  retval.append("\\n");
080                  continue;
081               case '\f':
082                  retval.append("\\f");
083                  continue;
084               case '\r':
085                  retval.append("\\r");
086                  continue;
087               case '\"':
088                  retval.append("\\\"");
089                  continue;
090               case '\'':
091                  retval.append("\\\'");
092                  continue;
093               case '\\':
094                  retval.append("\\\\");
095                  continue;
096               default:
097                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
098                     String s = "0000" + Integer.toString(ch, 16);
099                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
100                  } else {
101                     retval.append(ch);
102                  }
103                  continue;
104            }
105          }
106          return retval.toString();
107       }
108    
109       /**
110        * Returns a detailed message for the Error when it is thrown by the
111        * token manager to indicate a lexical error.
112        * Parameters : 
113        *    EOFSeen     : indicates if EOF caused the lexicl error
114        *    curLexState : lexical state in which this error occured
115        *    errorLine   : line number when the error occured
116        *    errorColumn : column number when the error occured
117        *    errorAfter  : prefix that was seen before this error occured
118        *    curchar     : the offending character
119        * Note: You can customize the lexical error message by modifying this method.
120        */
121       private static final String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
122          return("Lexical error at line " +
123               errorLine + ", column " +
124               errorColumn + ".  Encountered: " +
125               (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
126               "after : \"" + addEscapes(errorAfter) + "\"");
127       }
128    
129       /**
130        * You can also modify the body of this method to customize your error messages.
131        * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
132        * of end-users concern, so you can return something like : 
133        *
134        *     "Internal Error : Please file a bug report .... "
135        *
136        * from this method for such cases in the release version of your parser.
137        */
138       public String getMessage() {
139          return super.getMessage();
140       }
141    
142       /*
143        * Constructors of various flavors follow.
144        */
145    
146       public TokenMgrError() {
147       }
148    
149       public TokenMgrError(String message, int reason) {
150          super(message);
151          errorCode = reason;
152       }
153    
154       public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
155          this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
156       }
157    }