public Object next() { if (firstTime) { // initial condition firstTime = false; if (adaptor.getChildCount(tree) == 0) { // single node tree (special) nodes.add(eof); return tree; } return tree; } // if any queued up, use those first if (nodes != null && nodes.size() > 0) return nodes.remove(); // no nodes left? if (tree == null) return eof; // next node will be child 0 if any children if (adaptor.getChildCount(tree) > 0) { tree = adaptor.getChild(tree, 0); nodes.add(tree); // real node is next after DOWN return down; } // if no children, look for next sibling of tree or ancestor Object parent = adaptor.getParent(tree); // while we're out of siblings, keep popping back up towards root while (parent != null && adaptor.getChildIndex(tree) + 1 >= adaptor.getChildCount(parent)) { nodes.add(up); // we're moving back up tree = parent; parent = adaptor.getParent(tree); } // no nodes left? if (parent == null) { tree = null; // back at root? nothing left then nodes.add(eof); // add to queue, might have UP nodes in there return nodes.remove(); } // must have found a node with an unvisited sibling // move to it and return it int nextSiblingIndex = adaptor.getChildIndex(tree) + 1; tree = adaptor.getChild(parent, nextSiblingIndex); nodes.add(tree); // add to queue, might have UP nodes in there return nodes.remove(); }
// $ANTLR start "topdown" // /Volumes/scratch/jcottam/workspace/Stencil/Stencil/Core/stencil/parser/DefaultPack.g:92:1: // topdown : ^(r= PACK DEFAULT ) ->; public final DefaultPack.topdown_return topdown() throws RecognitionException { DefaultPack.topdown_return retval = new DefaultPack.topdown_return(); retval.start = input.LT(1); StencilTree root_0 = null; StencilTree _first_0 = null; StencilTree _last = null; StencilTree r = null; StencilTree DEFAULT1 = null; StencilTree r_tree = null; StencilTree DEFAULT1_tree = null; RewriteRuleNodeStream stream_DEFAULT = new RewriteRuleNodeStream(adaptor, "token DEFAULT"); RewriteRuleNodeStream stream_PACK = new RewriteRuleNodeStream(adaptor, "token PACK"); try { // /Volumes/scratch/jcottam/workspace/Stencil/Stencil/Core/stencil/parser/DefaultPack.g:92:8: // ( ^(r= PACK DEFAULT ) ->) // /Volumes/scratch/jcottam/workspace/Stencil/Stencil/Core/stencil/parser/DefaultPack.g:92:10: // ^(r= PACK DEFAULT ) { _last = (StencilTree) input.LT(1); { StencilTree _save_last_1 = _last; StencilTree _first_1 = null; _last = (StencilTree) input.LT(1); r = (StencilTree) match(input, PACK, FOLLOW_PACK_in_topdown68); if (state.failed) return retval; if (state.backtracking == 1) stream_PACK.add(r); if (state.backtracking == 1) if (_first_0 == null) _first_0 = r; match(input, Token.DOWN, null); if (state.failed) return retval; _last = (StencilTree) input.LT(1); DEFAULT1 = (StencilTree) match(input, DEFAULT, FOLLOW_DEFAULT_in_topdown70); if (state.failed) return retval; if (state.backtracking == 1) stream_DEFAULT.add(DEFAULT1); match(input, Token.UP, null); if (state.failed) return retval; _last = _save_last_1; } // AST REWRITE // elements: // token labels: // rule labels: retval // token list labels: // rule list labels: // wildcard labels: if (state.backtracking == 1) { retval.tree = root_0; RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream( adaptor, "rule retval", retval != null ? retval.tree : null); root_0 = (StencilTree) adaptor.nil(); // 92:28: -> { adaptor.addChild(root_0, fromDefault((Pack) r)); } retval.tree = (StencilTree) adaptor.rulePostProcessing(root_0); input.replaceChildren( adaptor.getParent(retval.start), adaptor.getChildIndex(retval.start), adaptor.getChildIndex(_last), retval.tree); } } } catch (RecognitionException re) { reportError(re); recover(input, re); } finally { } return retval; }