Skip to content

Commit

Permalink
[ngcodegen] bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 29, 2023
1 parent 367f730 commit 37c0dc4
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions tatsu/ngcodegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ def param_repr(p):
elif kwparams:
params = kwparams

if not isinstance(rule.exp, grammars.Choice):
with self.indent():
self._gen_defines_declaration(rule)

leftrec = '\n@leftrec' if rule.is_leftrec else ''
nomemo = (
'\n@nomemo'
Expand All @@ -145,6 +141,9 @@ def _{rule.name}_(self):
)
with self.indent():
self.print(self.walk(rule.exp))
if not isinstance(rule.exp, grammars.Choice):
self._gen_defines_declaration(rule)


def walk_BasedRule(self, rule: grammars.BasedRule):
# FIXME: the following override is to not alter the previous codegen
Expand All @@ -154,7 +153,7 @@ def walk_BasedRule(self, rule: grammars.BasedRule):
def walk_RuleRef(self, ref: grammars.RuleRef):
self.print(f'self._{ref.name}_()')

def wal_RuleInclude(self, include: grammars.RuleInclude):
def walk_RuleInclude(self, include: grammars.RuleInclude):
self.walk(include.rule.exp)

def walk_Void(self, void: grammars.Void):
Expand Down Expand Up @@ -208,8 +207,7 @@ def walk_NegativeLookahead(self, lookahead: grammars.NegativeLookahead):

def walk_Sequence(self, seq: grammars.Sequence):
self.walk(seq.sequence)
with self.indent():
self._gen_defines_declaration(seq)
self._gen_defines_declaration(seq)

def walk_Choice(self, choice: grammars.Choice):
if len(choice.options) == 1:
Expand Down Expand Up @@ -274,14 +272,14 @@ def walk_RightJoin(self, join: grammars.RightJoin):
self.print(f'self._right_join(block{n}, sep{n})')

def walk_Gather(self, gather: grammars.Gather):
n = self._gen_block(gather, name='sep')
m = self._gen_block(gather, name='sep')
n = self._gen_block(gather)
self.print(f'self._gather(block{n}, sep{n})')
self.print(f'self._gather(block{n}, sep{m})')

def walk_PositiveGather(self, gather: grammars.PositiveGather):
n = self._gen_block(gather, name='sep')
m = self._gen_block(gather, name='sep')
n = self._gen_block(gather)
self.print(f'self._positive_gather(block{n}, sep{n})')
self.print(f'self._positive_gather(block{n}, sep{m})')

def walk_SkipTo(self, skipto: grammars.SkipTo):
n = self._gen_block(skipto)
Expand Down Expand Up @@ -325,7 +323,7 @@ def _gen_init(self, grammar: grammars.Grammar):
config = ParserConfig.new(
config,
owner=self,
whitespace={grammar.config.whitespace},
whitespace={grammar.config.whitespace!r},
nameguard={grammar.config.nameguard},
ignorecase={grammar.config.ignorecase},
namechars={grammar.config.namechars or None},
Expand All @@ -336,7 +334,6 @@ def _gen_init(self, grammar: grammars.Grammar):
start={start!r},
)
config = config.replace(**settings)
super().__init__(text, config=config)
''',
)
self.print()
Expand All @@ -348,6 +345,7 @@ def _gen_buffering(self, grammar: grammars.Grammar):
self.print('def __init__(self, text, /, config: ParserConfig | None = None, **settings):')
with self.indent():
self._gen_init(grammar)
self.print('super().__init__(text, config=config)')
self.print()


Expand All @@ -357,6 +355,7 @@ def _gen_parsing(self, grammar: grammars.Grammar):
self.print('def __init__(self, /, config: ParserConfig | None = None, **settings):')
with self.indent():
self._gen_init(grammar)
self.print('super().__init__(config=config)')
self.walk(grammar.rules)

def _gen_defines_declaration(self, node: grammars.Model):
Expand Down

0 comments on commit 37c0dc4

Please sign in to comment.