Skip to content

Commit

Permalink
[ngcodegen] allow naming the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 29, 2023
1 parent 3a849fb commit 21cbaf9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tatsu/ngcodegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .python import PythonCodeGenerator


def codegen(model: ParseModel) -> str:
generator = PythonCodeGenerator()
def codegen(model: ParseModel, parser_name: str = '') -> str:
generator = PythonCodeGenerator(parser_name=parser_name)
generator.walk(model)
return generator.printed_text()
13 changes: 8 additions & 5 deletions tatsu/ngcodegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
HEADER = """\
#!/usr/bin/env python
# WARNING:
#
# CAVEAT UTILITOR
# WARNING: CAVEAT UTILITOR
#
# This file was automatically generated by TatSu.
#
Expand All @@ -35,6 +33,10 @@

class PythonCodeGenerator(IndentPrintMixin, NodeWalker):

def __init__(self, parser_name: str = ''):
super().__init__()
self.parser_name = parser_name

def print(self, *args, **kwargs):
args = [trim(arg) for arg in args]
super().print(*args, **kwargs)
Expand Down Expand Up @@ -66,9 +68,10 @@ def _gen_keywords(self, grammar: grammars.Grammar):
self.print()

def _gen_buffering(self, grammar: grammars.Grammar):
self.print(f'class {grammar.name}Buffer(Buffer):')
start = grammar.config.start or grammar.rules[0].name
name = self.parser_name or grammar.name
self.print(f'class {name}Buffer(Buffer):')

start = grammar.config.start or grammar.rules[0].name
with self.indent():
self.print('def __init__(self, text, /, config: ParserConfig | None = None, **settings):')
with self.indent():
Expand Down

0 comments on commit 21cbaf9

Please sign in to comment.