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