public abstract class Forest<T> {
/** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
- public final Tree<T> expand1() throws Parser.Ambiguous, Parser.Failed {
+ public final Tree<T> expand1() throws Ambiguous, ParseFailed {
Iterator<Tree<T>> it = expand(true).iterator();
- if (!it.hasNext()) throw new Parser.Failed();
+ if (!it.hasNext()) throw new ParseFailed();
return it.next();
}
/** expand this forest into a set of trees */
public abstract HashSet<Tree<T>> expand(boolean toss);
- static <T> Forest<T> singleton(Token.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
- static <T> Forest<T> singleton(Token.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
- static <T> Forest<T> leaf(Token.Location loc, T tag) { return create(loc, tag, null, false, false); }
- public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ 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);
}
protected static class Body<T> {
- private final Token.Location location;
+ private final Input.Location location;
private final T tag;
private final Forest<T>[] tokens;
private final boolean unwrap;
private final boolean singleton;
- private Body(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ private Body(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
this.location = loc;
this.tag = tag;
this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
private static class MultiForest<T> extends IterableForest<T> {
private final FastSet<Body<T>> results;
private MultiForest(FastSet<Body<T>> results) { this.results = results; }
- public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+ 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));
}
public Iterator<Body<T>> iterator() { return results.iterator(); }
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 Parser.Ambiguous(this);
+ if (toss && ret.size() > 1) throw new Ambiguous(this);
return ret;
}