blob: ab64900ca50ff642e53f4585fbfb816f9890b751 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
27package com.sun.tools.example.debug.expr;
28
29/**
30 * This exception is thrown when parse errors are encountered.
31 * You can explicitly create objects of this exception type by
32 * calling the method generateParseException in the generated
33 * parser.
34 *
35 * You can modify this class to customize your error reporting
36 * mechanisms so long as you retain the public fields.
37 */
38public class ParseException extends Exception {
39
40 /**
41 * This constructor is used by the method "generateParseException"
42 * in the generated parser. Calling this constructor generates
43 * a new object of this type with the fields "currentToken",
44 * "expectedTokenSequences", and "tokenImage" set. The boolean
45 * flag "specialConstructor" is also set to true to indicate that
46 * this constructor was used to create this object.
47 * This constructor calls its super class with the empty string
48 * to force the "toString" method of parent class "Throwable" to
49 * print the error message in the form:
50 * ParseException: <result of getMessage>
51 */
52 public ParseException(Token currentTokenVal,
53 int[][] expectedTokenSequencesVal,
54 String[] tokenImageVal
55 )
56 {
57 super("");
58 specialConstructor = true;
59 currentToken = currentTokenVal;
60 expectedTokenSequences = expectedTokenSequencesVal;
61 tokenImage = tokenImageVal;
62 }
63
64 /**
65 * The following constructors are for use by you for whatever
66 * purpose you can think of. Constructing the exception in this
67 * manner makes the exception behave in the normal way - i.e., as
68 * documented in the class "Throwable". The fields "errorToken",
69 * "expectedTokenSequences", and "tokenImage" do not contain
70 * relevant information. The JavaCC generated code does not use
71 * these constructors.
72 */
73
74 public ParseException() {
75 super();
76 specialConstructor = false;
77 }
78
79 public ParseException(String message) {
80 super(message);
81 specialConstructor = false;
82 }
83
84 /**
85 * This variable determines which constructor was used to create
86 * this object and thereby affects the semantics of the
87 * "getMessage" method (see below).
88 */
89 protected boolean specialConstructor;
90
91 /**
92 * This is the last token that has been consumed successfully. If
93 * this object has been created due to a parse error, the token
94 * followng this token will (therefore) be the first error token.
95 */
96 public Token currentToken;
97
98 /**
99 * Each entry in this array is an array of integers. Each array
100 * of integers represents a sequence of tokens (by their ordinal
101 * values) that is expected at this point of the parse.
102 */
103 public int[][] expectedTokenSequences;
104
105 /**
106 * This is a reference to the "tokenImage" array of the generated
107 * parser within which the parse error occurred. This array is
108 * defined in the generated ...Constants interface.
109 */
110 public String[] tokenImage;
111
112 /**
113 * This method has the standard behavior when this object has been
114 * created using the standard constructors. Otherwise, it uses
115 * "currentToken" and "expectedTokenSequences" to generate a parse
116 * error message and returns it. If this object has been created
117 * due to a parse error, and you do not catch it (it gets thrown
118 * from the parser), then this method is called during the printing
119 * of the final stack trace, and hence the correct error message
120 * gets displayed.
121 */
122 public String getMessage() {
123 if (!specialConstructor) {
124 return super.getMessage();
125 }
126 String expected = "";
127 int maxSize = 0;
128 for (int i = 0; i < expectedTokenSequences.length; i++) {
129 if (maxSize < expectedTokenSequences[i].length) {
130 maxSize = expectedTokenSequences[i].length;
131 }
132 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
133 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
134 }
135 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
136 expected += "...";
137 }
138 expected += eol + " ";
139 }
140 String retval = "Encountered \"";
141 Token tok = currentToken.next;
142 for (int i = 0; i < maxSize; i++) {
143 if (i != 0) retval += " ";
144 if (tok.kind == 0) {
145 retval += tokenImage[0];
146 break;
147 }
148 retval += add_escapes(tok.image);
149 tok = tok.next;
150 }
151 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
152 if (expectedTokenSequences.length == 1) {
153 retval += "Was expecting:" + eol + " ";
154 } else {
155 retval += "Was expecting one of:" + eol + " ";
156 }
157 retval += expected;
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 protected 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}