Debian Codegraph @

templates/TokenMgrError.template

raw
1 /** Token Manager Error. */
2 ${SUPPORT_CLASS_VISIBILITY_PUBLIC?public :}class TokenMgrError extends Error
3 {
4
5 /**
6 * The version identifier for this Serializable class.
7 * Increment only if the <i>serialized</i> form of the
8 * class changes.
9 */
10 private static final long serialVersionUID = 1L;
11
12 /*
13 * Ordinals for various reasons why an Error of this type can be thrown.
14 */
15
16 /**
17 * Lexical error occurred.
18 */
19 static final int LEXICAL_ERROR = 0;
20
21 /**
22 * An attempt was made to create a second instance of a static token manager.
23 */
24 static final int STATIC_LEXER_ERROR = 1;
25
26 /**
27 * Tried to change to an invalid lexical state.
28 */
29 static final int INVALID_LEXICAL_STATE = 2;
30
31 /**
32 * Detected (and bailed out of) an infinite loop in the token manager.
33 */
34 static final int LOOP_DETECTED = 3;
35
36 /**
37 * Indicates the reason why the exception is thrown. It will have
38 * one of the above 4 values.
39 */
40 int errorCode;
41
42 /**
43 * Replaces unprintable characters by their escaped (or unicode escaped)
44 * equivalents in the given string
45 */
46 protected static final String addEscapes(String str) {
47 StringBuffer retval = new StringBuffer();
48 char ch;
49 for (int i = 0; i < str.length(); i++) {
50 switch (str.charAt(i))
51 {
52 case 0 :
53 continue;
54 case '\b':
55 retval.append("\\b");
56 continue;
57 case '\t':
58 retval.append("\\t");
59 continue;
60 case '\n':
61 retval.append("\\n");
62 continue;
63 case '\f':
64 retval.append("\\f");
65 continue;
66 case '\r':
67 retval.append("\\r");
68 continue;
69 case '\"':
70 retval.append("\\\"");
71 continue;
72 case '\'':
73 retval.append("\\\'");
74 continue;
75 case '\\':
76 retval.append("\\\\");
77 continue;
78 default:
79 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
80 String s = "0000" + Integer.toString(ch, 16);
81 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
82 } else {
83 retval.append(ch);
84 }
85 continue;
86 }
87 }
88 return retval.toString();
89 }
90
91 /**
92 * Returns a detailed message for the Error when it is thrown by the
93 * token manager to indicate a lexical error.
94 * Parameters :
95 * EOFSeen : indicates if EOF caused the lexical error
96 * curLexState : lexical state in which this error occurred
97 * errorLine : line number when the error occurred
98 * errorColumn : column number when the error occurred
99 * errorAfter : prefix that was seen before this error occurred
100 * curchar : the offending character
101 * Note: You can customize the lexical error message by modifying this method.
102 */
103 protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
104 return("Lexical error at line " +
105 errorLine + ", column " +
106 errorColumn + ". Encountered: " +
107 (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
108 "after : \"" + addEscapes(errorAfter) + "\"");
109 }
110
111 /**
112 * You can also modify the body of this method to customize your error messages.
113 * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
114 * of end-users concern, so you can return something like :
115 *
116 * "Internal Error : Please file a bug report .... "
117 *
118 * from this method for such cases in the release version of your parser.
119 */
120 public String getMessage() {
121 return super.getMessage();
122 }
123
124 /*
125 * Constructors of various flavors follow.
126 */
127
128 /** No arg constructor. */
129 public TokenMgrError() {
130 }
131
132 /** Constructor with message and reason. */
133 public TokenMgrError(String message, int reason) {
134 super(message);
135 errorCode = reason;
136 }
137
138 /** Full Constructor. */
139 public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
140 this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
141 }
142 }