KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

34   public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String JavaDoc[] tokenImageVal)
35   {
36     super("");
37     specialConstructor = true;
38     currentToken = currentTokenVal;
39     expectedTokenSequences = expectedTokenSequencesVal;
40     tokenImage = tokenImageVal;
41
42     _line = currentToken.next.beginLine;
43     _column = currentToken.next.beginColumn;
44
45   }
46
47   public ParseException(String JavaDoc message, SimpleNode cause)
48   {
49     super(message);
50     specialConstructor = false;
51     _line = cause.getLine();
52     _column = cause.getColumn();
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(Throwable 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(Throwable JavaDoc chainThis, SimpleNode cause)
73   {
74     super(chainThis.getClass().getName() + ": " + chainThis.getMessage());
75     storeStack(chainThis);
76     specialConstructor = false;
77     _line = cause.getLine();
78     _column = cause.getColumn();
79   }
80
81   public ParseException(Throwable JavaDoc chainThis)
82   {
83     super(chainThis.getClass().getName() + ": " + chainThis.getMessage());
84     storeStack(chainThis);
85     specialConstructor = false;
86
87     if (chainThis instanceof ParseException)
88     {
89       ParseException cause = (ParseException) chainThis;
90       _line = cause._line;
91       _column = cause._line;
92     }
93   }
94
95   /**
96    * The following constructors are for use by you for whatever purpose you can think of. Constructing the exception in
97    * this manner makes the exception behave in the normal way - i.e., as documented in the class "Throwable". The
98    * fields "errorToken", "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC
99    * generated code does not use these constructors.
100    */

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

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

193   public Token currentToken;
194
195   /**
196    * Each entry in this array is an array of integers. Each array of integers represents a sequence of tokens (by their
197    * ordinal values) that is expected at this point of the parse.
198    */

199   public int[][] expectedTokenSequences;
200
201   /**
202    * This is a reference to the "tokenImage" array of the generated parser within which the parse error occurred. This
203    * array is defined in the generated ...Constants interface.
204    */

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

214   public String JavaDoc getMessage()
215   {
216     if (!specialConstructor)
217     {
218       return super.getMessage();
219     }
220     String JavaDoc expected = "";
221     int maxSize = 0;
222     for (int i = 0; i < expectedTokenSequences.length; i++)
223     {
224       if (maxSize < expectedTokenSequences[i].length)
225       {
226         maxSize = expectedTokenSequences[i].length;
227       }
228       for (int j = 0; j < expectedTokenSequences[i].length; j++)
229       {
230         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
231       }
232       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
233       {
234         expected += "...";
235       }
236       expected += eol + " ";
237     }
238     String JavaDoc retval = Messages.getString("ParseException.Encountered") + " \"";
239     Token tok = currentToken.next;
240     for (int i = 0; i < maxSize; i++)
241     {
242       if (i != 0) retval += " ";
243       if (tok.kind == 0)
244       {
245         retval += tokenImage[0];
246         break;
247       }
248       retval += add_escapes(tok.image);
249       tok = tok.next;
250     }
251     retval += "\" "
252               + Messages.getString("ParseException.position", String.valueOf(currentToken.next.beginLine), String
253                   .valueOf(currentToken.next.beginColumn));
254     retval += "." + eol;
255     if (expectedTokenSequences.length == 1)
256     {
257       retval += Messages.getString("ParseException.WasExpecting") + eol + " ";
258     }
259     else
260     {
261       retval += Messages.getString("ParseException.WasExpectingOne") + eol + " ";
262     }
263     retval += expected;
264     return retval;
265   }
266
267   /**
268    * The end of line string for this machine.
269    */

270   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
271
272   /**
273    * Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII
274    * string literal.
275    */

276   protected String JavaDoc add_escapes(String JavaDoc str)
277   {
278     StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
279     char ch;
280     for (int i = 0; i < str.length(); i++)
281     {
282       switch (str.charAt(i))
283       {
284         case 0 :
285           continue;
286         case '\b' :
287           retval.append("\\b");
288           continue;
289         case '\t' :
290           retval.append("\\t");
291           continue;
292         case '\n' :
293           retval.append("\\n");
294           continue;
295         case '\f' :
296           retval.append("\\f");
297           continue;
298         case '\r' :
299           retval.append("\\r");
300           continue;
301         case '\"' :
302           retval.append("\\\"");
303           continue;
304         case '\'' :
305           retval.append("\\\'");
306           continue;
307         case '\\' :
308           retval.append("\\\\");
309           continue;
310         default :
311           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e)
312           {
313             String JavaDoc s = "0000" + Integer.toString(ch, 16);
314             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
315           }
316           else
317           {
318             retval.append(ch);
319           }
320           continue;
321       }
322     }
323     return retval.toString();
324   }
325
326 }
Popular Tags