001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2004-2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.parser;
020:
021: import java.util.List;
022:
023: import xtc.Constants;
024:
025: import xtc.tree.Attribute;
026: import xtc.tree.Node;
027:
028: import xtc.type.Type;
029:
030: import xtc.util.Utilities;
031:
032: /**
033: * The superclass of full and partial productions.
034: *
035: * @author Robert Grimm
036: * @version $Revision: 1.36 $
037: */
038: public abstract class Production extends Node {
039:
040: /**
041: * The optional attribute list.
042: *
043: * <p />Note that while a production's attributes are represented as
044: * an ordered list, they should be treated as a set.
045: */
046: public List<Attribute> attributes;
047:
048: /** The production's declared type as a string. */
049: public String dType;
050:
051: /**
052: * The production's actual type, which must be filled in after
053: * parsing a module.
054: */
055: public Type type;
056:
057: /**
058: * The name. A production's name does not have any qualification
059: * when the production is parsed. However, after uniquely renaming
060: * all productions to avoid name conflicts in the generated parser,
061: * this name may have a qualifier.
062: *
063: * @see Analyzer#uniquify()
064: */
065: public NonTerminal name;
066:
067: /** The fully qualified name. */
068: public NonTerminal qName;
069:
070: /** The ordered choice. */
071: public OrderedChoice choice;
072:
073: /**
074: * Create a new production.
075: *
076: * @param attributes The attributes.
077: * @param dType The declared type.
078: * @param name The name.
079: * @param qName The fully qualified name.
080: * @param choice The choice.
081: */
082: public Production(List<Attribute> attributes, String dType,
083: NonTerminal name, NonTerminal qName, OrderedChoice choice) {
084: this .attributes = attributes;
085: this .dType = Utilities.withoutSpace(dType);
086: this .name = name;
087: this .qName = qName;
088: this .choice = choice;
089: }
090:
091: /**
092: * Create a new production.
093: *
094: * @param attributes The attributes.
095: * @param type The actual type.
096: * @param name The name.
097: * @param qName The fully qualified name.
098: * @param choice The choice.
099: */
100: public Production(List<Attribute> attributes, Type type,
101: NonTerminal name, NonTerminal qName, OrderedChoice choice) {
102: this .attributes = attributes;
103: this .type = type;
104: this .name = name;
105: this .qName = qName;
106: this .choice = choice;
107: }
108:
109: public int hashCode() {
110: return (null == qName) ? name.hashCode() : qName.hashCode();
111: }
112:
113: /**
114: * Determine whether this production is a {@link FullProduction full
115: * production}.
116: *
117: * @return <code>true</code> if this production is full.
118: */
119: public boolean isFull() {
120: return false;
121: }
122:
123: /**
124: * Determine whether this production is a {@link PartialProduction
125: * partial production}.
126: *
127: * @return <code>true</code> if this production is partial.
128: */
129: public boolean isPartial() {
130: return false;
131: }
132:
133: /**
134: * Determine whether this production is an {@link
135: * AlternativeAddition alternative addition}.
136: *
137: * @return <code>true</code> if this production is an alternative
138: * addition.
139: */
140: public boolean isAddition() {
141: return false;
142: }
143:
144: /**
145: * Determine whether this production is an {@link AlternativeRemoval
146: * alternative removal}.
147: *
148: * @return <code>true</code> if this production is an alternative
149: * removal.
150: */
151: public boolean isRemoval() {
152: return false;
153: }
154:
155: /**
156: * Determine whether this production is a {@link ProductionOverride
157: * production override}.
158: *
159: * @return <code>true</code> if this production is a production
160: * override.
161: */
162: public boolean isOverride() {
163: return false;
164: }
165:
166: /**
167: * Determine whether this production has the specified attribute.
168: *
169: * @param att The attribute.
170: * @return <code>true</code> if this production has the specified
171: * attribute.
172: */
173: public boolean hasAttribute(Attribute att) {
174: return ((null != attributes) && attributes.contains(att));
175: }
176:
177: /**
178: * Determine whether this production is memoized. This method is
179: * semantically equivalent to:
180: * <pre>
181: * hasAttribute(Constants.ATT_MEMOIZED) ||
182: * ((! hasAttribute(Constants.ATT_TRANSIENT)) &&
183: * (! hasAttribute(Constants.ATT_INLINE)))
184: * </pre>
185: *
186: * @return <code>true</code> if this production is memoized.
187: */
188: public boolean isMemoized() {
189: return ((null == attributes)
190: || attributes.contains(Constants.ATT_MEMOIZED) || ((!attributes
191: .contains(Constants.ATT_TRANSIENT)) && (!attributes
192: .contains(Constants.ATT_INLINE))));
193: }
194:
195: }
|