MAJOR: huge revamp of Sequence, Result, Reduction, Parser, Node, GSS
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 2c36c8d..3e260f1 100644 (file)
@@ -98,7 +98,10 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
     protected Sequence(Element[] elements) {
         this.elements = elements;
-        this.firstp = new Position(0);
+        for(int i=0; i<elements.length; i++)
+            if (elements[i]==null)
+                throw new RuntimeException("cannot have nulls in a sequence: " + this);
+        this.firstp = new Position(0, null);
     }
 
     // DO NOT MESS WITH THE FOLLOWING LINE!!!
@@ -119,35 +122,22 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
     class Position implements IntegerMappable {
 
         public int ord = -1;
-        public int compareTo(Position p, Walk.Cache cache) {
-            Position position = this;
-            Position rposition = p;
-            int ret = 0;
-            if (Reduction.canKill(cache, position, rposition) &&
-                Reduction.canKill(cache, rposition, position)) throw new Error();
-            if      (Reduction.canKill(cache, position,   rposition)) ret =  1;
-            else if (Reduction.canKill(cache, rposition, position)) ret = -1;
-            if      (Reduction.canNeed(cache, position,   rposition)) ret =  1;
-            else if (Reduction.canNeed(cache, rposition, position)) ret = -1;
-            return ret;
-        }
 
         private Forest zero = null;
         public Forest zero(Input.Region reg) {
             if (zero != null) return zero;
-            if (pos > 0) throw new Error();
+            if (pos > 0) throw new RuntimeException("Position.zero(): pos>0");
             return zero = rewrite(reg);
         }
 
-
                 final int      pos;
         private final Position next;
         private final Position prev;
                 final Forest[] holder;
         
-        private Position(int pos) {
+        private Position(int pos, Position prev) {
             this.pos      = pos;
-            this.next     = pos==elements.length ? null : new Position(pos+1);
+            this.next     = pos==elements.length ? null : new Position(pos+1, this);
             this.holder   = new Forest[elements.length];
             this.prev     = prev;
         }
@@ -178,9 +168,7 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
                 if (holder[i]==null) holder[i] = elements[i].epsilonForm(loc);
                 if (holder[i]==null) throw new Error("bad " + i);
             }
-            Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
-            //for(int k=0; k<pos; k++) holder[k] = null; // to help GC
-            return ret;
+            return Sequence.this.postReduce(loc, holder, this);
         }
 
         public String   toString() {
@@ -283,7 +271,6 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
             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, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {