diff --git a/src/Neon/Token.php b/src/Neon/Token.php index 0671aff..9985185 100644 --- a/src/Neon/Token.php +++ b/src/Neon/Token.php @@ -27,4 +27,10 @@ public function __construct( public string $text, ) { } + + + public function is(int|string ...$kind): bool + { + return in_array($this->type, $kind, strict: true); + } } diff --git a/src/Neon/TokenStream.php b/src/Neon/TokenStream.php index 3f44e42..e756ba2 100644 --- a/src/Neon/TokenStream.php +++ b/src/Neon/TokenStream.php @@ -35,21 +35,27 @@ public function seek(int $index): void } - public function is(int|string ...$types): bool + /** + * Tells whether the token at current position is of given kind. + */ + public function is(int|string ...$kind): bool { - while (in_array($this->tokens[$this->index]->type, [Token::Comment, Token::Whitespace], strict: true)) { + while ($this->tokens[$this->index]->is(Token::Comment, Token::Whitespace)) { $this->index++; } - return $types - ? in_array($this->tokens[$this->index]->type, $types, strict: true) + return $kind + ? $this->tokens[$this->index]->is(...$kind) : $this->tokens[$this->index]->type !== Token::End; } - public function tryConsume(int|string ...$types): ?Token + /** + * Consumes the current token of given kind or returns null. + */ + public function tryConsume(int|string ...$kind): ?Token { - return $this->is(...$types) + return $this->is(...$kind) ? $this->tokens[$this->index++] : null; }