checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index fcff530..ee3fd4b 100644 (file)
@@ -35,6 +35,14 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     ////////////////////////////////////////////////////////////////////////////////
 
+    public Element noFollow = null;
+    public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); }
+
+    Topology toAtom() {
+        if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
+        return elements[0].toAtom();
+    }
+
     protected final Element[] elements;
 
     final HashSet<Sequence> needs;
@@ -48,15 +56,21 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
         this.needs = and==null ? new HashSet<Sequence>() : and;
         this.hates = not==null ? new HashSet<Sequence>() : not;
-        //for(Sequence s : needs) s.lame = true;
-        //for(Sequence s : hates) s.lame = true;
         this.elements = elements;
         this.firstp = new Position(0);
     }
 
-    void reachable(HashSet<Position> h) { firstp().reachable(h); }
-
-    Forest epsilonForm() { return firstp().rewrite(null); }
+    // DO NOT MESS WITH THE FOLLOWING LINE!!!
+    private Forest.Ref epsilonForm = null;
+    private boolean eps = false;
+    Forest epsilonForm() {
+        if (epsilonForm==null) {
+            epsilonForm = new Forest.Ref();
+            Forest fo = firstp().rewrite(null);
+            epsilonForm.merge(fo);
+        }
+        return epsilonForm;
+    }
 
     protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
 
@@ -66,12 +80,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
     public class Position {
 
-        void reachable(HashSet<Position> h) {
-            if (h.contains(this)) return;
-            h.add(this);
-            if (element() != null) element().reachable(h);
-        }
-
                 final int      pos;
         private final Position next;
                 final Forest[] holder;
@@ -103,10 +111,13 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Reduction /////////////////////////////////////////////////////////////////////////////////
 
-        <T> Forest<T> rewrite(Token.Location loc) {
+        final <T> Forest<T> rewrite(Token.Location loc) {
+            if (this==firstp() && eps) return epsilonForm;
+            eps = true;
             for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
             Forest<T> ret = Sequence.this.postReduce(loc, holder);
             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
+            if (this==firstp()) { if (epsilonForm==null) epsilonForm=new Forest.Ref(); epsilonForm.merge(ret); return epsilonForm; }
             return ret;
         }
 
@@ -192,6 +203,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
+            //System.out.println("reduce \""+tag+"\"");
             return Forest.create(loc, (T)tag, args2, this, false, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {