checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index e5db6c8..5ee8433 100644 (file)
@@ -7,18 +7,29 @@ import java.util.*;
 import java.lang.reflect.*;
 import java.lang.ref.*;
 
-/** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
+/** <font color=green>juxtaposition; zero or more adjacent Elements; can specify a rewriting</font> */
 public abstract class Sequence extends Element implements Iterable<Element> {
 
+    protected final Element[] elements;
+
+    final HashSet<Sequence> hated  = new HashSet<Sequence>();
+
+    final HashSet<Sequence> needs  = new HashSet<Sequence>();
+    final HashSet<Sequence> hates  = new HashSet<Sequence>();
+
+    final Position          firstp;
+
+    public Atom follow = null;
+    public final Topology follow() { return follow; }
+
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
     abstract Sequence _clone();
     Sequence dup() {
         Sequence ret = _clone();
-        for(Sequence s : needs) { ret.needs.add(s); s.needed.add(ret); }
+        for(Sequence s : needs) { ret.needs.add(s); }
         for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
         ret.follow = follow;
-        ret.lame = lame;
         return ret;
     }
 
@@ -26,7 +37,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     public static final Sequence empty = new Sequence.Constant.Empty();
 
     /** after matching the sequence, do not add anything to the output tree */
-    public static Sequence drop(Element[] e, boolean lame) { return new Constant.Drop(e, lame); }
+    public static Sequence drop(Element[] e) { return new Constant.Drop(e); }
 
     /** after matching the sequence, insert a constant into the output tree */
     public static Sequence constant(Element[] e, Object o) { return new Constant(e, o); }
@@ -50,27 +61,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     ////////////////////////////////////////////////////////////////////////////////
 
-    public Element follow = null;
-    public final Topology follow() { return follow==null ? null : Atom.toAtom(follow); }
-
-    Topology toAtom() {
-        if (elements.length!=1)
-            throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
-        return Atom.toAtom(elements[0]);
-    }
-
-    public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; }
+    public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); return ret; }
     public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; }
 
-    protected final Element[] elements;
-
-    final HashSet<Sequence> needed = new HashSet<Sequence>();
-    final HashSet<Sequence> hated  = new HashSet<Sequence>();
-    final HashSet<Sequence> needs  = new HashSet<Sequence>();
-    final HashSet<Sequence> hates  = new HashSet<Sequence>();
-    public boolean           lame  = false;
+    public Iterable<Sequence> needs() { return needs; }
+    public Iterable<Sequence> hates() { return hates; }
 
-    final Position          firstp;
     Position firstp() { return firstp; }
 
     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
@@ -80,10 +76,10 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     }
 
     // DO NOT MESS WITH THE FOLLOWING LINE!!!
-    private Forest.Ref epsilonForm = null;
+    private Forest.Many epsilonForm = null;
     Forest epsilonForm() {
         if (epsilonForm!=null) return epsilonForm;
-        epsilonForm = new Forest.Ref();
+        epsilonForm = new Forest.Many();
         epsilonForm.merge(firstp().rewrite(null, false));
         return epsilonForm;
     }
@@ -187,15 +183,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             return (Forest<T>)Forest.create(loc, result, null, false);
         }
         static class Drop extends Constant {
-            Sequence _clone() { return new Drop(elements, lame); }
-            public Drop(Element[] e, boolean lame) {
-                super(e, null);
-                this.lame = lame;
-            }
+            Sequence _clone() { return new Drop(elements); }
+            public Drop(Element[] e) { super(e, null); }
         }
         static class Empty extends Sequence.Constant.Drop {
             Sequence _clone() { return new Empty(); }
-            public Empty() { super(new Element[] { }, false); } }
+            public Empty() { super(new Element[] { }); } }
     }
 
     static class Singleton extends Sequence {