-
- /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
- public void shift(Phase next, Forest result) throws Parser.Failed {
- if (prev!=null) prev.hash = null;
- this.next = next;
- closed = true;
- Forest res = null;
- boolean ok = false;
- for(Phase.Node n : hash.values()) {
- if (n.holder==null) continue;
- n.holder.resolve();
- if (token == null && n.state.isAccepting()) {
- if (finalResult==null) finalResult = new Forest.Ref();
- finalResult.merge(n.holder);
- }
- if (!n.holder.valid()) continue;
- if (token == null) continue;
- n.state.invokeShifts(token, this, result, n);
- }
-
- if (!good && token!=null)
- throw new Parser.Failed(error(red("unexpected character")+" "+purple(token)+" encountered at "+green(getLocation())+"\n"),
- getLocation());
- if (token==null && finalResult==null)
- throw new Parser.Failed(error(red("unexpected end of file\n")),
- getLocation());
-
- // this massively improves GC performance
- //hash = null;
- }
-
-
- public class Waiting {
- Node parent;
- Forest pending;
- State state;
- boolean fromEmptyReduction;
- Reduction reduction;
- public Waiting(Node parent, Forest pending, State state, boolean fromEmptyReduction, Reduction reduction) {
- waits++;
- this.parent = parent;
- this.pending = pending;
- this.state = state;
- this.fromEmptyReduction = fromEmptyReduction;
- this.reduction = reduction;
- }
- public void perform() {
- System.out.println("performing: " + reduction.position);
- newNode(parent, pending, state, fromEmptyReduction, reduction);
- }
- }
-
- // GSS Nodes //////////////////////////////////////////////////////////////////////////////
-
- /** a node in the GSS */
- public final class Node extends FastSet<Node> implements Invokable<Reduction, Node, Node> {
-
- public boolean touched = false;
- 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 queueReductions() {
- if (!reducing) return;
- if (allqueued) return;
- allqueued = true;
- int where = parents().size();
- state.invokeReductions(token, this, this, null);
- }
-
- public void queueReductions(Node n2) {
- if (!allqueued) { queueReductions(); return; }
- state.invokeReductions(token, this, this, n2);
- }
-
- public final void invoke(Reduction r, Node n, Node n2) {
- if (n==null) {
- if (r.numPop==0) r.reduce(this);
- return;
- }
- if (r.numPop==0) return;
- if (n2==null) r.reduce(n);
- else r.reduce(n, n2);
- }
- public void queueEmptyReductions() {
- if (!reducing) return;
- state.invokeReductions(token, this, null, null);
- }
-
- private boolean fe;
- public boolean dead = false;
- public boolean redo = false;
- private Node(Node parent, Forest pending, State state, boolean fe) {
- this.fe = fe;
- this.state = state;
- for(Position p : state) {
- if (p.owner().needs!=null)
- for(Sequence s : p.owner().needs) {
- //dead = true;
- //redo = false;
- }
- }
- Phase start = parent==null ? null : parent.phase();
- if (pending != null) this.holder().merge(pending);
- if (parent != null) parents().add(parent, true);
- if (Phase.this.hash.get(code(state, start)) != null) throw new Error("severe problem!");
- Phase.this.hash.put(code(state, start), this);
- Phase.this.numNodes++;
- if (parent==null) holder().valid = true; // hack to make sure that the "base" node is always considered valid
- }