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