KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ParserCreationException.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 parser creation exception. This exception is used for signalling
28  * an error in the token or production patterns, making it impossible
29  * to create a working parser or tokenizer.
30  *
31  * @author Per Cederberg, <per at percederberg dot net>
32  * @version 1.0
33  */

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

41     public static final int INTERNAL_ERROR = 0;
42
43     /**
44      * The invalid parser error type constant. This type is used when
45      * the parser as such is invalid. This error is typically caused
46      * by using a parser without any patterns.
47      */

48     public static final int INVALID_PARSER_ERROR = 1;
49
50     /**
51      * The invalid token error type constant. This type is used when a
52      * token pattern is erroneous. This error is typically caused by
53      * an invalid pattern type or an erroneous regular expression.
54      */

55     public static final int INVALID_TOKEN_ERROR = 2;
56
57     /**
58      * The invalid production error type constant. This type is used
59      * when a production pattern is erroneous. This error is typically
60      * caused by referencing undeclared productions, or violating some
61      * other production pattern constraint.
62      */

63     public static final int INVALID_PRODUCTION_ERROR = 3;
64
65     /**
66      * The infinite loop error type constant. This type is used when
67      * an infinite loop has been detected in the grammar. One of the
68      * productions in the loop will be reported.
69      */

70     public static final int INFINITE_LOOP_ERROR = 4;
71
72     /**
73      * The inherent ambiguity error type constant. This type is used
74      * when the set of production patterns (i.e. the grammar) contains
75      * ambiguities that cannot be resolved.
76      */

77     public static final int INHERENT_AMBIGUITY_ERROR = 5;
78
79     /**
80      * The error type.
81      */

82     private int type;
83
84     /**
85      * The token or production pattern name. This variable is only
86      * set for some error types.
87      */

88     private String JavaDoc name;
89
90     /**
91      * The additional error information string. This variable is only
92      * set for some error types.
93      */

94     private String JavaDoc info;
95
96     /**
97      * The error details list. This variable is only set for some
98      * error types.
99      */

100     private ArrayList JavaDoc details;
101
102     /**
103      * Creates a new parser creation exception.
104      *
105      * @param type the parse error type
106      * @param info the additional error information
107      */

108     public ParserCreationException(int type,
109                                    String JavaDoc info) {
110
111         this(type, null, info);
112     }
113
114     /**
115      * Creates a new parser creation exception.
116      *
117      * @param type the parse error type
118      * @param name the token or production pattern name
119      * @param info the additional error information
120      */

121     public ParserCreationException(int type,
122                                    String JavaDoc name,
123                                    String JavaDoc info) {
124
125         this(type, name, info, null);
126     }
127
128     /**
129      * Creates a new parser creation exception.
130      *
131      * @param type the parse error type
132      * @param name the token or production pattern name
133      * @param info the additional error information
134      * @param details the error details list
135      */

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

152     public int getErrorType() {
153         return type;
154     }
155
156     /**
157      * Returns the token or production name.
158      *
159      * @return the token or production name
160      */

161     public String JavaDoc getName() {
162         return name;
163     }
164
165     /**
166      * Returns the additional error information.
167      *
168      * @return the additional error information
169      */

170     public String JavaDoc getInfo() {
171         return info;
172     }
173
174     /**
175      * Returns the detailed error information as a string
176      *
177      * @return the detailed error information
178      */

179     public String JavaDoc getDetails() {
180         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
181
182         if (details == null) {
183             return null;
184         }
185         for (int i = 0; i < details.size(); i++) {
186             if (i > 0) {
187                 buffer.append(", ");
188                 if (i + 1 == details.size()) {
189                     buffer.append("and ");
190                 }
191             }
192             buffer.append(details.get(i));
193         }
194
195         return buffer.toString();
196     }
197
198     /**
199      * Returns the error message. This message will contain all the
200      * information available.
201      *
202      * @return the error message
203      */

204     public String JavaDoc getMessage() {
205         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
206
207         switch (type) {
208         case INVALID_PARSER_ERROR:
209             buffer.append("parser is invalid, as ");
210             buffer.append(info);
211             break;
212         case INVALID_TOKEN_ERROR:
213             buffer.append("token '");
214             buffer.append(name);
215             buffer.append("' is invalid, as ");
216             buffer.append(info);
217             break;
218         case INVALID_PRODUCTION_ERROR:
219             buffer.append("production '");
220             buffer.append(name);
221             buffer.append("' is invalid, as ");
222             buffer.append(info);
223             break;
224         case INFINITE_LOOP_ERROR:
225             buffer.append("infinite loop found in production pattern '");
226             buffer.append(name);
227             buffer.append("'");
228             break;
229         case INHERENT_AMBIGUITY_ERROR:
230             buffer.append("inherent ambiguity in production '");
231             buffer.append(name);
232             buffer.append("'");
233             if (info != null) {
234                 buffer.append(" ");
235                 buffer.append(info);
236             }
237             if (details != null) {
238                 buffer.append(" starting with ");
239                 if (details.size() > 1) {
240                     buffer.append("tokens ");
241                 } else {
242                     buffer.append("token ");
243                 }
244                 buffer.append(getDetails());
245             }
246             break;
247         default:
248             buffer.append("internal error");
249         }
250
251         return buffer.toString();
252     }
253 }
254
Popular Tags