implements Invokable<Pos, ResultNode, Object> {
private final Parser.Table.State state;
- private boolean fromEmptyReduction;
/** which GSS.Phase this StateNode belongs to */
- public Iterator<ResultNode> iterator() { return predecessors.iterator(); }
public Parser.Table.State state() { return state; }
public boolean isDoomedState() { return state.doomed; }
+ public boolean hasPathToRoot() {
+ if (state.doomed) return false;
+ return super.hasPathToRoot();
+ }
public void check() {
if (destroyed) return;
// - be on the frontier or
// - have a non-doomed node closer to the frontier than themself
if (phase().isFrontier()) dead = false;
- else for(ResultNode r : successors)
+ else for(ResultNode r : successors())
if (state.doomed) { if (r.hasSuccessors()) { dead = false; break; } }
else { if (r.hasNonDoomedSuccessors()) { dead = false; break; } }
- dead |= predecessors.size()==0;
+ dead |= !hasPredecessors();
if (!dead) return;
if (phase() != null && phase().hash != null)
phase().hash.remove(state, predecessorPhase());
public final void invoke(Pos r, ResultNode only, Object o) {
boolean emptyProductions = only==null;
if (emptyProductions != (r.numPops()==0)) return;
- if (r.numPops()!=0) reduce(r, r.numPops()-1, phase(), only);
- else {
+ if (r.numPops()==0) {
Input.Region region = phase().getLocation().createRegion(phase().getLocation());
phase().newNodeFromReduction(r.rewrite(region), r, this);
+ } else {
+ // never start reductions at a node that was created via a right-nulled rule
+ if (only.reduction() != null && only.reduction().numPops()==0) return;
+ reduce(r, r.numPops()-1, phase(), only);
}
}
private void reduce(Pos r, int pos, GSS.Phase target, ResultNode only) {
- for(ResultNode res : predecessors)
+ for(ResultNode res : this)
if (only == null || res == only)
- for(StateNode pred : res)
- reduce2(r, pos, target, pred, res.getForest());
+ for(StateNode pred : res) {
+ Forest[] holder = r.holder;
+ Forest old = pos >= holder.length ? null : holder[pos];
+ if (pos < holder.length) holder[pos] = res.getForest();
+ if (pos>0) pred.reduce(r, pos-1, target, null);
+ else new Reduction(pred, r,
+ pred.hasPathToRoot()
+ ? r.rewrite(pred.phase().getLocation().createRegion(target.getLocation()))
+ : null,
+ target);
+ if (pos < holder.length) holder[pos] = old;
+ }
}
- void reduce2(Pos r, int pos, GSS.Phase target, StateNode pred, Forest f) {
- Forest[] holder = r.holder;
- Forest old = pos >= holder.length ? null : holder[pos];
- if (pos < holder.length) holder[pos] = f;
- if (pos>0) pred.reduce(r, pos-1, target, null);
- else {
- Input.Region region = pred.phase().getLocation().createRegion(target.getLocation());
- new Reduction(pred, r, r.rewrite(region), target);
- }
- if (pos < holder.length) holder[pos] = old;
- }
-
- StateNode(GSS.Phase phase, Forest f, Pos reduction, StateNode pred, State state, boolean fromEmptyReduction) {
- this(phase, new ResultNode(f, reduction, pred), state, fromEmptyReduction);
- }
- StateNode(GSS.Phase phase, ResultNode pred, State state, boolean fromEmptyReduction) {
+ StateNode(GSS.Phase phase, ResultNode pred, State state) {
super(phase, pred.phase());
this.state = state;
- this.fromEmptyReduction = fromEmptyReduction;
if (phase.hash.get(state, pred.phase()) != null) throw new Error("severe problem!");
phase.hash.put(state, pred.phase(), this);
addPred(pred);
// Add/Remove Successors/Predecessors //////////////////////////////////////////////////////////////////////////////
public void addPred(Forest f, Pos reduction, StateNode pred) {
- for(ResultNode r : predecessors)
+ for(ResultNode r : this)
if (r.predecessorsContains(pred)) {
r.merge(f);
return;
}
protected void addPred(ResultNode result) {
super.addPred(result);
- if (!this.fromEmptyReduction)
- state.invokeReductions(phase().getToken(), this, result);
+ state.invokeReductions(phase().getToken(), this, result);
}
}