removed illegal use of double-star
[sbp.git] / src / edu / berkeley / sbp / Union.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.lang.ref.*;
9
10 /** an element which can produce one of several alternatives */
11 public class Union extends Element implements Iterable<Sequence> {
12
13     final String shortForm;
14     final boolean synthetic;
15     private final List<Sequence> alternatives = new ArrayList<Sequence>();
16
17     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
18
19     void reachable(HashSet<Sequence.Position> h) { for(Sequence s : alternatives) s.reachable(h); }
20
21     Topology toAtom() {
22         if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
23         Topology ret = null;
24         for(Sequence s : this) {
25             Topology a = s.toAtom();
26             if (a==null) continue;
27             if (ret==null) ret = a.dup();
28             else           ret.add(a.dup());
29         }
30         return ret;
31     }
32
33     /** adds an alternative */
34     public void add(Sequence s) { alternatives.add(s); }
35
36     /**
37      *  Since every cycle in a non-degenerate grammar contains at
38      *  least one Union, every instance of this class must be able to
39      *  display itself in both "long form" (list of the long forms of
40      *  its alternatives) and "short form" (some abbreviation).
41      *
42      *  @param shortForm the "short form" display; usually 
43      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
44      */
45     public Union(String shortForm) { this(shortForm, false); }
46     public Union(String shortForm, boolean synthetic) {
47         this.shortForm = shortForm;
48         this.synthetic = synthetic;
49     }
50
51     public static Union epsilon = new Union("()");
52     static { epsilon.add(Sequence.empty); }
53
54     private Forest.Ref epsilonForm = null;
55     Forest epsilonForm() {
56         if (epsilonForm != null) return epsilonForm;
57         epsilonForm = new Forest.Ref();
58         for(Sequence s : this)
59             if (s.possiblyEpsilon(null))
60                 epsilonForm.merge(s.epsilonForm());
61         return epsilonForm;
62     }
63
64     // Display //////////////////////////////////////////////////////////////////////////////
65
66     public String toString() { return shortForm; }
67     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
68     public void toString(StringBuffer sb) {
69         if (synthetic) return;
70         boolean first = true;
71         if (alternatives.size()==0) {
72             sb.append(pad(15, shortForm) + "::= ");
73         } else for(Sequence s : this) {
74             sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
75             first = false;
76             sb.append(s.toString());
77             sb.append('\n');
78         }
79         sb.append('\n');
80     }
81
82     // SubUnion //////////////////////////////////////////////////////////////////////////////
83
84     /** FIXME this is kind of a hack */
85     public class Subset extends Union {
86         private final Set<Sequence> exclude;
87         public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
88         public Iterator<Sequence> iterator() {
89             final Iterator<Sequence> it = Union.this.iterator();
90             return new Iterator<Sequence>() {
91                 private Sequence next = it.hasNext() ? it.next() : null;
92                 public boolean hasNext() { return next != null; }
93                 public Sequence next() {
94                     Sequence ret = next;
95                     do {
96                         next = it.hasNext() ? it.next() : null;
97                     } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
98                     return ret;
99                 }
100                 public void remove() { throw new Error(); }
101             };
102         }
103     }
104
105 }