blob: e8ff73317b331642aa93f235ee4dea4de093c732 (
plain)
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
|
<?php
/**
* Defines an array of tokens and utility functions to iterate through it.
*/
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser;
use ArrayAccess;
/**
* A structure representing a list of tokens.
*/
class TokensList implements ArrayAccess
{
/**
* The array of tokens.
*
* @var array
*/
public $tokens = [];
/**
* The count of tokens.
*
* @var int
*/
public $count = 0;
/**
* The index of the next token to be returned.
*
* @var int
*/
public $idx = 0;
/**
* @param array $tokens the initial array of tokens
* @param int $count the count of tokens in the initial array
*/
public function __construct(array $tokens = [], $count = -1)
{
if (! empty($tokens)) {
$this->tokens = $tokens;
if ($count === -1) {
$this->count = count($tokens);
}
}
}
/**
* Builds an array of tokens by merging their raw value.
*
* @param string|Token[]|TokensList $list the tokens to be built
*
* @return string
*/
public static function build($list)
{
if (is_string($list)) {
return $list;
}
if ($list instanceof self) {
$list = $list->tokens;
}
$ret = '';
if (is_array($list)) {
foreach ($list as $tok) {
$ret .= $tok->token;
}
}
return $ret;
}
/**
* Adds a new token.
*
* @param Token $token token to be added in list
*/
public function add(Token $token)
{
$this->tokens[$this->count++] = $token;
}
/**
* Gets the next token. Skips any irrelevant token (whitespaces and
* comments).
*
* @return Token|null
*/
public function getNext()
{
for (; $this->idx < $this->count; ++$this->idx) {
if (($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
&& ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Gets the next token.
*
* @param int $type the type
*
* @return Token|null
*/
public function getNextOfType($type)
{
for (; $this->idx < $this->count; ++$this->idx) {
if ($this->tokens[$this->idx]->type === $type) {
return $this->tokens[$this->idx++];
}
}
return null;
}
/**
* Gets the next token.
*
* @param int $type the type of the token
* @param string $value the value of the token
*
* @return Token|null
*/
public function getNextOfTypeAndValue($type, $value)
{
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;
}
/**
* Sets an value inside the container.
*
* @param int $offset the offset to be set
* @param Token $value the token to be saved
*/
public function offsetSet($offset, $value)
{
if ($offset === null) {
$this->tokens[$this->count++] = $value;
} else {
$this->tokens[$offset] = $value;
}
}
/**
* Gets a value from the container.
*
* @param int $offset the offset to be returned
*
* @return Token
*/
public function offsetGet($offset)
{
return $offset < $this->count ? $this->tokens[$offset] : null;
}
/**
* Checks if an offset was previously set.
*
* @param int $offset the offset to be checked
*
* @return bool
*/
public function offsetExists($offset)
{
return $offset < $this->count;
}
/**
* Unsets the value of an offset.
*
* @param int $offset the offset to be unset
*/
public function offsetUnset($offset)
{
unset($this->tokens[$offset]);
--$this->count;
for ($i = $offset; $i < $this->count; ++$i) {
$this->tokens[$i] = $this->tokens[$i + 1];
}
unset($this->tokens[$this->count]);
}
}
|