KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > oql > core > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 package com.genimen.djeneric.repository.oql.core;
3
4 import java.io.PrintStream JavaDoc;
5 import java.io.PrintWriter JavaDoc;
6 import java.io.StringWriter JavaDoc;
7
8 /**
9  * This exception is thrown when parse errors are encountered.
10  * You can explicitly create objects of this exception type by
11  * calling the method generateParseException in the generated
12  * parser.
13  *
14  * You can modify this class to customize your error reporting
15  * mechanisms so long as you retain the public fields.
16  */

17 public class ParseException extends Exception JavaDoc
18 {
19   private static final long serialVersionUID = 3690196534013015865L;
20
21   private static final String JavaDoc CHAINED_MSG = "Chained to: ";
22
23   protected int _line = 0;
24   protected int _column = 0;
25
26   private String JavaDoc _stackTrace = null;
27   private String JavaDoc _additionalInfo = null;
28   transient private boolean _ignoreMyOwnStack = false;
29
30   /**
31    * This constructor is used by the method "generateParseException"
32    * in the generated parser. Calling this constructor generates
33    * a new object of this type with the fields "currentToken",
34    * "expectedTokenSequences", and "tokenImage" set. The boolean
35    * flag "specialConstructor" is also set to true to indicate that
36    * this constructor was used to create this object.
37    * This constructor calls its super class with the empty string
38    * to force the "toString" method of parent class "Throwable" to
39    * print the error message in the form:
40    * ParseException: <result of getMessage>
41    */

42   public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String JavaDoc[] tokenImageVal)
43   {
44     super("");
45     specialConstructor = true;
46     currentToken = currentTokenVal;
47     expectedTokenSequences = expectedTokenSequencesVal;
48     tokenImage = tokenImageVal;
49
50     _line = currentToken.next.beginLine;
51     _column = currentToken.next.beginColumn;
52
53   }
54
55   public ParseException(String JavaDoc message, int atLine, int atColumn)
56   {
57     super(message);
58     specialConstructor = false;
59     _line = atLine;
60     _column = atColumn;
61   }
62
63   public ParseException(Exception JavaDoc chainThis, int atLine, int atColumn)
64   {
65     super(chainThis.getClass().getName() + ": " + chainThis.getMessage());
66     storeStack(chainThis);
67     specialConstructor = false;
68     _line = atLine;
69     _column = atColumn;
70   }
71
72   public ParseException(Exception JavaDoc chainThis, int atLine, int atColumn, String JavaDoc additionalInfo)
73   {
74     super(chainThis.getClass().getName() + ": " + chainThis.getMessage());
75     storeStack(chainThis);
76     specialConstructor = false;
77     _line = atLine;
78     _column = atColumn;
79     _additionalInfo = additionalInfo;
80   }
81
82   /**
83    * The following constructors are for use by you for whatever
84    * purpose you can think of. Constructing the exception in this
85    * manner makes the exception behave in the normal way - i.e., as
86    * documented in the class "Throwable". The fields "errorToken",
87    * "expectedTokenSequences", and "tokenImage" do not contain
88    * relevant information. The JavaCC generated code does not use
89    * these constructors.
90    */

91
92   public ParseException()
93   {
94     super();
95     specialConstructor = false;
96   }
97
98   public ParseException(String JavaDoc message)
99   {
100     super(message);
101     specialConstructor = false;
102   }
103
104   public void printStackTrace()
105   {
106     if (_stackTrace != null)
107     {
108       System.err.println(_stackTrace);
109       if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG);
110     }
111     if (_additionalInfo != null)
112     {
113       System.err.println(_additionalInfo);
114     }
115     if (!_ignoreMyOwnStack) super.printStackTrace();
116   }
117
118   public void printStackTrace(PrintStream JavaDoc printStream)
119   {
120     if (_stackTrace != null)
121     {
122       printStream.println(_stackTrace);
123       if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG);
124     }
125     if (_additionalInfo != null)
126     {
127       printStream.println(_additionalInfo);
128     }
129     if (!_ignoreMyOwnStack) super.printStackTrace(printStream);
130   }
131
132   public void printStackTrace(PrintWriter JavaDoc writer)
133   {
134     if (_stackTrace != null)
135     {
136       writer.println(_stackTrace);
137       if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG);
138     }
139     if (_additionalInfo != null)
140     {
141       writer.println(_additionalInfo);
142     }
143     if (!_ignoreMyOwnStack) super.printStackTrace(writer);
144   }
145
146   private void storeStack(Exception JavaDoc theEx)
147   {
148     _stackTrace = stack2String(theEx);
149     _ignoreMyOwnStack = true; // Zie kommentaar bovenaan deze source
150
}
151
152   private String JavaDoc stack2String(Exception JavaDoc x)
153   {
154     if (x == null) return "";
155
156     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
157     java.io.PrintWriter JavaDoc stackTrace = new java.io.PrintWriter JavaDoc(stringWriter);
158     x.printStackTrace(stackTrace);
159     String JavaDoc result = stringWriter.toString().trim();
160     if (result.length() == 0) result = null;
161
162     return result;
163   }
164
165   public int getLine()
166   {
167     return _line;
168   }
169
170   public int getColumn()
171   {
172     return _column;
173   }
174
175   public void setLine(int l)
176   {
177     _line = l;
178   }
179
180   public void setColumn(int c)
181   {
182     _column = c;
183   }
184
185   /**
186    * This variable determines which constructor was used to create
187    * this object and thereby affects the semantics of the
188    * "getMessage" method (see below).
189    */

190   protected boolean specialConstructor;
191
192   /**
193    * This is the last token that has been consumed successfully. If
194    * this object has been created due to a parse error, the token
195    * followng this token will (therefore) be the first error token.
196    */

197   public Token currentToken;
198
199   /**
200    * Each entry in this array is an array of integers. Each array
201    * of integers represents a sequence of tokens (by their ordinal
202    * values) that is expected at this point of the parse.
203    */

204   public int[][] expectedTokenSequences;
205
206   /**
207    * This is a reference to the "tokenImage" array of the generated
208    * parser within which the parse error occurred. This array is
209    * defined in the generated ...Constants interface.
210    */

211   public String JavaDoc[] tokenImage;
212
213   /**
214    * This method has the standard behavior when this object has been
215    * created using the standard constructors. Otherwise, it uses
216    * "currentToken" and "expectedTokenSequences" to generate a parse
217    * error message and returns it. If this object has been created
218    * due to a parse error, and you do not catch it (it gets thrown
219    * from the parser), then this method is called during the printing
220    * of the final stack trace, and hence the correct error message
221    * gets displayed.
222    */

223   public String JavaDoc getMessage()
224   {
225     if (!specialConstructor)
226     {
227       return super.getMessage();
228     }
229     String JavaDoc expected = "";
230     int maxSize = 0;
231     for (int i = 0; i < expectedTokenSequences.length; i++)
232     {
233       if (maxSize < expectedTokenSequences[i].length)
234       {
235         maxSize = expectedTokenSequences[i].length;
236       }
237       for (int j = 0; j < expectedTokenSequences[i].length; j++)
238       {
239         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
240       }
241       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
242       {
243         expected += "...";
244       }
245       expected += eol + " ";
246     }
247     String JavaDoc retval = "Encountered \"";
248     Token tok = currentToken.next;
249     for (int i = 0; i < maxSize; i++)
250     {
251       if (i != 0) retval += " ";
252       if (tok.kind == 0)
253       {
254         retval += tokenImage[0];
255         break;
256       }
257       retval += add_escapes(tok.image);
258       tok = tok.next;
259     }
260     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
261     retval += "." + eol;
262     if (expectedTokenSequences.length == 1)
263     {
264       retval += "Was expecting:" + eol + " ";
265     }
266     else
267     {
268       retval += "Was expecting one of:" + eol + " ";
269     }
270     retval += expected;
271     return retval;
272   }
273
274   /**
275    * The end of line string for this machine.
276    */

277   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
278
279   /**
280    * Used to convert raw characters to their escaped version
281    * when these raw version cannot be used as part of an ASCII
282    * string literal.
283    */

284   protected String JavaDoc add_escapes(String JavaDoc str)
285   {
286     StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
287     char ch;
288     for (int i = 0; i < str.length(); i++)
289     {
290       switch (str.charAt(i))
291       {
292         case 0 :
293           continue;
294         case '\b' :
295           retval.append("\\b");
296           continue;
297         case '\t' :
298           retval.append("\\t");
299           continue;
300         case '\n' :
301           retval.append("\\n");
302           continue;
303         case '\f' :
304           retval.append("\\f");
305           continue;
306         case '\r' :
307           retval.append("\\r");
308           continue;
309         case '\"' :
310           retval.append("\\\"");
311           continue;
312         case '\'' :
313           retval.append("\\\'");
314           continue;
315         case '\\' :
316           retval.append("\\\\");
317           continue;
318         default :
319           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e)
320           {
321             String JavaDoc s = "0000" + Integer.toString(ch, 16);
322             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
323           }
324           else
325           {
326             retval.append(ch);
327           }
328           continue;
329       }
330     }
331     return retval.toString();
332   }
333
334   public String JavaDoc getAdditionalInfo()
335   {
336     return _additionalInfo;
337   }
338
339   public void setAdditionalInfo(String JavaDoc additionalInfo)
340   {
341     _additionalInfo = additionalInfo;
342   }
343
344 }
Popular Tags