checkpoint harmony
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 065150c..d274013 100644 (file)
@@ -51,7 +51,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
           HashSet<Sequence> hated;
     final HashSet<Sequence> needs;
     final HashSet<Sequence> hates;
-          boolean           lame  = false;
+    public boolean           lame  = false;
 
     final Position          firstp;
     Position firstp() { return firstp; }
@@ -76,8 +76,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     Forest epsilonForm() {
         if (epsilonForm==null) {
             epsilonForm = new Forest.Ref();
-            Forest fo = firstp().rewrite(null);
-            epsilonForm.merge(fo);
+            epsilonForm.merge(firstp().rewrite2(null));
         }
         return epsilonForm;
     }
@@ -101,11 +100,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         }
 
         boolean isFirst() { return pos==0; }
-        boolean isRightNullable(Walk.Cache cache) {
-            if (isLast()) return true;
-            if (!element().possiblyEpsilon(cache)) return false;
-            return next().isRightNullable(cache);
-        }
 
         /** the element immediately after this Position, or null if this is the last Position */
         public Element  element() { return pos>=elements.length ? null : elements[pos]; }
@@ -122,8 +116,11 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         // Reduction /////////////////////////////////////////////////////////////////////////////////
 
         final <T> Forest<T> rewrite(Token.Location loc) {
-            if (this==firstp() && eps) return epsilonForm;
-            eps = true;
+            if (this==firstp()) return epsilonForm();
+            return rewrite2(loc);
+        }
+
+        final <T> Forest<T> rewrite2(Token.Location loc) {
             for(int i=0; i<pos; i++) if (holder[i]==null) throw new Error("realbad " + i);
             for(int i=pos; i<elements.length; i++) {
                 if (holder[i]==null) holder[i] = elements[i].epsilonForm();
@@ -131,7 +128,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             }
             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;
         }
 
@@ -141,7 +137,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
                 ret.append(' ');
                 if (p==this) ret.append(" | ");
-                if (p.element()!=null) ret.append(p.element().possiblyEpsilon(null) ? "["+p.element()+"]" : p.element());
+                if (p.element()!=null) ret.append(p.element());
                 else                   ret.append(' ');
             }
             ret.append("}>");
@@ -169,7 +165,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final Object result;
         public Constant(Element[] e, Object result, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.result = result; }
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
-            return (Forest<T>)Forest.leaf(loc, result, this);
+            return (Forest<T>)Forest.leaf(loc, result);
         }
         static class Drop extends Constant {
             public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
@@ -184,7 +180,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final int idx;
         public Singleton(Element e, HashSet<Sequence> and, HashSet<Sequence> not) { this(new Element[] { e }, 0, and, not); }
         public Singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.idx = idx; }
-        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
+        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
     }
 
     public static class Unwrap extends Sequence {
@@ -193,13 +189,13 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public Unwrap(Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) { super(e, and, not); this.drops = drops; }
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
             for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
-            if (drops==null) return Forest.create(loc, null, args, this, true, false);
+            if (drops==null) return Forest.create(loc, null, args, true, false);
             int count = 0;
             for(int i=0; i<drops.length; i++) if (!drops[i]) count++;
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];
-            return Forest.create(loc, null, args2, this, true, false);            
+            return Forest.create(loc, null, args2, true, false);            
         }
     }
 
@@ -219,7 +215,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             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);
+            return Forest.create(loc, (T)tag, args2, false, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {
             int len = sb.length();