-
- // Forest / Completed Reductions //////////////////////////////////////////////////////////////////////////////
-
- /** a pending or completed reduction */
- class Reduct {
-
- /** the node from which the reduction should begin */
- public Node n = null;
-
- /** the node on the other end of the edge to be reduced along (either: null, the second node of the reduction,
- * or the parent of the result of a length-one reduction)
- */
- public Node n2 = null;
-
- /** true iff the reduction has already been performed */
- private boolean done = false;
-
- /** the reduction to be applied */
- public Parser.Table.Reduction r;
-
- public Tree<String> result = null;
-
- public Reduct(Node n, Node n2, Parser.Table.Reduction r) {
- this.n = n;
- this.n2 = n2;
- this.r = r;
- if (reductions.contains(this)) { done = true; return; }
- reductions.add(this);
- pendingReduct.add(this);
- pendingReductions++;
- }
-
- /** perform the reduction */
- public void go() {
- if (done) return;
- done = true;
- pendingReduct.remove(this);
- pendingReductions--;
-
- // FIXME: explain this
- if (r==null) {
- for(Parser.Table.Reduction r : token==null ? n.state.getEofReductions() : n.state.getReductions(token)) {
- if (r.numPop <= 1) continue;
- r.reduce(n, n2, Phase.this, null);
- }
- } else if (r.numPop<=1) {
- // UGLY HACK
- // The problem here is that a "reduction of length 0/1"
- // performed twice with different values of n2 needs
- // to only create a *single* new result, but must add
- // multiple parents to the node holding that result.
- // The current reducer doesn't differentiate between
- // the next node of an n-pop reduction and the
- // ultimate parent of the last pop, so we need to
- // cache instances here as a way of avoiding
- // recreating them.
-
- Forest ret = (r.numPop==0 ? pcache : n.cache()).get(r);
- if (ret != null) r.reduce(n, n2, n.phase, ret);
- else (r.numPop==0 ? pcache : n.cache()).put(r, r.reduce(n, n2, n.phase, null));
-
- } else {
- r.reduce(n, n2, Phase.this, null);
- }
- }
-
- // FIXME: this is a PITA
- public int hashCode() { return n.hashCode() ^ (r==null ? 0 : r.hashCode()) ^ (n2==null ? 0 : n2.hashCode()); }
- public boolean equals(Object o) {
- if (o==null) return false;
- if (o==this) return true;
- if (!(o instanceof Reduct)) return false;
- Reduct other = (Reduct)o;
- return equal(r, other.r) && equal(n, other.n) && equal(n2, other.n2);
- }
- }
-