Skip to content

Commit

Permalink
Ast: Type - fixed parsing of type in parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Mar 19, 2024
1 parent 8f448ea commit d4ffa4e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Ast/NodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public function isNext(...$types)
}


/**
* Look ahead, ignore whitespace.
* @param int|string ...$types
* @return bool
* @phpstan-impure
*/
public function isAhead(...$types)
{
return $this->stream->isAhead($types, [T_WHITESPACE]);
}


/**
* @param int|string ...$types
* @return Lexer\IToken
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function parse(NodeParser $parser)
$types[] = NamedType::parse($parser->createSubParser());
$parser->tryConsumeWhitespace();

while ($parser->isCurrent('&', '|')) {
while ($parser->isCurrent('&', '|') && !$parser->isAhead(T_ELLIPSIS, T_VARIABLE)) {
$parser->consumeAsIndentation('&', '|');
$parser->tryConsumeWhitespace();
$types[] = NamedType::parse($parser->createSubParser());
Expand Down
2 changes: 1 addition & 1 deletion src/Lexer/ITokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getCurrent();
/**
* @return IToken|NULL
*/
function getNext();
function getNext(int $position = 0);


/**
Expand Down
4 changes: 2 additions & 2 deletions src/Lexer/PhpTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function getCurrent()
/**
* @return PhpToken|NULL
*/
public function getNext()
public function getNext(int $position = 0)
{
$position = $this->position + 1;
$position = $this->position + max(0, $position) + 1;

if (!isset($this->tokens[$position])) {
return NULL;
Expand Down
32 changes: 32 additions & 0 deletions src/Lexer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,38 @@ public function isNext(...$types): bool
}


/**
* @param array<int|string> $types
* @param array<int|string> $ignoredTypes
*/
public function isAhead(array $types, array $ignoredTypes = []): bool
{
$pos = 0;
$found = FALSE;

while (($token = $this->tokens->getNext($pos)) !== NULL) {
foreach ($ignoredTypes as $ignoredType) {
if ($token->isOfType($ignoredType)) {
$pos++;
continue 2;
}
}

foreach ($types as $type) {
if ($token->isOfType($type)) {
$pos++;
$found = TRUE;
continue 2;
}
}

break;
}

return $found;
}


/**
* @return IToken
*/
Expand Down

0 comments on commit d4ffa4e

Please sign in to comment.