UnwrapLeft, error reporting improvements
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 968d78f..c41be4f 100644 (file)
@@ -54,6 +54,11 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
             ? new Unwrap(e, head, drop)
             : new RewritingSequence(head, e, drop);
     }
+    public static Sequence createLeft(Object head, Element[] e, boolean[] drop, boolean foster) {
+        return foster
+            ? new UnwrapLeft(e, head, drop)
+            : new RewritingSequence(head, e, drop);
+    }
 
     /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
     public Sequence and(Sequence s) {
@@ -256,6 +261,27 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
         }
     }
 
+    static class UnwrapLeft extends Sequence {
+        private boolean[] drops;
+        private final Object tag;
+        public UnwrapLeft(Element[] e, Object tag)                  { this(e, tag, null); }
+        public UnwrapLeft(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
+        Sequence _clone() { return new UnwrapLeft(elements, tag, drops); }
+        public <T> Forest<T> postReduce(Input.Region 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, (T)tag, args, false, true);
+            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, (T)tag, args2, false, true);
+        }
+        Forest epsilonForm(Input.Region loc, Grammar cache) {
+            return Forest.create(loc, tag, new Forest[0], false);
+        }
+    }
+
     static class RewritingSequence extends Sequence {
         private Object tag;
         private final boolean[] drops;