3
3
import copy
4
4
import dataclasses
5
5
import re
6
- from collections .abc import Callable , Mapping
6
+ from collections .abc import Callable , MutableMapping
7
7
from itertools import starmap
8
8
from typing import Any , NamedTuple
9
+ from warnings import warn
9
10
10
11
from .ast import AST
11
12
from .tokenizing import Tokenizer
@@ -30,8 +31,8 @@ class ParserConfig:
30
31
start_rule : str | None = None # FIXME
31
32
rule_name : str | None = None # Backward compatibility
32
33
33
- comments_re : re .Pattern | None = None
34
- eol_comments_re : re .Pattern | None = None
34
+ _comments_re : re .Pattern | None = dataclasses . field ( default = None , init = False , repr = False )
35
+ _eol_comments_re : re .Pattern | None = dataclasses . field ( default = None , init = False , repr = False )
35
36
36
37
tokenizercls : type [Tokenizer ] | None = None # FIXME
37
38
semantics : type | None = None
@@ -64,9 +65,19 @@ def __post_init__(self): # pylint: disable=W0235
64
65
if self .ignorecase :
65
66
self .keywords = [k .upper () for k in self .keywords ]
66
67
if self .comments :
67
- self .comments_re = re .compile (self .comments )
68
+ self ._comments_re = re .compile (self .comments )
68
69
if self .eol_comments :
69
- self .eol_comments_re = re .compile (self .eol_comments )
70
+ self ._eol_comments_re = re .compile (self .eol_comments )
71
+
72
+ @property
73
+ def comments_re (self ) -> re .Pattern :
74
+ warn (f"{ self .__class__ .name } .comments_re is deprecated" , DeprecationWarning )
75
+ return self ._comments_re
76
+
77
+ @property
78
+ def eol_comments_re (self ) -> re .Pattern :
79
+ warn (f"{ self .__class__ .name } .eol_comments_re is deprecated. Use " , DeprecationWarning )
80
+ return self ._eol_comments_re
70
81
71
82
@classmethod
72
83
def new (
@@ -84,7 +95,7 @@ def effective_rule_name(self):
84
95
# note: there are legacy reasons for this mess
85
96
return self .start_rule or self .rule_name or self .start
86
97
87
- def _find_common (self , ** settings : Any ) -> Mapping [str , Any ]:
98
+ def _find_common (self , ** settings : Any ) -> MutableMapping [str , Any ]:
88
99
return {
89
100
name : value
90
101
for name , value in settings .items ()
@@ -103,6 +114,11 @@ def replace_config(
103
114
104
115
def replace (self , ** settings : Any ) -> ParserConfig :
105
116
overrides = self ._find_common (** settings )
117
+ for field in [
118
+ field .name for field in dataclasses .fields (self ) if field .init == False
119
+ ]:
120
+ if field in overrides .keys ():
121
+ overrides .pop (field )
106
122
result = dataclasses .replace (self , ** overrides )
107
123
if 'grammar' in overrides :
108
124
result .name = result .grammar
0 commit comments