Skip to content

Commit 8ca4bdd

Browse files
committed
TokenStream: refactoring
1 parent b4dffa5 commit 8ca4bdd

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Neon/Token.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function __construct(
2727
public string $text,
2828
) {
2929
}
30+
31+
32+
public function is(int|string ...$kind): bool
33+
{
34+
return in_array($this->type, $kind, strict: true);
35+
}
3036
}

src/Neon/TokenStream.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,27 @@ public function seek(int $index): void
3535
}
3636

3737

38-
public function is(int|string ...$types): bool
38+
/**
39+
* Tells whether the token at current position is of given kind.
40+
*/
41+
public function is(int|string ...$kind): bool
3942
{
40-
while (in_array($this->tokens[$this->index]->type, [Token::Comment, Token::Whitespace], strict: true)) {
43+
while ($this->tokens[$this->index]->is(Token::Comment, Token::Whitespace)) {
4144
$this->index++;
4245
}
4346

44-
return $types
45-
? in_array($this->tokens[$this->index]->type, $types, strict: true)
47+
return $kind
48+
? $this->tokens[$this->index]->is(...$kind)
4649
: $this->tokens[$this->index]->type !== Token::End;
4750
}
4851

4952

50-
public function tryConsume(int|string ...$types): ?Token
53+
/**
54+
* Consumes the current token of given kind or returns null.
55+
*/
56+
public function tryConsume(int|string ...$kind): ?Token
5157
{
52-
return $this->is(...$types)
58+
return $this->is(...$kind)
5359
? $this->tokens[$this->index++]
5460
: null;
5561
}

0 commit comments

Comments
 (0)