KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 package com.genimen.djeneric.tools.generator.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 = 1L;
20
21   private static final String JavaDoc CHAINED_MSG = "Chained to: ";
22
23   int _line = 0;
24   int _column = 0;
25
26   private String JavaDoc _stackTrace = null;
27   transient private boolean _ignoreMyOwnStack = false;
28
29   /**
30    * This constructor is used by the method "generateParseException"
31    * in the generated parser. Calling this constructor generates
32    * a new object of this type with the fields "currentToken",
33    * "expectedTokenSequences", and "tokenImage" set. The boolean
34    * flag "specialConstructor" is also set to true to indicate that
35    * this constructor was used to create this object.
36    * This constructor calls its super class with the empty string
37    * to force the "toString" method of parent class "Throwable" to
38    * print the error message in the form:
39    * ParseException: <result of getMessage>
40    */

41   public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String JavaDoc[] tokenImageVal)
42   {
43     super("");
44     specialConstructor = true;
45     currentToken = currentTokenVal;
46     expectedTokenSequences = expectedTokenSequencesVal;
47     tokenImage = tokenImageVal;
48
49     _line = currentToken.next.beginLine;
50     _column = currentToken.next.beginColumn;
51   }
52
53   public ParseException()
54   {
55     super();
56     specialConstructor = false;
57   }
58
59   /**
60    * The following constructors are for use by you for whatever
61    * purpose you can think of. Constructing the exception in this
62    * manner makes the exception behave in the normal way - i.e., as
63    * documented in the class "Throwable". The fields "errorToken",
64    * "expectedTokenSequences", and "tokenImage" do not contain
65    * relevant information. The JavaCC generated code does not use
66    * these constructors.
67    */

68
69   public ParseException(String JavaDoc message, int atLine, int atColumn)
70   {
71     super(message);
72     specialConstructor = false;
73     _line = atLine;
74     _column = atColumn;
75   }
76
77   public ParseException(Exception JavaDoc chainThis, int atLine, int atColumn)
78   {
79     super(chainThis.getClass().getName() + ": " + chainThis.getMessage());
80     storeStack(chainThis);
81     specialConstructor = false;
82
83     _line = atLine;
84     _column = atColumn;
85   }
86
87   public void printStackTrace()
88   {
89     if (_stackTrace != null)
90     {
91       System.err.println(_stackTrace);
92       if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG);
93     }
94     if (!_ignoreMyOwnStack) super.printStackTrace();
95   }
96
97   public void printStackTrace(PrintStream JavaDoc printStream)
98   {
99     if (_stackTrace != null)
100     {
101       printStream.println(_stackTrace);
102       if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG);
103     }
104     if (!_ignoreMyOwnStack) super.printStackTrace(printStream);
105   }
106
107   public void printStackTrace(PrintWriter JavaDoc writer)
108   {
109     if (_stackTrace != null)
110     {
111       writer.println(_stackTrace);
112       if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG);
113     }
114     if (!_ignoreMyOwnStack) super.printStackTrace(writer);
115   }
116
117   private void storeStack(Exception JavaDoc theEx)
118   {
119     _stackTrace = stack2String(theEx);
120     _ignoreMyOwnStack = true; // Zie kommentaar bovenaan deze source
121
}
122
123   private String JavaDoc stack2String(Exception JavaDoc x)
124   {
125     if (x == null) return "";
126
127     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
128     java.io.PrintWriter JavaDoc stackTrace = new java.io.PrintWriter JavaDoc(stringWriter);
129     x.printStackTrace(stackTrace);
130     String JavaDoc result = stringWriter.toString().trim();
131     if (result.length() == 0) result = null;
132
133     return result;
134   }
135
136   public int getLine()
137   {
138     return _line;
139   }
140
141   public int getColumn()
142   {
143     return _column;
144   }
145
146   public void setLine(int l)
147   {
148     _line = l;
149   }
150
151   public void setColumn(int c)
152   {
153     _column = c;
154   }
155
156   public ParseException(String JavaDoc message)
157   {
158     super(message);
159     specialConstructor = false;
160   }
161
162   /**
163    * This variable determines which constructor was used to create
164    * this object and thereby affects the semantics of the
165    * "getMessage" method (see below).
166    */

167   protected boolean specialConstructor;
168
169   /**
170    * This is the last token that has been consumed successfully. If
171    * this object has been created due to a parse error, the token
172    * followng this token will (therefore) be the first error token.
173    */

174   public Token currentToken;
175
176   /**
177    * Each entry in this array is an array of integers. Each array
178    * of integers represents a sequence of tokens (by their ordinal
179    * values) that is expected at this point of the parse.
180    */

181   public int[][] expectedTokenSequences;
182
183   /**
184    * This is a reference to the "tokenImage" array of the generated
185    * parser within which the parse error occurred. This array is
186    * defined in the generated ...Constants interface.
187    */

188   public String JavaDoc[] tokenImage;
189
190   /**
191    * This method has the standard behavior when this object has been
192    * created using the standard constructors. Otherwise, it uses
193    * "currentToken" and "expectedTokenSequences" to generate a parse
194    * error message and returns it. If this object has been created
195    * due to a parse error, and you do not catch it (it gets thrown
196    * from the parser), then this method is called during the printing
197    * of the final stack trace, and hence the correct error message
198    * gets displayed.
199    */

200   public String JavaDoc getMessage()
201   {
202     if (!specialConstructor)
203     {
204       return super.getMessage();
205     }
206     String JavaDoc expected = "";
207     int maxSize = 0;
208     for (int i = 0; i < expectedTokenSequences.length; i++)
209     {
210       if (maxSize < expectedTokenSequences[i].length)
211       {
212         maxSize = expectedTokenSequences[i].length;
213       }
214       for (int j = 0; j < expectedTokenSequences[i].length; j++)
215       {
216         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
217       }
218       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
219       {
220         expected += "...";
221       }
222       expected += eol + " ";
223     }
224     String JavaDoc retval = "Encountered \"";
225     Token tok = currentToken.next;
226     for (int i = 0; i < maxSize; i++)
227     {
228       if (i != 0) retval += " ";
229       if (tok.kind == 0)
230       {
231         retval += tokenImage[0];
232         break;
233       }
234       retval += add_escapes(tok.image);
235       tok = tok.next;
236     }
237     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
238     retval += "." + eol;
239     if (expectedTokenSequences.length == 1)
240     {
241       retval += "Was expecting:" + eol + " ";
242     }
243     else
244     {
245       retval += "Was expecting one of:" + eol + " ";
246     }
247     retval += expected;
248     return retval;
249   }
250
251   /**
252    * The end of line string for this machine.
253    */

254   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
255
256   /**
257    * Used to convert raw characters to their escaped version
258    * when these raw version cannot be used as part of an ASCII
259    * string literal.
260    */

261   protected String JavaDoc add_escapes(String JavaDoc str)
262   {
263     StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
264     char ch;
265     for (int i = 0; i < str.length(); i++)
266     {
267       switch (str.charAt(i))
268       {
269         case 0 :
270           continue;
271         case '\b' :
272           retval.append("\\b");
273           continue;
274         case '\t' :
275           retval.append("\\t");
276           continue;
277         case '\n' :
278           retval.append("\\n");
279           continue;
280         case '\f' :
281           retval.append("\\f");
282           continue;
283         case '\r' :
284           retval.append("\\r");
285           continue;
286         case '\"' :
287           retval.append("\\\"");
288           continue;
289         case '\'' :
290           retval.append("\\\'");
291           continue;
292         case '\\' :
293           retval.append("\\\\");
294           continue;
295         default :
296           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e)
297           {
298             String JavaDoc s = "0000" + Integer.toString(ch, 16);
299             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
300           }
301           else
302           {
303             retval.append(ch);
304           }
305           continue;
306       }
307     }
308     return retval.toString();
309   }
310
311 }
Popular Tags