Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ParseException |
|
| 4.666666666666667;4.667 |
1 | /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */ | |
2 | package org.webmacro.parser; | |
3 | ||
4 | /** | |
5 | * This exception is thrown when parse errors are encountered. | |
6 | * You can explicitly create objects of this exception type by | |
7 | * calling the method generateParseException in the generated | |
8 | * parser. | |
9 | * | |
10 | * You can modify this class to customize your error reporting | |
11 | * mechanisms so long as you retain the public fields. | |
12 | * | |
13 | * NOTE: modified to extend org.webmacro.RethrowableException and | |
14 | * added constructer for nested exceptions. | |
15 | */ | |
16 | public class ParseException extends org.webmacro.RethrowableException | |
17 | { | |
18 | ||
19 | private static final long serialVersionUID = 1L; | |
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 | 2 | super(""); |
39 | 2 | specialConstructor = true; |
40 | 2 | currentToken = currentTokenVal; |
41 | 2 | expectedTokenSequences = expectedTokenSequencesVal; |
42 | 2 | tokenImage = tokenImageVal; |
43 | 2 | } |
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 | { | |
57 | 0 | super(); |
58 | 0 | specialConstructor = false; |
59 | 0 | } |
60 | ||
61 | public ParseException (String message) | |
62 | { | |
63 | 6 | super(message); |
64 | 6 | specialConstructor = false; |
65 | 6 | } |
66 | ||
67 | public ParseException (String message, Exception e) | |
68 | { | |
69 | 0 | super(message, e); |
70 | 0 | specialConstructor = false; |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * This variable determines which constructor was used to create | |
75 | * this object and thereby affects the semantics of the | |
76 | * "getMessage" method (see below). | |
77 | */ | |
78 | protected boolean specialConstructor; | |
79 | ||
80 | /** | |
81 | * This is the last token that has been consumed successfully. If | |
82 | * this object has been created due to a parse error, the token | |
83 | * followng this token will (therefore) be the first error token. | |
84 | */ | |
85 | public Token currentToken; | |
86 | ||
87 | /** | |
88 | * Each entry in this array is an array of integers. Each array | |
89 | * of integers represents a sequence of tokens (by their ordinal | |
90 | * values) that is expected at this point of the parse. | |
91 | */ | |
92 | public int[][] expectedTokenSequences; | |
93 | ||
94 | /** | |
95 | * This is a reference to the "tokenImage" array of the generated | |
96 | * parser within which the parse error occurred. This array is | |
97 | * defined in the generated ...Constants interface. | |
98 | */ | |
99 | public String[] tokenImage; | |
100 | ||
101 | /** | |
102 | * This method has the standard behavior when this object has been | |
103 | * created using the standard constructors. Otherwise, it uses | |
104 | * "currentToken" and "expectedTokenSequences" to generate a parse | |
105 | * error message and returns it. If this object has been created | |
106 | * due to a parse error, and you do not catch it (it gets thrown | |
107 | * from the parser), then this method is called during the printing | |
108 | * of the final stack trace, and hence the correct error message | |
109 | * gets displayed. | |
110 | */ | |
111 | public String getMessage () | |
112 | { | |
113 | 72 | if (!specialConstructor) |
114 | { | |
115 | 54 | return super.getMessage(); |
116 | } | |
117 | 18 | String expected = ""; |
118 | 18 | int maxSize = 0; |
119 | 117 | for (int i = 0; i < expectedTokenSequences.length; i++) |
120 | { | |
121 | 99 | if (maxSize < expectedTokenSequences[i].length) |
122 | { | |
123 | 18 | maxSize = expectedTokenSequences[i].length; |
124 | } | |
125 | 198 | for (int j = 0; j < expectedTokenSequences[i].length; j++) |
126 | { | |
127 | 99 | expected += tokenImage[expectedTokenSequences[i][j]] + " "; |
128 | } | |
129 | 99 | if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) |
130 | { | |
131 | 99 | expected += "..."; |
132 | } | |
133 | 99 | expected += eol + " "; |
134 | } | |
135 | 18 | String retval = "Encountered \""; |
136 | 18 | Token tok = currentToken.next; |
137 | 18 | for (int i = 0; i < maxSize; i++) |
138 | { | |
139 | 18 | if (i != 0) retval += " "; |
140 | 18 | if (tok.kind == 0) |
141 | { | |
142 | 18 | retval += tokenImage[0]; |
143 | 18 | break; |
144 | } | |
145 | 0 | retval += add_escapes(tok.image); |
146 | 0 | tok = tok.next; |
147 | } | |
148 | 18 | retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; |
149 | 18 | retval += "." + eol; |
150 | 18 | if (expectedTokenSequences.length == 1) |
151 | { | |
152 | 0 | retval += "Was expecting:" + eol + " "; |
153 | } | |
154 | else | |
155 | { | |
156 | 18 | retval += "Was expecting one of:" + eol + " "; |
157 | } | |
158 | 18 | retval += expected; |
159 | 18 | return retval; |
160 | } | |
161 | ||
162 | /** | |
163 | * The end of line string for this machine. | |
164 | */ | |
165 | 8 | protected String eol = System.getProperty("line.separator", "\n"); |
166 | ||
167 | /** | |
168 | * Used to convert raw characters to their escaped version | |
169 | * when these raw version cannot be used as part of an ASCII | |
170 | * string literal. | |
171 | */ | |
172 | protected String add_escapes (String str) | |
173 | { | |
174 | 0 | StringBuffer retval = new StringBuffer(); |
175 | char ch; | |
176 | 0 | for (int i = 0; i < str.length(); i++) |
177 | { | |
178 | 0 | switch (str.charAt(i)) |
179 | { | |
180 | case 0: | |
181 | 0 | continue; |
182 | case '\b': | |
183 | 0 | retval.append("\\b"); |
184 | 0 | continue; |
185 | case '\t': | |
186 | 0 | retval.append("\\t"); |
187 | 0 | continue; |
188 | case '\n': | |
189 | 0 | retval.append("\\n"); |
190 | 0 | continue; |
191 | case '\f': | |
192 | 0 | retval.append("\\f"); |
193 | 0 | continue; |
194 | case '\r': | |
195 | 0 | retval.append("\\r"); |
196 | 0 | continue; |
197 | case '\"': | |
198 | 0 | retval.append("\\\""); |
199 | 0 | continue; |
200 | case '\'': | |
201 | 0 | retval.append("\\\'"); |
202 | 0 | continue; |
203 | case '\\': | |
204 | 0 | retval.append("\\\\"); |
205 | 0 | continue; |
206 | default: | |
207 | 0 | if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) |
208 | { | |
209 | 0 | String s = "0000" + Integer.toString(ch, 16); |
210 | 0 | retval.append("\\u" + s.substring(s.length() - 4, s.length())); |
211 | 0 | } |
212 | else | |
213 | { | |
214 | 0 | retval.append(ch); |
215 | } | |
216 | continue; | |
217 | } | |
218 | } | |
219 | 0 | return retval.toString(); |
220 | } | |
221 | ||
222 | } |