@@ -73,51 +73,39 @@ public function parseGroups(string $regex): ?array
73
73
$ captureOnlyNamed = str_contains ($ modifiers , 'n ' );
74
74
}
75
75
76
- $ capturingGroups = [];
77
- $ alternationId = -1 ;
78
- $ captureGroupId = 100 ;
79
- $ markVerbs = [];
80
- $ this ->walkRegexAst (
76
+ $ astWalkResult = $ this ->walkRegexAst (
81
77
$ ast ,
82
78
null ,
83
- $ alternationId ,
84
79
0 ,
85
80
false ,
86
81
null ,
87
- $ captureGroupId ,
88
- $ capturingGroups ,
89
- $ markVerbs ,
90
82
$ captureOnlyNamed ,
91
83
false ,
92
84
$ modifiers ,
85
+ RegexAstWalkResult::createEmpty (),
93
86
);
94
87
95
- return [$ capturingGroups , $ markVerbs ];
88
+ return [$ astWalkResult -> getCapturingGroups () , $ astWalkResult -> getMarkVerbs () ];
96
89
}
97
90
98
- /**
99
- * @param array<int, RegexCapturingGroup> $capturingGroups
100
- * @param list<string> $markVerbs
101
- */
102
91
private function walkRegexAst (
103
92
TreeNode $ ast ,
104
93
?RegexAlternation $ alternation ,
105
- int &$ alternationId ,
106
94
int $ combinationIndex ,
107
95
bool $ inOptionalQuantification ,
108
96
RegexCapturingGroup |RegexNonCapturingGroup |null $ parentGroup ,
109
- int &$ captureGroupId ,
110
- array &$ capturingGroups ,
111
- array &$ markVerbs ,
112
97
bool $ captureOnlyNamed ,
113
98
bool $ repeatedMoreThanOnce ,
114
99
string $ patternModifiers ,
115
- ): void
100
+ RegexAstWalkResult $ astWalkResult ,
101
+ ): RegexAstWalkResult
116
102
{
117
103
$ group = null ;
118
104
if ($ ast ->getId () === '#capturing ' ) {
105
+ $ astWalkResult = $ astWalkResult ->nextCaptureGroupId ();
106
+
119
107
$ group = new RegexCapturingGroup (
120
- $ captureGroupId ++ ,
108
+ $ astWalkResult -> getCaptureGroupId () ,
121
109
null ,
122
110
$ alternation ,
123
111
$ inOptionalQuantification ,
@@ -130,9 +118,11 @@ private function walkRegexAst(
130
118
);
131
119
$ parentGroup = $ group ;
132
120
} elseif ($ ast ->getId () === '#namedcapturing ' ) {
121
+ $ astWalkResult = $ astWalkResult ->nextCaptureGroupId ();
122
+
133
123
$ name = $ ast ->getChild (0 )->getValueValue ();
134
124
$ group = new RegexCapturingGroup (
135
- $ captureGroupId ++ ,
125
+ $ astWalkResult -> getCaptureGroupId () ,
136
126
$ name ,
137
127
$ alternation ,
138
128
$ inOptionalQuantification ,
@@ -176,40 +166,36 @@ private function walkRegexAst(
176
166
}
177
167
178
168
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 ()));
181
171
}
182
172
183
173
if ($ ast ->getId () === '#mark ' ) {
184
- $ markVerbs [] = $ ast ->getChild (0 )->getValueValue ();
185
- return ;
174
+ return $ astWalkResult ->markVerb ($ ast ->getChild (0 )->getValueValue ());
186
175
}
187
176
188
177
if (
189
178
$ group instanceof RegexCapturingGroup &&
190
179
(!$ captureOnlyNamed || $ group ->isNamed ())
191
180
) {
192
- $ capturingGroups [ $ group -> getId ()] = $ group ;
181
+ $ astWalkResult = $ astWalkResult -> addCapturingGroup ( $ group) ;
193
182
194
183
if ($ alternation !== null ) {
195
184
$ alternation ->pushGroup ($ combinationIndex , $ group );
196
185
}
197
186
}
198
187
199
188
foreach ($ ast ->getChildren () as $ child ) {
200
- $ this ->walkRegexAst (
189
+ $ astWalkResult = $ this ->walkRegexAst (
201
190
$ child ,
202
191
$ alternation ,
203
- $ alternationId ,
204
192
$ combinationIndex ,
205
193
$ inOptionalQuantification ,
206
194
$ parentGroup ,
207
- $ captureGroupId ,
208
- $ capturingGroups ,
209
- $ markVerbs ,
210
195
$ captureOnlyNamed ,
211
196
$ repeatedMoreThanOnce ,
212
197
$ patternModifiers ,
198
+ $ astWalkResult ,
213
199
);
214
200
215
201
if ($ ast ->getId () !== '#alternation ' ) {
@@ -218,6 +204,8 @@ private function walkRegexAst(
218
204
219
205
$ combinationIndex ++;
220
206
}
207
+
208
+ return $ astWalkResult ;
221
209
}
222
210
223
211
private function allowConstantTypes (
0 commit comments