01: package org.kohsuke.rngom.digested;
02:
03: import java.util.HashMap;
04: import java.util.Iterator;
05: import java.util.Map;
06:
07: /**
08: * <grammar> pattern, which is a collection of named patterns.
09: *
10: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
11: */
12: public class DGrammarPattern extends DPattern implements
13: Iterable<DDefine> {
14: private final Map<String, DDefine> patterns = new HashMap<String, DDefine>();
15:
16: DPattern start;
17:
18: /**
19: * Gets the start pattern.
20: */
21: public DPattern getStart() {
22: return start;
23: }
24:
25: /**
26: * Gets the named pattern by its name.
27: *
28: * @return
29: * null if not found.
30: */
31: public DDefine get(String name) {
32: return patterns.get(name);
33: }
34:
35: DDefine getOrAdd(String name) {
36: if (patterns.containsKey(name)) {
37: return get(name);
38: } else {
39: DDefine d = new DDefine(name);
40: patterns.put(name, d);
41: return d;
42: }
43: }
44:
45: /**
46: * Iterates all the {@link DDefine}s in this grammar.
47: */
48: public Iterator<DDefine> iterator() {
49: return patterns.values().iterator();
50: }
51:
52: public boolean isNullable() {
53: return start.isNullable();
54: }
55:
56: public <V> V accept(DPatternVisitor<V> visitor) {
57: return visitor.onGrammar(this);
58: }
59: }
|