KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > ParseException


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.ParseException
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 /**
25  * This exception is thrown when parse errors are encountered.
26  * You can explicitly create objects of this exception type by
27  * calling the method generateParseException in the generated
28  * parser.
29  *
30  * You can modify this class to customize your error reporting
31  * mechanisms so long as you retain the public fields.
32  */

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

47   public ParseException(Token currentTokenVal,
48                         int[][] expectedTokenSequencesVal,
49                         String JavaDoc[] tokenImageVal
50                        )
51   {
52     super("");
53     specialConstructor = true;
54     currentToken = currentTokenVal;
55     expectedTokenSequences = expectedTokenSequencesVal;
56     tokenImage = tokenImageVal;
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() {
70     super();
71     specialConstructor = false;
72   }
73
74   public ParseException(String JavaDoc message) {
75     super(message);
76     specialConstructor = false;
77   }
78
79   /**
80    * This variable determines which constructor was used to create
81    * this object and thereby affects the semantics of the
82    * "getMessage" method (see below).
83    */

84   protected boolean specialConstructor;
85
86   /**
87    * This is the last token that has been consumed successfully. If
88    * this object has been created due to a parse error, the token
89    * followng this token will (therefore) be the first error token.
90    */

91   public Token currentToken;
92
93   /**
94    * Each entry in this array is an array of integers. Each array
95    * of integers represents a sequence of tokens (by their ordinal
96    * values) that is expected at this point of the parse.
97    */

98   public int[][] expectedTokenSequences;
99
100   /**
101    * This is a reference to the "tokenImage" array of the generated
102    * parser within which the parse error occurred. This array is
103    * defined in the generated ...Constants interface.
104    */

105   public String JavaDoc[] tokenImage;
106
107   /**
108    * This method has the standard behavior when this object has been
109    * created using the standard constructors. Otherwise, it uses
110    * "currentToken" and "expectedTokenSequences" to generate a parse
111    * error message and returns it. If this object has been created
112    * due to a parse error, and you do not catch it (it gets thrown
113    * from the parser), then this method is called during the printing
114    * of the final stack trace, and hence the correct error message
115    * gets displayed.
116    */

117   public String JavaDoc getMessage() {
118     if (!specialConstructor) {
119       return super.getMessage();
120     }
121     String JavaDoc expected = "";
122     int maxSize = 0;
123     for (int i = 0; i < expectedTokenSequences.length; i++) {
124       if (maxSize < expectedTokenSequences[i].length) {
125         maxSize = expectedTokenSequences[i].length;
126       }
127       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
128         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
129       }
130       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
131         expected += "...";
132       }
133       expected += eol + " ";
134     }
135     String JavaDoc retval = "Encountered \"";
136     Token tok = currentToken.next;
137     for (int i = 0; i < maxSize; i++) {
138       if (i != 0) retval += " ";
139       if (tok.kind == 0) {
140         retval += tokenImage[0];
141         break;
142       }
143       retval += add_escapes(tok.image);
144       tok = tok.next;
145     }
146     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
147  /*
148   * For output compatibility with previous releases, do not report expected tokens.
149   *
150     retval += "." + eol;
151     if (expectedTokenSequences.length == 1) {
152       retval += "Was expecting:" + eol + " ";
153     } else {
154       retval += "Was expecting one of:" + eol + " ";
155     }
156     retval += expected;
157   */

158     return retval;
159   }
160
161   /**
162    * The end of line string for this machine.
163    */

164   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
165
166   /**
167    * Used to convert raw characters to their escaped version
168    * when these raw version cannot be used as part of an ASCII
169    * string literal.
170    */

171   protected String JavaDoc add_escapes(String JavaDoc str) {
172       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
173       char ch;
174       for (int i = 0; i < str.length(); i++) {
175         switch (str.charAt(i))
176         {
177            case 0 :
178               continue;
179            case '\b':
180               retval.append("\\b");
181               continue;
182            case '\t':
183               retval.append("\\t");
184               continue;
185            case '\n':
186               retval.append("\\n");
187               continue;
188            case '\f':
189               retval.append("\\f");
190               continue;
191            case '\r':
192               retval.append("\\r");
193               continue;
194            case '\"':
195               retval.append("\\\"");
196               continue;
197            case '\'':
198               retval.append("\\\'");
199               continue;
200            case '\\':
201               retval.append("\\\\");
202               continue;
203            default:
204               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
205                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
206                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
207               } else {
208                  retval.append(ch);
209               }
210               continue;
211         }
212       }
213       return retval.toString();
214    }
215
216 }
217
Popular Tags