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