import java.lang.reflect.*;
/** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
-public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable {
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable, GraphViz.ToGraphViz {
private static int master_idx = 0;
private final int idx = master_idx++;
/** expand this forest into a set of trees */
public HashSet<Tree<T>> expand(boolean toss) {
- //if (toss) scan();
final HashSetTreeConsumer<T> ret = new HashSetTreeConsumer<T>();
visit(new TreeMaker2<T>(toss, ret), null, null);
if (toss && ret.size() > 1) throw new InnerAmbiguous(this);
return body;
}
static <T> Forest<T> leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, false, false, p); }
-
public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton, Position p) {
return new MyBody<T>(loc, tag, tokens, unwrap, singleton, p);
}
-
// Body //////////////////////////////////////////////////////////////////////////////
- protected static interface Body<T> {
+ protected static interface Body<T> extends GraphViz.ToGraphViz {
void expand(int i, TreeMaker<T> h);
}
-
+ public abstract void edges(GraphViz.Node n);
protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
+ public boolean isTransparent() { return false; }
+ public boolean isHidden() { return false; }
+ public GraphViz.Node toGraphViz(GraphViz gv) {
+ if (gv.hasNode(this)) return gv.createNode(this);
+ GraphViz.Node n = gv.createNode(this);
+ n.label = headToString()==null?"":headToString();
+ n.directed = true;
+ edges(n);
+ return n;
+ }
+ public void edges(GraphViz.Node n) {
+ for(int i=0; i<tokens.length; i++) {
+ if (i==tokens.length-1 && unwrap) {
+ tokens[i].edges(n);
+ } else {
+ n.edge(tokens[i]);
+ }
+ }
+ }
+
public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
ivbc.invoke(this, b, c);
}
}
}
- protected String headToString() { return null; }
+ protected String headToString() { return tag==null?null:tag.toString(); }
protected String headToJava() { return null; }
protected String left() { return "{"; }
protected String right() { return "}"; }
return super.toInt();
}
public void merge(Forest p) { if (p!=this) hp.add(p, true); }
+
+ public boolean isTransparent() { return hp.size()==1; }
+ public boolean isHidden() { return hp.size()==0; }
+ public void edges(GraphViz.Node n) { for(Forest f : hp) f.edges(n); }
+ public GraphViz.Node toGraphViz(GraphViz gv) {
+ if (hp.size()==0) return null;
+ if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
+ if (gv.hasNode(this)) return gv.createNode(this);
+ GraphViz.Node n = gv.createNode(this);
+ n.label = "?";
+ n.color = "red";
+ for(Forest f : hp) n.edge(f);
+ return n;
+ }
+
public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
if (hp==null) return;
for(Forest<T> f : hp)