From a0578881b0579e2de8e11ac9393e8d58df8963dd Mon Sep 17 00:00:00 2001 From: apalala Date: Wed, 29 Nov 2023 10:28:03 -0400 Subject: [PATCH] [ngcodegen] add more node types to walker --- tatsu/ngcodegen/python.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tatsu/ngcodegen/python.py b/tatsu/ngcodegen/python.py index 1b01ad0f..1a41be68 100644 --- a/tatsu/ngcodegen/python.py +++ b/tatsu/ngcodegen/python.py @@ -7,6 +7,7 @@ from .. import grammars from ..collections import OrderedSet as oset +from ..exceptions import CodegenError from ..mixins.indent import IndentPrintMixin from ..util import compress_seq, safe_name, trim from ..walkers import NodeWalker @@ -69,11 +70,11 @@ def __init__(self, parser_name: str = ''): self.parser_name = parser_name @classmethod - def counter(cls): + def _next_n(cls): return next(cls._counter) @classmethod - def reset_counter(cls): + def _reset_counter(cls): cls._counter = itertools.count() def print(self, *args, **kwargs): @@ -103,6 +104,7 @@ def param_repr(p): else: return repr(p.split('::')[0]) + self._reset_counter() params = kwparams = '' if rule.params: params = ', '.join( @@ -216,6 +218,21 @@ def walk_Choice(self, choice: grammars.Choice): self.print(errors) self.print(')') + def walk_Option(self, option: grammars.Option): + self.print('with self._option():') + with self.indent(): + self.walk(option.exp) + + def walk_Closure(self, closure: grammars.Closure): + if () in closure.exp.lookahead(): + raise CodegenError(f'{self.node} may repeat empty sequence') + + n = self._next_n() + self.print(f'def block{n}():') + with self.indent(): + self.walk(closure.exp) + self.print(f'self._closure(block{n})') + def _gen_keywords(self, grammar: grammars.Grammar): keywords = [str(k) for k in grammar.keywords if k is not None] if not keywords: