1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
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>>*/ implements Visitable<Forest.Body<T>> {
12 public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
13 private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
14 public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
15 public void invoke(Forest.Body<T> bod, Boolean toss, Integer i) {
17 ArrayList<Tree<T>> toks = this.toks;
18 this.toks = new ArrayList<Tree<T>>();
19 //addAll(bod.expand(toss, 0, new TreeMaker<T>()));
20 bod.expand(toss, 0, this);
23 bod.expand(toss, i, this);
28 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
29 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
30 Iterator<Tree<T>> it = expand(true).iterator();
31 if (!it.hasNext()) throw new ParseFailed();
35 /** expand this forest into a set of trees */
36 public HashSet<Tree<T>> expand(boolean toss) {
37 TreeMaker<T> ret = new TreeMaker<T>();
38 invoke(ret, new Boolean(toss), null);
39 if (toss && ret.size() > 1) throw new Ambiguous(this);
43 static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
44 static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
45 static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
46 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
47 return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
50 // Body //////////////////////////////////////////////////////////////////////////////
52 protected static interface Body<T> {
53 TreeMaker<T> expand(boolean toss, int i, TreeMaker<T> h);
56 protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
58 public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
59 ivbc.invoke(this, b, c);
62 private final Input.Location location;
64 private final Forest<T>[] tokens;
65 private final boolean unwrap;
66 private final boolean singleton;
68 private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
71 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
72 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
73 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
75 this.singleton = singleton;
78 public TreeMaker<T> expand(boolean toss, int i, TreeMaker<T> h) {
80 tokens[0].invoke(h, toss, i);
82 } else if (i==tokens.length) {
83 h.add(new Tree<T>(null, tag, h.toks.toArray(tree_hint)));
85 } else if (unwrap && i==tokens.length-1) {
86 if (tokens[i] != null)
87 tokens[i].invoke(h, toss, 0);
91 for(Tree<T> r : tokens[i].expand(toss)) {
93 int old = h.toks.size();
96 while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);
98 //if (!hit) throw new Error();
103 protected String headToString() { return null; }
104 protected String headToJava() { return null; }
105 protected String left() { return "{"; }
106 protected String right() { return "}"; }
107 protected boolean ignoreSingleton() { return false; }
111 // Ref //////////////////////////////////////////////////////////////////////////////
114 * This class represents a partially complete collection of
115 * forests to be viewed as a forest at some later date; once
116 * viewed, it becomes immutable
118 static class Ref<T> extends Forest<T> {
119 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
121 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
122 public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
123 if (hp==null) return;
124 for(Forest<T> f : hp) f.invoke(ivbc, b, c);
126 public Forest resolve() { return this; }
130 // Statics //////////////////////////////////////////////////////////////////////////////
132 private static Tree[] tree_hint = new Tree[0];
133 private static final Forest[] emptyForestArray = new Forest[0];
135 protected String headToString() { return null; }
136 protected String headToJava() { return null; }
137 protected String left() { return "<?"; }
138 protected String right() { return "?>"; }
139 protected boolean ignoreSingleton() { return true; }