-
- // Node /////////////////////////////////////////////////////////////////////////////////
-
- /** a node in the GSS */
- public final class Node extends FastSet<Node> implements Invokable<Reduction, Node, Node> {
-
- private Forest.Ref holder = null;
- private boolean allqueued = false;
-
- /** what state this node is in */
- public final State state;
-
- /** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
- public Phase phase() { return Phase.this; }
- public Forest.Ref holder() { return holder==null ? (holder = new Forest.Ref()) : holder; }
- public Forest pending() { return Phase.this.closed ? holder().resolve() : holder; }
- public FastSet<Node> parents() { return this; }
-
- public void performReductions() {
- if (allqueued) return;
- allqueued = true;
- state.invokeReductions(token, this, this, null);
- }
-
- public void performReductions(Node n2) {
- if (!allqueued) performReductions();
- else state.invokeReductions(token, this, this, n2);
- }
-
- public final void invoke(Reduction r, Node n, Node n2) {
- if (n==null || n2==null || r.position.pos==0) {
- if (r.position.pos==0) {
- if (n==null) n = this;
- else return;
- }
- if (n==null) return;
- Forest[] holder = new Forest[r.position.pos];
- if (r.position.pos==0) n.finish(r, r.zero(), n.phase(), holder);
- else r.reduce(n, r.position.pos-1, n.phase(), holder);
- } else {
- Forest[] holder = new Forest[r.position.pos];
- if (r.position.pos<=0) throw new Error("called wrong form of reduce()");
- int pos = r.position.pos-1;
- Forest old = holder[pos];
- holder[pos] = n.pending();
- if (pos==0) {
- System.arraycopy(holder, 0, r.position.holder, 0, holder.length);
- Forest rex = r.position.rewrite(n.phase().getLocation());
- n2.finish(r, rex, n.phase(), holder);
- } else {
- r.reduce(n2, pos-1, n.phase(), holder);
- }
- holder[pos] = old;
- }
- }
-
- public void finish(Reduction r, Forest result, GSS.Phase target, Forest[] holder) {
- State state0 = state.gotoSetNonTerminals.get(r.position.owner());
- if (result==null) throw new Error();
- if (state0!=null)
- target.newNode(this, result, state0, r.position.pos<=0, r);
- }
-
- public void performEmptyReductions() { state.invokeReductions(token, this, null, null); }
-
- private Node(Node parent, Forest pending, State state) {
- this.state = state;
- this.holder().merge(pending);
- Phase start = parent==null ? null : parent.phase();
- if (parent != null) parents().add(parent, true);
- if (Phase.this.hash.get(state, start) != null) throw new Error("severe problem!");
- Phase.this.hash.put(state, start, this);
- }