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