Skip to content

Commit

Permalink
[ngcodegen] add more node types to walker
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 29, 2023
1 parent 0bdd945 commit a057888
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions tatsu/ngcodegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -103,6 +104,7 @@ def param_repr(p):
else:
return repr(p.split('::')[0])

self._reset_counter()
params = kwparams = ''
if rule.params:
params = ', '.join(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a057888

Please sign in to comment.