GeezerVison is showing github.com — Add new method TokensList::getPreviousOfType + manage list of types i… · phpmyadmin/sql-parser@f3c7f82
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/Token.php +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,23 @@ class Token | 109 | 109 | */ | 110 | 110 | public const TYPE_LABEL = 10; | 111 | 111 |
| | 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 | + | 112 | 129 | // Flags that describe the tokens in more detail. | 113 | 130 | // All keywords must have flag 1 so `Context::isKeyword` method doesn't | 114 | 131 | // require strict comparison. | |
Collapse file Copy file name to clipboardExpand all lines: src/TokensList.php +26-2Lines changed: 26 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -131,17 +131,41 @@ public function getPrevious(): ?Token | 131 | 131 | return null; | 132 | 132 | } | 133 | 133 |
| | 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 | + | 134 | 155 | /** | 135 | 156 | * Gets the next token. | 136 | 157 | * | 137 | | - * @param int $type the type | | 158 | + * @param int|int[] $type the type | 138 | 159 | * | 139 | 160 | * @return Token|null | 140 | 161 | */ | 141 | 162 | public function getNextOfType($type) | 142 | 163 | { | | 164 | + if (!is_array($type)) { | | 165 | + $type = [$type]; | | 166 | + } | 143 | 167 | 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)) { | 145 | 169 | return $this->tokens[$this->idx++]; | 146 | 170 | } | 147 | 171 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/Lexer/TokensListTest.php +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,11 +87,23 @@ public function testGetNextOfType(): void | 87 | 87 | { | 88 | 88 | $list = new TokensList($this->tokens); | 89 | 89 | $this->assertEquals($this->tokens[0], $list->getNextOfType(Token::TYPE_KEYWORD)); | 90 | | - $this->assertEquals($this->tokens[4], $list->getNextOfType(Token::TYPE_KEYWORD)); | 91 | | - $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])); | 92 | 93 | $this->assertNull($list->getNextOfType(Token::TYPE_KEYWORD)); | 93 | 94 | } | 94 | 95 |
| | 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 | + | 95 | 107 | public function testGetNextOfTypeAndFlag(): void | 96 | 108 | { | 97 | 109 | $list = new TokensList($this->tokens); | |
|