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