Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented WIP grammar import memoization #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions parglare/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ class GrammarContext:
ignore_case: bool = False
imported_with: Optional['PGFileImport'] = None
inline_terminals: Dict = field(default_factory=dict)
imports: dict[str, 'PGFileImport'] = field(default_factory=dict)


class PGFile:
Expand Down Expand Up @@ -1185,14 +1186,18 @@ class PGFileImport:
pgfile (PGFile instance or None):

"""
def __init__(self, module_name: str, file_path: str,
context: GrammarContext):
def __new__(cls, module_name: str, file_path: str,
context: GrammarContext):
if file_path in context.imports:
return context.imports[file_path]
context.imports[file_path] = self = super().__new__(cls)
self.module_name = module_name
self.file_path: str = file_path
self.context = context
self.imported_with: Optional[PGFileImport] = context.imported_with
self.grammar: Optional[Grammar] = None
self.pgfile: Optional[PGFile] = None
return self

@property
def fqn(self):
Expand Down