KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > exp > parser > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 /* ====================================================================
3  *
4  * The ObjectStyle Group Software License, version 1.1
5  * ObjectStyle Group - http://objectstyle.org/
6  *
7  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
8  * of the software. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution, if any,
23  * must include the following acknowlegement:
24  * "This product includes software developed by independent contributors
25  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
26  * Alternately, this acknowlegement may appear in the software itself,
27  * if and wherever such third-party acknowlegements normally appear.
28  *
29  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
30  * or promote products derived from this software without prior written
31  * permission. For written permission, email
32  * "andrus at objectstyle dot org".
33  *
34  * 5. Products derived from this software may not be called "ObjectStyle"
35  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
36  * names without prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals and hosted on ObjectStyle Group web site. For more
54  * information on the ObjectStyle Group, please see
55  * <http://objectstyle.org/>.
56  *
57  * Some parts of the parser are based on OGNL parser,
58  * copyright (c) 2002, Drew Davidson and Luke Blanshard
59  */

60 package org.objectstyle.cayenne.exp.parser;
61
62 /**
63  * This exception is thrown when parse errors are encountered.
64  * You can explicitly create objects of this exception type by
65  * calling the method generateParseException in the generated
66  * parser.
67  *
68  * You can modify this class to customize your error reporting
69  * mechanisms so long as you retain the public fields.
70  */

71 public class ParseException extends Exception JavaDoc {
72
73   /**
74    * This constructor is used by the method "generateParseException"
75    * in the generated parser. Calling this constructor generates
76    * a new object of this type with the fields "currentToken",
77    * "expectedTokenSequences", and "tokenImage" set. The boolean
78    * flag "specialConstructor" is also set to true to indicate that
79    * this constructor was used to create this object.
80    * This constructor calls its super class with the empty string
81    * to force the "toString" method of parent class "Throwable" to
82    * print the error message in the form:
83    * ParseException: <result of getMessage>
84    */

85   public ParseException(Token currentTokenVal,
86                         int[][] expectedTokenSequencesVal,
87                         String JavaDoc[] tokenImageVal
88                        )
89   {
90     super("");
91     specialConstructor = true;
92     currentToken = currentTokenVal;
93     expectedTokenSequences = expectedTokenSequencesVal;
94     tokenImage = tokenImageVal;
95   }
96
97   /**
98    * The following constructors are for use by you for whatever
99    * purpose you can think of. Constructing the exception in this
100    * manner makes the exception behave in the normal way - i.e., as
101    * documented in the class "Throwable". The fields "errorToken",
102    * "expectedTokenSequences", and "tokenImage" do not contain
103    * relevant information. The JavaCC generated code does not use
104    * these constructors.
105    */

106
107   public ParseException() {
108     super();
109     specialConstructor = false;
110   }
111
112   public ParseException(String JavaDoc message) {
113     super(message);
114     specialConstructor = false;
115   }
116
117   /**
118    * This variable determines which constructor was used to create
119    * this object and thereby affects the semantics of the
120    * "getMessage" method (see below).
121    */

122   protected boolean specialConstructor;
123
124   /**
125    * This is the last token that has been consumed successfully. If
126    * this object has been created due to a parse error, the token
127    * followng this token will (therefore) be the first error token.
128    */

129   public Token currentToken;
130
131   /**
132    * Each entry in this array is an array of integers. Each array
133    * of integers represents a sequence of tokens (by their ordinal
134    * values) that is expected at this point of the parse.
135    */

136   public int[][] expectedTokenSequences;
137
138   /**
139    * This is a reference to the "tokenImage" array of the generated
140    * parser within which the parse error occurred. This array is
141    * defined in the generated ...Constants interface.
142    */

143   public String JavaDoc[] tokenImage;
144
145   /**
146    * This method has the standard behavior when this object has been
147    * created using the standard constructors. Otherwise, it uses
148    * "currentToken" and "expectedTokenSequences" to generate a parse
149    * error message and returns it. If this object has been created
150    * due to a parse error, and you do not catch it (it gets thrown
151    * from the parser), then this method is called during the printing
152    * of the final stack trace, and hence the correct error message
153    * gets displayed.
154    */

155   public String JavaDoc getMessage() {
156     if (!specialConstructor) {
157       return super.getMessage();
158     }
159     String JavaDoc expected = "";
160     int maxSize = 0;
161     for (int i = 0; i < expectedTokenSequences.length; i++) {
162       if (maxSize < expectedTokenSequences[i].length) {
163         maxSize = expectedTokenSequences[i].length;
164       }
165       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
166         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
167       }
168       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
169         expected += "...";
170       }
171       expected += eol + " ";
172     }
173     String JavaDoc retval = "Encountered \"";
174     Token tok = currentToken.next;
175     for (int i = 0; i < maxSize; i++) {
176       if (i != 0) retval += " ";
177       if (tok.kind == 0) {
178         retval += tokenImage[0];
179         break;
180       }
181       retval += add_escapes(tok.image);
182       tok = tok.next;
183     }
184     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
185     retval += "." + eol;
186     if (expectedTokenSequences.length == 1) {
187       retval += "Was expecting:" + eol + " ";
188     } else {
189       retval += "Was expecting one of:" + eol + " ";
190     }
191     retval += expected;
192     return retval;
193   }
194
195   /**
196    * The end of line string for this machine.
197    */

198   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
199  
200   /**
201    * Used to convert raw characters to their escaped version
202    * when these raw version cannot be used as part of an ASCII
203    * string literal.
204    */

205   protected String JavaDoc add_escapes(String JavaDoc str) {
206       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
207       char ch;
208       for (int i = 0; i < str.length(); i++) {
209         switch (str.charAt(i))
210         {
211            case 0 :
212               continue;
213            case '\b':
214               retval.append("\\b");
215               continue;
216            case '\t':
217               retval.append("\\t");
218               continue;
219            case '\n':
220               retval.append("\\n");
221               continue;
222            case '\f':
223               retval.append("\\f");
224               continue;
225            case '\r':
226               retval.append("\\r");
227               continue;
228            case '\"':
229               retval.append("\\\"");
230               continue;
231            case '\'':
232               retval.append("\\\'");
233               continue;
234            case '\\':
235               retval.append("\\\\");
236               continue;
237            default:
238               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
239                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
240                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
241               } else {
242                  retval.append(ch);
243               }
244               continue;
245         }
246       }
247       return retval.toString();
248    }
249
250 }
251
Popular Tags