KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > util > arl > TokenMgrError


1 /* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 2.1 */
2 package org.ejen.util.arl;
3
4 public class TokenMgrError extends Error JavaDoc {
5
6     /*
7      * Ordinals for various reasons why an Error of this type can be thrown.
8      */

9     
10     /**
11      * Lexical error occured.
12      */

13     static final int LEXICAL_ERROR = 0;
14
15     /**
16      * An attempt wass made to create a second instance of a static token manager.
17      */

18     static final int STATIC_LEXER_ERROR = 1;
19
20     /**
21      * Tried to change to an invalid lexical state.
22      */

23     static final int INVALID_LEXICAL_STATE = 2;
24
25     /**
26      * Detected (and bailed out of) an infinite loop in the token manager.
27      */

28     static final int LOOP_DETECTED = 3;
29
30     /**
31      * Indicates the reason why the exception is thrown. It will have
32      * one of the above 4 values.
33      */

34     int errorCode;
35
36     /**
37      * Replaces unprintable characters by their espaced (or unicode escaped)
38      * equivalents in the given string
39      */

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

108     private static final String JavaDoc LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar) {
109         return("Lexical error at line " + errorLine + ", column " + errorColumn
110                 + ". Encountered: "
111                 + (EOFSeen
112                         ? "<EOF> "
113                         : ("\"" + addEscapes(String.valueOf(curChar)) + "\"")
114                                 + " (" + (int) curChar + "), ") + "after : \""
115                 + addEscapes(errorAfter) + "\"");
116     }
117
118     /**
119      * You can also modify the body of this method to customize your error messages.
120      * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
121      * of end-users concern, so you can return something like :
122      *
123      * "Internal Error : Please file a bug report .... "
124      *
125      * from this method for such cases in the release version of your parser.
126      */

127     public String JavaDoc getMessage() {
128         return super.getMessage();
129     }
130
131     /*
132      * Constructors of various flavors follow.
133      */

134     public TokenMgrError() {}
135
136     public TokenMgrError(String JavaDoc message, int reason) {
137         super(message);
138         errorCode = reason;
139     }
140
141     public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String JavaDoc errorAfter, char curChar, int reason) {
142         this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter,
143                 curChar),
144                 reason);
145     }
146 }
147
Popular Tags