From 294f927c604862620a6ba0664cb433e3642b0dc9 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Sun, 26 Jan 2025 17:57:23 +0100 Subject: [PATCH] Improve the formating of (Parent)Rules (#270) - 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 --- grammarinator/runtime/rule.py | 52 +++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/grammarinator/runtime/rule.py b/grammarinator/runtime/rule.py index 611e78b..b207b2e 100644 --- a/grammarinator/runtime/rule.py +++ b/grammarinator/runtime/rule.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2024 Renata Hodovan, Akos Kiss. +# Copyright (c) 2017-2025 Renata Hodovan, Akos Kiss. # # Licensed under the BSD 3-Clause License # . @@ -6,11 +6,10 @@ # 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: """ @@ -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): @@ -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]) @@ -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): """ @@ -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]) @@ -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), '| '))