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