KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > sc > parser > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 2.1 */
2 /* [The preceding line is no longer true: this file has been edited, and is
3    no longer generated by JavaCC. However, the comment on the first line
4    is necessary to suppress a warning message when JavaCC is run.] */

5    
6 /* ****************************************************************************
7  * ParseException.java
8 * ****************************************************************************/

9
10 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
11 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
12 * Use is subject to license terms. *
13 * J_LZ_COPYRIGHT_END *********************************************************/

14
15 package org.openlaszlo.sc.parser;
16 import org.openlaszlo.sc.CompilerException;
17
18 /**
19  * This exception is thrown when parse errors are encountered.
20  * You can explicitly create objects of this exception type by
21  * calling the method generateParseException in the generated
22  * parser.
23  *
24  * You can modify this class to customize your error reporting
25  * mechanisms so long as you retain the public fields.
26  */

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

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

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

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

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

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

99     public String JavaDoc[] tokenImage;
100
101     public int getBeginLine() {
102         if (currentToken.next != null) {
103             return currentToken.next.beginLine;
104         } else {
105             return -1;
106         }
107     }
108
109     public int getBeginColumn() {
110         if (currentToken.next != null) {
111             return currentToken.next.beginColumn;
112         } else {
113             return -1;
114         }
115     }
116
117     /**
118      * This method has the standard behavior when this object has been
119      * created using the standard constructors. Otherwise, it uses
120      * "currentToken" and "expectedTokenSequences" to generate a parse
121      * error message and returns it. If this object has been created
122      * due to a parse error, and you do not catch it (it gets thrown
123      * from the parser), then this method is called during the printing
124      * of the final stack trace, and hence the correct error message
125      * gets displayed.
126      */

127     public String JavaDoc getMessage() {
128         return getMessage(true);
129     }
130     
131     public String JavaDoc getMessage(boolean includeSourceLocation) {
132         if (!specialConstructor) {
133             return super.getMessage();
134         }
135         String JavaDoc expected = "";
136         int expectedCount = 0;
137         int maxSize = 0;
138         boolean firstTime = true;
139         for (int i = 0; i < expectedTokenSequences.length; i++) {
140             if (maxSize < expectedTokenSequences[i].length) {
141                 maxSize = expectedTokenSequences[i].length;
142             }
143             String JavaDoc token = tokenImage[expectedTokenSequences[i][0]];
144             if (token.length() > 0 &&
145                 token.charAt(0) == '"' &&
146                 token.charAt(token.length()-1) == '"') {
147                 token = token.substring(1, token.length() - 1);
148             }
149             if (token.charAt(0) != '#') {
150                 if (!firstTime)
151                     expected += ", ";
152                 firstTime = false;
153                 expected += token;
154                 expectedCount += 1;
155             }
156         }
157         String JavaDoc tokenstr = "";
158         Token tok = currentToken.next;
159         for (int i = 0; i < maxSize; i++) {
160             if (i != 0) tokenstr += " ";
161             if (tok.kind == 0) {
162                 tokenstr += tokenImage[0];
163                 break;
164             }
165             tokenstr += tok.image; //add_escapes(tok.image);
166
tok = tok.next;
167         }
168         String JavaDoc msg = "the token \"" + tokenstr +
169             "\" was not expected at this position";
170         boolean showMismatchSolution = false;
171         if (tokenstr.equals("#endAttribute")) {
172             msg = "the attribute value ended in mid-expression";
173             showMismatchSolution = true;
174         }
175         if (tokenstr.equals("#endAttributeStatements")) {
176             msg = "the attribute value ended in mid-statement";
177             showMismatchSolution = true;
178         }
179         if (tokenstr.equals("#endContent")) {
180             msg = "the element content ended in mid-program";
181             showMismatchSolution = true;
182         }
183         String JavaDoc retval = "Syntax error: " + msg;
184         if (currentToken.next != null && includeSourceLocation) {
185             retval += " at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
186         }
187         retval += ".";
188         if (expectedCount == 1) {
189             retval += eol + "Was expecting " + expected;
190         } else if (0 < expectedCount && expectedCount <= 5) {
191             retval += eol + "Was expecting one of: " + expected;
192         } else if (showMismatchSolution) {
193             retval += eol + "Look for an unclosed '(', '{', or '['.";
194         }
195         return retval;
196     }
197
198     /**
199      * The end of line string for this machine.
200      */

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

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