KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > ejb > query > ParseException


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
13 package com.versant.core.ejb.query;
14
15 /**
16  * This exception is thrown when parse errors are encountered.
17  * You can explicitly create objects of this exception type by
18  * calling the method generateParseException in the generated
19  * parser.
20  *
21  * You can modify this class to customize your error reporting
22  * mechanisms so long as you retain the public fields.
23  */

24 public class ParseException extends Exception JavaDoc {
25
26   /**
27    * This constructor is used by the method "generateParseException"
28    * in the generated parser. Calling this constructor generates
29    * a new object of this type with the fields "currentToken",
30    * "expectedTokenSequences", and "tokenImage" set. The boolean
31    * flag "specialConstructor" is also set to true to indicate that
32    * this constructor was used to create this object.
33    * This constructor calls its super class with the empty string
34    * to force the "toString" method of parent class "Throwable" to
35    * print the error message in the form:
36    * ParseException: <result of getMessage>
37    */

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

59
60   public ParseException() {
61     super();
62     specialConstructor = false;
63   }
64
65   public ParseException(String JavaDoc message) {
66     super(message);
67     specialConstructor = false;
68   }
69
70   /**
71    * This variable determines which constructor was used to create
72    * this object and thereby affects the semantics of the
73    * "getMessage" method (see below).
74    */

75   protected boolean specialConstructor;
76
77   /**
78    * This is the last token that has been consumed successfully. If
79    * this object has been created due to a parse error, the token
80    * followng this token will (therefore) be the first error token.
81    */

82   public Token currentToken;
83
84   /**
85    * Each entry in this array is an array of integers. Each array
86    * of integers represents a sequence of tokens (by their ordinal
87    * values) that is expected at this point of the parse.
88    */

89   public int[][] expectedTokenSequences;
90
91   /**
92    * This is a reference to the "tokenImage" array of the generated
93    * parser within which the parse error occurred. This array is
94    * defined in the generated ...Constants interface.
95    */

96   public String JavaDoc[] tokenImage;
97
98   /**
99    * This method has the standard behavior when this object has been
100    * created using the standard constructors. Otherwise, it uses
101    * "currentToken" and "expectedTokenSequences" to generate a parse
102    * error message and returns it. If this object has been created
103    * due to a parse error, and you do not catch it (it gets thrown
104    * from the parser), then this method is called during the printing
105    * of the final stack trace, and hence the correct error message
106    * gets displayed.
107    */

108   public String JavaDoc getMessage() {
109     if (!specialConstructor) {
110       return super.getMessage();
111     }
112     String JavaDoc expected = "";
113     int maxSize = 0;
114     for (int i = 0; i < expectedTokenSequences.length; i++) {
115       if (maxSize < expectedTokenSequences[i].length) {
116         maxSize = expectedTokenSequences[i].length;
117       }
118       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
119         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
120       }
121       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
122         expected += "...";
123       }
124       expected += eol + " ";
125     }
126     String JavaDoc retval = "Encountered \"";
127     Token tok = currentToken.next;
128     for (int i = 0; i < maxSize; i++) {
129       if (i != 0) retval += " ";
130       if (tok.kind == 0) {
131         retval += tokenImage[0];
132         break;
133       }
134       retval += add_escapes(tok.image);
135       tok = tok.next;
136     }
137     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
138     retval += "." + eol;
139     if (expectedTokenSequences.length == 1) {
140       retval += "Was expecting:" + eol + " ";
141     } else {
142       retval += "Was expecting one of:" + eol + " ";
143     }
144     retval += expected;
145     return retval;
146   }
147
148   /**
149    * The end of line string for this machine.
150    */

151   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
152  
153   /**
154    * Used to convert raw characters to their escaped version
155    * when these raw version cannot be used as part of an ASCII
156    * string literal.
157    */

158   protected String JavaDoc add_escapes(String JavaDoc str) {
159       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
160       char ch;
161       for (int i = 0; i < str.length(); i++) {
162         switch (str.charAt(i))
163         {
164            case 0 :
165               continue;
166            case '\b':
167               retval.append("\\b");
168               continue;
169            case '\t':
170               retval.append("\\t");
171               continue;
172            case '\n':
173               retval.append("\\n");
174               continue;
175            case '\f':
176               retval.append("\\f");
177               continue;
178            case '\r':
179               retval.append("\\r");
180               continue;
181            case '\"':
182               retval.append("\\\"");
183               continue;
184            case '\'':
185               retval.append("\\\'");
186               continue;
187            case '\\':
188               retval.append("\\\\");
189               continue;
190            default:
191               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
192                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
193                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
194               } else {
195                  retval.append(ch);
196               }
197               continue;
198         }
199       }
200       return retval.toString();
201    }
202
203 }
204
Popular Tags