KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TokenPattern.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 /**
25  * A token pattern. This class contains the definition of a token
26  * (i.e. it's pattern), and allows testing a string against this
27  * pattern. A token pattern is uniquely identified by an integer id,
28  * that must be provided upon creation.
29  *
30  * @author Per Cederberg, <per at percederberg dot net>
31  * @version 1.1
32  */

33 public class TokenPattern {
34
35     /**
36      * The string pattern type. This pattern type is used for tokens
37      * that only match an exact string.
38      */

39     public static final int STRING_TYPE = 1;
40
41     /**
42      * The regular expression pattern type. This pattern type is used
43      * for tokens that match a regular expression.
44      */

45     public static final int REGEXP_TYPE = 2;
46
47     /**
48      * The token pattern identity.
49      */

50     private int id;
51
52     /**
53      * The token pattern name.
54      */

55     private String JavaDoc name;
56
57     /**
58      * The token pattern type.
59      */

60     private int type;
61
62     /**
63      * The token pattern.
64      */

65     private String JavaDoc pattern;
66
67     /**
68      * The token error flag. If this flag is set, it means that an
69      * error should be reported if the token is found. The error
70      * message is present in the errorMessage variable.
71      *
72      * @see #errorMessage
73      */

74     private boolean error = false;
75
76     /**
77      * The token error message. This message will only be set if the
78      * token error flag is set.
79      *
80      * @see #error
81      */

82     private String JavaDoc errorMessage = null;
83
84     /**
85      * The token ignore flag. If this flag is set, it means that the
86      * token should be ignored if found. If an ignore message is
87      * present in the ignoreMessage variable, it will also be reported
88      * as a warning.
89      *
90      * @see #ignoreMessage
91      */

92     private boolean ignore = false;
93
94     /**
95      * The token ignore message. If this message is set when the token
96      * ignore flag is also set, a warning message will be printed if
97      * the token is found.
98      *
99      * @see #ignore
100      */

101     private String JavaDoc ignoreMessage = null;
102
103     /**
104      * Creates a new token pattern.
105      *
106      * @param id the token pattern id
107      * @param name the token pattern name
108      * @param type the token pattern type
109      * @param pattern the token pattern
110      */

111     public TokenPattern(int id, String JavaDoc name, int type, String JavaDoc pattern) {
112
113         this.id = id;
114         this.name = name;
115         this.type = type;
116         this.pattern = pattern;
117     }
118
119     /**
120      * Checks if the pattern corresponds to an error token. If this
121      * is true, it means that an error should be reported if a
122      * matching token is found.
123      *
124      * @return true if the pattern maps to an error token, or
125      * false otherwise
126      */

127     public boolean isError() {
128         return error;
129     }
130
131     /**
132      * Checks if the pattern corresponds to an ignored token. If this
133      * is true, it means that the token should be ignored if found.
134      *
135      * @return true if the pattern maps to an ignored token, or
136      * false otherwise
137      */

138     public boolean isIgnore() {
139         return ignore;
140     }
141
142     /**
143      * Returns the unique token pattern identity value.
144      *
145      * @return the token pattern id
146      */

147     public int getId() {
148         return id;
149     }
150
151     /**
152      * Returns the token pattern name.
153      *
154      * @return the token pattern name
155      */

156     public String JavaDoc getName() {
157         return name;
158     }
159
160     /**
161      * Returns the token pattern type.
162      *
163      * @return the token pattern type
164      *
165      * @see #STRING_TYPE
166      * @see #REGEXP_TYPE
167      */

168     public int getType() {
169         return type;
170     }
171
172     /**
173      * Returns te token pattern.
174      *
175      * @return the token pattern
176      */

177     public String JavaDoc getPattern() {
178         return pattern;
179     }
180
181     /**
182      * Returns the token error message if the pattern corresponds to
183      * an error token.
184      *
185      * @return the token error message
186      */

187     public String JavaDoc getErrorMessage() {
188         return errorMessage;
189     }
190
191     /**
192      * Returns the token ignore message if the pattern corresponds to
193      * an ignored token.
194      *
195      * @return the token ignore message
196      */

197     public String JavaDoc getIgnoreMessage() {
198         return ignoreMessage;
199     }
200
201     /**
202      * Sets the token error flag and assigns a default error message.
203      */

204     public void setError() {
205         setError("unrecognized token found");
206     }
207
208     /**
209      * Sets the token error flag and assigns the specified error
210      * message.
211      *
212      * @param message the error message to display
213      */

214     public void setError(String JavaDoc message) {
215         error = true;
216         errorMessage = message;
217     }
218
219     /**
220      * Sets the token ignore flag and clears the ignore message.
221      */

222     public void setIgnore() {
223         setIgnore(null);
224     }
225
226     /**
227      * Sets the token ignore flag and assigns the specified ignore
228      * message.
229      *
230      * @param message the ignore message to display
231      */

232     public void setIgnore(String JavaDoc message) {
233         ignore = true;
234         ignoreMessage = message;
235     }
236
237     /**
238      * Returns a detailed string representation of this object.
239      *
240      * @return a detailed string representation of this object
241      */

242     public String JavaDoc toString() {
243         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
244
245         buffer.append(name);
246         buffer.append(" (");
247         buffer.append(id);
248         buffer.append(") = ");
249         if (type == STRING_TYPE) {
250             buffer.append("\"");
251             buffer.append(pattern);
252             buffer.append("\"");
253         } else if (type == REGEXP_TYPE) {
254             buffer.append("<<");
255             buffer.append(pattern);
256             buffer.append(">>");
257         }
258         if (error) {
259             buffer.append(" ERROR: \"");
260             buffer.append(errorMessage);
261             buffer.append("\"");
262         }
263         if (ignore) {
264             buffer.append(" IGNORE");
265             if (ignoreMessage != null) {
266                 buffer.append(": \"");
267                 buffer.append(ignoreMessage);
268                 buffer.append("\"");
269             }
270         }
271
272         return buffer.toString();
273     }
274
275     /**
276      * Returns a short string representation of this object.
277      *
278      * @return a short string representation of this object
279      */

280     public String JavaDoc toShortString() {
281         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
282         int newline = pattern.indexOf('\n');
283
284         if (type == STRING_TYPE) {
285             buffer.append("\"");
286             if (newline >= 0) {
287                 if (newline > 0 && pattern.charAt(newline - 1) == '\r') {
288                     newline--;
289                 }
290                 buffer.append(pattern.substring(0, newline));
291                 buffer.append("(...)");
292             } else {
293                 buffer.append(pattern);
294             }
295             buffer.append("\"");
296         } else {
297             buffer.append("<");
298             buffer.append(name);
299             buffer.append(">");
300         }
301
302         return buffer.toString();
303     }
304 }
305
Popular Tags