}
/** expand this forest into a set of trees */
- public abstract HashSet<Tree<T>> expand(boolean toss);
+ //public abstract HashSet<Tree<T>> expand(boolean toss);
+ public HashSet<Tree<T>> expand(boolean toss) {
+ HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
+ for(Body<T> b : this)
+ ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
+ if (toss && ret.size() > 1) throw new Ambiguous(this);
+ return ret;
+ }
static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
- return new MultiForest<T>(loc, tag, tokens, unwrap, singleton);
+ return new MultiForest<T>(new Body<T>(loc, tag, tokens, unwrap, singleton));
}
// Body //////////////////////////////////////////////////////////////////////////////
return h;
}
- void addTo(FastSet<Body> h) {
- if (!singleton) h.add(this, true);
- else for(Body b : tokens[0]) b.addTo(h);
+ void addTo(FastSet<Body<T>> h) {
+ /*if (!singleton)*/ h.add(this, true);
+ //else for(Body b : tokens[0]) b.addTo(h);
}
protected String headToString() { return null; }
* viewed, it becomes immutable
*/
static class Ref<T> extends Forest<T> {
- private FastSet<Forest> hp = new FastSet<Forest>();
- private Forest res = null;
+ private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
public Ref() { }
- public void merge(Forest p) {
- if (res != null) throw new Error("already resolved!");
- if (p==null) throw new Error();
- if (p!=this) hp.add(p, true);
- }
- public Iterator<Body<T>> iterator() { return ((Forest<T>)resolve()).iterator(); }
- public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
- public Forest resolve() {
- if (hp==null) return res;
- FastSet<Body> nh = new FastSet<Body>();
- for(Forest<?> p : hp)
- for(Body<?> b : (Forest<?>)p)
- b.addTo(nh);
- res = new MultiForest(nh);
- hp = null;
- return res;
+ public void merge(Forest p) { if (p!=this) hp.add(p, true); }
+ public Iterator<Body<T>> iterator() {
+ final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
+ return new Iterator<Body<T>>() {
+ Iterator<Body<T>> ibt = ift==null ? null : ift.hasNext() ? ift.next().iterator() : null;
+ public void remove() { throw new RuntimeException("not supported"); }
+ public boolean hasNext() {
+ if (ibt==null) return false;
+ if (ibt.hasNext()) return true;
+ ibt = ift.hasNext() ? ift.next().iterator() : null;
+ return hasNext();
+ }
+ public Body<T> next() {
+ return ibt.next();
+ }
+ };
+
}
+ public Forest resolve() { return this; }
}
- // Implementations //////////////////////////////////////////////////////////////////////////////
-
private static class MultiForest<T> extends Forest<T> {
- private final FastSet<Body<T>> results;
- private MultiForest(FastSet<Body<T>> results) { this.results = results; }
- public MultiForest(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
- this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, unwrap, singleton));
- }
+ private final FastSet<Body<T>> results = new FastSet<Body<T>>();
+ public MultiForest(Body<T> b) { results.add(b); }
public Iterator<Body<T>> iterator() { return results.iterator(); }
- public HashSet<Tree<T>> expand(boolean toss) {
- HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
- for(Body<T> b : results)
- ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
- if (toss && ret.size() > 1) throw new Ambiguous(this);
- return ret;
- }
}
// Statics //////////////////////////////////////////////////////////////////////////////