Skip to content

Commit

Permalink
Add new method TokensList::getPreviousOfType + manage list of types i…
Browse files Browse the repository at this point in the history
…n TokensList::getPreviousOfType and TokensList::getNextOfType
  • Loading branch information
Tithugues committed Mar 8, 2023
1 parent aa8a915 commit f3c7f82
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
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
28 changes: 26 additions & 2 deletions src/TokensList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions tests/Lexer/TokensListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,23 @@ 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

0 comments on commit f3c7f82

Please sign in to comment.