Skip to content

Commit

Permalink
make re patterns private
Browse files Browse the repository at this point in the history
  • Loading branch information
vfazio committed Dec 27, 2024
1 parent bdaadf7 commit ae00ecf
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions tatsu/infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import copy
import dataclasses
import re
from collections.abc import Callable, Mapping
from collections.abc import Callable, MutableMapping
from itertools import starmap
from typing import Any, NamedTuple
from warnings import warn

from .ast import AST
from .tokenizing import Tokenizer
Expand All @@ -30,8 +31,8 @@ class ParserConfig:
start_rule: str | None = None # FIXME
rule_name: str | None = None # Backward compatibility

comments_re: re.Pattern | None = None
eol_comments_re: re.Pattern | None = None
_comments_re: re.Pattern | None = dataclasses.field(default=None, init=False, repr=False)
_eol_comments_re: re.Pattern | None = dataclasses.field(default=None, init=False, repr=False)

tokenizercls: type[Tokenizer] | None = None # FIXME
semantics: type | None = None
Expand Down Expand Up @@ -64,9 +65,19 @@ def __post_init__(self): # pylint: disable=W0235
if self.ignorecase:
self.keywords = [k.upper() for k in self.keywords]
if self.comments:
self.comments_re = re.compile(self.comments)
self._comments_re = re.compile(self.comments)
if self.eol_comments:
self.eol_comments_re = re.compile(self.eol_comments)
self._eol_comments_re = re.compile(self.eol_comments)

@property
def comments_re(self) -> re.Pattern:
warn(f"{self.__class__.name}.comments_re is deprecated", DeprecationWarning)
return self._comments_re

@property
def eol_comments_re(self) -> re.Pattern:
warn(f"{self.__class__.name}.eol_comments_re is deprecated. Use ", DeprecationWarning)
return self._eol_comments_re

@classmethod
def new(
Expand All @@ -84,7 +95,7 @@ def effective_rule_name(self):
# note: there are legacy reasons for this mess
return self.start_rule or self.rule_name or self.start

def _find_common(self, **settings: Any) -> Mapping[str, Any]:
def _find_common(self, **settings: Any) -> MutableMapping[str, Any]:
return {
name: value
for name, value in settings.items()
Expand All @@ -103,6 +114,11 @@ def replace_config(

def replace(self, **settings: Any) -> ParserConfig:
overrides = self._find_common(**settings)
for field in [
field.name for field in dataclasses.fields(self) if field.init == False
]:
if field in overrides.keys():
overrides.pop(field)
result = dataclasses.replace(self, **overrides)
if 'grammar' in overrides:
result.name = result.grammar
Expand Down

0 comments on commit ae00ecf

Please sign in to comment.