- public void uninhibit(int p, Sequence s) {
- if (s.hated!=null)
- for(Sequence s2 : s.hated)
- inhibited.remove(p, s2);
- }
-
- public void inhibit(Reduction r, int p) {
- if (r.position.owner().hated == null) return;
- // remember that dead states are still allowed to shift -- just not allowed to reduce
- boolean reset = false;
- for(Sequence seq : r.position.owner().hated) {
- if (performed.contains(p,seq)) {
- uninhibit(p, seq);
- //System.out.println("\nresetting due to " + r.position.owner() + " killing " + seq);
- //inhibited.clear();
- inhibited.add(p, seq);
- //inhibited = new HashMapBag<Integer,Sequence>();
- reset = true;
- resets++;
- throw new Reset();
- }
- inhibited.add(p, seq);
- }
- }
-
- /** perform all reduction operations */
- public void reduce() {
- try {
- reducing = true;
- if (reducing_list==null || reducing_list.length < hash.size())
- reducing_list = new Phase.Node[hash.size() * 4];
- hash.toArray(reducing_list);
- int num = hash.size();
- for(int i=0; i<num; i++) {
- Node n = reducing_list[i];
- n.performEmptyReductions();
- // INVARIANT: we never "see" a node until its parent-set is complete, modulo merges
- }
- for(int i=0; i<num; i++) {
- Node n = reducing_list[i];
- reducing_list[i] = null;
- n.performReductions();
- }
- } catch (Reset r) {
- reset();
- reduce();
- }
- }
-
- class Reset extends RuntimeException { }
-
- public void invoke(State st, Forest result, Node n) {
- good |= next.newNode(n, result, st, false);
- }
-
- /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
- public void shift(Phase next, Forest result) throws ParseFailed {
- // this massively improves GC performance
- if (prev!=null) prev.hash = null;
- this.next = next;
- closed = true;
- Forest res = null;
- boolean ok = false;
- for(Phase.Node n : hash.values()) {
- if (token == null && n.state.isAccepting()) {
- if (finalResult==null) finalResult = new Forest.Ref();
- finalResult.merge(n.holder);
- }
- if (token == null) continue;
- n.state.invokeShifts(token, this, result, n);
- }
-
- if (!good && token!=null)
- throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected character")+" "+ANSI.purple(token)+" encountered at "+ANSI.green(getLocation())+"\n", token, hash.values()),
- getLocation());
- if (token==null && finalResult==null)
- throw new ParseFailed(ParseFailed.error(ANSI.red("unexpected end of file\n"), token, hash.values()),
- getLocation());
- }
-
-
- 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);
- }
+ public int toInt() { return pos+1; }
+ public int size() { return hash==null ? 0 : hash.size(); }
+ public int pos() { return pos; }
+ public Tok getToken() { return token; }
+ public Iterator<Node> iterator() { return hash.iterator(); }
+ public GSS getGSS() { return GSS.this; }
+
+ // GraphViz //////////////////////////////////////////////////////////////////////////////
+
+ public GraphViz.Node toGraphViz(GraphViz gv) {
+ if (gv.hasNode(this)) return gv.createNode(this);
+ GraphViz.Group g = gv.createGroup(this);
+ g.label = "Phase " + pos;
+ g.color = "gray";
+ g.cluster = true;
+ return g;