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