- public void performReductions(Node n2) {
- if (!allqueued) phase().reductionQueue.add(this);//performReductions();
- else state.invokeReductions(phase().token(), this, this, n2);
- }
-
- public Parser.Table.State state() { return state; }
-
- public void performEmptyReductions() { state.invokeReductions(phase().token, this, null, null); }
- public final void invoke(Position r, Node n, Node n2) {
- //reductions++;
- //if (r.pos==1) single_newnode++;
- //if (r.pos>1) multi_newnode++;
- if (n==null || n2==null || r.pos==0) {
- if (r.pos==0) {
- if (n==null) n = this;
- else return;
- }
- if (n==null) return;
- Forest[] holder = new Forest[r.pos];
- if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
- else n.reduce(r, r.pos-1, n.phase(), null);
- } else {
- if (r.pos<=0) throw new Error("called wrong form of reduce()");
- int pos = r.pos-1;
- n.reduce(r, pos, n.phase(), n2);
- }
- }
-
- public void reduce(Position r, int pos, GSS.Phase target, Node only) {
- Forest[] holder = r.holder;
- Forest old = holder[pos];
-
- //toplevel_reductions++;
- HashSet<Forest> rr = new HashSet<Forest>();
- for(Forest result : results()) {
- rr.add(result);
- }
- //System.out.println(r);
- for(Forest result : rr) {
- for(Node child : ((Forest.Many<?>)result).parents) {
- if (only != null && child!=only) continue;
- holder[pos] = result;
- if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
- else child.reduce(r, pos-1, target, null);
- }
- }
- holder[pos] = old;
- }
-
- public void finish(Position r, Forest result, GSS.Phase target) {
- Parser.Table.State state0 = (Parser.Table.State)state.gotoSetNonTerminals.get(r.owner());
- if (result==null) throw new Error();
- if (state0!=null)
- target.newNode(this, result, state0, r.pos<=0, r);
- }
-
- Node(GSS.Phase phase, Node parent, Forest pending, State state) {
- this.phase = phase;
- this.state = state;
- this.merge(parent, pending);
- GSS.Phase start = parent==null ? null : parent.phase();
- if (parent != null) addParent(parent, true);
- if (phase.hash.get(state, start) != null) throw new Error("severe problem!");
- phase.hash.put(state, start, this);
- }
- public int toInt() { return idx; }
- private final int idx = node_idx++;
-