Skip to content

Commit

Permalink
lint: style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Sep 13, 2024
1 parent a5f53db commit e6a01cf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 40 deletions.
6 changes: 3 additions & 3 deletions parglare/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 5 additions & 6 deletions parglare/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]])
Expand Down Expand Up @@ -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)


Expand Down
35 changes: 16 additions & 19 deletions parglare/glr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions parglare/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
10 changes: 4 additions & 6 deletions parglare/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e6a01cf

Please sign in to comment.