checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index a8c44f0..ceb67eb 100644 (file)
@@ -11,19 +11,26 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
-        Iterator<Tree<T>> it = expand(true).iterator();
-        if (!it.hasNext()) throw new ParseFailed();
-        return it.next();
+        try {
+            Iterator<Tree<T>> it = expand(true).iterator();
+            if (!it.hasNext()) throw new ParseFailed();
+            return it.next();
+        } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
     }
 
     /** expand this forest into a set of trees */
     public HashSet<Tree<T>> expand(boolean toss) {
         final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
         visit(new TreeMaker2<T>(toss, ret), null, null);
-        if (toss && ret.size() > 1) throw new Ambiguous(this);
+        if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
         return ret;
     }
 
+    private static class InnerAmbiguous extends RuntimeException {
+        public final Forest<?> f;
+        public InnerAmbiguous(Forest<?> f) { this.f = f; }
+    }
+
     public static interface TreeConsumer<T> {
         public void addTree(Tree<T> t);
     }