From e6a01cfa47b77e8dcb3c6919f7438efbb55222f4 Mon Sep 17 00:00:00 2001 From: Igor Dejanovic Date: Fri, 13 Sep 2024 13:55:34 +0200 Subject: [PATCH] lint: style fixes --- parglare/cli.py | 6 +++--- parglare/exceptions.py | 11 +++++------ parglare/glr.py | 35 ++++++++++++++++------------------- parglare/grammar.py | 11 +++++------ parglare/trees.py | 10 ++++------ 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/parglare/cli.py b/parglare/cli.py index 3d15578..c2cbc21 100644 --- a/parglare/cli.py +++ b/parglare/cli.py @@ -113,11 +113,11 @@ def viz(ctx, grammar_file): grammar, table = compile_get_grammar_table(grammar_file, debug, colors, prefer_shifts, prefer_shifts_over_empty) - prints("Generating '%s.dot' file for the grammar PDA." % grammar_file) + prints(f"Generating '{grammar_file}.dot' file for the grammar PDA.") prints("Use dot viewer (e.g. xdot) " - "or convert to pdf by running 'dot -Tpdf -O %s.dot'" % grammar_file) + f"or convert to pdf by running 'dot -Tpdf -O {grammar_file}.dot'") t.colors = False - grammar_pda_export(table, "%s.dot" % grammar_file) + grammar_pda_export(table, f"{grammar_file}.dot") @pglr.command() diff --git a/parglare/exceptions.py b/parglare/exceptions.py index 0b90f2e..08084f9 100644 --- a/parglare/exceptions.py +++ b/parglare/exceptions.py @@ -74,9 +74,9 @@ def __init__(self, context, actions): self.actions = actions from parglare.parser import SHIFT - message = "{}\nIn state {}:{} and input symbol '{}' after calling"\ - " dynamic disambiguation still can't decide "\ - .format(str(state), state.state_id, state.symbol, token) + message = f"{str(state)}\nIn state {state.state_id}:{state.symbol} "\ + f"and input symbol '{token}' after calling"\ + " dynamic disambiguation still can't decide " if actions[0].action == SHIFT: prod_str = " or ".join([f"'{str(a.prod)}'" for a in actions[1:]]) @@ -140,9 +140,8 @@ def __str__(self): class LRConflicts(Exception): def __init__(self, conflicts): self.conflicts = conflicts - message = "\n{} conflicts in following states: {}"\ - .format(self.kind, - set([c.state.state_id for c in conflicts])) + message = f"\n{self.kind} conflicts in following states: "\ + f"{set([c.state.state_id for c in conflicts])}" super().__init__(message) diff --git a/parglare/glr.py b/parglare/glr.py index 7bec15b..c371be1 100644 --- a/parglare/glr.py +++ b/parglare/glr.py @@ -592,28 +592,26 @@ def _trace_frontier(self): parents_processed = set() for head in self._trace_frontier_heads: - self._dot_trace += '{} [label="{}. {}:{}"];\n'\ - .format(head.key, head.frontier, head.state.state_id, - dot_escape(head.state.symbol.name)) + self._dot_trace += f'{head.key} [label="{head.frontier}. '\ + f'{head.state.state_id}:{dot_escape(head.state.symbol.name)}"];\n' for step_no, step in enumerate(self._trace_frontier_steps): step_no += 1 from_head, parent = step if parent not in parents_processed: - self._dot_trace += '{} -> {} [label="{}"];\n' \ - .format(parent.head.key, parent.root.key, - parent.ambiguity) + self._dot_trace += f'{parent.head.key} -> {parent.root.key} '\ + f'[label="{parent.ambiguity}"];\n' parents_processed.add(parent) if parent.production: # Reduce step label = f"R:{dot_escape(parent.production)}" else: # Shift step - label = "S:{}({})".format(dot_escape(parent.token.symbol.name), - dot_escape(parent.token.value)) - self._dot_trace += '{} -> {} [label="{}.{} {}" {}];\n'.format( - from_head.key, parent.head.key, parent.head.frontier, step_no, - label, TRACE_DOT_STEP_STYLE) + label = f"S:{dot_escape(parent.token.symbol.name)}"\ + f"({dot_escape(parent.token.value)})" + self._dot_trace += f'{from_head.key} -> {parent.head.key} '\ + f'[label="{parent.head.frontier}.{step_no} '\ + f'{label}" {TRACE_DOT_STEP_STYLE}];\n' self._dot_trace_ranks += \ '{{rank=same; {}; {}}}\n'.format( @@ -627,14 +625,14 @@ def _trace_frontier(self): def _trace_step_kill(self, from_head): self._dot_trace += \ f'{from_head.key}_killed [shape="diamond" fillcolor="red" label="killed"];\n' - self._dot_trace += '{} -> {}_killed [label="{}." {}];\n'\ - .format(from_head.key, from_head.key, self._debug_step_str(), - TRACE_DOT_STEP_STYLE) + self._dot_trace += \ + f'{from_head.key} -> {from_head.key}_killed '\ + f'[label="{self._debug_step_str()}." {TRACE_DOT_STEP_STYLE}];\n' @no_colors def _trace_step_drop(self, from_head, to_head): - self._dot_trace += '{} -> {} [label="drop empty" {}];\n'\ - .format(from_head.key, to_head.key, TRACE_DOT_DROP_STYLE) + self._dot_trace += f'{from_head.key} -> {to_head.key} '\ + f'[label="drop empty" {TRACE_DOT_DROP_STYLE}];\n' @no_colors def _trace_finish(self): @@ -783,9 +781,8 @@ def __hash__(self): return hash(self.id) def __str__(self): - return '{}({})<-{}({}) [{}]'.format( - self.root.id, self.root.symbol, self.head.id, self.head.symbol, - self.ambiguity) + return f'{self.root.id}({self.root.symbol})<-{self.head.id}'\ + f'({self.head.symbol}) [{self.ambiguity}]' def __repr__(self): return str(self) diff --git a/parglare/grammar.py b/parglare/grammar.py index 76d5e3e..fc646e3 100644 --- a/parglare/grammar.py +++ b/parglare/grammar.py @@ -523,10 +523,9 @@ def _make_symbols_resolution_map(self): if rec.value in terminals_by_str_rec: raise GrammarError( location=terminal.location, - message='Terminals "{}" and "{}" match ' - 'the same string.' - .format(terminal.name, - terminals_by_str_rec[rec.value].name)) + message=f'Terminals "{terminal.name}" and ' + f'"{terminals_by_str_rec[rec.value].name}" match ' + 'the same string.') terminals_by_str_rec[rec.value] = terminal terminals_by_name[terminal.name] = terminal @@ -647,8 +646,8 @@ def _load_recognizers(self): if not isinstance(symbol, Terminal): raise GrammarError( location=Location(file_name=recognizers_file), - message='Recognizer given for non-terminal "{}".' - .format(recognizer_name)) + message='Recognizer given for non-terminal ' + f'"{recognizer_name}".') symbol.recognizer = recognizer def resolve_symbol_by_name( diff --git a/parglare/trees.py b/parglare/trees.py index 4f3cbb2..d29b60c 100644 --- a/parglare/trees.py +++ b/parglare/trees.py @@ -142,9 +142,8 @@ def is_nonterm(self): return True def __str__(self): - return 'NonTerm({}, {}-{})'\ - .format(self.production.symbol, - self.start_position, self.end_position) + return f'NonTerm({self.production.symbol}, '\ + f'{self.start_position}-{self.end_position})' def __iter__(self): return iter(self.children) @@ -179,9 +178,8 @@ def is_term(self): return True def __str__(self): - return 'Term({} "{}", {}-{})'\ - .format(self.symbol, self.value[:20], - self.start_position, self.end_position) + return f'Term({self.symbol} "{self.value[:20]}", '\ + f'{self.start_position}-{self.end_position})' class Tree: