diff --git a/tatsu/ngcodegen/__init__.py b/tatsu/ngcodegen/__init__.py index 0c8c1d96..01d5264d 100644 --- a/tatsu/ngcodegen/__init__.py +++ b/tatsu/ngcodegen/__init__.py @@ -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() diff --git a/tatsu/ngcodegen/python.py b/tatsu/ngcodegen/python.py index 5650ff81..5664f82b 100644 --- a/tatsu/ngcodegen/python.py +++ b/tatsu/ngcodegen/python.py @@ -8,9 +8,7 @@ HEADER = """\ #!/usr/bin/env python - # WARNING: - # - # CAVEAT UTILITOR + # WARNING: CAVEAT UTILITOR # # This file was automatically generated by TatSu. # @@ -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) @@ -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():