Debian Codegraph @

templates/ParseException.template

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