01: package ro.infoiasi.donald.compiler.cfg;
02:
03: import java.util.*;
04:
05: public class Terminals {
06: private Map termByName = new HashMap();
07: private List termByIndex = new ArrayList();
08:
09: public Terminal addNew(String name, String type) {
10: Terminal term = new Terminal(name, count(), type);
11: if (termByName.containsKey(name)) {
12: throw new RuntimeException("Duplicate Terminal (" + name
13: + ")");
14: }
15: termByName.put(name, term);
16: termByIndex.add(term);
17: return term;
18: }
19:
20: public Terminal addNew(String name) {
21: return addNew(name, null);
22: }
23:
24: /** End of input. */
25: public final Terminal EOF = find("EOF") != null ? find("EOF")
26: : addNew("EOF");
27:
28: public int count() {
29: return termByIndex.size();
30: }
31:
32: public Iterator iterator() {
33: return Collections.unmodifiableList(termByIndex).iterator();
34: }
35:
36: public Terminal find(String name) {
37: return (Terminal) termByName.get(name);
38: }
39:
40: public Terminal find(int index) {
41: if (index < 0 || index >= count()) {
42: return null;
43: } else {
44: return (Terminal) termByIndex.get(index);
45: }
46: }
47:
48: public void clear() {
49: termByName.clear();
50: termByIndex.clear();
51: }
52:
53: public String toString() {
54: StringBuffer sb = new StringBuffer();
55: Iterator it = iterator();
56: while (it.hasNext()) {
57: sb.append(it.next());
58: if (it.hasNext()) {
59: sb.append(", ");
60: }
61: }
62: return sb.toString();
63: }
64: }
|