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> {
12 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13 public final Tree<T> expand1() throws Parser.Ambiguous, Parser.Failed {
14 Iterator<Tree<T>> it = expand(true).iterator();
15 if (!it.hasNext()) throw new Parser.Failed();
19 /** expand this forest into a set of trees */
20 public abstract HashSet<Tree<T>> expand(boolean toss);
21 public abstract boolean empty();
23 static <T> Forest<T> singleton(Token.Location loc, Sequence creator) { return create(loc, null, new Forest[] { }, creator, false, true); }
24 static <T> Forest<T> singleton(Token.Location loc, Forest<T> body, Sequence creator) { return create(loc, null, new Forest[] { body }, creator, false, true); }
25 static <T> Forest<T> leaf(Token.Location loc, T tag, Sequence creator) { return create(loc, tag, null, creator, false, false); }
26 public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
27 return new MultiForest<T>(loc, tag, tokens, creator, unwrap, singleton);
30 // Body //////////////////////////////////////////////////////////////////////////////
32 protected static class Body<T> {
34 private final Token.Location location;
36 private final Forest<T>[] tokens;
37 private final Sequence creator;
38 private final boolean unwrap;
39 private final boolean singleton;
41 private Body(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
44 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
45 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
46 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
47 this.creator = creator;
49 this.singleton = singleton;
52 private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
54 for(Body<T> b : (IterableForest<T>)tokens[0]) b.expand(toss, toks, i, h);
56 } else if (i==tokens.length) {
57 h.add(new Tree<T>(null, tag, toks.toArray(tree_hint)));
59 } else if (unwrap && i==tokens.length-1) {
60 if (tokens[i] != null)
61 for(Body b : (IterableForest<T>)tokens[i])
62 b.expand(toss, toks, 0, h);
66 for(Tree<T> r : tokens[i].expand(toss)) {
68 int old = toks.size();
70 expand(toss, toks, i+1, h);
71 while(toks.size() > old) toks.remove(toks.size()-1);
73 //if (!hit) throw new Error();
77 void addTo(HashSet<Body> h) {
78 if (!singleton) h.add(this);
79 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
81 void addTo(FastSet<Body> h) {
82 if (!singleton) h.add(this, true);
83 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
87 private boolean rep = false;
88 public String toString() {
89 if (rep) return "***";
92 StringBuffer ret = new StringBuffer();
93 for(int i=0; i<tokens.length; i++) {
94 String q = tokens[i]==null ? "null" : tokens[i].toString();
100 String tail = ret.toString().trim();
101 String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : "";
102 if (tail.length() > 0) tail = "{" + tail + "}";
111 // Ref //////////////////////////////////////////////////////////////////////////////
113 static abstract class IterableForest<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
114 public abstract Iterator<Forest.Body<T>> iterator();
118 * This class represents a partially complete collection of
119 * forests to be viewed as a forest at some later date; once
120 * viewed, it becomes immutable
122 static class Ref<T> extends IterableForest<T> {
123 public boolean empty() {
124 if (res!=null) return res.empty();
125 for(Forest f : hp) if (!f.empty()) return false;
128 private FastSet<Forest> hp = new FastSet<Forest>();
129 private Forest res = null;
131 public void merge(Forest p) {
132 if (res != null) throw new Error("already resolved!");
133 if (p==null) throw new Error();
134 if (p!=this) hp.add(p, true);
136 public Iterator<Body<T>> iterator() { return ((IterableForest<T>)resolve()).iterator(); }
137 public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
138 public String toString() { return resolve().toString(); }
139 public Forest resolve() {
140 if (hp==null) return res;
141 FastSet<Body> nh = new FastSet<Body>();
142 for(Forest<?> p : hp)
143 for(Body<?> b : (IterableForest<?>)p)
145 res = new MultiForest(nh);
151 // Implementations //////////////////////////////////////////////////////////////////////////////
153 private static class MultiForest<T> extends IterableForest<T> {
154 public boolean empty() { return results.size()>0; }
155 private final FastSet<Body<T>> results;
156 private MultiForest(FastSet<Body<T>> results) { this.results = results; }
157 public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
158 this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, creator, unwrap, singleton));
160 public Iterator<Body<T>> iterator() { return results.iterator(); }
162 public HashSet<Tree<T>> expand(boolean toss) {
163 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
164 for(Body<T> b : results)
165 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
166 if (toss && ret.size() > 1) throw new Parser.Ambiguous(this);
170 // Display //////////////////////////////////////////////////////////////////////////////
172 private String toString = null;
173 public String toString() {
174 if (toString != null) return toString;
175 StringBuffer ret = new StringBuffer();
176 if (results.size()==1) {
177 for(Forest.Body<T> r : results)
179 return toString = ret.toString();
182 boolean first = true;
183 for(Forest.Body<T> r : results) {
184 if (!first) ret.append(' ');
189 return toString = ret.toString();
193 // Statics //////////////////////////////////////////////////////////////////////////////
195 private static Tree[] tree_hint = new Tree[0];
196 private static Body[] body_hint = new Body[0];
197 private static final Forest[] emptyForestArray = new Forest[0];