cleanups, reorg, and commenting
[sbp.git] / src / edu / berkeley / sbp / Union.java
index 87aefda..c164262 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.*;
@@ -7,105 +9,123 @@ import java.util.*;
 import java.lang.reflect.*;
 import java.lang.ref.*;
 
-/** an element which can produce one of several alternatives */
+/**
+ *  <font color=green>an element which can produce one of several alternatives</font>.
+ *  <p>
+ *
+ *  Unlike the other Elements, Union is not immutable once
+ *  constructed.  To simulate this desirable feature, it is immutable
+ *  <i>once examined</i> by taking its iterator or calling contains().
+ */
 public class Union extends Element implements Iterable<Sequence> {
 
-    final String shortForm;
-    final boolean synthetic;
-    private final List<Sequence> alternatives = new ArrayList<Sequence>();
+    private final String  name;
+    private final boolean synthetic;
+    private boolean viewed = false;
 
-    public Iterator<Sequence> iterator() { return alternatives.iterator(); }
-    public boolean contains(Sequence s) { return alternatives.contains(s); }
+    private final List<Sequence> alternatives = new ArrayList<Sequence>();
 
-    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);
-        for(Sequence n : s.needs) add(n);
-        for(Sequence n : s.hates) add(n);
-        if (/*!synthetic &&*/ shortForm!=null
-            //&& Character.isUpperCase(shortForm.charAt(0))
-            )
-            s.setName(toString());
-    }
+    public Union(String name) { this(name, false); }
+    public Union(String name, Sequence s) { this(name, s, false); }
+    public Union(String name, Sequence s, boolean synthetic) { this(name, synthetic); add(s); }
 
     /**
      *  Since every cycle in a non-degenerate grammar contains at
      *  least one Union, every instance of this class must be able to
      *  display itself in both "long form" (list of the long forms of
-     *  its alternatives) and "short form" (some abbreviation).
+     *  its alternatives) and "short form" (some name).
      *
-     *  @param shortForm the "short form" display; usually 
+     *  @param shortForm the "short form" display; for display purposes only
      *  @param synthetic if true, this Union's "long form" is "obvious" and should not be displayed when printing the grammar
      */
-    public Union(String shortForm) { this(shortForm, false); }
-    public Union(String shortForm, boolean synthetic) {
-        this.shortForm = shortForm;
+    public Union(String name, boolean synthetic) {
+        this.name = name;
         this.synthetic = synthetic;
     }
 
-    public static Union epsilon = new Union("()");
-    static { epsilon.add(Sequence.empty); }
+    public boolean contains(Sequence s) {
+        viewed = true;
+        return alternatives.contains(s);
+    }
+
+    /** iterator over this Union's Sequences */
+    public Iterator<Sequence> iterator() {
+        viewed = true;
+        return alternatives.iterator();
+    }
+
+    /** adds an alternative */
+    public Union add(Sequence s) {
+        if (viewed)
+            throw new RuntimeException("once Union.contains() or Union.iterator() has been invoked, "+
+                                       "you may not add any more Sequences to it\n  "+
+                                       "  union in question: " + this);
+        if (s.needed_or_hated)
+            throw new RuntimeException("you may not add a conjunct directly to a Union");
+        s.in_a_union = true;
+        if (alternatives.contains(s)) return this;
+        alternatives.add(s);
+        return this;
+    }
+
+    /** adds a one-element sequence */
+    public void add(Element e) {
+        add(Sequence.create(e));
+    }
 
-    private Forest.Ref epsilonForm = null;
-    Forest epsilonForm() {
-        if (epsilonForm != null) return epsilonForm;
-        epsilonForm = new Forest.Ref();
+    /** the Forest which results from matching this Union against the empty string at region <tt>region</tt> */
+    Forest epsilonForm(Input.Region region, Cache cache) {
+        viewed = true;
+        Forest.Many epsilonForm = new Forest.Many();
         for(Sequence s : this)
-            if (s.possiblyEpsilon(null))
-                epsilonForm.merge(s.epsilonForm());
+            if (cache.possiblyEpsilon(s))
+                epsilonForm.merge(s.epsilonForm(region, cache));
         return epsilonForm;
     }
 
+
     // Display //////////////////////////////////////////////////////////////////////////////
 
-    public String toString() { return shortForm; }
-    private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
-    public void toString(StringBuffer sb) {
-        if (synthetic) return;
+    boolean isSynthetic() { return synthetic; }
+    String getName()      { return name==null ? "(anon_union)" : name; }
+
+    public String toString() {
+        // technically this should be turned on, but we don't make a big deal
+        //viewed = true;
+        if (name != null) return name;
+        StringBuffer sb = new StringBuffer();
+        sb.append("(");
+        bodyToString(sb, "", " | ");
+        sb.append(")");
+        return sb.toString();
+    }
+
+    /** display this union in long/expanded form */
+    public StringBuffer toString(StringBuffer sb) {
+        // technically this should be turned on, but we don't make a big deal
+        //viewed = true;
+        if (synthetic) return sb;
         boolean first = true;
+        String before = StringUtil.pad(15, getName()) + " = ";
         if (alternatives.size()==0) {
-            sb.append(pad(15, shortForm) + "::= ");
-        } else for(Sequence s : this) {
-            sb.append(pad(15, first ? shortForm : "") + (first ? "::= " : "  | "));
-            first = false;
-            sb.append(s.toString());
+            sb.append(before);
+        } else {
+            bodyToString(sb,
+                         before,
+                         "\n" + StringUtil.pad(15, "")        + " | ");
             sb.append('\n');
         }
-        sb.append('\n');
+        return sb;
     }
-
-    // SubUnion //////////////////////////////////////////////////////////////////////////////
-
-    /** FIXME this is kind of a hack */
-    public class Subset extends Union {
-        private final Set<Sequence> exclude;
-        public Subset(String shortForm, Set<Sequence> exclude) { super(shortForm, true); this.exclude = exclude; }
-        public Iterator<Sequence> iterator() {
-            final Iterator<Sequence> it = Union.this.iterator();
-            return new Iterator<Sequence>() {
-                private Sequence next = it.hasNext() ? it.next() : null;
-                public boolean hasNext() { return next != null; }
-                public Sequence next() {
-                    Sequence ret = next;
-                    do {
-                        next = it.hasNext() ? it.next() : null;
-                    } while (next != null && (next instanceof Sequence) && exclude.contains((Sequence)next));
-                    return ret;
-                }
-                public void remove() { throw new Error(); }
-            };
+    
+    private void bodyToString(StringBuffer sb, String before, String between) {
+        viewed = true;
+        boolean first = true;
+        for(Sequence s : this) {
+            // FIXME: what to do here about printing out negated sequences?
+            sb.append(first ? before : between);
+            first = false;
+            sb.append(s.toString());
         }
     }