KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > parser > ParseException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
23 package com.sun.el.parser;
24
25 /**
26  * This exception is thrown when parse errors are encountered.
27  * You can explicitly create objects of this exception type by
28  * calling the method generateParseException in the generated
29  * parser.
30  *
31  * You can modify this class to customize your error reporting
32  * mechanisms so long as you retain the public fields.
33  */

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

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

69
70   public ParseException() {
71     super();
72     specialConstructor = false;
73   }
74
75   public ParseException(String JavaDoc message) {
76     super(message);
77     specialConstructor = false;
78   }
79
80   /**
81    * This variable determines which constructor was used to create
82    * this object and thereby affects the semantics of the
83    * "getMessage" method (see below).
84    */

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

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

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

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

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

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

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