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(
95 boolean EOFSeen,
96 int lexState,
97 int errorLine,
98 int errorColumn,
99 String errorAfter,
100 char curChar) {
101 return ("Lexical error at line "
102 + errorLine
103 + ", column "
104 + errorColumn
105 + ". Encountered: "
106 + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int) curChar + "), ")
107 + "after : \""
108 + addEscapes(errorAfter) + "\"");
109 }
110
111 /***
112 * You can also modify the body of this method to customize your error messages. For example, cases like
113 * LOOP_DETECTED and INVALID_LEXICAL_STATE are not of end-users concern, so you can return something like :
114 * "Internal Error : Please file a bug report .... " from this method for such cases in the release version of your
115 * parser.
116 */
117 public String getMessage() {
118 return super.getMessage();
119 }
120
121
122
123
124
125 public TokenMgrError() {
126 }
127
128 public TokenMgrError(String message, int reason) {
129 super(message);
130 errorCode = reason;
131 }
132
133 public TokenMgrError(boolean EOFSeen,
134 int lexState,
135 int errorLine,
136 int errorColumn,
137 String errorAfter,
138 char curChar,
139 int reason) {
140 this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
141 }
142 }