1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.util.*;
7 import java.lang.reflect.*;
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/
11 implements IntegerMappable,
14 private static int master_idx = 0;
15 private final int idx = master_idx++;
16 public int toInt() { return idx; }
18 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
19 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
21 Iterator<Tree<T>> it = expand(true).iterator();
22 if (!it.hasNext()) throw new ParseFailed();
24 } catch (InnerAmbiguous ia) { throw new Ambiguous(ia.f); }
27 /** expand this forest into a set of trees */
28 public abstract void expand(HashSet<Tree<T>> ht);
29 public HashSet<Tree<T>> expand(boolean toss) {
30 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
35 private static class InnerAmbiguous extends RuntimeException {
36 public final Forest<?> f;
37 public InnerAmbiguous(Forest<?> f) { this.f = f; }
40 static <T> Forest<T> leaf(Input.Region loc, T tag) { return create(loc, tag, null, false); }
41 public static <T> Forest<T> create(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
42 return new Body<T>(loc, tag, tokens, unwrap);
44 // Body //////////////////////////////////////////////////////////////////////////////
46 public abstract void edges(GraphViz.Node n);
47 public boolean ambiguous() { return false; }
48 public /*protected*/ static class Body<T> extends Forest<T> /* extends PrintableTree<Forest<T>> implements */ {
50 private final Input.Region location;
52 private final Forest<T>[] tokens;
53 private final boolean unwrap;
55 public boolean isTransparent() { return false; }
56 public boolean isHidden() { return false; }
57 public GraphViz.Node toGraphViz(GraphViz gv) {
58 if (gv.hasNode(this)) return gv.createNode(this);
59 GraphViz.Node n = gv.createNode(this);
60 n.label = headToString()==null?"":headToString();
62 //n.comment = reduction==null?null:reduction+"";
66 boolean edges = false;
67 public void edges(GraphViz.Node n) {
70 for(int i=0; i<tokens.length; i++) {
71 if (i==tokens.length-1 && unwrap && !tokens[i].ambiguous()) {
74 n.edge(tokens[i], null);
79 private Body(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
82 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
83 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
84 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
88 public void expand(HashSet<Tree<T>> ht) {
89 expand(0, new Tree[tokens.length], ht);
91 public void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht) {
92 if (i==tokens.length) {
93 ht.add(new Tree<T>(location, tag, ta, unwrap));
96 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
97 tokens[i].expand(ht2);
98 for(Tree<T> tc : ht2) {
105 protected String headToString() { return tag==null?null:tag.toString(); }
106 protected String headToJava() { return "null"; }
107 protected String left() { return "{"; }
108 protected String right() { return "}"; }
109 protected boolean ignoreSingleton() { return false; }
113 // Ref //////////////////////////////////////////////////////////////////////////////
116 * This class represents a partially complete collection of
117 * forests to be viewed as a forest at some later date; once
118 * viewed, it becomes immutable
120 static class Ref<T> extends Forest<T> {
121 public void expand(HashSet<Tree<T>> ht) {
122 for (Forest<T> f : hp)
125 public HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
126 public boolean contains(Forest f) {
127 return hp.contains(f);
129 public boolean ambiguous() {
130 if (hp.size()==0) return false;
131 if (hp.size()==1) return hp.iterator().next().ambiguous();
134 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
137 if (hp.size()==1) return hp.iterator().next().toInt();
138 return super.toInt();
140 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
142 public boolean isTransparent() { return hp.size()==1; }
143 public boolean isHidden() { return hp.size()==0; }
144 public void edges(GraphViz.Node n) {
145 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
146 for(Forest f : hp) f.edges(n);
148 public GraphViz.Node toGraphViz(GraphViz gv) {
149 //if (hp.size()==0) return null;
150 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
151 if (gv.hasNode(this)) return gv.createNode(this);
152 GraphViz.Node n = gv.createNode(this);
155 for(Forest f : hp) n.edge(f, null);
159 public Forest resolve() { return this; }
162 // Statics //////////////////////////////////////////////////////////////////////////////
164 private static Tree[] tree_hint = new Tree[0];
165 private static String[] string_hint = new String[0];
166 private static final Forest[] emptyForestArray = new Forest[0];
168 protected String headToString() { return null; }
169 protected String headToJava() { return "null"; }
170 protected String left() { return "<?"; }
171 protected String right() { return "?>"; }
172 protected boolean ignoreSingleton() { return true; }