Skip to content

Commit

Permalink
Improve the formating of (Parent)Rules (#270)
Browse files Browse the repository at this point in the history
- Vertically compact the repr format of ParentRules with children
- Avoid empty lines in the dbg format of ParentRules with no
  children
- Don't show None as the name in the repr format of
  UnparserRuleQuantifieds

- Implement reusable computations of repr and dbg formats of
  children of ParentRules
- Use f-strings instead of .format() where possible
  • Loading branch information
akosthekiss authored Jan 26, 2025
1 parent 8c81d4f commit 294f927
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions grammarinator/runtime/rule.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Copyright (c) 2017-2024 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2025 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
# This file may not be copied, modified, or distributed except
# according to those terms.

from copy import deepcopy
from itertools import zip_longest
from math import inf
from textwrap import indent

from itertools import zip_longest


class RuleSize:
"""
Expand Down Expand Up @@ -346,16 +345,11 @@ def tokens(self):
def __str__(self):
return ''.join(str(child) for child in self.children)

def __repr__(self):
parts = [
f'name={self.name!r}',
]
if self.children:
parts.append('children=[\n{children}\n]'.format(children=indent(',\n'.join(repr(child) for child in self.children), ' ')))
return f'{self.__class__.__name__}({", ".join(parts)})'
def _repr_children(self):
return 'children=[\n{children}]'.format(children=indent(',\n'.join(repr(child) for child in self.children), ' '))

def _dbg_(self):
return '{name}\n{children}'.format(name=self.name or self.__class__.__name__, children=indent('\n'.join(child._dbg_() for child in self.children), '| '))
def _dbg_children(self):
return '\n' + indent('\n'.join(child._dbg_() for child in self.children), '| ') if self.children else ""


class UnparserRule(ParentRule):
Expand Down Expand Up @@ -387,6 +381,17 @@ def __getattr__(self, item):

return result[0] if len(result) == 1 else result

def __repr__(self):
parts = [
f'name={self.name!r}',
]
if self.children:
parts.append(self._repr_children())
return f'{self.__class__.__name__}({", ".join(parts)})'

def _dbg_(self):
return f'{self.name}{self._dbg_children()}'

def __deepcopy__(self, memo):
return UnparserRule(name=deepcopy(self.name, memo), children=[deepcopy(child, memo) for child in self.children])

Expand Down Expand Up @@ -464,15 +469,15 @@ def __repr__(self):
f'stop={self.stop}',
]
if self.children:
parts.append('children=[\n{children}\n]'.format(children=indent(',\n'.join(repr(child) for child in self.children), ' ')))
parts.append(self._repr_children())
return f'{self.__class__.__name__}({", ".join(parts)})'

def _dbg_(self):
return f'{self.__class__.__name__}:[{self.idx}]{self._dbg_children()}'

def __deepcopy__(self, memo):
return UnparserRuleQuantifier(idx=deepcopy(self.idx, memo), start=deepcopy(self.start, memo), stop=deepcopy(self.stop, memo), children=[deepcopy(child, memo) for child in self.children])

def _dbg_(self):
return '{name}:[{idx}]\n{children}'.format(idx=self.idx, name=self.__class__.__name__, children=indent('\n'.join(child._dbg_() for child in self.children), '| '))


class UnparserRuleQuantified(ParentRule):
"""
Expand All @@ -485,6 +490,12 @@ class UnparserRuleQuantified(ParentRule):
def __init__(self, *, children=None):
super().__init__(name=None, children=children)

def __repr__(self):
return f'{self.__class__.__name__}({self._repr_children() if self.children else ""})'

def _dbg_(self):
return f'{self.__class__.__name__}{self._dbg_children()}'

def __deepcopy__(self, memo):
return UnparserRuleQuantified(children=[deepcopy(child, memo) for child in self.children])

Expand All @@ -511,14 +522,13 @@ def __repr__(self):
f'idx={self.idx}',
]
if self.children:
parts.append('children=[\n{children}\n]'.format(
children=indent(',\n'.join(repr(child) for child in self.children), ' ')))
parts.append(self._repr_children())
return f'{self.__class__.__name__}({", ".join(parts)})'

def _dbg_(self):
return f'{self.__class__.__name__}:[{self.alt_idx}/{self.idx}]{self._dbg_children()}'

def __deepcopy__(self, memo):
return UnparserRuleAlternative(alt_idx=deepcopy(self.alt_idx, memo),
idx=deepcopy(self.idx, memo),
children=[deepcopy(child, memo) for child in self.children])

def _dbg_(self):
return '{name}:[{alt_idx}/{idx}]\n{children}'.format(name=self.__class__.__name__, alt_idx=self.alt_idx, idx=self.idx, children=indent('\n'.join(child._dbg_() for child in self.children), '| '))

0 comments on commit 294f927

Please sign in to comment.