KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > wocompat > parser > ParseException


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

20
21
22 package org.apache.cayenne.wocompat.parser;
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     retval += "." + eol;
148     if (expectedTokenSequences.length == 1) {
149       retval += "Was expecting:" + eol + " ";
150     } else {
151       retval += "Was expecting one of:" + eol + " ";
152     }
153     retval += expected;
154     return retval;
155   }
156
157   /**
158    * The end of line string for this machine.
159    */

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

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