Skip to content

Commit

Permalink
introduce tryConsumeTokenTypeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
shmax committed Oct 4, 2023
1 parent b00d20a commit 1c3f514
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 9 additions & 8 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,19 @@ public function flushComments(): array
return $res;
}


/** @phpstan-impure */
public function tryConsumeTokenType(int $tokenType, bool $consumeAll = false): bool
public function tryConsumeTokenTypeAll(int $tokenType): bool
{
if ($consumeAll) {
$found = false;
while ($this->tryConsumeTokenType($tokenType)) {
$found = true;
}
return $found;
$found = false;
while ($this->tryConsumeTokenType($tokenType)) {
$found = true;
}
return $found;
}

/** @phpstan-impure */
public function tryConsumeTokenType(int $tokenType): bool
{
if ($this->tokens[$this->index][Lexer::TYPE_OFFSET] !== $tokenType) {
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private function parseConditional(TokenIterator $tokens, Ast\Type\TypeNode $subj

$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
$tokens->tryConsumeTokenTypeAll(Lexer::TOKEN_PHPDOC_EOL);

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

Expand Down Expand Up @@ -411,7 +411,7 @@ public function isHtml(TokenIterator $tokens): bool
public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
$tokens->tryConsumeTokenTypeAll(Lexer::TOKEN_PHPDOC_EOL);

$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
Expand Down Expand Up @@ -482,7 +482,7 @@ private function parseCallable(TokenIterator $tokens, Ast\Type\IdentifierTypeNod
$parameters[] = $this->parseCallableParameter($tokens);
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
$tokens->tryConsumeTokenTypeAll(Lexer::TOKEN_PHPDOC_EOL);
if ($tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_PARENTHESES)) {
break;
}
Expand Down Expand Up @@ -796,7 +796,7 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
$startIndex = $tokens->currentTokenIndex();

// parse any comments above the item
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
$tokens->tryConsumeTokenTypeAll(Lexer::TOKEN_PHPDOC_EOL);

try {
$tokens->pushSavePoint();
Expand Down Expand Up @@ -902,7 +902,7 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();

$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL, true);
$tokens->tryConsumeTokenTypeAll(Lexer::TOKEN_PHPDOC_EOL);

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

0 comments on commit 1c3f514

Please sign in to comment.