Print Translation¶
竜 TatSu doesn’t impose a way to create translators, but it exposes the facilities it uses to generate the Python source code for parsers.
Translation in 竜 TatSu is based on subclasses of Walker
and on classes that
inherit from IndentPrintMixin
, a strategy copied from the new PEG parser
in Python (see PEP 617).
IndentPrintMixin
provides an indent()
method, which is a context manager,
and should be used thus:
class MyTranslationWalker(NodeWalker, IndentPrintMixin):
def walk_SomeNode(self, node):
with self.indent():
# ccontinue walking the tree
The self.print()
method takes note of the current level of indentation, so
output will be indented by the indent
passed to
the IndentPrintConstructor
:
def walk_SomeNode(self, node):
with self.indent():
self.print(walk_expression(node.exp))
The printed code can be retrieved using the printed_text()
method. Other
posibilities are available by assigning a text-like object to
self.output_stream
in the __init__()
method.