1
1
from typing import Union , Callable
2
2
from functools import lru_cache
3
+ import itertools
3
4
import math
4
5
from .lexer import Lexer
5
6
from .tokens import *
@@ -61,7 +62,7 @@ def next_tkn(without_consuming: bool = False) -> Union[Token, None]:
61
62
def parse_re () -> RE :
62
63
return RE (parse_re_seq ())
63
64
64
- def parse_re_seq (capturing : bool = True , group_name : str = 'default' ) -> Union [OrNode , GroupNode ]:
65
+ def parse_re_seq (capturing : bool = True , group_name : str = None ) -> Union [OrNode , GroupNode ]:
65
66
match_start , match_end = False , False
66
67
if type (curr_tkn ) is Start or type (curr_tkn ) is Circumflex :
67
68
next_tkn ()
@@ -86,7 +87,12 @@ def parse_re_seq(capturing: bool = True, group_name: str = 'default') -> Union[O
86
87
87
88
return node
88
89
89
- def parse_group (capturing : bool = True , group_name : str = 'default' ) -> GroupNode :
90
+ def parse_group (capturing : bool = True , group_name : str = None ) -> GroupNode :
91
+ nonlocal groups_counter
92
+
93
+ if group_name is None :
94
+ group_name = "Group " + str (next (groups_counter ))
95
+
90
96
elements = deque () # holds the children of the GroupNode
91
97
92
98
while curr_tkn is not None and not isinstance (curr_tkn , OrToken ) and \
@@ -229,7 +235,8 @@ def parse_inner_el() -> RangeElement:
229
235
return RangeElement (match_str = "" .join (sorted (set (match_str ))), is_positive_logic = positive_logic )
230
236
231
237
def parse_el () -> Union [Element , OrNode , GroupNode ]:
232
- group_name = "default"
238
+ group_name : Union [str ,None ]
239
+ group_name = None
233
240
if isinstance (curr_tkn , ElementToken ):
234
241
return Element (match_ch = curr_tkn .char )
235
242
elif isinstance (curr_tkn , Wildcard ):
@@ -277,6 +284,8 @@ def parse_group_name() -> str:
277
284
raise Exception ('Unexpected empty named group name.' )
278
285
next_tkn () # consumes '>'
279
286
return group_name
287
+
288
+ groups_counter = itertools .count (start = 1 )
280
289
281
290
curr_tkn = None
282
291
next_tkn = next_tkn_initializer (re )
0 commit comments