def walk(dispatcher, node, definition=None):
<EOL>nodes = []<EOL>sourcepath_stack = [NotImplemented]<EOL>def _walk(dispatcher, node, definition=None, token=None):<EOL><INDENT>if not isinstance(node, Node):<EOL><INDENT>for fragment in dispatcher.token(<EOL>token, nodes[-<NUM_LIT:1>], node, sourcepath_stack):<EOL><INDENT>yield fragment<EOL><DEDENT>return<EOL><DEDENT>push = bool(node.sourcepath)<EOL>if push:<EOL><INDENT>sourcepath_stack.append(node.sourcepath)<EOL><DEDENT>nodes.append(node)<EOL>if definition is None:<EOL><INDENT>definition = dispatcher.get_optimized_definition(node)<EOL><DEDENT>for rule in definition:<EOL><INDENT>for chunk in rule(_walk, dispatcher, node):<EOL><INDENT>yield chunk<EOL><DEDENT><DEDENT>nodes.pop(-<NUM_LIT:1>)<EOL>if push:<EOL><INDENT>sourcepath_stack.pop(-<NUM_LIT:1>)<EOL><DEDENT><DEDENT>def process_layouts(layout_rule_chunks, last_chunk, chunk):<EOL><INDENT>before_text = last_chunk.text if last_chunk else None<EOL>after_text = chunk.text if chunk else None<EOL>prev_text = None<EOL>lrcs_stack = []<EOL>for lrc in layout_rule_chunks:<EOL><INDENT>lrcs_stack.append(lrc)<EOL>for idx in range(len(lrcs_stack)):<EOL><INDENT>rule = tuple(lrc.rule for lrc in lrcs_stack[idx:])<EOL>handler = dispatcher.layout(rule)<EOL>if handler is not NotImplemented:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT>lrcs_stack[:] = lrcs_stack[:idx]<EOL>lrcs_stack.append(LayoutChunk(<EOL>rule, handler,<EOL>layout_rule_chunks[idx].node,<EOL>))<EOL><DEDENT>for lr_chunk in lrcs_stack:<EOL><INDENT>gen = lr_chunk.handler(<EOL>dispatcher, lr_chunk.node, before_text, after_text, prev_text)<EOL>if not gen:<EOL><INDENT>continue<EOL><DEDENT>for chunk_from_layout in gen:<EOL><INDENT>yield chunk_from_layout<EOL>prev_text = chunk_from_layout.text<EOL><DEDENT><DEDENT><DEDENT>def walk():<EOL><INDENT>last_chunk = None<EOL>layout_rule_chunks = []<EOL>for chunk in _walk(dispatcher, node, definition):<EOL><INDENT>if isinstance(chunk, LayoutChunk):<EOL><INDENT>layout_rule_chunks.append(chunk)<EOL><DEDENT>else:<EOL><INDENT>for chunk_from_layout in process_layouts(<EOL>layout_rule_chunks, last_chunk, chunk):<EOL><INDENT>yield chunk_from_layout<EOL><DEDENT>layout_rule_chunks[:] = []<EOL>yield chunk<EOL>last_chunk = chunk<EOL><DEDENT><DEDENT>for chunk_from_layout in process_layouts(<EOL>layout_rule_chunks, last_chunk, None):<EOL><INDENT>yield chunk_from_layout<EOL><DEDENT><DEDENT>for chunk in walk():<EOL><INDENT>yield chunk<EOL><DEDENT>
The default, standalone walk function following the standard
argument ordering for the unparsing walkers.

Arguments:

dispatcher
    a Dispatcher instance, defined earlier in this module.  This
    instance will dispatch out the correct callable for the various
    object types encountered throughout this recursive function.

node
    the starting Node from asttypes.

definition
    a standalone definition tuple to start working on the node with;
    if none is provided, an initial definition will be looked up
    using the dispatcher with the node for the generation of output.

While the dispatcher object is able to provide the lookup directly,
this extra definition argument allow more flexibility in having
Token subtypes being able to provide specific definitions also that
may be required, such as the generation of optional rendering
output.