@@ -73,51 +73,39 @@ public function parseGroups(string $regex): ?array
7373 $ captureOnlyNamed = str_contains ($ modifiers , 'n ' );
7474 }
7575
76- $ capturingGroups = [];
77- $ alternationId = -1 ;
78- $ captureGroupId = 100 ;
79- $ markVerbs = [];
80- $ this ->walkRegexAst (
76+ $ astWalkResult = $ this ->walkRegexAst (
8177 $ ast ,
8278 null ,
83- $ alternationId ,
8479 0 ,
8580 false ,
8681 null ,
87- $ captureGroupId ,
88- $ capturingGroups ,
89- $ markVerbs ,
9082 $ captureOnlyNamed ,
9183 false ,
9284 $ modifiers ,
85+ RegexAstWalkResult::createEmpty (),
9386 );
9487
95- return [$ capturingGroups , $ markVerbs ];
88+ return [$ astWalkResult -> getCapturingGroups () , $ astWalkResult -> getMarkVerbs () ];
9689 }
9790
98- /**
99- * @param array<int, RegexCapturingGroup> $capturingGroups
100- * @param list<string> $markVerbs
101- */
10291 private function walkRegexAst (
10392 TreeNode $ ast ,
10493 ?RegexAlternation $ alternation ,
105- int &$ alternationId ,
10694 int $ combinationIndex ,
10795 bool $ inOptionalQuantification ,
10896 RegexCapturingGroup |RegexNonCapturingGroup |null $ parentGroup ,
109- int &$ captureGroupId ,
110- array &$ capturingGroups ,
111- array &$ markVerbs ,
11297 bool $ captureOnlyNamed ,
11398 bool $ repeatedMoreThanOnce ,
11499 string $ patternModifiers ,
115- ): void
100+ RegexAstWalkResult $ astWalkResult ,
101+ ): RegexAstWalkResult
116102 {
117103 $ group = null ;
118104 if ($ ast ->getId () === '#capturing ' ) {
105+ $ astWalkResult = $ astWalkResult ->nextCaptureGroupId ();
106+
119107 $ group = new RegexCapturingGroup (
120- $ captureGroupId ++ ,
108+ $ astWalkResult -> getCaptureGroupId () ,
121109 null ,
122110 $ alternation ,
123111 $ inOptionalQuantification ,
@@ -130,9 +118,11 @@ private function walkRegexAst(
130118 );
131119 $ parentGroup = $ group ;
132120 } elseif ($ ast ->getId () === '#namedcapturing ' ) {
121+ $ astWalkResult = $ astWalkResult ->nextCaptureGroupId ();
122+
133123 $ name = $ ast ->getChild (0 )->getValueValue ();
134124 $ group = new RegexCapturingGroup (
135- $ captureGroupId ++ ,
125+ $ astWalkResult -> getCaptureGroupId () ,
136126 $ name ,
137127 $ alternation ,
138128 $ inOptionalQuantification ,
@@ -176,40 +166,36 @@ private function walkRegexAst(
176166 }
177167
178168 if ($ ast ->getId () === '#alternation ' ) {
179- $ alternationId ++ ;
180- $ alternation = new RegexAlternation ($ alternationId , count ($ ast ->getChildren ()));
169+ $ astWalkResult = $ astWalkResult -> nextAlternationId () ;
170+ $ alternation = new RegexAlternation ($ astWalkResult -> getAlternationId () , count ($ ast ->getChildren ()));
181171 }
182172
183173 if ($ ast ->getId () === '#mark ' ) {
184- $ markVerbs [] = $ ast ->getChild (0 )->getValueValue ();
185- return ;
174+ return $ astWalkResult ->markVerb ($ ast ->getChild (0 )->getValueValue ());
186175 }
187176
188177 if (
189178 $ group instanceof RegexCapturingGroup &&
190179 (!$ captureOnlyNamed || $ group ->isNamed ())
191180 ) {
192- $ capturingGroups [ $ group -> getId ()] = $ group ;
181+ $ astWalkResult = $ astWalkResult -> addCapturingGroup ( $ group) ;
193182
194183 if ($ alternation !== null ) {
195184 $ alternation ->pushGroup ($ combinationIndex , $ group );
196185 }
197186 }
198187
199188 foreach ($ ast ->getChildren () as $ child ) {
200- $ this ->walkRegexAst (
189+ $ astWalkResult = $ this ->walkRegexAst (
201190 $ child ,
202191 $ alternation ,
203- $ alternationId ,
204192 $ combinationIndex ,
205193 $ inOptionalQuantification ,
206194 $ parentGroup ,
207- $ captureGroupId ,
208- $ capturingGroups ,
209- $ markVerbs ,
210195 $ captureOnlyNamed ,
211196 $ repeatedMoreThanOnce ,
212197 $ patternModifiers ,
198+ $ astWalkResult ,
213199 );
214200
215201 if ($ ast ->getId () !== '#alternation ' ) {
@@ -218,6 +204,8 @@ private function walkRegexAst(
218204
219205 $ combinationIndex ++;
220206 }
207+
208+ return $ astWalkResult ;
221209 }
222210
223211 private function allowConstantTypes (
0 commit comments