Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ class Token
*/
public const TYPE_LABEL = 10;

/**
* All tokens types
*/
public const TYPE_ALL = [
Token::TYPE_NONE,
Token::TYPE_KEYWORD,
Token::TYPE_OPERATOR,
Token::TYPE_WHITESPACE,
Token::TYPE_COMMENT,
Token::TYPE_BOOL,
Token::TYPE_NUMBER,
Token::TYPE_STRING,
Token::TYPE_SYMBOL,
Token::TYPE_DELIMITER,
Token::TYPE_LABEL,
];

// Flags that describe the tokens in more detail.
// All keywords must have flag 1 so `Context::isKeyword` method doesn't
// require strict comparison.
Expand Down
30 changes: 27 additions & 3 deletions src/TokensList.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getNext()
*/
public function getPrevious(): ?Token
{
for (; $this->idx > 0; --$this->idx) {
for (; $this->idx >= 0; --$this->idx) {
if (
($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
&& ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
Expand All @@ -131,17 +131,41 @@ public function getPrevious(): ?Token
return null;
}

/**
* Gets the previous token.
*
* @param int|int[] $type the type
*
* @return Token|null
*/
public function getPreviousOfType($type)
{
if (!is_array($type)) {
$type = [$type];
}
for (; $this->idx >= 0; --$this->idx) {
if (in_array($this->tokens[$this->idx]->type, $type, true)) {
return $this->tokens[$this->idx--];
}
}

return null;
}

/**
* Gets the next token.
*
* @param int $type the type
* @param int|int[] $type the type
*
* @return Token|null
*/
public function getNextOfType($type)
{
if (!is_array($type)) {
$type = [$type];
}
for (; $this->idx < $this->count; ++$this->idx) {
if ($this->tokens[$this->idx]->type === $type) {
if (in_array($this->tokens[$this->idx]->type, $type, true)) {
return $this->tokens[$this->idx++];
}
}
Expand Down
17 changes: 15 additions & 2 deletions tests/Lexer/TokensListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,31 @@ public function testGetPrevious(): void
$this->assertEquals($this->tokens[6], $list->getPrevious());
$this->assertEquals($this->tokens[4], $list->getPrevious());
$this->assertEquals($this->tokens[2], $list->getPrevious());
$this->assertEquals($this->tokens[0], $list->getPrevious());
$this->assertNull($list->getPrevious());
}

public function testGetNextOfType(): void
{
$list = new TokensList($this->tokens);
$this->assertEquals($this->tokens[0], $list->getNextOfType(Token::TYPE_KEYWORD));
$this->assertEquals($this->tokens[4], $list->getNextOfType(Token::TYPE_KEYWORD));
$this->assertEquals($this->tokens[8], $list->getNextOfType(Token::TYPE_KEYWORD));
$this->assertEquals($this->tokens[4], $list->getNextOfType([Token::TYPE_KEYWORD]));
$this->assertEquals($this->tokens[6], $list->getNextOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
$this->assertEquals($this->tokens[8], $list->getNextOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
$this->assertNull($list->getNextOfType(Token::TYPE_KEYWORD));
}

public function testGetPreviousOfType(): void
{
$list = new TokensList($this->tokens);
$list->idx = 9;
$this->assertEquals($this->tokens[8], $list->getPreviousOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
$this->assertEquals($this->tokens[6], $list->getPreviousOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
$this->assertEquals($this->tokens[4], $list->getPreviousOfType([Token::TYPE_KEYWORD]));
$this->assertEquals($this->tokens[0], $list->getPreviousOfType(Token::TYPE_KEYWORD));
$this->assertNull($list->getPreviousOfType(Token::TYPE_KEYWORD));
}

public function testGetNextOfTypeAndFlag(): void
{
$list = new TokensList($this->tokens);
Expand Down