factored exceptions into non-inner classes
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index e675f63..917fea5 100644 (file)
@@ -10,21 +10,20 @@ import java.lang.reflect.*;
 public abstract class Forest<T> {
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
 public abstract class Forest<T> {
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
-    public final Tree<T> expand1() throws Parser.Ambiguous, Parser.Failed {
+    public final Tree<T> expand1() throws Ambiguous, ParseFailed {
         Iterator<Tree<T>> it = expand(true).iterator();
         Iterator<Tree<T>> it = expand(true).iterator();
-        if (!it.hasNext()) throw new Parser.Failed();
+        if (!it.hasNext()) throw new ParseFailed();
         return it.next();
     }
 
     /** expand this forest into a set of trees */
     public abstract HashSet<Tree<T>>  expand(boolean toss);
         return it.next();
     }
 
     /** expand this forest into a set of trees */
     public abstract HashSet<Tree<T>>  expand(boolean toss);
-    public abstract boolean empty();
 
 
-    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); }
-    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);
+    static        <T> Forest<T> singleton(Token.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
+    static        <T> Forest<T> singleton(Token.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
+    static        <T> Forest<T> leaf(Token.Location loc, T tag) { return create(loc, tag, null, false, false); }
+    public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+        return new MultiForest<T>(loc, tag, tokens, unwrap, singleton);
     }
 
     // Body //////////////////////////////////////////////////////////////////////////////
     }
 
     // Body //////////////////////////////////////////////////////////////////////////////
@@ -34,17 +33,15 @@ public abstract class Forest<T> {
         private final Token.Location    location;
         private final T                 tag;
         private final Forest<T>[]       tokens;
         private final Token.Location    location;
         private final T                 tag;
         private final Forest<T>[]       tokens;
-        private final Sequence          creator;
         private final boolean           unwrap;
         private final boolean           singleton;
 
         private final boolean           unwrap;
         private final boolean           singleton;
 
-        private Body(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
+        private Body(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
-            this.creator = creator;
             this.unwrap = unwrap;
             this.singleton = singleton;
         }
             this.unwrap = unwrap;
             this.singleton = singleton;
         }
@@ -74,43 +71,32 @@ public abstract class Forest<T> {
             }
             return h;
         }
             }
             return h;
         }
-        void addTo(HashSet<Body> h) {
-            if (!singleton) h.add(this);
-            else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
-        }
+
         void addTo(FastSet<Body> h) {
             if (!singleton) h.add(this, true);
             else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
         }
 
         void addTo(FastSet<Body> h) {
             if (!singleton) h.add(this, true);
             else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
         }
 
-
-        private boolean rep = false;
         public String toString() {
         public String toString() {
-            if (rep) return "***";
-            try {
-                rep = true;
-                StringBuffer ret = new StringBuffer();
-                for(int i=0; i<tokens.length; i++) {
-                    String q = tokens[i]==null ? "null" : tokens[i].toString();
-                    if (q.length() > 0) {
-                        ret.append(q);
-                        ret.append(" ");
-                    }
+            StringBuffer ret = new StringBuffer();
+            for(int i=0; i<tokens.length; i++) {
+                String q = tokens[i]==null ? "null" : tokens[i].toString();
+                if (q.length() > 0) {
+                    ret.append(q);
+                    ret.append(" ");
                 }
                 }
-                String tail = ret.toString().trim();
-                String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : "";
-                if (tail.length() > 0) tail = "{" + tail + "}";
-                return head + tail;
-            } finally {
-                rep = false;
             }
             }
+            String tail = ret.toString().trim();
+            String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : "";
+            if (tail.length() > 0) tail = "{" + tail + "}";
+            return head + tail;
         }
     }
 
 
     // Ref //////////////////////////////////////////////////////////////////////////////
 
         }
     }
 
 
     // Ref //////////////////////////////////////////////////////////////////////////////
 
-    static abstract class IterableForest<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
+    private static abstract class IterableForest<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
         public abstract Iterator<Forest.Body<T>> iterator();
     }
 
         public abstract Iterator<Forest.Body<T>> iterator();
     }
 
@@ -120,11 +106,6 @@ public abstract class Forest<T> {
      *  viewed, it becomes immutable
      */
     static class Ref<T> extends IterableForest<T> {
      *  viewed, it becomes immutable
      */
     static class Ref<T> extends IterableForest<T> {
-        public boolean empty() {
-            if (res!=null) return res.empty();
-            for(Forest f : hp) if (!f.empty()) return false;
-            return true;
-        }
         private FastSet<Forest> hp = new FastSet<Forest>();
         private Forest res = null;
         public Ref() { }
         private FastSet<Forest> hp = new FastSet<Forest>();
         private Forest res = null;
         public Ref() { }
@@ -151,11 +132,10 @@ public abstract class Forest<T> {
     // Implementations //////////////////////////////////////////////////////////////////////////////
 
     private static class MultiForest<T> extends IterableForest<T> {
     // Implementations //////////////////////////////////////////////////////////////////////////////
 
     private static class MultiForest<T> extends IterableForest<T> {
-        public boolean empty() { return results.size()>0; }
         private final FastSet<Body<T>> results;
         private MultiForest(FastSet<Body<T>> results) { this.results = results; }
         private final FastSet<Body<T>> results;
         private MultiForest(FastSet<Body<T>> results) { this.results = results; }
-        public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
-            this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, creator, unwrap, singleton));
+        public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+            this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, unwrap, singleton));
         }
         public Iterator<Body<T>> iterator() { return results.iterator(); }
 
         }
         public Iterator<Body<T>> iterator() { return results.iterator(); }
 
@@ -163,7 +143,7 @@ public abstract class Forest<T> {
             HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
             for(Body<T> b : results)
                 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
             HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
             for(Body<T> b : results)
                 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
-            if (toss && ret.size() > 1) throw new Parser.Ambiguous(this);
+            if (toss && ret.size() > 1) throw new Ambiguous(this);
             return ret;
         }
         
             return ret;
         }
         
@@ -193,6 +173,5 @@ public abstract class Forest<T> {
     // Statics //////////////////////////////////////////////////////////////////////////////
 
     private static Tree[] tree_hint = new Tree[0];
     // Statics //////////////////////////////////////////////////////////////////////////////
 
     private static Tree[] tree_hint = new Tree[0];
-    private static Body[] body_hint = new Body[0];
     private static final Forest[] emptyForestArray = new Forest[0];
 }
     private static final Forest[] emptyForestArray = new Forest[0];
 }