checkpoint
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index ee3fd4b..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();
 
@@ -36,6 +42,8 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     ////////////////////////////////////////////////////////////////////////////////
 
     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(); }
 
     Topology toAtom() {
@@ -45,9 +53,11 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     protected final Element[] elements;
 
+          HashSet<Sequence> needed;
+          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; }
@@ -56,6 +66,12 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     protected Sequence(Element[] elements, HashSet<Sequence> and, HashSet<Sequence> not) {
         this.needs = and==null ? new HashSet<Sequence>() : and;
         this.hates = not==null ? new HashSet<Sequence>() : not;
+        if (this.needs != null)
+            for(Sequence s : this.needs)
+                (s.needed==null?(s.needed=new HashSet<Sequence>()):s.needed).add(this);
+        if (this.hates != null)
+            for(Sequence s : this.hates)
+                (s.hated==null?(s.hated=new HashSet<Sequence>()):s.hated).add(this);
         this.elements = elements;
         this.firstp = new Position(0);
     }
@@ -66,19 +82,26 @@ 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;
     }
 
-    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 //////////////////////////////////////////////////////////////////////////////
 
     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
-    public class Position {
+    public class Position implements IntegerMappable {
+
+        private Forest zero = null;
+        public Forest zero() {
+            if (zero != null) return zero;
+            if (pos > 0) throw new Error();
+            return zero = rewrite(null);
+        }
+
 
                 final int      pos;
         private final Position next;
@@ -91,11 +114,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]; }
@@ -109,15 +127,21 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         /** true iff this Position is the last one in the sequence */
         public boolean isLast() { return next()==null; }
 
-        // Reduction /////////////////////////////////////////////////////////////////////////////////
+        // Position /////////////////////////////////////////////////////////////////////////////////
+
+        final <T> Forest<T> rewrite(Input.Location loc) {
+            if (this==firstp()) return epsilonForm();
+            return rewrite2(loc);
+        }
 
-        final <T> Forest<T> rewrite(Token.Location loc) {
-            if (this==firstp() && eps) return epsilonForm;
-            eps = true;
-            for(int i=pos; i<elements.length; i++) if (holder[i]==null) holder[i] = elements[i].epsilonForm();
+        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();
+                if (holder[i]==null) throw new Error("bad " + i);
+            }
             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;
         }
 
@@ -127,14 +151,16 @@ 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("}>");
             return ret.toString();
         }
+        private final int idx = master_position_idx++;
+        public int toInt() { return idx; }
     }
-
+    private static int master_position_idx = 0;
 
     // toString //////////////////////////////////////////////////////////////////////////////
 
@@ -154,8 +180,10 @@ 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) {
-            return (Forest<T>)Forest.leaf(loc, result, this);
+        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 {
             public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
@@ -170,28 +198,35 @@ 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(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; }
     }
 
-    static class Unwrap extends Sequence {
+    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) {
-            if (drops==null) return Forest.create(loc, null, args, this, true, false);
+        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;
             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);            
         }
     }
 
     static class RewritingSequence extends Sequence {
-        private final Object tag;
+        /*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);
@@ -199,12 +234,12 @@ 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];
             //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();