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