Use ordinal pre-sorting rather than on-the-fly compares
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 610af58..a029274 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.*;
@@ -7,87 +9,130 @@ import java.util.*;
 import java.lang.reflect.*;
 import java.lang.ref.*;
 
-/** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
-public abstract class Sequence extends Element implements Iterable<Element> {
+/** <font color=green>juxtaposition; zero or more adjacent Elements; can specify a rewriting</font> */
+public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
+
+    protected final Element[] elements;
+
+    public boolean needed_or_hated = false;
+
+    public HashMap<Sequence,Boolean> canNeed = new HashMap<Sequence,Boolean>();
+    public HashMap<Sequence,Boolean> canKill = new HashMap<Sequence,Boolean>();
+
+    final HashSet<Sequence> hated   = new HashSet<Sequence>();
+
+    final HashSet<Sequence> needs  = new HashSet<Sequence>();
+    final HashSet<Sequence> hates  = new HashSet<Sequence>();
+
+    final Position          firstp;
+
+    Atom follow = null;
 
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
-    /** the empty sequence (matches the empty string) */
-    public static final Sequence empty = new Sequence.Constant.Empty();
+    abstract Sequence _clone();
+    Sequence dup() {
+        Sequence ret = _clone();
+        for(Sequence s : needs) { ret.needs.add(s); }
+        for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
+        ret.follow = follow;
+        return ret;
+    }
+
+    /** create an empty sequence (matches the empty string) */
+    public static Sequence create() { return new Sequence.Constant.Empty(); }
 
-    /** after matching the sequence, do not add anything to the output tree */
-    public static Sequence drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) { return new Constant.Drop(e, and, not, lame); }
+    /** create a sequence of one element */
+    public static Sequence create(Element e) { return create(new Element[] { e }, 0); }
 
-    /** after matching the sequence, insert a constant into the output tree */
-    public static Sequence constant(Element[] e, Object o, HashSet<Sequence> and, HashSet<Sequence> not) { return new Constant(e, o, and, not); }
+    /** create a sequence which drops the result of all but one of its element */
+    public static Sequence create(Element[] e, int which) { return new Singleton(e, which); }
 
-    /** after matching the sequence, place the result of the <tt>idx</tt>th match in the output tree */
-    public static Sequence singleton(Element[] e, int idx, HashSet<Sequence> and, HashSet<Sequence> not) { return new Singleton(e, idx, and, not); }
+    /** create a sequence which always evaluates to a constant result  */
+    public static Sequence create(Element[] e, Object result) { return new Constant(e, result); }
 
     /**
-     *  after matching the sequence, create the specified output tree
-     *  @param tag   the tag for the output tree
-     *  @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
+     *  create a sequence (general form)
+     *  @param head   the head of the output tree
+     *  @param e      the elements to match
+     *  @param drop   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
+     *  @param foster if true, all children of the last child (ie
+     *                grandchildren) are promoted to children of this
+     *                node; this is very useful for matching repetitions
      **/
-    public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops, HashSet<Sequence> and, HashSet<Sequence> not) {
-        return new RewritingSequence(tag, e, drops, and, not); }
+    public static Sequence create(Object head, Element[] e, boolean[] drop, boolean foster) {
+        return foster
+            ? new Unwrap(e, head, drop)
+            : new RewritingSequence(head, e, drop);
+    }
 
     ////////////////////////////////////////////////////////////////////////////////
 
-    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(); }
+    /** return a new sequence identical to this one, but with a positive conjunct <tt>s</tt> */
+    public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed_or_hated=true; return ret; }
 
-    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 a new sequence identical to this one, but with a negative conjunct <tt>s</tt> */
+    public Sequence andnot(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); s.needed_or_hated=true; return ret; }
 
-    protected final Element[] elements;
+    /** return a new sequence identical to this one, but with a follow-set restricted to <tt>a</tt> */
+    public Sequence followedBy(Atom a) { Sequence ret = dup(); ret.follow = a; return ret; }
 
-          HashSet<Sequence> needed;
-          HashSet<Sequence> hated;
-    final HashSet<Sequence> needs;
-    final HashSet<Sequence> hates;
-    public boolean           lame  = false;
+    boolean hatesAny(Iterable<Sequence> it) {
+        if (hates.isEmpty()) return false;
+        for(Sequence s : it) if (hates.contains(s)) return true;
+        return false;
+    }
+
+    Iterable<Sequence> needs() { return needs; }
+    Iterable<Sequence> hates() { return hates; }
 
-    final Position          firstp;
     Position firstp() { return firstp; }
 
     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
-    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);
+    protected Sequence(Element[] elements) {
         this.elements = elements;
         this.firstp = new Position(0);
     }
 
     // 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));
-        }
+    private Forest.Many epsilonForm = null;
+    Forest epsilonForm(Input.Region loc) {
+        if (epsilonForm!=null) return epsilonForm;
+        epsilonForm = new Forest.Many();
+        epsilonForm.merge(firstp().rewrite(loc, false));
         return epsilonForm;
     }
 
-    protected abstract <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args);
+    protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
 
     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
-    public class Position {
+    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();
+            return zero = rewrite(reg);
+        }
+
 
                 final int      pos;
         private final Position next;
@@ -100,11 +145,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]; }
@@ -118,21 +158,18 @@ 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(Token.Location loc) {
-            if (this==firstp()) return epsilonForm();
-            return rewrite2(loc);
-        }
-
-        final <T> Forest<T> rewrite2(Token.Location loc) {
+        final <T> Forest<T> rewrite(Input.Region loc) { return rewrite(loc, true); }
+        private final <T> Forest<T> rewrite(Input.Region loc, boolean epsilonCheck) {
+            if (epsilonCheck && this==firstp()) return epsilonForm(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) holder[i] = elements[i].epsilonForm(loc);
                 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
+            Forest<T> ret = Sequence.this.postReduce(loc, holder, this);
+            //for(int k=0; k<pos; k++) holder[k] = null; // to help GC
             return ret;
         }
 
@@ -142,14 +179,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 //////////////////////////////////////////////////////////////////////////////
 
@@ -160,6 +199,18 @@ public abstract class Sequence extends Element implements Iterable<Element> {
             sb.append(elements[i].toString());
             sb.append(' ');
         }
+        if (follow != null) {
+            sb.append("-> ");
+            sb.append(follow);
+        }
+        for(Sequence s : needs) {
+            sb.append("& ");
+            sb.append(s);
+        }
+        for(Sequence s : hates) {
+            sb.append("&~ ");
+            sb.append(s);
+        }
         return sb;
     }
 
@@ -168,68 +219,90 @@ 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 Constant(Element[] e, Object result) { super(e); this.result = result; }
+        Sequence _clone() { return new Constant(elements, result); }
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
+            return (Forest<T>)Forest.create(loc, result, null, false);
         }
         static class Drop extends Constant {
-            public Drop(Element[] e, HashSet<Sequence> and, HashSet<Sequence> not, boolean lame) {
-                super(e, null, and, not);
-                this.lame = lame;
-            }
+            Sequence _clone() { return new Drop(elements); }
+            public Drop(Element[] e) { super(e, null); }
         }
-        static class Empty extends Sequence.Constant.Drop { public Empty() { super(new Element[] { }, null, null, false); } }
+        static class Empty extends Sequence.Constant.Drop {
+            Sequence _clone() { return new Empty(); }
+            public Empty() { super(new Element[] { }); } }
     }
 
     static class Singleton extends Sequence {
         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 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.Region loc, Forest<T>[] args, Position p) { return args[idx]; }
+        Sequence _clone() { return new Singleton(elements,idx); }
     }
 
-    public static class Unwrap extends Sequence {
+    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) {
+        private final Object tag;
+        public Unwrap(Element[] e, Object tag)                  { super(e); this.drops = null; this.tag = tag; }
+        public Unwrap(Element[] e, Object tag, boolean[] drops) { super(e); this.drops = drops; this.tag = tag; }
+        Sequence _clone() { return new Unwrap(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, null, args, this, true, false);
+            if (drops==null) return Forest.create(loc, (T)tag, args, 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, null, args2, this, true, false);            
+            return Forest.create(loc, (T)tag, args2, true);
+        }
+    }
+
+
+
+    static class RegionRewritingSequence extends RewritingSequence {
+        private Functor<Input.Region, Object> tagf;
+        public RegionRewritingSequence(Functor<Input.Region,Object> tagfunctor, Element[] e, boolean[] drops) {
+            super(null, e, drops);
+            this.tagf = tagfunctor;
+        }
+        public <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p) {
+            this.tag = tagf.invoke(loc);
+            Forest<T> ret = super.postReduce(loc, args, p);
+            this.tag = null;
+            return ret;
         }
     }
 
     static class RewritingSequence extends Sequence {
-        /*private*/public final Object tag;
+        /*private*/public /*final*/ Object tag;
         private final boolean[] drops;
         private int count = 0;
-        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);
+        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) {
+            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++;
         }
-        public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) {
+        public <T> Forest<T> postReduce(Input.Region 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, this, false, false);
+            return Forest.create(loc, (T)tag, args2, false);
         }
         public StringBuffer toString(StringBuffer sb, boolean spacing) {
             int len = sb.length();
+            if (tag != null)
+                sb.append("\""+StringUtil.escapify(tag.toString(),"\"\r\n")+"\":: ");
             super.toString(sb, spacing);
             len = sb.length()-len;
             if (spacing) for(int i=0; i<50-len; i++) sb.append(' ');
-            sb.append(" => ");
-            sb.append(tag);
             return sb;
         }
     }
+
 }