Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
added Token::is()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 9, 2022
1 parent 746e8d9 commit 370c5e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/Tokenizer/Stream.php
Expand Up @@ -161,13 +161,8 @@ public function joinUntil(...$args): string
*/
public function isCurrent(...$args): bool
{
if (!isset($this->tokens[$this->position])) {
return false;
}

$token = $this->tokens[$this->position];
return in_array($token->value, $args, true)
|| in_array($token->type, $args, true);
$token = $this->tokens[$this->position] ?? null;
return $token && $token->is(...$args);
}


Expand Down Expand Up @@ -236,10 +231,7 @@ protected function scan(
$token = $this->tokens[$pos];
if (
!$wanted
|| (
in_array($token->value, $wanted, true)
|| in_array($token->type, $wanted, true)
) ^ $until
|| $token->is(...$wanted) ^ $until
) {
while ($advance && !$prev && $pos > $this->position) {
$this->next();
Expand All @@ -252,7 +244,7 @@ protected function scan(
} else {
$res[] = $token;
}
} elseif ($until || !in_array($token->type, $this->ignored, true)) {
} elseif ($until || !$token->is(...$this->ignored)) {
return $res;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Tokenizer/Token.php
Expand Up @@ -31,4 +31,12 @@ public function __construct(string $value, $type, int $offset)
$this->type = $type;
$this->offset = $offset;
}


/** @param int|string ...$args */
public function is(...$args): bool
{
return in_array($this->value, $args, true)
|| in_array($this->type, $args, true);
}
}

0 comments on commit 370c5e4

Please sign in to comment.