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