checkpoint harmony
[sbp.git] / src / edu / berkeley / sbp / Union.java
index df6c374..b690450 100644 (file)
@@ -15,11 +15,28 @@ public class Union extends Element implements Iterable<Sequence> {
     private final List<Sequence> alternatives = new ArrayList<Sequence>();
 
     public Iterator<Sequence> iterator() { return alternatives.iterator(); }
+    public boolean contains(Sequence s) { return alternatives.contains(s); }
 
-    void reachable(HashSet<Sequence.Position> h) { for(Sequence s : alternatives) s.reachable(h); }
+    Topology toAtom() {
+        if (alternatives.size()==0) throw new RuntimeException("cannot build an Atom from a Union with no productions");
+        Topology ret = null;
+        for(Sequence s : this) {
+            Topology a = s.toAtom();
+            if (ret==null) ret = a;
+            else           ret = ret.union(a);
+        }
+        if (ret==null) throw new RuntimeException("confusion on " + this);
+        return ret;
+    }
 
     /** adds an alternative */
-    public void add(Sequence s) { alternatives.add(s); }
+    public void add(Sequence s) {
+        alternatives.add(s);
+        if (/*!synthetic &&*/ shortForm!=null
+            //&& Character.isUpperCase(shortForm.charAt(0))
+            )
+            s.setName(toString());
+    }
 
     /**
      *  Since every cycle in a non-degenerate grammar contains at
@@ -36,6 +53,9 @@ public class Union extends Element implements Iterable<Sequence> {
         this.synthetic = synthetic;
     }
 
+    public static Union epsilon = new Union("()");
+    static { epsilon.add(Sequence.empty); }
+
     private Forest.Ref epsilonForm = null;
     Forest epsilonForm() {
         if (epsilonForm != null) return epsilonForm;
@@ -50,7 +70,7 @@ public class Union extends Element implements Iterable<Sequence> {
 
     public String toString() { return shortForm; }
     private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
-    void toString(StringBuffer sb) {
+    public void toString(StringBuffer sb) {
         if (synthetic) return;
         boolean first = true;
         if (alternatives.size()==0) {