- if (tokens[i]!=null) {
- HashSet<Tree<T>> exp = tokens[i].expand(toss);
- if (exp != null)
- for(Tree<T> r : exp) {
- int old = toks.size();
- toks.add(r);
- expand(toss, toks, i+1, h);
- while(toks.size() > old) toks.remove(toks.size()-1);
- }
- }
- }
- return h;
- }
- void addTo(HashSet<Body> h) {
- if (!singleton) h.add(this);
- else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
- }
- void addTo(FastSet<Body> h) {
- if (!singleton) h.add(this, true);
- else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
- }
-
- private boolean kcache = false;
- private boolean keep = false;
- public boolean keep() {
- if (kcache) return keep;
- kcache = true;
- for(Forest<T> token : tokens) if (!token.valid()) return keep = false;
- return keep = creator==null || (creator.needs.size()==0 && creator.hates.size()==0);
- }
- public boolean keep(Iterable<Body<T>> h) {
- if (keep()) return true;
- for(Forest<T> token : tokens) if (!token.valid()) return false;
- int needs = 0;
- for(Body<T> b : h) {
- if (creator.hates.contains(b.creator) && b.keep(h)) return false;
- if (creator.needs.contains(b.creator) && b.keep(h)) needs--;
- }
- return needs <= -1 * creator.needs.size();
- }
-
- private boolean rep = false;
- public String toString() {
- if (rep) return "***";
- try {
- rep = true;
- StringBuffer ret = new StringBuffer();
- for(int i=0; i<tokens.length; i++) {
- String q = tokens[i]==null ? "null" : tokens[i].toString();
- if (q.length() > 0) {
- ret.append(q);
- ret.append(" ");
+ tokens[i].visit(new TreeMaker<T>(h.toss) {
+ public void start(T head, Input.Location loc) { }
+ public void addTree(Tree<T> t) { toks.add(t); }
+ public void finish(T head, Input.Location loc) {
+ int old = h.toks.size();
+ h.addTree(new Tree<T>(loc, head, toks.toArray(tree_hint)));
+ expand(i+1, h);
+ while(h.toks.size() > old) h.toks.remove(h.toks.size()-1);