checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 2eddfc4..fb1262f 100644 (file)
@@ -12,8 +12,6 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
-    public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; }
-    public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; }
     abstract Sequence _clone();
     Sequence dup() {
         Sequence ret = _clone();
@@ -41,20 +39,22 @@ public abstract class Sequence extends Element implements Iterable<Element> {
      *  @param e     the elements to match
      *  @param drops only elements of <tt>e</tt> whose corresponding <tt>boolean</tt> in <tt>drops</tt> is <i>false</i> will be included in the output tree
      **/
-    public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) { return new RewritingSequence(tag, e, drops); }
+    public static Sequence rewritingSequence(Object tag, Element[] e, Object[] labs, boolean[] drops) {
+        return new RewritingSequence(tag, e, labs, drops); }
 
     ////////////////////////////////////////////////////////////////////////////////
 
     public Element noFollow = null;
-    public String name = null;
-    public void setName(String name) { this.name = name; }
-    public final Topology noFollow() { return noFollow==null ? null : noFollow.toAtom(); }
+    public final Topology noFollow() { return noFollow==null ? null : Atom.toAtom(noFollow); }
 
     Topology toAtom() {
         if (elements.length!=1) throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
-        return elements[0].toAtom();
+        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 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>();
@@ -74,16 +74,14 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     // 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();
-            epsilonForm.merge(firstp().rewrite2(null));
-        }
+        if (epsilonForm!=null) return epsilonForm;
+        epsilonForm = new Forest.Ref();
+        epsilonForm.merge(firstp().rewrite(null, false));
         return epsilonForm;
     }
 
-    protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args);
+    protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p);
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
@@ -125,18 +123,15 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Position /////////////////////////////////////////////////////////////////////////////////
 
-        final <T> Forest<T> rewrite(Input.Location loc) {
-            if (this==firstp()) return epsilonForm();
-            return rewrite2(loc);
-        }
-
-        final <T> Forest<T> rewrite2(Input.Location loc) {
+        final <T> Forest<T> rewrite(Input.Location loc) { return rewrite(loc, true); }
+        private final <T> Forest<T> rewrite(Input.Location loc, boolean epsilonCheck) {
+            if (epsilonCheck && this==firstp()) return epsilonForm();
             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();
                 if (holder[i]==null) throw new Error("bad " + i);
             }
-            Forest<T> ret = Sequence.this.postReduce(loc, holder);
+            Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
             for(int k=0; k<pos; k++) holder[k] = null; // to help GC
             return ret;
         }
@@ -177,8 +172,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final Object result;
         public Constant(Element[] e, Object result) { super(e); this.result = result; }
         Sequence _clone() { return new Constant(elements, result); }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
-            return (Forest<T>)Forest.leaf(loc, result);
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
+            return (Forest<T>)Forest.leaf(loc, result, p);
         }
         static class Drop extends Constant {
             Sequence _clone() { return new Drop(elements, lame); }
@@ -196,7 +191,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         private final int idx;
         public Singleton(Element e) { this(new Element[] { e }, 0); }
         public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) { return (Forest<T>)Forest.singleton(loc, args[idx], p); }
         Sequence _clone() { return new Singleton(elements,idx); }
     }
 
@@ -205,36 +200,38 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public Unwrap(Element[] e)                  { super(e); this.drops = null; }
         public Unwrap(Element[] e, boolean[] drops) { super(e); this.drops = drops; }
         Sequence _clone() { return new Unwrap(elements, drops); }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
             for(int i=0; i<args.length; i++) if (args[i]==null) throw new Error();
-            if (drops==null) return Forest.create(loc, null, args, true, false);
+            if (drops==null) return Forest.create(loc, null, args, new Object[args.length], true, false, p);
             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, true, false);            
+            return Forest.create(loc, null, args2, new Object[args.length], true, false, p);
         }
     }
 
     static class RewritingSequence extends Sequence {
         /*private*/public final Object tag;
         private final boolean[] drops;
+        private final Object[] labs;
         private int count = 0;
-        Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
-        public RewritingSequence(Object tag, Element[] e) { this(tag, e, null); }
-        public RewritingSequence(Object tag, Element[] e, boolean[] drops) {
+        Sequence _clone() { return new RewritingSequence(tag, elements, labs, drops); }
+        public RewritingSequence(Object tag, Element[] e, Object[] labs) { this(tag, e, labs, null); }
+        public RewritingSequence(Object tag, Element[] e, Object[] labs, boolean[] drops) {
             super(e);
             this.tag = tag;
             this.drops = drops == null ? new boolean[e.length] : drops;
             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
+            this.labs = labs;
         }
-        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args, Position p) {
             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, false);
+            return Forest.create(loc, (T)tag, args2, labs, false, false, p);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {
             int len = sb.length();