1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines an array of tokens and utility functions to iterate through it. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace PhpMyAdmin\SqlParser; |
9
|
|
|
|
10
|
|
|
use ArrayAccess; |
11
|
|
|
use function count; |
12
|
|
|
use function is_array; |
13
|
|
|
use function is_string; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A structure representing a list of tokens. |
17
|
|
|
*/ |
18
|
|
|
class TokensList implements ArrayAccess |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The array of tokens. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $tokens = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The count of tokens. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
public $count = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The index of the next token to be returned. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
public $idx = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $tokens the initial array of tokens |
43
|
|
|
* @param int $count the count of tokens in the initial array |
44
|
|
|
*/ |
45
|
2000 |
|
public function __construct(array $tokens = [], $count = -1) |
46
|
|
|
{ |
47
|
2000 |
|
if (empty($tokens)) { |
48
|
1984 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
$this->tokens = $tokens; |
52
|
20 |
|
if ($count !== -1) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
20 |
|
$this->count = count($tokens); |
57
|
20 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Builds an array of tokens by merging their raw value. |
61
|
|
|
* |
62
|
|
|
* @param string|Token[]|TokensList $list the tokens to be built |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
28 |
|
public static function build($list) |
67
|
|
|
{ |
68
|
28 |
|
if (is_string($list)) { |
69
|
4 |
|
return $list; |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
if ($list instanceof self) { |
73
|
4 |
|
$list = $list->tokens; |
74
|
|
|
} |
75
|
|
|
|
76
|
24 |
|
$ret = ''; |
77
|
24 |
|
if (is_array($list)) { |
|
|
|
|
78
|
24 |
|
foreach ($list as $tok) { |
79
|
24 |
|
$ret .= $tok->token; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
24 |
|
return $ret; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Adds a new token. |
88
|
|
|
* |
89
|
|
|
* @param Token $token token to be added in list |
90
|
|
|
*/ |
91
|
4 |
|
public function add(Token $token) |
92
|
|
|
{ |
93
|
4 |
|
$this->tokens[$this->count++] = $token; |
94
|
4 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the next token. Skips any irrelevant token (whitespaces and |
98
|
|
|
* comments). |
99
|
|
|
* |
100
|
|
|
* @return Token|null |
101
|
|
|
*/ |
102
|
332 |
|
public function getNext() |
103
|
|
|
{ |
104
|
332 |
|
for (; $this->idx < $this->count; ++$this->idx) { |
105
|
332 |
|
if (($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE) |
106
|
332 |
|
&& ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT) |
107
|
|
|
) { |
108
|
332 |
|
return $this->tokens[$this->idx++]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets the next token. |
117
|
|
|
* |
118
|
|
|
* @param int $type the type |
119
|
|
|
* |
120
|
|
|
* @return Token|null |
121
|
|
|
*/ |
122
|
180 |
|
public function getNextOfType($type) |
123
|
|
|
{ |
124
|
180 |
|
for (; $this->idx < $this->count; ++$this->idx) { |
125
|
180 |
|
if ($this->tokens[$this->idx]->type === $type) { |
126
|
180 |
|
return $this->tokens[$this->idx++]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets the next token. |
135
|
|
|
* |
136
|
|
|
* @param int $type the type of the token |
137
|
|
|
* @param string $value the value of the token |
138
|
|
|
* |
139
|
|
|
* @return Token|null |
140
|
|
|
*/ |
141
|
1968 |
|
public function getNextOfTypeAndValue($type, $value) |
142
|
|
|
{ |
143
|
1968 |
|
for (; $this->idx < $this->count; ++$this->idx) { |
144
|
1968 |
|
if (($this->tokens[$this->idx]->type === $type) |
145
|
1968 |
|
&& ($this->tokens[$this->idx]->value === $value) |
146
|
|
|
) { |
147
|
344 |
|
return $this->tokens[$this->idx++]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
1968 |
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Sets an value inside the container. |
156
|
|
|
* |
157
|
|
|
* @param int $offset the offset to be set |
158
|
|
|
* @param Token $value the token to be saved |
159
|
|
|
*/ |
160
|
4 |
|
public function offsetSet($offset, $value) |
161
|
|
|
{ |
162
|
4 |
|
if ($offset === null) { |
|
|
|
|
163
|
4 |
|
$this->tokens[$this->count++] = $value; |
164
|
|
|
} else { |
165
|
4 |
|
$this->tokens[$offset] = $value; |
166
|
|
|
} |
167
|
4 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets a value from the container. |
171
|
|
|
* |
172
|
|
|
* @param int $offset the offset to be returned |
173
|
|
|
* |
174
|
|
|
* @return Token |
175
|
|
|
*/ |
176
|
28 |
|
public function offsetGet($offset) |
177
|
|
|
{ |
178
|
28 |
|
return $offset < $this->count ? $this->tokens[$offset] : null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Checks if an offset was previously set. |
183
|
|
|
* |
184
|
|
|
* @param int $offset the offset to be checked |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
4 |
|
public function offsetExists($offset) |
189
|
|
|
{ |
190
|
4 |
|
return $offset < $this->count; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Unsets the value of an offset. |
195
|
|
|
* |
196
|
|
|
* @param int $offset the offset to be unset |
197
|
|
|
*/ |
198
|
4 |
|
public function offsetUnset($offset) |
199
|
|
|
{ |
200
|
4 |
|
unset($this->tokens[$offset]); |
201
|
4 |
|
--$this->count; |
202
|
4 |
|
for ($i = $offset; $i < $this->count; ++$i) { |
203
|
4 |
|
$this->tokens[$i] = $this->tokens[$i + 1]; |
204
|
|
|
} |
205
|
|
|
|
206
|
4 |
|
unset($this->tokens[$this->count]); |
207
|
4 |
|
} |
208
|
|
|
} |
209
|
|
|
|