checkpoint
[sbp.git] / src / edu / berkeley / sbp / Walk.java
index 04e36ae..a82cf52 100644 (file)
@@ -37,7 +37,13 @@ abstract class Walk<T> {
         else if (e instanceof Sequence) return sequence((Sequence)e);
         else if (e instanceof Union) {
             T ret = bottom(e);
-            for(Sequence s : (Union)e) ret = union((Union)e, ret, walk(s));
+            for(Sequence s : (Union)e) {
+                ret = union((Union)e, ret, walk(s));
+
+                // FIXME
+                for(Sequence ss : s.needs()) ret = union((Union)e, ret, walk(ss));
+                for(Sequence ss : s.hates()) ret = union((Union)e, ret, walk(ss));
+            }
             return ret;
         } else {
             throw new Error("unknown element of class " + e.getClass().getName() + ": " + e);
@@ -82,7 +88,7 @@ abstract class Walk<T> {
         public WalkTokenSet(Topology<Tok> cs)          { this.cs = cs; }
         public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
         public Topology<Tok> bottom(Element e)         { return cs; }
-        public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r); return cs; }
+        public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r.getTokenTopology()); return cs; }
     }
 
     static class First<Tok extends Input> extends WalkTokenSet<Tok> {
@@ -90,7 +96,7 @@ abstract class Walk<T> {
         public Topology<Tok> sequence(Sequence seq) {
             for(Position p = seq.firstp(); p!=null && !p.isLast(); p = p.next()) {
                 walk(p.element());
-                if (!p.element().possiblyEpsilon(c)) break;
+                if (!c.possiblyEpsilon(p.element())) break;
             }
             return cs;
         }
@@ -130,7 +136,7 @@ abstract class Walk<T> {
                 Position mp = null;
                 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
                     if (matched) cs = cs.union(c.first(pos.element(), cs.empty()));
-                    if (pos.isLast()) { matched = (matched && pos.element().possiblyEpsilon(c)); continue; }
+                    if (pos.isLast()) { matched = (matched && c.possiblyEpsilon(pos.element())); continue; }
                     boolean good = false;
                     if (e instanceof Atom) {
                         Topology top = c.atoms.get(pos.element());
@@ -149,7 +155,7 @@ abstract class Walk<T> {
 
             if (e instanceof Sequence) {
                 Sequence s = (Sequence)e;
-                if (s.noFollow() != null) cs = cs.minus(s.noFollow());
+                if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
             }
 
             if (c != null && e==me) {
@@ -175,5 +181,13 @@ abstract class Walk<T> {
         public <Tok extends Input> Topology<Tok> first(Element e, Topology<Tok> empty) {
             return new Walk.First<Tok>(empty, this).walk(e);
         }
+        final boolean possiblyEpsilon(Element e) {
+            Walk.Cache cache = this;
+            Boolean ret = possiblyEpsilon.get(e);
+            if (ret != null) return ret.booleanValue();
+            ret = new Walk.PossiblyEpsilon().walk(e) ? Boolean.TRUE : Boolean.FALSE;
+            possiblyEpsilon.put(e, ret);
+            return ret;
+        }
     }
 }