KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > parser > ParseException


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el.parser;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 /**
23  * This exception is thrown when parse errors are encountered.
24  * You can explicitly create objects of this exception type by
25  * calling the method generateParseException in the generated
26  * parser.
27  * <p/>
28  * You can modify this class to customize your error reporting
29  * mechanisms so long as you retain the public fields.
30  */

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

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

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

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

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

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

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

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

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

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