Skip to content

Commit

Permalink
[ngcodegen][lint] clear linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 29, 2023
1 parent e38a3dc commit 0cc8f0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
49 changes: 14 additions & 35 deletions tatsu/ngcodegen/objectmodel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from collections import namedtuple
from typing import cast

from .. import grammars, objectmodel
from ..mixins.indent import IndentPrintMixin
from ..util import safe_name, compress_seq
from ..walkers import NodeWalker

from ..util import compress_seq, safe_name

HEADER = """\
#!/usr/bin/env python3
Expand Down Expand Up @@ -42,12 +41,12 @@ def __init__(self, context=None, types=None):
"""


BaseClassSpec = namedtuple('TypeSpec', ['class_name', 'base'])
BaseClassSpec = namedtuple('BaseClassSpec', ['class_name', 'base'])


def modelgen(model: grammars.Model, parser_name: str = '', base_type: type = objectmodel.Node) -> str:
generator = PythonModelGenerator(parser_name=parser_name, base_type=base_type)
return generator.generate_model(model)
return generator.generate_model(cast(grammars.Grammar, model))


class PythonModelGenerator(IndentPrintMixin):
Expand All @@ -58,16 +57,16 @@ def __init__(self, parser_name: str = '', base_type: type = objectmodel.Node):
self.parser_name = parser_name or None

def generate_model(self, grammar: grammars.Grammar):
base_type_qual = self.base_type.__module__
base_type_import = f'from {self.base_type.__module__} import {self.base_type.__name__.split('.')[-1]}'
base_type = self.base_type
base_type_import = f'from {base_type.__module__} import {base_type.__name__.split('.')[-1]}'

self.parser_name = self.parser_name or grammar.name
self.print(
HEADER.format(
name=self.parser_name,
base_type=self.base_type.__name__,
base_type_import=base_type_import,
)
),
)
self.print()
self.print()
Expand Down Expand Up @@ -95,10 +94,11 @@ def generate_model(self, grammar: grammars.Grammar):

return self.printed_text()

def _gen_base_class(self, spec: BaseClassSpec):
def _gen_base_class(self, specs: list[BaseClassSpec]):
self.print()
self.print()
if spec.base:
spec = specs[0]
if specs[0].base:
self.print(f'class {spec.class_name}({spec.base}):')
else:
self.print(f'class {spec.class_name}:')
Expand All @@ -121,34 +121,13 @@ def _gen_rule_class(self, rule: grammars.Rule, specs: list[BaseClassSpec]):
for arg in arguments:
self.print(f'{arg}: Any = None')

def walk_Rule(self, rule: grammars.Rule):
specs = self._base_class_specs(rule)
if not specs:
return

arguments = sorted({safe_name(d) for d, _ in compress_seq(rule.defines())})

self.print()
self.print()

node_spec = specs[0]
base_specs = list(reversed(specs[1:]))
base = base_specs and base_specs[0] or f'{self.parser_name}ModelBase'
self.print(f'class {node_spec.class_name}({base}):')

with self.indent():
if not arguments:
self.print('pass')
for arg in arguments:
self.print(f'{arg}: Any = None')

def _base_class_specs(self, rule: grammars.Rule) -> BaseClassSpec:
def _base_class_specs(self, rule: grammars.Rule) -> list[BaseClassSpec]:
if not rule.params:
return ()
return []

spec = rule.params[0].split('::')
class_names = [safe_name(n) for n in spec] + [f'{self.parser_name}ModelBase']
return tuple(
return [
BaseClassSpec(class_name, class_names[i + 1])
for i, class_name in enumerate(class_names[:-1])
)
]
4 changes: 2 additions & 2 deletions tatsu/ngcodegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..collections import OrderedSet as oset
from ..exceptions import CodegenError
from ..mixins.indent import IndentPrintMixin
from ..util import compress_seq, safe_name, trim
from ..util import compress_seq, safe_name
from ..walkers import NodeWalker

HEADER = """\
Expand Down Expand Up @@ -379,7 +379,7 @@ def _gen_defines_declaration(self, node: grammars.Model):

def _gen_block(self, exp: grammars.Model, name='block'):
if () in exp.lookahead():
raise CodegenError(f'{self.node} may repeat empty sequence')
raise CodegenError(f'{exp} may repeat empty sequence')

n = self._next_n()
self.print()
Expand Down

0 comments on commit 0cc8f0d

Please sign in to comment.