X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FForest.java;h=b2f4a2239f3b641e52ec7031ec466deed810e267;hb=61539aaf02d0537fd1df08b5d5bd03189992cf1e;hp=4f79bb607301ff1073248f8c9275a5a20f6ddb67;hpb=2c1c0293545f3d12c23220fd05c663e6aa3f3de1;p=sbp.git diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 4f79bb6..b2f4a22 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -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,6 +23,7 @@ public abstract class Forest implements GraphViz.ToGraphViz { public void expand(HashSet> ht) { expand(ht, new HashSet>(), null); } static Forest create(Input.Region loc, NodeType head, Forest[] children, boolean lift) { + if (loc == null) throw new Error(); return new One(loc, head, children, lift); } @@ -34,7 +37,8 @@ public abstract class Forest implements GraphViz.ToGraphViz { abstract void gather(HashSet> ignore); abstract void edges(GraphViz.Node n); boolean ambiguous() { return false; } - + + abstract Input.Region getRegion(); // One ////////////////////////////////////////////////////////////////////////////// @@ -48,6 +52,8 @@ public abstract class Forest implements GraphViz.ToGraphViz { /** if true, the last child's children are considered children of this node */ private final boolean lift; + Input.Region getRegion() { return location; } + private One(Input.Region loc, NodeType head, Forest[] children, boolean lift) { this.location = loc; this.head = head; @@ -123,12 +129,13 @@ public abstract class Forest implements GraphViz.ToGraphViz { /** An "ambiguity node"; this is immutable once it has been "looked at" */ static class Many extends Forest { - HashSet parents = new HashSet(); private FastSet> hp = new FastSet>(); private boolean touched = false; public Many() { } + Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical + public Tree expand1() throws Ambiguous { touched(); if (hp.size() > 1) { @@ -150,6 +157,14 @@ public abstract class Forest implements GraphViz.ToGraphViz { void gather(HashSet> 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 f : hp) f.gather(ht); }