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.Comment;
027: import xtc.tree.Node;
028:
029: /**
030: * A grammar module.
031: *
032: * @author Robert Grimm
033: * @version $Revision: 1.19 $
034: */
035: public class Module extends Node {
036:
037: /** The option documentation comment. */
038: public Comment documentation;
039:
040: /** The module name. */
041: public ModuleName name;
042:
043: /** The optional list of parameters. */
044: public ModuleList parameters = null;
045:
046: /** The optional list of {@link ModuleDependency Module dependencies}. */
047: public List<ModuleDependency> dependencies = null;
048:
049: /**
050: * The auxiliary field referencing this module's module
051: * modification, if the list of dependencies contains it.
052: */
053: public ModuleModification modification = null;
054:
055: /** The optional initial action code. */
056: public Action header = null;
057:
058: /** The optional main action code. */
059: public Action body = null;
060:
061: /** The optional final action code. */
062: public Action footer = null;
063:
064: /**
065: * The optional attribute list. Note that while a module's
066: * attributes are represented as a list, they should be treated as a
067: * set.
068: */
069: public List<Attribute> attributes = null;
070:
071: /** The list of productions. */
072: public List<Production> productions;
073:
074: /** Create a new grammar module. */
075: public Module() { /* Nothing to do. */
076: }
077:
078: /**
079: * Create a new grammar module.
080: *
081: * @param documentation The documentation.
082: * @param name The module name.
083: * @param parameters The list of parameters.
084: * @param dependencies The list of dependencies.
085: * @param header The header.
086: * @param body The body.
087: * @param footer The footer.
088: * @param attributes The list of attributes.
089: * @param productions The list of productions.
090: */
091: public Module(Comment documentation, ModuleName name,
092: ModuleList parameters, List<ModuleDependency> dependencies,
093: Action header, Action body, Action footer,
094: List<Attribute> attributes, List<Production> productions) {
095: this .documentation = documentation;
096: this .name = name;
097: this .parameters = parameters;
098: this .dependencies = dependencies;
099: this .header = header;
100: this .body = body;
101: this .footer = footer;
102: this .attributes = attributes;
103: this .productions = productions;
104: }
105:
106: /**
107: * Determine whether this module has the specified attribute.
108: *
109: * @param att The attribute.
110: * @return <code>true</code> if this module has the specified
111: * attribute.
112: */
113: public boolean hasAttribute(Attribute att) {
114: return ((null != attributes) && attributes.contains(att));
115: }
116:
117: /**
118: * Determine whether this module has an attribute with the
119: * specified name.
120: *
121: * @param name The name.
122: * @return <code>true</code> if this module has an attribute
123: * with the specified name.
124: */
125: public boolean hasAttribute(String name) {
126: return null != Attribute.get(name, attributes);
127: }
128:
129: /**
130: * Get the value of the attribute with the specified name.
131: *
132: * @param name The name.
133: * @return The corresponding attribute's value.
134: */
135: public Object getAttributeValue(String name) {
136: return Attribute.get(name, attributes).getValue();
137: }
138:
139: /**
140: * Get the class name for this grammar module. If this grammar
141: * module has a {@link Constants#NAME_PARSER parser} attribute, the
142: * class name is the value of that attribute. Otherwise, it is the
143: * module name.
144: *
145: * @return The class name.
146: */
147: public String getClassName() {
148: Attribute att = Attribute
149: .get(Constants.NAME_PARSER, attributes);
150: return null == att ? name.name : (String) att.getValue();
151: }
152:
153: }
|