throw error when Forest created with head()
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index 43ffb10..1730f60 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.Sequence.Position;
@@ -21,7 +23,7 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
     public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
 
     static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
-        if (loc == null) throw new Error();
+        if (loc == null) throw new RuntimeException("invoked Forest.create(null,,,,) -- this should never happen");
         return new One<NodeType>(loc, head, children, lift);
     }
 
@@ -55,6 +57,7 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
             this.location = loc;
             this.head = head;
+            if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen");
             this.children = children==null ? emptyForestArray : new Forest[children.length];
             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
@@ -127,7 +130,6 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
     /** An "ambiguity node"; this is immutable once it has been "looked at" */
     static class Many<NodeType> extends Forest<NodeType> {
 
-        HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
         private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
         private boolean touched = false;
 
@@ -156,6 +158,14 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
         
         void gather(HashSet<Forest<NodeType>> ht) {
             touched();
+
+            // FIXME: do something more sensible here
+            if (ht.contains(this)) {
+                System.err.println("WARNING: grammar produced a circular forest\n" + this);
+                //throw new Error("grammar produced a circular forest:\n" + this);
+                return;
+            }
+
             ht.add(this);
             for(Forest<NodeType> f : hp) f.gather(ht);
         }