KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ProductionPatternElement.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 production pattern element. This class represents a reference to
26  * either a token or a production. Each element also contains minimum
27  * and maximum occurence counters, controlling the number of
28  * repetitions allowed. A production pattern element is always
29  * contained within a production pattern rule.
30  *
31  * @author Per Cederberg, <per at percederberg dot net>
32  * @version 1.0
33  */

34 public class ProductionPatternElement {
35
36     /**
37      * The token flag. This flag is true for token elements, and
38      * false for production elements.
39      */

40     private boolean token;
41
42     /**
43      * The node identity.
44      */

45     private int id;
46
47     /**
48      * The minimum occurance count.
49      */

50     private int min;
51
52     /**
53      * The maximum occurance count.
54      */

55     private int max;
56
57     /**
58      * The look-ahead set associated with this element.
59      */

60     private LookAheadSet lookAhead;
61
62     /**
63      * Creates a new element. If the maximum value if zero (0) or
64      * negative, it will be set to Integer.MAX_VALUE.
65      *
66      * @param isToken the token flag
67      * @param id the node identity
68      * @param min the minimum number of occurancies
69      * @param max the maximum number of occurancies, or
70      * negative for infinite
71      */

72     public ProductionPatternElement(boolean isToken,
73                                     int id,
74                                     int min,
75                                     int max) {
76
77         this.token = isToken;
78         this.id = id;
79         if (min < 0) {
80             min = 0;
81         }
82         this.min = min;
83         if (max <= 0) {
84             max = Integer.MAX_VALUE;
85         } else if (max < min) {
86             max = min;
87         }
88         this.max = max;
89         this.lookAhead = null;
90     }
91
92     /**
93      * Returns true if this element represents a token.
94      *
95      * @return true if the element is a token, or
96      * false otherwise
97      */

98     public boolean isToken() {
99         return token;
100     }
101
102     /**
103      * Returns true if this element represents a production.
104      *
105      * @return true if the element is a production, or
106      * false otherwise
107      */

108     public boolean isProduction() {
109         return !token;
110     }
111
112     /**
113      * Checks if a specific token matches this element. This method
114      * will only return true if this element is a token element, and
115      * the token has the same id and this element.
116      *
117      * @param token the token to check
118      *
119      * @return true if the token matches this element, or
120      * false otherwise
121      */

122     public boolean isMatch(Token token) {
123         return isToken() && token != null && token.getId() == id;
124     }
125
126     /**
127      * Returns the node identity.
128      *
129      * @return the node identity
130      */

131     public int getId() {
132         return id;
133     }
134
135     /**
136      * Returns the minimum occurence count.
137      *
138      * @return the minimum occurence count
139      */

140     public int getMinCount() {
141         return min;
142     }
143
144     /**
145      * Returns the maximum occurence count.
146      *
147      * @return the maximum occurence count
148      */

149     public int getMaxCount() {
150         return max;
151     }
152
153     /**
154      * Checks if this object is equal to another. This method only
155      * returns true for another identical production pattern element.
156      *
157      * @param obj the object to compare with
158      *
159      * @return true if the object is identical to this one, or
160      * false otherwise
161      */

162     public boolean equals(Object JavaDoc obj) {
163         ProductionPatternElement elem;
164
165         if (obj instanceof ProductionPatternElement) {
166             elem = (ProductionPatternElement) obj;
167             return this.token == elem.token
168                 && this.id == elem.id
169                 && this.min == elem.min
170                 && this.max == elem.max;
171         } else {
172             return false;
173         }
174     }
175
176     /**
177      * Returns a string representation of this object.
178      *
179      * @return a string representation of this object
180      */

181     public String JavaDoc toString() {
182         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183
184         buffer.append(id);
185         if (token) {
186             buffer.append("(Token)");
187         } else {
188             buffer.append("(Production)");
189         }
190         if (min != 1 || max != 1) {
191             buffer.append("{");
192             buffer.append(min);
193             buffer.append(",");
194             buffer.append(max);
195             buffer.append("}");
196         }
197         return buffer.toString();
198     }
199
200     /**
201      * Returns the look-ahead set associated with this alternative.
202      *
203      * @return the look-ahead set associated with this alternative
204      */

205     LookAheadSet getLookAhead() {
206         return lookAhead;
207     }
208
209     /**
210      * Sets the look-ahead set for this alternative.
211      *
212      * @param lookAhead the new look-ahead set
213      */

214     void setLookAhead(LookAheadSet lookAhead) {
215         this.lookAhead = lookAhead;
216     }
217 }
218
Popular Tags