KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > ParseException


1 /*
2  * ParseException.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser;
23
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * A parse exception.
28  *
29  * @author Per Cederberg, <per at percederberg dot net>
30  * @version 1.1
31  */

32 public class ParseException extends Exception JavaDoc {
33
34     /**
35      * The internal error type constant. This type is only used to
36      * signal an error that is a result of a bug in the parser or
37      * tokenizer code.
38      */

39     public static final int INTERNAL_ERROR = 0;
40
41     /**
42      * The I/O error type constant. This type is used for stream I/O
43      * errors.
44      */

45     public static final int IO_ERROR = 1;
46
47     /**
48      * The unexpected end of file error type constant. This type is
49      * used when end of file is encountered instead of a valid token.
50      */

51     public static final int UNEXPECTED_EOF_ERROR = 2;
52
53     /**
54      * The unexpected character error type constant. This type is used
55      * when a character is read that isn't handled by one of the token
56      * patterns.
57      */

58     public static final int UNEXPECTED_CHAR_ERROR = 3;
59
60     /**
61      * The unexpected token error type constant. This type is used
62      * when another token than the expected one is encountered.
63      */

64     public static final int UNEXPECTED_TOKEN_ERROR = 4;
65
66     /**
67      * The invalid token error type constant. This type is used when
68      * a token pattern with an error message is matched. The
69      * additional information provided should contain the error
70      * message.
71      */

72     public static final int INVALID_TOKEN_ERROR = 5;
73
74     /**
75      * The analysis error type constant. This type is used when an
76      * error is encountered in the analysis. The additional
77      * information provided should contain the error message.
78      */

79     public static final int ANALYSIS_ERROR = 6;
80
81     /**
82      * The error type.
83      */

84     private int type;
85
86     /**
87      * The additional information string.
88      */

89     private String JavaDoc info;
90
91     /**
92      * The additional details information. This variable is only used
93      * for unexpected token errors.
94      */

95     private ArrayList JavaDoc details;
96
97     /**
98      * The line number.
99      */

100     private int line;
101
102     /**
103      * The column number.
104      */

105     private int column;
106
107     /**
108      * Creates a new parse exception.
109      *
110      * @param type the parse error type
111      * @param info the additional information
112      * @param line the line number, or -1 for unknown
113      * @param column the column number, or -1 for unknown
114      */

115     public ParseException(int type,
116                           String JavaDoc info,
117                           int line,
118                           int column) {
119
120         this(type, info, null, line, column);
121     }
122
123     /**
124      * Creates a new parse exception. This constructor is only used
125      * to supply the detailed information array, which is only used
126      * for expected token errors. The list then contains descriptions
127      * of the expected tokens.
128      *
129      * @param type the parse error type
130      * @param info the additional information
131      * @param details the additional detailed information
132      * @param line the line number, or -1 for unknown
133      * @param column the column number, or -1 for unknown
134      */

135     public ParseException(int type,
136                           String JavaDoc info,
137                           ArrayList JavaDoc details,
138                           int line,
139                           int column) {
140
141         super();
142         this.type = type;
143         this.info = info;
144         this.details = details;
145         this.line = line;
146         this.column = column;
147     }
148
149     /**
150      * Returns the error type.
151      *
152      * @return the error type
153      */

154     public int getErrorType() {
155         return type;
156     }
157
158     /**
159      * Returns the additional error information.
160      *
161      * @return the additional error information
162      */

163     public String JavaDoc getInfo() {
164         return info;
165     }
166
167     /**
168      * Returns the additional detailed error information.
169      *
170      * @return the additional detailed error information
171      */

172     public ArrayList JavaDoc getDetails() {
173         return new ArrayList JavaDoc(details);
174     }
175
176     /**
177      * Returns the line number where the error occured.
178      *
179      * @return the line number of the error, or
180      * -1 if unknown
181      */

182     public int getLine() {
183         return line;
184     }
185
186     /**
187      * Returns the column number where the error occured.
188      *
189      * @return the column number of the error, or
190      * -1 if unknown
191      */

192     public int getColumn() {
193         return column;
194     }
195
196     /**
197      * Returns the detailed error message. This message will contain
198      * the same string as getErrorMessage(), but with line number and
199      * column number information appended.
200      *
201      * @return the detailed error message
202      */

203     public String JavaDoc getMessage() {
204         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
205
206         // Add error description
207
buffer.append(getErrorMessage());
208
209         // Add line and column
210
if (line > 0 && column > 0) {
211             buffer.append(", on line ");
212             buffer.append(line);
213             buffer.append(" column: ");
214             buffer.append(column);
215         }
216
217         return buffer.toString();
218     }
219
220     /**
221      * Returns the error message. This message will contain all the
222      * information available, except for the line and column number
223      * information.
224      *
225      * @return the error message
226      */

227     public String JavaDoc getErrorMessage() {
228         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
229
230         switch (type) {
231         case IO_ERROR:
232             buffer.append("I/O error: ");
233             buffer.append(info);
234             break;
235         case UNEXPECTED_EOF_ERROR:
236             buffer.append("unexpected end of file");
237             break;
238         case UNEXPECTED_CHAR_ERROR:
239             buffer.append("unexpected character '");
240             buffer.append(info);
241             buffer.append("'");
242             break;
243         case UNEXPECTED_TOKEN_ERROR:
244             buffer.append("unexpected token ");
245             buffer.append(info);
246             if (details != null) {
247                 buffer.append(", expected ");
248                 if (details.size() > 1) {
249                     buffer.append("one of ");
250                 }
251                 buffer.append(getMessageDetails());
252             }
253             break;
254         case INVALID_TOKEN_ERROR:
255             buffer.append(info);
256             break;
257         case ANALYSIS_ERROR:
258             buffer.append(info);
259             break;
260         default:
261             buffer.append("internal error");
262             if (info != null) {
263                 buffer.append(": ");
264                 buffer.append(info);
265             }
266         }
267
268         return buffer.toString();
269     }
270
271     /**
272      * Returns a string containing all the detailed information in
273      * a list. The elements are separated with a comma.
274      *
275      * @return the detailed information string
276      */

277     private String JavaDoc getMessageDetails() {
278         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
279
280         for (int i = 0; i < details.size(); i++) {
281             if (i > 0) {
282                 buffer.append(", ");
283                 if (i + 1 == details.size()) {
284                     buffer.append("or ");
285                 }
286             }
287             buffer.append(details.get(i));
288         }
289
290         return buffer.toString();
291     }
292 }
293
Popular Tags