added decent error reporting
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 83d9b6f..065150c 100644 (file)
@@ -36,6 +36,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,6 +47,8 @@ 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;
@@ -56,6 +60,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);
     }
@@ -177,7 +187,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
         public <T> Forest<T> postReduce(Token.Location loc, Forest<T>[] args) { return (Forest<T>)Forest.singleton(loc, args[idx], this); }
     }
 
-    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; }
@@ -194,7 +204,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     }
 
     static class RewritingSequence extends Sequence {
-        private 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); }