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 /**
11 * This exception is thrown when parse errors are encountered.
12 * You can explicitly create objects of this exception type by
13 * calling the method generateParseException in the generated
14 * parser.
15 *
16 * You can modify this class to customize your error reporting
17 * mechanisms so long as you retain the public fields.
18 */
19 public class ParseException extends net.sourceforge.pmd.ast.ParseException {
20
21 /**
22 * This constructor is used by the method "generateParseException"
23 * in the generated parser. Calling this constructor generates
24 * a new object of this type with the fields "currentToken",
25 * "expectedTokenSequences", and "tokenImage" set. The boolean
26 * flag "specialConstructor" is also set to true to indicate that
27 * this constructor was used to create this object.
28 * This constructor calls its super class with the empty string
29 * to force the "toString" method of parent class "Throwable" to
30 * print the error message in the form:
31 * ParseException: <result of getMessage>
32 */
33 public ParseException(Token currentTokenVal,
34 int[][] expectedTokenSequencesVal,
35 String[] tokenImageVal
36 )
37 {
38 super("");
39 specialConstructor = true;
40 currentToken = currentTokenVal;
41 expectedTokenSequences = expectedTokenSequencesVal;
42 tokenImage = tokenImageVal;
43 }
44
45 /**
46 * The following constructors are for use by you for whatever
47 * purpose you can think of. Constructing the exception in this
48 * manner makes the exception behave in the normal way - i.e., as
49 * documented in the class "Throwable". The fields "errorToken",
50 * "expectedTokenSequences", and "tokenImage" do not contain
51 * relevant information. The JavaCC generated code does not use
52 * these constructors.
53 */
54
55 public ParseException() {
56 super();
57 specialConstructor = false;
58 }
59
60 /** Constructor with message. */
61 public ParseException(String message) {
62 super(message);
63 specialConstructor = false;
64 }
65
66 /**
67 * This variable determines which constructor was used to create
68 * this object and thereby affects the semantics of the
69 * "getMessage" method (see below).
70 */
71 protected boolean specialConstructor;
72
73 /**
74 * This is the last token that has been consumed successfully. If
75 * this object has been created due to a parse error, the token
76 * followng this token will (therefore) be the first error token.
77 */
78 public Token currentToken;
79
80 /**
81 * Each entry in this array is an array of integers. Each array
82 * of integers represents a sequence of tokens (by their ordinal
83 * values) that is expected at this point of the parse.
84 */
85 public int[][] expectedTokenSequences;
86
87 /**
88 * This is a reference to the "tokenImage" array of the generated
89 * parser within which the parse error occurred. This array is
90 * defined in the generated ...Constants interface.
91 */
92 public String[] tokenImage;
93
94 /**
95 * This method has the standard behavior when this object has been
96 * created using the standard constructors. Otherwise, it uses
97 * "currentToken" and "expectedTokenSequences" to generate a parse
98 * error message and returns it. If this object has been created
99 * due to a parse error, and you do not catch it (it gets thrown
100 * from the parser), then this method is called during the printing
101 * of the final stack trace, and hence the correct error message
102 * gets displayed.
103 */
104 public String getMessage() {
105 if (!specialConstructor) {
106 return super.getMessage();
107 }
108 StringBuffer expected = new StringBuffer();
109 int maxSize = 0;
110 for (int i = 0; i < expectedTokenSequences.length; i++) {
111 if (maxSize < expectedTokenSequences[i].length) {
112 maxSize = expectedTokenSequences[i].length;
113 }
114 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
115 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
116 }
117 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
118 expected.append("...");
119 }
120 expected.append(eol).append(" ");
121 }
122 String retval = "Encountered \"";
123 Token tok = currentToken.next;
124 for (int i = 0; i < maxSize; i++) {
125 if (i != 0) retval += " ";
126 if (tok.kind == 0) {
127 retval += tokenImage[0];
128 break;
129 }
130 retval += " " + tokenImage[tok.kind];
131 retval += " \"";
132 retval += add_escapes(tok.image);
133 retval += " \"";
134 tok = tok.next;
135 }
136 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
137 retval += "." + eol;
138 if (expectedTokenSequences.length == 1) {
139 retval += "Was expecting:" + eol + " ";
140 } else {
141 retval += "Was expecting one of:" + eol + " ";
142 }
143 retval += expected.toString();
144 return retval;
145 }
146
147 /**
148 * The end of line string for this machine.
149 */
150 protected String eol = System.getProperty("line.separator", "\n");
151
152 /**
153 * Used to convert raw characters to their escaped version
154 * when these raw version cannot be used as part of an ASCII
155 * string literal.
156 */
157 protected String add_escapes(String str) {
158 StringBuffer retval = new StringBuffer();
159 char ch;
160 for (int i = 0; i < str.length(); i++) {
161 switch (str.charAt(i))
162 {
163 case 0 :
164 continue;
165 case '\b':
166 retval.append("\\b");
167 continue;
168 case '\t':
169 retval.append("\\t");
170 continue;
171 case '\n':
172 retval.append("\\n");
173 continue;
174 case '\f':
175 retval.append("\\f");
176 continue;
177 case '\r':
178 retval.append("\\r");
179 continue;
180 case '\"':
181 retval.append("\\\"");
182 continue;
183 case '\'':
184 retval.append("\\\'");
185 continue;
186 case '\\':
187 retval.append("\\\\");
188 continue;
189 default:
190 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
191 String s = "0000" + Integer.toString(ch, 16);
192 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
193 } else {
194 retval.append(ch);
195 }
196 continue;
197 }
198 }
199 return retval.toString();
200 }
201
202 }
203