Skip to content

Commit 85b06ad

Browse files
committed
add consumeAll flag
1 parent 476b363 commit 85b06ad

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/Parser/TokenIterator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class TokenIterator
2828
private $savePoints = [];
2929

3030
/** @var list<int> */
31-
private $skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS, Lexer::TOKEN_COMMENT];
31+
private $skippedTokenTypes = [
32+
Lexer::TOKEN_HORIZONTAL_WS,
33+
Lexer::TOKEN_COMMENT];
3234

3335
/** @var string|null */
3436
private $newline = null;
@@ -199,8 +201,16 @@ public function flushComments(): array
199201

200202

201203
/** @phpstan-impure */
202-
public function tryConsumeTokenType(int $tokenType): bool
204+
public function tryConsumeTokenType(int $tokenType, bool $consumeAll = false): bool
203205
{
206+
if ($consumeAll) {
207+
$found = false;
208+
while ($this->tryConsumeTokenType($tokenType)) {
209+
$found = true;
210+
}
211+
return $found;
212+
}
213+
204214
if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] !== $tokenType) {
205215
return false;
206216
}

src/Parser/TypeParser.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ private function parseConditional(TokenIterator $tokens, Ast\Type\TypeNode $subj
323323

324324
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
325325
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);
326-
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
327-
continue;
328-
}
326+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
329327

330328
$ifType = $this->parse($tokens);
331329

@@ -413,9 +411,7 @@ public function isHtml(TokenIterator $tokens): bool
413411
public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
414412
{
415413
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
416-
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
417-
continue;
418-
}
414+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
419415

420416
$genericTypes = [];
421417
$variances = [];
@@ -808,9 +804,7 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
808804
$startIndex = $tokens->currentTokenIndex();
809805

810806
// parse any comments above the item
811-
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
812-
continue;
813-
}
807+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
814808

815809
try {
816810
$tokens->pushSavePoint();
@@ -916,9 +910,7 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
916910
$startLine = $tokens->currentTokenLine();
917911
$startIndex = $tokens->currentTokenIndex();
918912

919-
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
920-
continue;
921-
}
913+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
922914

923915
$key = $this->parseObjectShapeKey($tokens);
924916
$optional = $tokens->tryConsumeTokenType(Lexer::TOKEN_NULLABLE);

0 commit comments

Comments
 (0)