KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 /*
3     This file was auto generated, but has been modified since then. If we
4     need to regenerate it for some reason we should be careful to look at
5     the notes below.
6     
7     All BeanShell modifications are demarcated by "Begin BeanShell
8     Modification - ... " and "End BeanShell Modification - ..."
9
10     Note: Please leave the ^M carriage return in the above auto-generated line
11     or JavaCC will complain about version on Win systems.
12
13     BeanShell Modification to generated file
14     ----------------------------------------
15
16     - Added sourceFile attribute
17         setErrorSourceFile()
18         getErrorSourceFile()
19     - Modified getMessage() to print more tersely except on debug
20       (removed "Was expecting one of...)
21     - Added sourceFile info to getMessage()
22     - Made ParseException extend EvalError
23     - Modified constructors to use EvalError
24     - Override
25         getErrorLineNumber()
26         getErrorText()
27     - Add
28         toString()
29
30 */

31
32 package bsh;
33
34 /**
35  * This exception is thrown when parse errors are encountered.
36  * You can explicitly create objects of this exception type by
37  * calling the method generateParseException in the generated
38  * parser.
39  *
40  * You can modify this class to customize your error reporting
41  * mechanisms so long as you retain the public fields.
42  */

43  
44 // Begin BeanShell Modification - public, extend EvalError
45
public class ParseException extends EvalError {
46 // End BeanShell Modification - public, extend EvalError
47

48     // Begin BeanShell Modification - sourceFile
49

50     String JavaDoc sourceFile = "<unknown>";
51
52     /**
53         Used to add source file info to exception
54     */

55     public void setErrorSourceFile( String JavaDoc file ) {
56         this.sourceFile = file;
57     }
58
59     public String JavaDoc getErrorSourceFile() {
60         return sourceFile;
61     }
62
63     // End BeanShell Modification - sourceFile
64

65   /**
66    * This constructor is used by the method "generateParseException"
67    * in the generated parser. Calling this constructor generates
68    * a new object of this type with the fields "currentToken",
69    * "expectedTokenSequences", and "tokenImage" set. The boolean
70    * flag "specialConstructor" is also set to true to indicate that
71    * this constructor was used to create this object.
72    * This constructor calls its super class with the empty string
73    * to force the "toString" method of parent class "Throwable" to
74    * print the error message in the form:
75    * ParseException: <result of getMessage>
76    */

77   public ParseException(Token currentTokenVal,
78                         int[][] expectedTokenSequencesVal,
79                         String JavaDoc[] tokenImageVal
80                        )
81   {
82     // Begin BeanShell Modification - constructor
83
this();
84     // End BeanShell Modification - constructor
85
specialConstructor = true;
86     currentToken = currentTokenVal;
87     expectedTokenSequences = expectedTokenSequencesVal;
88     tokenImage = tokenImageVal;
89   }
90
91   /**
92    * The following constructors are for use by you for whatever
93    * purpose you can think of. Constructing the exception in this
94    * manner makes the exception behave in the normal way - i.e., as
95    * documented in the class "Throwable". The fields "errorToken",
96    * "expectedTokenSequences", and "tokenImage" do not contain
97    * relevant information. The JavaCC generated code does not use
98    * these constructors.
99    */

100
101   public ParseException() {
102     // Begin BeanShell Modification - constructor
103
this("");
104     // End BeanShell Modification - constructor
105
specialConstructor = false;
106   }
107
108   public ParseException(String JavaDoc message) {
109     // Begin BeanShell Modification - super constructor args
110
// null node, null callstack, ParseException knows where the error is.
111
super( message, null, null );
112     // End BeanShell Modification - super constructor args
113
specialConstructor = false;
114   }
115
116   /**
117    * This variable determines which constructor was used to create
118    * this object and thereby affects the semantics of the
119    * "getMessage" method (see below).
120    */

121   protected boolean specialConstructor;
122
123   /**
124    * This is the last token that has been consumed successfully. If
125    * this object has been created due to a parse error, the token
126    * followng this token will (therefore) be the first error token.
127    */

128   public Token currentToken;
129
130   /**
131    * Each entry in this array is an array of integers. Each array
132    * of integers represents a sequence of tokens (by their ordinal
133    * values) that is expected at this point of the parse.
134    */

135   public int[][] expectedTokenSequences;
136
137   /**
138    * This is a reference to the "tokenImage" array of the generated
139    * parser within which the parse error occurred. This array is
140    * defined in the generated ...Constants interface.
141    */

142   public String JavaDoc[] tokenImage;
143
144   // Begin BeanShell Modification - moved body to overloaded getMessage()
145
public String JavaDoc getMessage() {
146     return getMessage( false );
147   }
148   // End BeanShell Modification - moved body to overloaded getMessage()
149

150   /**
151    * This method has the standard behavior when this object has been
152    * created using the standard constructors. Otherwise, it uses
153    * "currentToken" and "expectedTokenSequences" to generate a parse
154    * error message and returns it. If this object has been created
155    * due to a parse error, and you do not catch it (it gets thrown
156    * from the parser), then this method is called during the printing
157    * of the final stack trace, and hence the correct error message
158    * gets displayed.
159    */

160   // Begin BeanShell Modification - added debug param
161
public String JavaDoc getMessage( boolean debug ) {
162   // End BeanShell Modification - added debug param
163
if (!specialConstructor) {
164       return super.getMessage();
165     }
166     String JavaDoc expected = "";
167     int maxSize = 0;
168     for (int i = 0; i < expectedTokenSequences.length; i++) {
169       if (maxSize < expectedTokenSequences[i].length) {
170         maxSize = expectedTokenSequences[i].length;
171       }
172       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
173         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
174       }
175       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
176         expected += "...";
177       }
178       expected += eol + " ";
179     }
180     // Begin BeanShell Modification - added sourceFile info
181
String JavaDoc retval = "In file: "+ sourceFile +" Encountered \"";
182     // End BeanShell Modification - added sourceFile info
183
Token tok = currentToken.next;
184     for (int i = 0; i < maxSize; i++) {
185       if (i != 0) retval += " ";
186       if (tok.kind == 0) {
187         retval += tokenImage[0];
188         break;
189       }
190       retval += add_escapes(tok.image);
191       tok = tok.next;
192     }
193     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
194
195     // Begin BeanShell Modification - made conditional on debug
196
if ( debug )
197     {
198         if (expectedTokenSequences.length == 1) {
199           retval += "Was expecting:" + eol + " ";
200         } else {
201           retval += "Was expecting one of:" + eol + " ";
202         }
203
204         retval += expected;
205     }
206     // End BeanShell Modification - made conditional on debug
207

208     return retval;
209   }
210
211   /**
212    * The end of line string for this machine.
213    */

214   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
215  
216   /**
217    * Used to convert raw characters to their escaped version
218    * when these raw version cannot be used as part of an ASCII
219    * string literal.
220    */

221   protected String JavaDoc add_escapes(String JavaDoc str) {
222       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
223       char ch;
224       for (int i = 0; i < str.length(); i++) {
225         switch (str.charAt(i))
226         {
227            case 0 :
228               continue;
229            case '\b':
230               retval.append("\\b");
231               continue;
232            case '\t':
233               retval.append("\\t");
234               continue;
235            case '\n':
236               retval.append("\\n");
237               continue;
238            case '\f':
239               retval.append("\\f");
240               continue;
241            case '\r':
242               retval.append("\\r");
243               continue;
244            case '\"':
245               retval.append("\\\"");
246               continue;
247            case '\'':
248               retval.append("\\\'");
249               continue;
250            case '\\':
251               retval.append("\\\\");
252               continue;
253            default:
254               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
255                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
256                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
257               } else {
258                  retval.append(ch);
259               }
260               continue;
261         }
262       }
263       return retval.toString();
264    }
265
266     // Begin BeanShell Modification - override error methods and toString
267

268     public int getErrorLineNumber()
269     {
270         return currentToken.next.beginLine;
271     }
272
273     public String JavaDoc getErrorText() {
274         // copied from generated getMessage()
275
int maxSize = 0;
276         for (int i = 0; i < expectedTokenSequences.length; i++) {
277           if (maxSize < expectedTokenSequences[i].length)
278             maxSize = expectedTokenSequences[i].length;
279         }
280
281         String JavaDoc retval = "";
282         Token tok = currentToken.next;
283         for (int i = 0; i < maxSize; i++)
284         {
285           if (i != 0) retval += " ";
286           if (tok.kind == 0) {
287             retval += tokenImage[0];
288             break;
289           }
290           retval += add_escapes(tok.image);
291           tok = tok.next;
292         }
293         
294         return retval;
295     }
296
297     public String JavaDoc toString() {
298         return getMessage();
299     }
300
301     // End BeanShell Modification - override error methods and toString
302

303 }
304
Popular Tags