source: josm/trunk/tools/javacc/templates/ParseException.template@ 16907

Last change on this file since 16907 was 16907, checked in by simon04, 4 years ago

see #19685, see #11593 - Add JavaCC ParseException.template

Source: https://github.com/javacc/javacc/blob/7.0.3/src/main/resources/templates/ParseException.template

File size: 5.8 KB
Line 
1/**
2 * This exception is thrown when parse errors are encountered.
3 * You can explicitly create objects of this exception type by
4 * calling the method generateParseException in the generated
5 * parser.
6 *
7 * You can modify this class to customize your error reporting
8 * mechanisms so long as you retain the public fields.
9 */
10public class ParseException extends Exception {
11
12 /**
13 * The version identifier for this Serializable class.
14 * Increment only if the <i>serialized</i> form of the
15 * class changes.
16 */
17 private static final long serialVersionUID = 1L;
18
19 /**
20 * The end of line string for this machine.
21 */
22 protected static String EOL = System.getProperty("line.separator", "\n");
23
24 /**
25 * This constructor is used by the method "generateParseException"
26 * in the generated parser. Calling this constructor generates
27 * a new object of this type with the fields "currentToken",
28 * "expectedTokenSequences", and "tokenImage" set.
29 */
30 public ParseException(Token currentTokenVal,
31 int[][] expectedTokenSequencesVal,
32 String[] tokenImageVal
33 )
34 {
35 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
36 currentToken = currentTokenVal;
37 expectedTokenSequences = expectedTokenSequencesVal;
38 tokenImage = tokenImageVal;
39 }
40
41 /**
42 * The following constructors are for use by you for whatever
43 * purpose you can think of. Constructing the exception in this
44 * manner makes the exception behave in the normal way - i.e., as
45 * documented in the class "Throwable". The fields "errorToken",
46 * "expectedTokenSequences", and "tokenImage" do not contain
47 * relevant information. The JavaCC generated code does not use
48 * these constructors.
49 */
50
51 public ParseException() {
52 super();
53 }
54
55 /** Constructor with message. */
56 public ParseException(String message) {
57 super(message);
58 }
59
60
61 /**
62 * This is the last token that has been consumed successfully. If
63 * this object has been created due to a parse error, the token
64 * followng this token will (therefore) be the first error token.
65 */
66 public Token currentToken;
67
68 /**
69 * Each entry in this array is an array of integers. Each array
70 * of integers represents a sequence of tokens (by their ordinal
71 * values) that is expected at this point of the parse.
72 */
73 public int[][] expectedTokenSequences;
74
75 /**
76 * This is a reference to the "tokenImage" array of the generated
77 * parser within which the parse error occurred. This array is
78 * defined in the generated ...Constants interface.
79 */
80 public String[] tokenImage;
81
82 /**
83 * It uses "currentToken" and "expectedTokenSequences" to generate a parse
84 * error message and returns it. If this object has been created
85 * due to a parse error, and you do not catch it (it gets thrown
86 * from the parser) the correct error message
87 * gets displayed.
88 */
89 private static String initialise(Token currentToken,
90 int[][] expectedTokenSequences,
91 String[] tokenImage) {
92
93 StringBuffer expected = new StringBuffer();
94 int maxSize = 0;
95 for (int i = 0; i < expectedTokenSequences.length; i++) {
96 if (maxSize < expectedTokenSequences[i].length) {
97 maxSize = expectedTokenSequences[i].length;
98 }
99 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
100 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
101 }
102 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
103 expected.append("...");
104 }
105 expected.append(EOL).append(" ");
106 }
107 String retval = "Encountered \"";
108 Token tok = currentToken.next;
109 for (int i = 0; i < maxSize; i++) {
110 if (i != 0) retval += " ";
111 if (tok.kind == 0) {
112 retval += tokenImage[0];
113 break;
114 }
115 retval += " " + tokenImage[tok.kind];
116 retval += " \"";
117 retval += add_escapes(tok.image);
118 retval += " \"";
119 tok = tok.next;
120 }
121#if KEEP_LINE_COLUMN
122 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
123#fi
124 retval += "." + EOL;
125
126
127 if (expectedTokenSequences.length == 0) {
128 // Nothing to add here
129 } else {
130 if (expectedTokenSequences.length == 1) {
131 retval += "Was expecting:" + EOL + " ";
132 } else {
133 retval += "Was expecting one of:" + EOL + " ";
134 }
135 retval += expected.toString();
136 }
137
138 return retval;
139 }
140
141
142 /**
143 * Used to convert raw characters to their escaped version
144 * when these raw version cannot be used as part of an ASCII
145 * string literal.
146 */
147 static String add_escapes(String str) {
148 StringBuffer retval = new StringBuffer();
149 char ch;
150 for (int i = 0; i < str.length(); i++) {
151 switch (str.charAt(i))
152 {
153 case '\b':
154 retval.append("\\b");
155 continue;
156 case '\t':
157 retval.append("\\t");
158 continue;
159 case '\n':
160 retval.append("\\n");
161 continue;
162 case '\f':
163 retval.append("\\f");
164 continue;
165 case '\r':
166 retval.append("\\r");
167 continue;
168 case '\"':
169 retval.append("\\\"");
170 continue;
171 case '\'':
172 retval.append("\\\'");
173 continue;
174 case '\\':
175 retval.append("\\\\");
176 continue;
177 default:
178 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
179 String s = "0000" + Integer.toString(ch, 16);
180 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
181 } else {
182 retval.append(ch);
183 }
184 continue;
185 }
186 }
187 return retval.toString();
188 }
189
190}
Note: See TracBrowser for help on using the repository browser.