01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2005, 2006 Robert Grimm
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program 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
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.parser;
20:
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import xtc.tree.Node;
25:
26: /**
27: * A grammar represented as a collection of modules.
28: *
29: * @author Robert Grimm
30: * @version $Revision: 1.26 $
31: */
32: public class Grammar extends Node {
33:
34: /**
35: * The list of {@link Module modules}. The first module in this
36: * list is the grammar's main module and the following modules are
37: * all the dependent modules.
38: */
39: public List<Module> modules;
40:
41: /**
42: * Create a new grammar.
43: *
44: * @param modules The list of modules.
45: */
46: public Grammar(List<Module> modules) {
47: this .modules = modules;
48: }
49:
50: /**
51: * Remove the specified module from this grammar. This method uses
52: * reference equality to locate the module to replace.
53: *
54: * @param module The module.
55: * @throws IllegalArgumentException
56: * Signals that the specified module is not part of this grammar.
57: */
58: public void remove(final Module module) {
59: for (Iterator<Module> iter = modules.iterator(); iter.hasNext();) {
60: if (module == iter.next()) {
61: iter.remove();
62: return;
63: }
64: }
65: throw new IllegalArgumentException("Module " + module
66: + " not part of grammar");
67: }
68:
69: /**
70: * Replace the specified module. This method uses reference
71: * equality to locate the module to replace.
72: *
73: * @param oldModule The old module.
74: * @param newModule The new module.
75: * @throws IllegalArgumentException
76: * Signals that the specified old module is not part of this grammar.
77: */
78: public void replace(final Module oldModule, final Module newModule) {
79: final int length = modules.size();
80: for (int i = 0; i < length; i++) {
81: if (oldModule == modules.get(i)) {
82: modules.set(i, newModule);
83: return;
84: }
85: }
86: throw new IllegalArgumentException("Module " + oldModule
87: + " not part of grammar");
88: }
89:
90: }
|