1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
2 /* JavaCCOptions:KEEP_LINE_COL=null */
3 /*
4 * Copyright (C) Christian Schulte, 2005-206
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $JOMC: VersionParser.jj 4613 2012-09-22 10:07:08Z schulte $
31 *
32 */
33 package org.jomc.util;
34
35 /**
36 * This exception is thrown when parse errors are encountered.
37 * You can explicitly create objects of this exception type by
38 * calling the method generateParseException in the generated
39 * parser.
40 *
41 * You can modify this class to customize your error reporting
42 * mechanisms so long as you retain the public fields.
43 */
44 public class ParseException extends Exception {
45
46 /**
47 * The version identifier for this Serializable class.
48 * Increment only if the <i>serialized</i> form of the
49 * class changes.
50 */
51 private static final long serialVersionUID = 1L;
52
53 /**
54 * This constructor is used by the method "generateParseException"
55 * in the generated parser. Calling this constructor generates
56 * a new object of this type with the fields "currentToken",
57 * "expectedTokenSequences", and "tokenImage" set.
58 */
59 public ParseException(Token currentTokenVal,
60 int[][] expectedTokenSequencesVal,
61 String[] tokenImageVal
62 )
63 {
64 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
65 currentToken = currentTokenVal;
66 expectedTokenSequences = expectedTokenSequencesVal;
67 tokenImage = tokenImageVal;
68 }
69
70 /**
71 * The following constructors are for use by you for whatever
72 * purpose you can think of. Constructing the exception in this
73 * manner makes the exception behave in the normal way - i.e., as
74 * documented in the class "Throwable". The fields "errorToken",
75 * "expectedTokenSequences", and "tokenImage" do not contain
76 * relevant information. The JavaCC generated code does not use
77 * these constructors.
78 */
79
80 public ParseException() {
81 super();
82 }
83
84 /** Constructor with message. */
85 public ParseException(String message) {
86 super(message);
87 }
88
89
90 /**
91 * This is the last token that has been consumed successfully. If
92 * this object has been created due to a parse error, the token
93 * followng this token will (therefore) be the first error token.
94 */
95 public Token currentToken;
96
97 /**
98 * Each entry in this array is an array of integers. Each array
99 * of integers represents a sequence of tokens (by their ordinal
100 * values) that is expected at this point of the parse.
101 */
102 public int[][] expectedTokenSequences;
103
104 /**
105 * This is a reference to the "tokenImage" array of the generated
106 * parser within which the parse error occurred. This array is
107 * defined in the generated ...Constants interface.
108 */
109 public String[] tokenImage;
110
111 /**
112 * It uses "currentToken" and "expectedTokenSequences" to generate a parse
113 * error message and returns it. If this object has been created
114 * due to a parse error, and you do not catch it (it gets thrown
115 * from the parser) the correct error message
116 * gets displayed.
117 */
118 private static String initialise(Token currentToken,
119 int[][] expectedTokenSequences,
120 String[] tokenImage) {
121 String eol = System.getProperty("line.separator", "\n");
122 StringBuffer expected = new StringBuffer();
123 int maxSize = 0;
124 for (int i = 0; i < expectedTokenSequences.length; i++) {
125 if (maxSize < expectedTokenSequences[i].length) {
126 maxSize = expectedTokenSequences[i].length;
127 }
128 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
129 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
130 }
131 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
132 expected.append("...");
133 }
134 expected.append(eol).append(" ");
135 }
136 String retval = "Encountered \"";
137 Token tok = currentToken.next;
138 for (int i = 0; i < maxSize; i++) {
139 if (i != 0) retval += " ";
140 if (tok.kind == 0) {
141 retval += tokenImage[0];
142 break;
143 }
144 retval += " " + tokenImage[tok.kind];
145 retval += " \"";
146 retval += add_escapes(tok.image);
147 retval += " \"";
148 tok = tok.next;
149 }
150 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
151 retval += "." + eol;
152 if (expectedTokenSequences.length == 1) {
153 retval += "Was expecting:" + eol + " ";
154 } else {
155 retval += "Was expecting one of:" + eol + " ";
156 }
157 retval += expected.toString();
158 return retval;
159 }
160
161 /**
162 * The end of line string for this machine.
163 */
164 protected String eol = System.getProperty("line.separator", "\n");
165
166 /**
167 * Used to convert raw characters to their escaped version
168 * when these raw version cannot be used as part of an ASCII
169 * string literal.
170 */
171 static String add_escapes(String str) {
172 StringBuffer retval = new StringBuffer();
173 char ch;
174 for (int i = 0; i < str.length(); i++) {
175 switch (str.charAt(i))
176 {
177 case 0 :
178 continue;
179 case '\b':
180 retval.append("\\b");
181 continue;
182 case '\t':
183 retval.append("\\t");
184 continue;
185 case '\n':
186 retval.append("\\n");
187 continue;
188 case '\f':
189 retval.append("\\f");
190 continue;
191 case '\r':
192 retval.append("\\r");
193 continue;
194 case '\"':
195 retval.append("\\\"");
196 continue;
197 case '\'':
198 retval.append("\\\'");
199 continue;
200 case '\\':
201 retval.append("\\\\");
202 continue;
203 default:
204 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
205 String s = "0000" + Integer.toString(ch, 16);
206 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
207 } else {
208 retval.append(ch);
209 }
210 continue;
211 }
212 }
213 return retval.toString();
214 }
215
216 }
217 /* JavaCC - OriginalChecksum=1e5c577c850bd2a3165b78397dc6b0c4 (do not edit this line) */