package stepwise; import java.util.LinkedList; // the last element of the parents list is the actual node (and of the right type). public final class Parents implements Stepwise { private X _syns; private LinkedList> _stack; public Parents(final Node node, final X syns) { _stack = new LinkedList>(); _stack.add(node); _syns = syns; } @Override public X lazyEval() { _stack.clear(); return _syns; } @Override public Report nextStep() { while (true) { Coroutine head = _stack.poll(); if (head == null) { return new ReportDone(); // stack empty, we are done } if (head instanceof Parents) { // merge stacks @SuppressWarnings("unchecked") Parents other = (Parents) head; _stack.addAll(0, other._stack); continue; } Report rep = head.nextStep(); if (rep instanceof ReportReplace) { ReportReplace repl = (ReportReplace) rep; Coroutine comp = (Coroutine) repl.get(); _stack.addFirst(comp); continue; } else if (rep instanceof ReportFail) { return rep; } else if (rep instanceof ReportChild) { ReportChild child = (ReportChild) rep; Coroutine comp = (Coroutine) child.get(); _stack.addFirst(head); _stack.addFirst(comp); continue; } else if (rep instanceof ReportDone) { continue; } else if (rep instanceof ReportInfo) { _stack.addFirst(head); return rep; } else { throw new RuntimeException( "Parents.nextStep(): unexpected report type."); } } } }