checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index ac34264..68e311e 100644 (file)
@@ -12,6 +12,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
+    public abstract Sequence and(Sequence s);
+    public abstract Sequence not(Sequence s);
+
+    private void needs(Sequence s) { s.needed.add(this); needs.add(s); }
+    private void hates(Sequence s) { s.hated.add(this); hates.add(s); }
+
     /** the empty sequence (matches the empty string) */
     public static final Sequence empty = new Sequence.Constant.Empty();
 
@@ -81,7 +87,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         return epsilonForm;
     }
 
-    protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
+    protected abstract <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args);
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
@@ -123,12 +129,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
         // Position /////////////////////////////////////////////////////////////////////////////////
 
-        final <T> Forest<T> rewrite(Token.Location loc) {
+        final <T> Forest<T> rewrite(Input.Location loc) {
             if (this==firstp()) return epsilonForm();
             return rewrite2(loc);
         }
 
-        final <T> Forest<T> rewrite2(Token.Location loc) {
+        final <T> Forest<T> rewrite2(Input.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();
@@ -174,7 +180,9 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     static class Constant extends Sequence {
         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) {
+        public Sequence and(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.needs(s); return ret; }
+        public Sequence not(Sequence s) { Sequence ret = new Constant(elements, result, needs, hates); ret.hates(s); return ret; }
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
             return (Forest<T>)Forest.leaf(loc, result);
         }
         static class Drop extends Constant {
@@ -190,14 +198,18 @@ 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]); }
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx]); }
+        public Sequence and(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.needs(s); return ret; }
+        public Sequence not(Sequence s) { Sequence ret = new Singleton(elements, idx, needs, hates); ret.hates(s); return ret; }
     }
 
     public static class Unwrap extends Sequence {
         private boolean[] drops;
         public Unwrap(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not)                  { super(e, and, not); this.drops = null; }
         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) {
+        public Sequence and(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.needs(s); return ret; }
+        public Sequence not(Sequence s) { Sequence ret = new Unwrap(elements, drops, needs, hates); ret.hates(s); return ret; }
+        public <T> Forest<T> postReduce(Input.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, true, false);
             int count = 0;
@@ -213,6 +225,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         /*private*/public final Object tag;
         private final boolean[] drops;
         private int count = 0;
+        public Sequence and(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.needs(s); return ret; }
+        public Sequence not(Sequence s) { Sequence ret = new RewritingSequence(tag, elements, drops, needs, hates); ret.hates(s); return ret; }
         public RewritingSequence(Object tag, Element[] e, HashSet<Sequence> and, HashSet<Sequence> not) { this(tag, e, null, and, not); }
         public RewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
             super(e, and, not);
@@ -220,7 +234,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             this.drops = drops == null ? new boolean[e.length] : drops;
             for(int i=0; i<this.drops.length; i++) if (!this.drops[i]) count++;
         }
-        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+        public <T> Forest<T> postReduce(Input.Location loc, Forest<T>[] args) {
             Forest<T>[] args2 = new Forest[count];
             int j = 0;
             for(int i=0; i<args.length; i++) if (!drops[i]) args2[j++] = args[i];