+ // Many //////////////////////////////////////////////////////////////////////////////
+
+ /** An "ambiguity node"; this is immutable once it has been "looked at" */
+ static class Many<NodeType> extends Forest<NodeType> {
+
+ private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
+ private boolean touched = false;
+
+ public Many() { }
+
+ public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
+
+ Forest<NodeType> simplify() throws Ambiguous {
+ touched();
+ if (hp.size() > 1) {
+ HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
+ Iterator<Forest<NodeType>> ih = hp.iterator();
+ ih.next().gather(hf0);
+ for(Forest<NodeType> f : hp) {
+ HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
+ f.gather(hf1);
+ hf0.retainAll(hf1);
+ }
+ HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
+ expand(ht, hf0, new Tree(null, "*"));
+ throw new Ambiguous((Forest<?>)this,
+ (HashSet<Tree<?>>)(Object)ht);
+ }
+
+ return hp.iterator().next();
+ }
+
+ public Tree<NodeType> expand1() throws Ambiguous {
+ return simplify().expand1();
+ }
+
+ 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);
+ }
+
+ private void touched() {
+ if (touched) return;
+ touched = true;
+ /*
+ FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
+ for(Forest f : hp)
+ if (f instanceof Forest.One) f2.add(f);
+ else for(Forest ff : ((Forest.Many<NodeType>)f))
+ f2.add(ff);
+ hp = f2;
+ */
+ }
+ public boolean contains(Forest f) {
+ touched();
+ return hp.contains(f);
+ }
+ public void merge(Forest p) {
+ if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
+ if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
+ hp.add(p, true);
+ }
+ boolean ambiguous() {
+ touched();
+ if (hp.size()==0) return false;
+ if (hp.size()==1) return hp.iterator().next().ambiguous();
+ return true;
+ }
+
+ void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
+ touched();
+ if (ignore.contains(this)) { ht.add(bogus); return; }
+ for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
+ }
+
+
+ // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
+
+ public boolean isTransparent() { return hp.size()==1; }
+ public boolean isHidden() { return hp.size()==0; }
+ public void edges(GraphViz.StateNode n) {
+ if (hp.size()==1) { hp.iterator().next().edges(n); return; }
+ for(Forest f : hp) f.edges(n);
+ }
+ public GraphViz.StateNode toGraphViz(GraphViz gv) {
+ if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
+ if (gv.hasNode(this)) return gv.createNode(this);
+ GraphViz.StateNode n = gv.createNode(this);
+ n.label = "?";
+ n.color = "red";
+ for(Forest f : hp) n.edge(f, null);
+ return n;
+ }
+ }
+
+
+ // Statics //////////////////////////////////////////////////////////////////////////////
+
+ /** Depth-first expansion, handling .One and .Many without recursion. */
+ private static <NodeType> Tree<NodeType> expand1ForOneAndMany(
+ Forest<NodeType> forest) throws Ambiguous {
+ List<Tree<NodeType>> nodes = new ArrayList<Tree<NodeType>>();
+ List<Tree<NodeType>> build = new ArrayList<Tree<NodeType>>();
+
+ // path stack
+ List<Forest.One<NodeType>> path = new ArrayList<Forest.One<NodeType>>();
+ List<Integer> sizes = new ArrayList<Integer>(),
+ poss = new ArrayList<Integer>();
+
+ // handle the left-most leaf
+ expand1Descend(path, nodes, sizes, poss, forest);