Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

refactoring of PrettyPrinter: extract base class BasePrinter #571

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.394
0.1.395
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pyk"
version = "0.1.394"
version = "0.1.395"
description = ""
authors = [
"Runtime Verification, Inc. <[email protected]>",
Expand Down
51 changes: 31 additions & 20 deletions src/pyk/kast/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,25 @@
SymbolTable = dict[str, Callable[..., str]]


class PrettyPrinter:
class BasePrinter:
_lazy_symbol_table: Callable[[], SymbolTable]

def __init__(self, lazy_symbol_table: Callable[[], SymbolTable]):
self._lazy_symbol_table = lazy_symbol_table

@cached_property
def symbol_table(self) -> SymbolTable:
return self._lazy_symbol_table()

def _applied_label_str(self, symbol: str) -> Callable[..., str]:
return lambda *args: symbol + ' ( ' + ' , '.join(args) + ' )'

def get_unparser_for(self, label: str) -> Callable[..., str]:
return self._applied_label_str(label) if label not in self.symbol_table else self.symbol_table[label]


class PrettyPrinter(BasePrinter):
definition: KDefinition
_extra_unparsing_modules: Iterable[KFlatModule]
_patch_symbol_table: Callable[[SymbolTable], None] | None
_unalias: bool
_sort_collections: bool

Expand All @@ -59,23 +74,22 @@ def __init__(
unalias: bool = True,
sort_collections: bool = False,
):
def lazy_symbol_table() -> SymbolTable:
symb_table = build_symbol_table(
definition,
extra_modules=extra_unparsing_modules,
opinionated=True,
)
if patch_symbol_table is not None:
patch_symbol_table(symb_table)
return symb_table

super().__init__(lazy_symbol_table)

self.definition = definition
self._extra_unparsing_modules = extra_unparsing_modules
self._patch_symbol_table = patch_symbol_table
self._unalias = unalias
self._sort_collections = sort_collections

@cached_property
def symbol_table(self) -> SymbolTable:
symb_table = build_symbol_table(
self.definition,
extra_modules=self._extra_unparsing_modules,
opinionated=True,
)
if self._patch_symbol_table is not None:
self._patch_symbol_table(symb_table)
return symb_table

def print(self, kast: KAst) -> str:
"""Print out KAST terms/outer syntax.
- Input: KAST term.
Expand Down Expand Up @@ -172,7 +186,7 @@ def _print_kapply(self, kapply: KApply) -> str:
cell_contents = '\n'.join(unparsed_args).rstrip()
cell_str = label + '\n' + indent(cell_contents) + '\n</' + label[1:]
return cell_str.rstrip()
unparser = self._applied_label_str(label) if label not in self.symbol_table else self.symbol_table[label]
unparser = self.get_unparser_for(label)
return unparser(*unparsed_args)

def _print_kas(self, kas: KAs) -> str:
Expand Down Expand Up @@ -340,9 +354,6 @@ def join_sep(s: str) -> str:
else:
return self.print(kast)

def _applied_label_str(self, symbol: str) -> Callable[..., str]:
return lambda *args: symbol + ' ( ' + ' , '.join(args) + ' )'


def build_symbol_table(
definition: KDefinition,
Expand Down