checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index 13ac199..6437341 100644 (file)
@@ -24,7 +24,7 @@ public abstract class Forest<T> {
     static <T> Forest<T>   singleton(Token.Location loc, Sequence creator)                       { return create(loc, null, new Forest[] { }, creator, false, true); }
     static <T> Forest<T>   singleton(Token.Location loc, Forest<T> body, Sequence creator)       { return create(loc, null, new Forest[] { body }, creator, false, true); }
     static <T> Forest<T>   leaf(Token.Location loc, T tag,                     Sequence creator) { return create(loc, tag, null, creator, false, false); }
-    static <T> Forest<T>   create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
+    public static <T> Forest<T>   create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
         return new MultiForest<T>(loc, tag, tokens, creator, unwrap, singleton);
     }
 
@@ -32,7 +32,7 @@ public abstract class Forest<T> {
 
     protected static class Body<T> {
 
-        private final Token.Location          location;
+        private final Token.Location    location;
         private final T                 tag;
         private final Forest<T>[]       tokens;
         private final Sequence          creator;
@@ -144,7 +144,7 @@ public abstract class Forest<T> {
         }
         public Iterator<Body<T>> iterator() { return ((IterableForest<T>)resolve()).iterator(); }
         public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
-        public boolean valid() { resolve(); return valid; }
+        public boolean valid() { if (valid) return true; resolve(); return valid; }
         public String toString() { return resolve().toString(); }
         public Forest resolve() {
             if (hp==null) return res;
@@ -156,11 +156,13 @@ public abstract class Forest<T> {
                     else results = new HashSet<Body>();
                 }
             if (results != null) {
-                for(Forest<?> p : hp) for(Body<?> b : (IterableForest<?>)p) results.add(b);
+                for(Forest<?> p : hp)
+                    for(Body<?> b : (IterableForest<?>)p)
+                        results.add(b);
                 for(Body b : results) {
                     if (b.keep() && (b.creator==null || !b.creator.lame)) continue;
-                    if (!b.keep(results)) continue;
                     if (b.creator!=null && b.creator.lame) continue;
+                    if (!b.keep(results)) continue;
                     valid = true;
                     b.addTo(nh);
                 }
@@ -197,6 +199,11 @@ public abstract class Forest<T> {
         public String toString() {
             if (toString != null) return toString;
             StringBuffer ret = new StringBuffer();
+            if (results.size()==1) {
+                for(Forest.Body<T> r : results)
+                    ret.append(r);
+                return toString = ret.toString();
+            }
             ret.append("<?");
             boolean first = true;
             for(Forest.Body<T> r : results) {