3
3
namespace PHPStan \PhpDocParser \Parser ;
4
4
5
5
use LogicException ;
6
+ use PHPStan \PhpDocParser \Ast \Comment ;
6
7
use PHPStan \PhpDocParser \Lexer \Lexer ;
7
8
use function array_pop ;
8
9
use function assert ;
@@ -19,7 +20,10 @@ class TokenIterator
19
20
20
21
private int $ index ;
21
22
22
- /** @var int[] */
23
+ /** @var list<Comment> */
24
+ private array $ comments = [];
25
+
26
+ /** @var list<array{int, list<Comment>}> */
23
27
private array $ savePoints = [];
24
28
25
29
/** @var list<int> */
@@ -152,8 +156,7 @@ public function consumeTokenType(int $tokenType): void
152
156
}
153
157
}
154
158
155
- $ this ->index ++;
156
- $ this ->skipIrrelevantTokens ();
159
+ $ this ->next ();
157
160
}
158
161
159
162
@@ -166,8 +169,7 @@ public function consumeTokenValue(int $tokenType, string $tokenValue): void
166
169
$ this ->throwError ($ tokenType , $ tokenValue );
167
170
}
168
171
169
- $ this ->index ++;
170
- $ this ->skipIrrelevantTokens ();
172
+ $ this ->next ();
171
173
}
172
174
173
175
@@ -178,12 +180,20 @@ public function tryConsumeTokenValue(string $tokenValue): bool
178
180
return false ;
179
181
}
180
182
181
- $ this ->index ++;
182
- $ this ->skipIrrelevantTokens ();
183
+ $ this ->next ();
183
184
184
185
return true ;
185
186
}
186
187
188
+ /**
189
+ * @return list<Comment>
190
+ */
191
+ public function flushComments (): array
192
+ {
193
+ $ res = $ this ->comments ;
194
+ $ this ->comments = [];
195
+ return $ res ;
196
+ }
187
197
188
198
/** @phpstan-impure */
189
199
public function tryConsumeTokenType (int $ tokenType ): bool
@@ -198,14 +208,15 @@ public function tryConsumeTokenType(int $tokenType): bool
198
208
}
199
209
}
200
210
201
- $ this ->index ++;
202
- $ this ->skipIrrelevantTokens ();
211
+ $ this ->next ();
203
212
204
213
return true ;
205
214
}
206
215
207
216
208
- /** @phpstan-impure */
217
+ /**
218
+ * @deprecated Use skipNewLineTokensAndConsumeComments instead (when parsing a type)
219
+ */
209
220
public function skipNewLineTokens (): void
210
221
{
211
222
if (!$ this ->isCurrentTokenType (Lexer::TOKEN_PHPDOC_EOL )) {
@@ -218,6 +229,29 @@ public function skipNewLineTokens(): void
218
229
}
219
230
220
231
232
+ public function skipNewLineTokensAndConsumeComments (): void
233
+ {
234
+ if ($ this ->currentTokenType () === Lexer::TOKEN_COMMENT ) {
235
+ $ this ->comments [] = new Comment ($ this ->currentTokenValue (), $ this ->currentTokenLine (), $ this ->currentTokenIndex ());
236
+ $ this ->next ();
237
+ }
238
+
239
+ if (!$ this ->isCurrentTokenType (Lexer::TOKEN_PHPDOC_EOL )) {
240
+ return ;
241
+ }
242
+
243
+ do {
244
+ $ foundNewLine = $ this ->tryConsumeTokenType (Lexer::TOKEN_PHPDOC_EOL );
245
+ if ($ this ->currentTokenType () !== Lexer::TOKEN_COMMENT ) {
246
+ continue ;
247
+ }
248
+
249
+ $ this ->comments [] = new Comment ($ this ->currentTokenValue (), $ this ->currentTokenLine (), $ this ->currentTokenIndex ());
250
+ $ this ->next ();
251
+ } while ($ foundNewLine === true );
252
+ }
253
+
254
+
221
255
private function detectNewline (): void
222
256
{
223
257
$ value = $ this ->currentTokenValue ();
@@ -293,7 +327,7 @@ public function forwardToTheEnd(): void
293
327
294
328
public function pushSavePoint (): void
295
329
{
296
- $ this ->savePoints [] = $ this ->index ;
330
+ $ this ->savePoints [] = [ $ this ->index , $ this -> comments ] ;
297
331
}
298
332
299
333
@@ -305,9 +339,9 @@ public function dropSavePoint(): void
305
339
306
340
public function rollback (): void
307
341
{
308
- $ index = array_pop ($ this ->savePoints );
309
- assert ($ index !== null );
310
- $ this ->index = $ index ;
342
+ $ savepoint = array_pop ($ this ->savePoints );
343
+ assert ($ savepoint !== null );
344
+ [ $ this ->index , $ this -> comments ] = $ savepoint ;
311
345
}
312
346
313
347
0 commit comments