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