forked from phpmyadmin/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokensList.php
More file actions
236 lines (206 loc) · 6.12 KB
/
Copy pathTokensList.php
File metadata and controls
236 lines (206 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser;
use ArrayAccess;
use function array_splice;
use function count;
use function in_array;
use function is_array;
/**
* Defines an array of tokens and utility functions to iterate through it.
*
* A structure representing a list of tokens.
*
* @implements ArrayAccess<int, Token>
*/
class TokensList implements ArrayAccess
{
/**
* The count of tokens.
*/
public int $count = 0;
/**
* The index of the next token to be returned.
*/
public int $idx = 0;
/** @param Token[] $tokens The array of tokens. */
public function __construct(public array $tokens = [])
{
$this->count = count($tokens);
}
/**
* Builds an array of tokens by merging their raw value.
*/
public function build(): string
{
return static::buildFromArray($this->tokens);
}
/**
* Builds an array of tokens by merging their raw value.
*
* @param Token[] $list the tokens to be built
*/
public static function buildFromArray(array $list): string
{
$ret = '';
foreach ($list as $token) {
$ret .= $token->token;
}
return $ret;
}
/**
* Adds a new token.
*
* @param Token $token token to be added in list
*/
public function add(Token $token): void
{
$this->tokens[$this->count++] = $token;
}
/**
* Gets the next token. Skips any irrelevant token (whitespaces and
* comments).
*/
public function getNext(): Token|null
{
for (; $this->idx < $this->count; ++$this->idx) {
if (
($this->tokens[$this->idx]->type !== TokenType::Whitespace)
&& ($this->tokens[$this->idx]->type !== TokenType::Comment)
) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Gets the previous token. Skips any irrelevant token (whitespaces and
* comments).
*/
public function getPrevious(): Token|null
{
for (; $this->idx >= 0; --$this->idx) {
if (
($this->tokens[$this->idx]->type !== TokenType::Whitespace)
&& ($this->tokens[$this->idx]->type !== TokenType::Comment)
) {
return $this->tokens[$this->idx--];
}
}
return null;
}
/**
* Gets the previous token.
*
* @param TokenType|TokenType[] $type the type
*/
public function getPreviousOfType(TokenType|array $type): Token|null
{
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 TokenType|TokenType[] $type the type
*/
public function getNextOfType(TokenType|array $type): Token|null
{
if (! is_array($type)) {
$type = [$type];
}
for (; $this->idx < $this->count; ++$this->idx) {
if (in_array($this->tokens[$this->idx]->type, $type, true)) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Gets the next token.
*
* @param TokenType $type the type of the token
* @param string $value the value of the token
*/
public function getNextOfTypeAndValue(TokenType $type, string $value): Token|null
{
for (; $this->idx < $this->count; ++$this->idx) {
if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->value === $value)) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Gets the next token.
*
* @param TokenType $type the type of the token
* @param int $flag the flag of the token
*/
public function getNextOfTypeAndFlag(TokenType $type, int $flag): Token|null
{
for (; $this->idx < $this->count; ++$this->idx) {
if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->flags === $flag)) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Sets a Token inside the list of tokens.
* When defined, offset must be positive otherwise the offset is ignored.
* If the offset is not defined (like in array_push) or if it is greater than the number of Tokens already stored,
* the Token is appended to the list of tokens.
*
* @param int|null $offset the offset to be set. Must be positive otherwise, nothing will be stored.
* @param Token $value the token to be saved
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if ($offset === null || $offset >= $this->count) {
$this->tokens[$this->count++] = $value;
} elseif ($offset >= 0) {
$this->tokens[$offset] = $value;
}
}
/**
* Gets a Token from the list of tokens.
* If the offset is negative or above the number of tokens set in the list, will return null.
*
* @param int $offset the offset to be returned
*/
public function offsetGet(mixed $offset): Token|null
{
return $this->offsetExists($offset) ? $this->tokens[$offset] : null;
}
/**
* Checks if an offset was previously set.
* If the offset is negative or above the number of tokens set in the list, will return false.
*
* @param int $offset the offset to be checked
*/
public function offsetExists(mixed $offset): bool
{
return $offset >= 0 && $offset < $this->count;
}
/**
* Unsets the value of an offset, if the offset exists.
*
* @param int $offset the offset to be unset
*/
public function offsetUnset(mixed $offset): void
{
if (! $this->offsetExists($offset)) {
return;
}
array_splice($this->tokens, $offset, 1);
--$this->count;
}
}