Skip to content

Commit b5c995a

Browse files
committed
Merge #428 - Fix TokensList::getPrevious which was not able to reach very first token
Pull-request: #428 Signed-off-by: William Desportes <[email protected]>
2 parents a5b5b37 + f3c7f82 commit b5c995a

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/Token.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ class Token
109109
*/
110110
public const TYPE_LABEL = 10;
111111

112+
/**
113+
* All tokens types
114+
*/
115+
public const TYPE_ALL = [
116+
Token::TYPE_NONE,
117+
Token::TYPE_KEYWORD,
118+
Token::TYPE_OPERATOR,
119+
Token::TYPE_WHITESPACE,
120+
Token::TYPE_COMMENT,
121+
Token::TYPE_BOOL,
122+
Token::TYPE_NUMBER,
123+
Token::TYPE_STRING,
124+
Token::TYPE_SYMBOL,
125+
Token::TYPE_DELIMITER,
126+
Token::TYPE_LABEL,
127+
];
128+
112129
// Flags that describe the tokens in more detail.
113130
// All keywords must have flag 1 so `Context::isKeyword` method doesn't
114131
// require strict comparison.

src/TokensList.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function getNext()
119119
*/
120120
public function getPrevious(): ?Token
121121
{
122-
for (; $this->idx > 0; --$this->idx) {
122+
for (; $this->idx >= 0; --$this->idx) {
123123
if (
124124
($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
125125
&& ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
@@ -131,17 +131,41 @@ public function getPrevious(): ?Token
131131
return null;
132132
}
133133

134+
/**
135+
* Gets the previous token.
136+
*
137+
* @param int|int[] $type the type
138+
*
139+
* @return Token|null
140+
*/
141+
public function getPreviousOfType($type)
142+
{
143+
if (!is_array($type)) {
144+
$type = [$type];
145+
}
146+
for (; $this->idx >= 0; --$this->idx) {
147+
if (in_array($this->tokens[$this->idx]->type, $type, true)) {
148+
return $this->tokens[$this->idx--];
149+
}
150+
}
151+
152+
return null;
153+
}
154+
134155
/**
135156
* Gets the next token.
136157
*
137-
* @param int $type the type
158+
* @param int|int[] $type the type
138159
*
139160
* @return Token|null
140161
*/
141162
public function getNextOfType($type)
142163
{
164+
if (!is_array($type)) {
165+
$type = [$type];
166+
}
143167
for (; $this->idx < $this->count; ++$this->idx) {
144-
if ($this->tokens[$this->idx]->type === $type) {
168+
if (in_array($this->tokens[$this->idx]->type, $type, true)) {
145169
return $this->tokens[$this->idx++];
146170
}
147171
}

tests/Lexer/TokensListTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,31 @@ public function testGetPrevious(): void
7979
$this->assertEquals($this->tokens[6], $list->getPrevious());
8080
$this->assertEquals($this->tokens[4], $list->getPrevious());
8181
$this->assertEquals($this->tokens[2], $list->getPrevious());
82+
$this->assertEquals($this->tokens[0], $list->getPrevious());
8283
$this->assertNull($list->getPrevious());
8384
}
8485

8586
public function testGetNextOfType(): void
8687
{
8788
$list = new TokensList($this->tokens);
8889
$this->assertEquals($this->tokens[0], $list->getNextOfType(Token::TYPE_KEYWORD));
89-
$this->assertEquals($this->tokens[4], $list->getNextOfType(Token::TYPE_KEYWORD));
90-
$this->assertEquals($this->tokens[8], $list->getNextOfType(Token::TYPE_KEYWORD));
90+
$this->assertEquals($this->tokens[4], $list->getNextOfType([Token::TYPE_KEYWORD]));
91+
$this->assertEquals($this->tokens[6], $list->getNextOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
92+
$this->assertEquals($this->tokens[8], $list->getNextOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
9193
$this->assertNull($list->getNextOfType(Token::TYPE_KEYWORD));
9294
}
9395

96+
public function testGetPreviousOfType(): void
97+
{
98+
$list = new TokensList($this->tokens);
99+
$list->idx = 9;
100+
$this->assertEquals($this->tokens[8], $list->getPreviousOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
101+
$this->assertEquals($this->tokens[6], $list->getPreviousOfType([Token::TYPE_KEYWORD, Token::TYPE_SYMBOL]));
102+
$this->assertEquals($this->tokens[4], $list->getPreviousOfType([Token::TYPE_KEYWORD]));
103+
$this->assertEquals($this->tokens[0], $list->getPreviousOfType(Token::TYPE_KEYWORD));
104+
$this->assertNull($list->getPreviousOfType(Token::TYPE_KEYWORD));
105+
}
106+
94107
public function testGetNextOfTypeAndFlag(): void
95108
{
96109
$list = new TokensList($this->tokens);

0 commit comments

Comments
 (0)