KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > annotation > factory > ast > TokenMgrError


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.annotation.factory.ast;
23
24 public class TokenMgrError extends Error JavaDoc
25 {
26    /** The serialVersionUID */
27    private static final long serialVersionUID = -8256510811060336766L;
28
29    /*
30     * Ordinals for various reasons why an Error of this type can be thrown.
31     */

32
33    /**
34     * Lexical error occured.
35     */

36    static final int LEXICAL_ERROR = 0;
37
38    /**
39     * An attempt wass made to create a second instance of a static token manager.
40     */

41    static final int STATIC_LEXER_ERROR = 1;
42
43    /**
44     * Tried to change to an invalid lexical state.
45     */

46    static final int INVALID_LEXICAL_STATE = 2;
47
48    /**
49     * Detected (and bailed out of) an infinite loop in the token manager.
50     */

51    static final int LOOP_DETECTED = 3;
52
53    /**
54     * Indicates the reason why the exception is thrown. It will have
55     * one of the above 4 values.
56     */

57    int errorCode;
58
59    /**
60     * Replaces unprintable characters by their espaced (or unicode escaped)
61     * equivalents in the given string
62     */

63    protected static final String JavaDoc addEscapes(String JavaDoc str) {
64       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
65       char ch;
66       for (int i = 0; i < str.length(); i++) {
67         switch (str.charAt(i))
68         {
69            case 0 :
70               continue;
71            case '\b':
72               retval.append("\\b");
73               continue;
74            case '\t':
75               retval.append("\\t");
76               continue;
77            case '\n':
78               retval.append("\\n");
79               continue;
80            case '\f':
81               retval.append("\\f");
82               continue;
83            case '\r':
84               retval.append("\\r");
85               continue;
86            case '\"':
87               retval.append("\\\"");
88               continue;
89            case '\'':
90               retval.append("\\\'");
91               continue;
92            case '\\':
93               retval.append("\\\\");
94               continue;
95            default:
96               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
97                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
98                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
99               } else {
100                  retval.append(ch);
101               }
102               continue;
103         }
104       }
105       return retval.toString();
106    }
107
108    /**
109     * Returns a detailed message for the Error when it is thrown by the
110     * token manager to indicate a lexical error.
111     * Parameters :
112     * EOFSeen : indicates if EOF caused the lexicl error
113     * curLexState : lexical state in which this error occured
114     * errorLine : line number when the error occured
115     * errorColumn : column number when the error occured
116     * errorAfter : prefix that was seen before this error occured
117     * curchar : the offending character
118     * Note: You can customize the lexical error message by modifying this method.
119     */

120    protected static String JavaDoc LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar) {
121       return("Lexical error at line " +
122            errorLine + ", column " +
123            errorColumn + ". Encountered: " +
124            (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
125            "after : \"" + addEscapes(errorAfter) + "\"");
126    }
127
128    /**
129     * You can also modify the body of this method to customize your error messages.
130     * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
131     * of end-users concern, so you can return something like :
132     *
133     * "Internal Error : Please file a bug report .... "
134     *
135     * from this method for such cases in the release version of your parser.
136     */

137    public String JavaDoc getMessage() {
138       return super.getMessage();
139    }
140
141    /*
142     * Constructors of various flavors follow.
143     */

144
145    public TokenMgrError() {
146    }
147
148    public TokenMgrError(String JavaDoc message, int reason) {
149       super(message);
150       errorCode = reason;
151    }
152
153    public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar, int reason) {
154       this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
155    }
156 }
157
Popular Tags