1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Exceptions\LexerException; |
8
|
|
|
|
9
|
|
|
use function in_array; |
10
|
|
|
use function mb_strlen; |
11
|
|
|
use function sprintf; |
12
|
|
|
use function str_ends_with; |
13
|
|
|
use function strlen; |
14
|
|
|
use function substr; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Defines the lexer of the library. |
18
|
|
|
* |
19
|
|
|
* This is one of the most important components, along with the parser. |
20
|
|
|
* |
21
|
|
|
* Depends on context to extract lexemes. |
22
|
|
|
* |
23
|
|
|
* Performs lexical analysis over a SQL statement and splits it in multiple tokens. |
24
|
|
|
* |
25
|
|
|
* The output of the lexer is affected by the context of the SQL statement. |
26
|
|
|
* |
27
|
|
|
* @see Context |
28
|
|
|
*/ |
29
|
|
|
class Lexer extends Core |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* A list of methods that are used in lexing the SQL query. |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
public static $parserMethods = [ |
37
|
|
|
// It is best to put the parsers in order of their complexity |
38
|
|
|
// (ascending) and their occurrence rate (descending). |
39
|
|
|
// |
40
|
|
|
// Conflicts: |
41
|
|
|
// |
42
|
|
|
// 1. `parseDelimiter`, `parseUnknown`, `parseKeyword`, `parseNumber` |
43
|
|
|
// They fight over delimiter. The delimiter may be a keyword, a |
44
|
|
|
// number or almost any character which makes the delimiter one of |
45
|
|
|
// the first tokens that must be parsed. |
46
|
|
|
// |
47
|
|
|
// 1. `parseNumber` and `parseOperator` |
48
|
|
|
// They fight over `+` and `-`. |
49
|
|
|
// |
50
|
|
|
// 2. `parseComment` and `parseOperator` |
51
|
|
|
// They fight over `/` (as in ```/*comment*/``` or ```a / b```) |
52
|
|
|
// |
53
|
|
|
// 3. `parseBool` and `parseKeyword` |
54
|
|
|
// They fight over `TRUE` and `FALSE`. |
55
|
|
|
// |
56
|
|
|
// 4. `parseKeyword` and `parseUnknown` |
57
|
|
|
// They fight over words. `parseUnknown` does not know about |
58
|
|
|
// keywords. |
59
|
|
|
|
60
|
|
|
'parseDelimiter', |
61
|
|
|
'parseWhitespace', |
62
|
|
|
'parseNumber', |
63
|
|
|
'parseComment', |
64
|
|
|
'parseOperator', |
65
|
|
|
'parseBool', |
66
|
|
|
'parseString', |
67
|
|
|
'parseSymbol', |
68
|
|
|
'parseKeyword', |
69
|
|
|
'parseLabel', |
70
|
|
|
'parseUnknown', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* A list of keywords that indicate that the function keyword |
76
|
|
|
* is not used as a function |
77
|
|
|
* |
78
|
|
|
* @var string[] |
79
|
|
|
*/ |
80
|
|
|
public $keywordNameIndicators = [ |
81
|
|
|
'FROM', |
82
|
|
|
'SET', |
83
|
|
|
'WHERE', |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* A list of operators that indicate that the function keyword |
88
|
|
|
* is not used as a function |
89
|
|
|
* |
90
|
|
|
* @var string[] |
91
|
|
|
*/ |
92
|
|
|
public $operatorNameIndicators = [ |
93
|
|
|
',', |
94
|
|
|
'.', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The string to be parsed. |
99
|
|
|
* |
100
|
|
|
* @var string|UtfString |
101
|
|
|
*/ |
102
|
|
|
public $str = ''; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The length of `$str`. |
106
|
|
|
* |
107
|
|
|
* By storing its length, a lot of time is saved, because parsing methods |
108
|
|
|
* would call `strlen` everytime. |
109
|
|
|
* |
110
|
|
|
* @var int |
111
|
|
|
*/ |
112
|
|
|
public $len = 0; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The index of the last parsed character. |
116
|
|
|
* |
117
|
|
|
* @var int |
118
|
|
|
*/ |
119
|
|
|
public $last = 0; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Tokens extracted from given strings. |
123
|
|
|
* |
124
|
|
|
* @var TokensList |
125
|
|
|
*/ |
126
|
|
|
public $list; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The default delimiter. This is used, by default, in all new instances. |
130
|
|
|
* |
131
|
|
|
* @var string |
132
|
|
|
*/ |
133
|
|
|
public static $defaultDelimiter = ';'; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Statements delimiter. |
137
|
|
|
* This may change during lexing. |
138
|
|
|
* |
139
|
|
|
* @var string |
140
|
|
|
*/ |
141
|
|
|
public $delimiter; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* The length of the delimiter. |
145
|
|
|
* |
146
|
|
|
* Because `parseDelimiter` can be called a lot, it would perform a lot of |
147
|
|
|
* calls to `strlen`, which might affect performance when the delimiter is |
148
|
|
|
* big. |
149
|
|
|
* |
150
|
|
|
* @var int |
151
|
|
|
*/ |
152
|
|
|
public $delimiterLen; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string|UtfString $str the query to be lexed |
156
|
|
|
* @param bool $strict whether strict mode should be |
157
|
|
|
* enabled or not |
158
|
|
|
* @param string $delimiter the delimiter to be used |
159
|
|
|
*/ |
160
|
|
|
public function __construct($str, $strict = false, $delimiter = null) |
161
|
|
|
{ |
162
|
2 |
|
parent::__construct(); |
163
|
|
|
|
164
|
2 |
|
// `strlen` is used instead of `mb_strlen` because the lexer needs to |
165
|
|
|
// parse each byte of the input. |
166
|
2 |
|
$len = $str instanceof UtfString ? $str->length() : strlen($str); |
167
|
|
|
|
168
|
|
|
// For multi-byte strings, a new instance of `UtfString` is initialized. |
169
|
|
|
if (! $str instanceof UtfString && $len !== mb_strlen($str, 'UTF-8')) { |
170
|
|
|
$str = new UtfString($str); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->str = $str; |
174
|
|
|
$this->len = $str instanceof UtfString ? $str->length() : $len; |
175
|
1418 |
|
|
176
|
|
|
$this->strict = $strict; |
177
|
1418 |
|
|
178
|
|
|
// Setting the delimiter. |
179
|
|
|
$this->setDelimiter(! empty($delimiter) ? $delimiter : static::$defaultDelimiter); |
180
|
|
|
|
181
|
1418 |
|
$this->lex(); |
182
|
|
|
} |
183
|
|
|
|
184
|
1418 |
|
/** |
185
|
10 |
|
* Sets the delimiter. |
186
|
|
|
* |
187
|
|
|
* @param string $delimiter the new delimiter |
188
|
1418 |
|
*/ |
189
|
1418 |
|
public function setDelimiter($delimiter): void |
190
|
|
|
{ |
191
|
1418 |
|
$this->delimiter = $delimiter; |
192
|
|
|
$this->delimiterLen = strlen($delimiter); |
193
|
|
|
} |
194
|
1418 |
|
|
195
|
|
|
/** |
196
|
1418 |
|
* Parses the string and extracts lexemes. |
197
|
|
|
*/ |
198
|
|
|
public function lex(): void |
199
|
|
|
{ |
200
|
|
|
// TODO: Sometimes, static::parse* functions make unnecessary calls to |
201
|
|
|
// is* functions. For a better performance, some rules can be deduced |
202
|
|
|
// from context. |
203
|
|
|
// For example, in `parseBool` there is no need to compare the token |
204
|
1418 |
|
// every time with `true` and `false`. The first step would be to |
205
|
|
|
// compare with 'true' only and just after that add another letter from |
206
|
1418 |
|
// context and compare again with `false`. |
207
|
1418 |
|
// Another example is `parseComment`. |
208
|
|
|
|
209
|
|
|
$list = new TokensList(); |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Last processed token. |
213
|
1418 |
|
* |
214
|
|
|
* @var Token |
215
|
|
|
*/ |
216
|
|
|
$lastToken = null; |
217
|
|
|
|
218
|
|
|
for ($this->last = 0, $lastIdx = 0; $this->last < $this->len; $lastIdx = ++$this->last) { |
219
|
|
|
/** |
220
|
|
|
* The new token. |
221
|
|
|
* |
222
|
|
|
* @var Token |
223
|
|
|
*/ |
224
|
1418 |
|
$token = null; |
225
|
|
|
|
226
|
|
|
foreach (static::$parserMethods as $method) { |
227
|
|
|
$token = $this->$method(); |
228
|
|
|
|
229
|
|
|
if ($token) { |
230
|
|
|
break; |
231
|
1418 |
|
} |
232
|
|
|
} |
233
|
1418 |
|
|
234
|
|
|
if ($token === null) { |
235
|
|
|
// @assert($this->last === $lastIdx); |
236
|
|
|
$token = new Token($this->str[$this->last]); |
237
|
|
|
$this->error('Unexpected character.', $this->str[$this->last], $this->last); |
238
|
|
|
} elseif ( |
239
|
1408 |
|
$lastToken !== null |
240
|
|
|
&& $token->type === Token::TYPE_SYMBOL |
241
|
1408 |
|
&& $token->flags & Token::FLAG_SYMBOL_VARIABLE |
242
|
1408 |
|
&& ( |
243
|
|
|
$lastToken->type === Token::TYPE_STRING |
244
|
1408 |
|
|| ( |
245
|
1408 |
|
$lastToken->type === Token::TYPE_SYMBOL |
246
|
|
|
&& $lastToken->flags & Token::FLAG_SYMBOL_BACKTICK |
247
|
|
|
) |
248
|
|
|
) |
249
|
1408 |
|
) { |
250
|
|
|
// Handles ```... FROM 'user'@'%' ...```. |
251
|
4 |
|
$lastToken->token .= $token->token; |
252
|
4 |
|
$lastToken->type = Token::TYPE_SYMBOL; |
253
|
|
|
$lastToken->flags = Token::FLAG_SYMBOL_USER; |
254
|
1408 |
|
$lastToken->value .= '@' . $token->value; |
255
|
1408 |
|
continue; |
256
|
1408 |
|
} elseif ( |
257
|
|
|
$lastToken !== null |
258
|
1408 |
|
&& $token->type === Token::TYPE_KEYWORD |
259
|
1408 |
|
&& $lastToken->type === Token::TYPE_OPERATOR |
260
|
1408 |
|
&& $lastToken->value === '.' |
261
|
1408 |
|
) { |
262
|
1408 |
|
// Handles ```... tbl.FROM ...```. In this case, FROM is not |
263
|
|
|
// a reserved word. |
264
|
|
|
$token->type = Token::TYPE_NONE; |
265
|
|
|
$token->flags = 0; |
266
|
46 |
|
$token->value = $token->token; |
267
|
46 |
|
} |
268
|
46 |
|
|
269
|
46 |
|
$token->position = $lastIdx; |
270
|
46 |
|
|
271
|
|
|
$list->tokens[$list->count++] = $token; |
272
|
1408 |
|
|
273
|
1408 |
|
// Handling delimiters. |
274
|
1408 |
|
if ($token->type === Token::TYPE_NONE && $token->value === 'DELIMITER') { |
275
|
1408 |
|
if ($this->last + 1 >= $this->len) { |
276
|
|
|
$this->error('Expected whitespace(s) before delimiter.', '', $this->last + 1); |
277
|
|
|
continue; |
278
|
|
|
} |
279
|
30 |
|
|
280
|
30 |
|
// Skipping last R (from `delimiteR`) and whitespaces between |
281
|
30 |
|
// the keyword `DELIMITER` and the actual delimiter. |
282
|
|
|
$pos = ++$this->last; |
283
|
|
|
$token = $this->parseWhitespace(); |
284
|
1408 |
|
|
285
|
|
|
if ($token !== null) { |
286
|
1408 |
|
$token->position = $pos; |
287
|
|
|
$list->tokens[$list->count++] = $token; |
288
|
|
|
} |
289
|
1408 |
|
|
290
|
36 |
|
// Preparing the token that holds the new delimiter. |
291
|
2 |
|
if ($this->last + 1 >= $this->len) { |
292
|
2 |
|
$this->error('Expected delimiter.', '', $this->last + 1); |
293
|
|
|
continue; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$pos = $this->last + 1; |
297
|
34 |
|
|
298
|
34 |
|
// Parsing the delimiter. |
299
|
|
|
$this->delimiter = null; |
300
|
34 |
|
$delimiterLen = 0; |
301
|
32 |
|
while ( |
302
|
32 |
|
++$this->last < $this->len |
303
|
|
|
&& ! Context::isWhitespace($this->str[$this->last]) |
|
|
|
|
304
|
|
|
&& $delimiterLen < 15 |
305
|
|
|
) { |
306
|
34 |
|
$this->delimiter .= $this->str[$this->last]; |
307
|
2 |
|
++$delimiterLen; |
308
|
2 |
|
} |
309
|
|
|
|
310
|
|
|
if (empty($this->delimiter)) { |
311
|
32 |
|
$this->error('Expected delimiter.', '', $this->last); |
312
|
|
|
$this->delimiter = ';'; |
313
|
|
|
} |
314
|
32 |
|
|
315
|
32 |
|
--$this->last; |
316
|
|
|
|
317
|
32 |
|
// Saving the delimiter and its token. |
318
|
32 |
|
$this->delimiterLen = strlen($this->delimiter); |
319
|
32 |
|
$token = new Token($this->delimiter, Token::TYPE_DELIMITER); |
320
|
|
|
$token->position = $pos; |
321
|
30 |
|
$list->tokens[$list->count++] = $token; |
322
|
30 |
|
} |
323
|
|
|
|
324
|
|
|
$lastToken = $token; |
325
|
32 |
|
} |
326
|
2 |
|
|
327
|
2 |
|
// Adding a final delimiter to mark the ending. |
328
|
|
|
$list->tokens[$list->count++] = new Token(null, Token::TYPE_DELIMITER); |
329
|
|
|
|
330
|
32 |
|
// Saving the tokens list. |
331
|
|
|
$this->list = $list; |
332
|
|
|
|
333
|
32 |
|
$this->solveAmbiguityOnStarOperator(); |
334
|
32 |
|
$this->solveAmbiguityOnFunctionKeywords(); |
335
|
32 |
|
} |
336
|
32 |
|
|
337
|
|
|
/** |
338
|
|
|
* Resolves the ambiguity when dealing with the "*" operator. |
339
|
1404 |
|
* |
340
|
|
|
* In SQL statements, the "*" operator can be an arithmetic operator (like in 2*3) or an SQL wildcard (like in |
341
|
|
|
* SELECT a.* FROM ...). To solve this ambiguity, the solution is to find the next token, excluding whitespaces and |
342
|
|
|
* comments, right after the "*" position. The "*" is for sure an SQL wildcard if the next token found is any of: |
343
|
1418 |
|
* - "FROM" (the FROM keyword like in "SELECT * FROM..."); |
344
|
|
|
* - "USING" (the USING keyword like in "DELETE table_name.* USING..."); |
345
|
|
|
* - "," (a comma separator like in "SELECT *, field FROM..."); |
346
|
1418 |
|
* - ")" (a closing parenthesis like in "COUNT(*)"). |
347
|
|
|
* This methods will change the flag of the "*" tokens when any of those condition above is true. Otherwise, the |
348
|
1418 |
|
* default flag (arithmetic) will be kept. |
349
|
1418 |
|
*/ |
350
|
|
|
private function solveAmbiguityOnStarOperator(): void |
351
|
|
|
{ |
352
|
|
|
$iBak = $this->list->idx; |
353
|
|
|
while (($starToken = $this->list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, '*')) !== null) { |
354
|
|
|
// getNext() already gets rid of whitespaces and comments. |
355
|
|
|
$next = $this->list->getNext(); |
356
|
|
|
|
357
|
|
|
if ($next === null) { |
358
|
|
|
continue; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
if ( |
362
|
|
|
($next->type !== Token::TYPE_KEYWORD || ! in_array($next->value, ['FROM', 'USING'], true)) |
363
|
|
|
&& ($next->type !== Token::TYPE_OPERATOR || ! in_array($next->value, [',', ')'], true)) |
364
|
|
|
) { |
365
|
1418 |
|
continue; |
366
|
|
|
} |
367
|
1418 |
|
|
368
|
1418 |
|
$starToken->flags = Token::FLAG_OPERATOR_SQL; |
369
|
|
|
} |
370
|
200 |
|
|
371
|
|
|
$this->list->idx = $iBak; |
372
|
200 |
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Resolves the ambiguity when dealing with the functions keywords. |
376
|
|
|
* |
377
|
200 |
|
* In SQL statements, the function keywords might be used as table names or columns names. |
378
|
200 |
|
* To solve this ambiguity, the solution is to find the next token, excluding whitespaces and |
379
|
|
|
* comments, right after the function keyword position. The function keyword is for sure used |
380
|
16 |
|
* as column name or table name if the next token found is any of: |
381
|
|
|
* |
382
|
|
|
* - "FROM" (the FROM keyword like in "SELECT Country x, AverageSalary avg FROM..."); |
383
|
186 |
|
* - "WHERE" (the WHERE keyword like in "DELETE FROM emp x WHERE x.salary = 20"); |
384
|
|
|
* - "SET" (the SET keyword like in "UPDATE Country x, City y set x.Name=x.Name"); |
385
|
|
|
* - "," (a comma separator like 'x,' in "UPDATE Country x, City y set x.Name=x.Name"); |
386
|
1418 |
|
* - "." (a dot separator like in "x.asset_id FROM (SELECT evt.asset_id FROM evt)". |
387
|
|
|
* - "NULL" (when used as a table alias like in "avg.col FROM (SELECT ev.col FROM ev) avg"). |
388
|
|
|
* |
389
|
|
|
* This method will change the flag of the function keyword tokens when any of those |
390
|
|
|
* condition above is true. Otherwise, the |
391
|
|
|
* default flag (function keyword) will be kept. |
392
|
|
|
*/ |
393
|
|
|
private function solveAmbiguityOnFunctionKeywords(): void |
394
|
|
|
{ |
395
|
|
|
$iBak = $this->list->idx; |
396
|
|
|
$keywordFunction = Token::TYPE_KEYWORD | Token::FLAG_KEYWORD_FUNCTION; |
397
|
|
|
while (($keywordToken = $this->list->getNextOfTypeAndFlag(Token::TYPE_KEYWORD, $keywordFunction)) !== null) { |
398
|
|
|
$next = $this->list->getNext(); |
399
|
|
|
if ( |
400
|
|
|
($next->type !== Token::TYPE_KEYWORD |
401
|
|
|
|| ! in_array($next->value, $this->keywordNameIndicators, true) |
402
|
|
|
) |
403
|
|
|
&& ($next->type !== Token::TYPE_OPERATOR |
404
|
|
|
|| ! in_array($next->value, $this->operatorNameIndicators, true) |
405
|
|
|
) |
406
|
|
|
&& ($next->value !== null) |
407
|
|
|
) { |
408
|
1418 |
|
continue; |
409
|
|
|
} |
410
|
1418 |
|
|
411
|
1418 |
|
$keywordToken->type = Token::TYPE_NONE; |
412
|
1418 |
|
$keywordToken->flags = Token::TYPE_NONE; |
413
|
214 |
|
$keywordToken->keyword = $keywordToken->value; |
414
|
|
|
} |
415
|
214 |
|
|
416
|
214 |
|
$this->list->idx = $iBak; |
417
|
|
|
} |
418
|
214 |
|
|
419
|
214 |
|
/** |
420
|
|
|
* Creates a new error log. |
421
|
214 |
|
* |
422
|
|
|
* @param string $msg the error message |
423
|
204 |
|
* @param string $str the character that produced the error |
424
|
|
|
* @param int $pos the position of the character |
425
|
|
|
* @param int $code the code of the error |
426
|
12 |
|
* |
427
|
12 |
|
* @throws LexerException throws the exception, if strict mode is enabled. |
428
|
12 |
|
*/ |
429
|
|
|
public function error($msg, $str = '', $pos = 0, $code = 0): void |
430
|
|
|
{ |
431
|
1418 |
|
$error = new LexerException( |
432
|
|
|
Translator::gettext($msg), |
433
|
|
|
$str, |
434
|
|
|
$pos, |
435
|
|
|
$code |
436
|
|
|
); |
437
|
|
|
parent::error($error); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Parses a keyword. |
442
|
|
|
*/ |
443
|
|
|
public function parseKeyword(): Token|null |
444
|
34 |
|
{ |
445
|
|
|
$token = ''; |
446
|
34 |
|
|
447
|
34 |
|
/** |
448
|
34 |
|
* Value to be returned. |
449
|
34 |
|
* |
450
|
34 |
|
* @var Token |
451
|
34 |
|
*/ |
452
|
34 |
|
$ret = null; |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* The value of `$this->last` where `$token` ends in `$this->str`. |
456
|
|
|
*/ |
457
|
|
|
$iEnd = $this->last; |
458
|
1390 |
|
|
459
|
|
|
/** |
460
|
1390 |
|
* Whether last parsed character is a whitespace. |
461
|
|
|
* |
462
|
|
|
* @var bool |
463
|
|
|
*/ |
464
|
|
|
$lastSpace = false; |
465
|
|
|
|
466
|
|
|
for ($j = 1; $j < Context::KEYWORD_MAX_LENGTH && $this->last < $this->len; ++$j, ++$this->last) { |
467
|
1390 |
|
// Composed keywords shouldn't have more than one whitespace between |
468
|
|
|
// keywords. |
469
|
|
|
if (Context::isWhitespace($this->str[$this->last])) { |
|
|
|
|
470
|
|
|
if ($lastSpace) { |
471
|
|
|
--$j; // The size of the keyword didn't increase. |
472
|
1390 |
|
continue; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
$lastSpace = true; |
476
|
|
|
} else { |
477
|
|
|
$lastSpace = false; |
478
|
|
|
} |
479
|
1390 |
|
|
480
|
|
|
$token .= $this->str[$this->last]; |
481
|
1390 |
|
$flags = Context::isKeyword($token); |
482
|
|
|
|
483
|
|
|
if (($this->last + 1 !== $this->len && ! Context::isSeparator($this->str[$this->last + 1])) || ! $flags) { |
|
|
|
|
484
|
1390 |
|
continue; |
485
|
1364 |
|
} |
486
|
264 |
|
|
487
|
264 |
|
$ret = new Token($token, Token::TYPE_KEYWORD, $flags); |
488
|
|
|
$iEnd = $this->last; |
489
|
|
|
|
490
|
1364 |
|
// We don't break so we find longest keyword. |
491
|
|
|
// For example, `OR` and `ORDER` have a common prefix `OR`. |
492
|
1390 |
|
// If we stopped at `OR`, the parsing would be invalid. |
493
|
|
|
} |
494
|
|
|
|
495
|
1390 |
|
$this->last = $iEnd; |
496
|
1390 |
|
|
497
|
|
|
return $ret; |
498
|
1390 |
|
} |
499
|
1390 |
|
|
500
|
|
|
/** |
501
|
|
|
* Parses a label. |
502
|
1356 |
|
*/ |
503
|
1356 |
|
public function parseLabel(): Token|null |
504
|
|
|
{ |
505
|
|
|
$token = ''; |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Value to be returned. |
509
|
|
|
* |
510
|
1390 |
|
* @var Token |
511
|
|
|
*/ |
512
|
1390 |
|
$ret = null; |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* The value of `$this->last` where `$token` ends in `$this->str`. |
516
|
|
|
*/ |
517
|
|
|
$iEnd = $this->last; |
518
|
1050 |
|
for ($j = 1; $j < Context::LABEL_MAX_LENGTH && $this->last < $this->len; ++$j, ++$this->last) { |
519
|
|
|
if ($this->str[$this->last] === ':' && $j > 1) { |
520
|
1050 |
|
// End of label |
521
|
|
|
$token .= $this->str[$this->last]; |
522
|
|
|
$ret = new Token($token, Token::TYPE_LABEL); |
523
|
|
|
$iEnd = $this->last; |
524
|
|
|
break; |
525
|
|
|
} |
526
|
|
|
|
527
|
1050 |
|
if (Context::isWhitespace($this->str[$this->last]) && $j > 1) { |
|
|
|
|
528
|
|
|
// Whitespace between label and : |
529
|
|
|
// The size of the keyword didn't increase. |
530
|
|
|
--$j; |
531
|
|
|
} elseif (Context::isSeparator($this->str[$this->last])) { |
|
|
|
|
532
|
1050 |
|
// Any other separator |
533
|
1050 |
|
break; |
534
|
1050 |
|
} |
535
|
|
|
|
536
|
4 |
|
$token .= $this->str[$this->last]; |
537
|
4 |
|
} |
538
|
4 |
|
|
539
|
4 |
|
$this->last = $iEnd; |
540
|
|
|
|
541
|
|
|
return $ret; |
542
|
1050 |
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
818 |
|
* Parses an operator. |
546
|
1050 |
|
*/ |
547
|
|
|
public function parseOperator(): Token|null |
548
|
800 |
|
{ |
549
|
|
|
$token = ''; |
550
|
|
|
|
551
|
1048 |
|
/** |
552
|
|
|
* Value to be returned. |
553
|
|
|
* |
554
|
1050 |
|
* @var Token |
555
|
|
|
*/ |
556
|
1050 |
|
$ret = null; |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* The value of `$this->last` where `$token` ends in `$this->str`. |
560
|
|
|
*/ |
561
|
|
|
$iEnd = $this->last; |
562
|
1408 |
|
|
563
|
|
|
for ($j = 1; $j < Context::OPERATOR_MAX_LENGTH && $this->last < $this->len; ++$j, ++$this->last) { |
564
|
1408 |
|
$token .= $this->str[$this->last]; |
565
|
|
|
$flags = Context::isOperator($token); |
566
|
|
|
|
567
|
|
|
if (! $flags) { |
|
|
|
|
568
|
|
|
continue; |
569
|
|
|
} |
570
|
|
|
|
571
|
1408 |
|
$ret = new Token($token, Token::TYPE_OPERATOR, $flags); |
572
|
|
|
$iEnd = $this->last; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
$this->last = $iEnd; |
576
|
1408 |
|
|
577
|
|
|
return $ret; |
578
|
1408 |
|
} |
579
|
1408 |
|
|
580
|
1408 |
|
/** |
581
|
|
|
* Parses a whitespace. |
582
|
1408 |
|
*/ |
583
|
1404 |
|
public function parseWhitespace(): Token|null |
584
|
|
|
{ |
585
|
|
|
$token = $this->str[$this->last]; |
586
|
1002 |
|
|
587
|
1002 |
|
if (! Context::isWhitespace($token)) { |
|
|
|
|
588
|
|
|
return null; |
589
|
|
|
} |
590
|
1408 |
|
|
591
|
|
|
while (++$this->last < $this->len && Context::isWhitespace($this->str[$this->last])) { |
592
|
1408 |
|
$token .= $this->str[$this->last]; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
--$this->last; |
596
|
|
|
|
597
|
|
|
return new Token($token, Token::TYPE_WHITESPACE); |
598
|
1408 |
|
} |
599
|
|
|
|
600
|
1408 |
|
/** |
601
|
|
|
* Parses a comment. |
602
|
1408 |
|
*/ |
603
|
1408 |
|
public function parseComment(): Token|null |
604
|
|
|
{ |
605
|
|
|
$iBak = $this->last; |
606
|
1380 |
|
$token = $this->str[$this->last]; |
607
|
268 |
|
|
608
|
|
|
// Bash style comments. (#comment\n) |
609
|
|
|
if (Context::isComment($token)) { |
|
|
|
|
610
|
1380 |
|
while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { |
611
|
|
|
$token .= $this->str[$this->last]; |
612
|
1380 |
|
} |
613
|
|
|
|
614
|
|
|
// Include trailing \n as whitespace token |
615
|
|
|
if ($this->last < $this->len) { |
616
|
|
|
--$this->last; |
617
|
|
|
} |
618
|
1408 |
|
|
619
|
|
|
return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_BASH); |
620
|
1408 |
|
} |
621
|
1408 |
|
|
622
|
|
|
// C style comments. (/*comment*\/) |
623
|
|
|
if (++$this->last < $this->len) { |
624
|
1408 |
|
$token .= $this->str[$this->last]; |
625
|
6 |
|
if (Context::isComment($token)) { |
|
|
|
|
626
|
6 |
|
// There might be a conflict with "*" operator here, when string is "*/*". |
627
|
|
|
// This can occurs in the following statements: |
628
|
|
|
// - "SELECT */* comment */ FROM ..." |
629
|
|
|
// - "SELECT 2*/* comment */3 AS `six`;" |
630
|
6 |
|
$next = $this->last + 1; |
631
|
6 |
|
if (($next < $this->len) && $this->str[$next] === '*') { |
632
|
|
|
// Conflict in "*/*": first "*" was not for ending a comment. |
633
|
|
|
// Stop here and let other parsing method define the true behavior of that first star. |
634
|
6 |
|
$this->last = $iBak; |
635
|
|
|
|
636
|
|
|
return null; |
637
|
|
|
} |
638
|
1408 |
|
|
639
|
1404 |
|
$flags = Token::FLAG_COMMENT_C; |
640
|
1404 |
|
|
641
|
|
|
// This comment already ended. It may be a part of a |
642
|
|
|
// previous MySQL specific command. |
643
|
|
|
if ($token === '*/') { |
644
|
|
|
return new Token($token, Token::TYPE_COMMENT, $flags); |
645
|
100 |
|
} |
646
|
100 |
|
|
647
|
|
|
// Checking if this is a MySQL-specific command. |
648
|
|
|
if ($this->last + 1 < $this->len && $this->str[$this->last + 1] === '!') { |
649
|
2 |
|
$flags |= Token::FLAG_COMMENT_MYSQL_CMD; |
650
|
|
|
$token .= $this->str[++$this->last]; |
651
|
2 |
|
|
652
|
|
|
while ( |
653
|
|
|
++$this->last < $this->len |
654
|
100 |
|
&& $this->str[$this->last] >= '0' |
655
|
|
|
&& $this->str[$this->last] <= '9' |
656
|
|
|
) { |
657
|
|
|
$token .= $this->str[$this->last]; |
658
|
100 |
|
} |
659
|
34 |
|
|
660
|
|
|
--$this->last; |
661
|
|
|
|
662
|
|
|
// We split this comment and parse only its beginning |
663
|
100 |
|
// here. |
664
|
34 |
|
return new Token($token, Token::TYPE_COMMENT, $flags); |
665
|
34 |
|
} |
666
|
|
|
|
667
|
|
|
// Parsing the comment. |
668
|
34 |
|
while ( |
669
|
34 |
|
++$this->last < $this->len |
670
|
34 |
|
&& ( |
671
|
|
|
$this->str[$this->last - 1] !== '*' |
672
|
32 |
|
|| $this->str[$this->last] !== '/' |
673
|
|
|
) |
674
|
|
|
) { |
675
|
34 |
|
$token .= $this->str[$this->last]; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
// Adding the ending. |
679
|
34 |
|
if ($this->last < $this->len) { |
680
|
|
|
$token .= $this->str[$this->last]; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
return new Token($token, Token::TYPE_COMMENT, $flags); |
684
|
70 |
|
} |
685
|
70 |
|
} |
686
|
70 |
|
|
687
|
70 |
|
// SQL style comments. (-- comment\n) |
688
|
70 |
|
if (++$this->last < $this->len) { |
689
|
|
|
$token .= $this->str[$this->last]; |
690
|
70 |
|
$end = false; |
691
|
|
|
} else { |
692
|
|
|
--$this->last; |
693
|
|
|
$end = true; |
694
|
70 |
|
} |
695
|
70 |
|
|
696
|
|
|
if (Context::isComment($token, $end)) { |
|
|
|
|
697
|
|
|
// Checking if this comment did not end already (```--\n```). |
698
|
70 |
|
if ($this->str[$this->last] !== "\n") { |
699
|
|
|
while (++$this->last < $this->len && $this->str[$this->last] !== "\n") { |
700
|
|
|
$token .= $this->str[$this->last]; |
701
|
|
|
} |
702
|
|
|
} |
703
|
1408 |
|
|
704
|
1402 |
|
// Include trailing \n as whitespace token |
705
|
1402 |
|
if ($this->last < $this->len) { |
706
|
|
|
--$this->last; |
707
|
410 |
|
} |
708
|
410 |
|
|
709
|
|
|
return new Token($token, Token::TYPE_COMMENT, Token::FLAG_COMMENT_SQL); |
710
|
|
|
} |
711
|
1408 |
|
|
712
|
|
|
$this->last = $iBak; |
713
|
70 |
|
|
714
|
70 |
|
return null; |
715
|
70 |
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Parses a boolean. |
719
|
|
|
*/ |
720
|
70 |
|
public function parseBool(): Token|null |
721
|
62 |
|
{ |
722
|
|
|
if ($this->last + 3 >= $this->len) { |
723
|
|
|
// At least `min(strlen('TRUE'), strlen('FALSE'))` characters are |
724
|
70 |
|
// required. |
725
|
|
|
return null; |
726
|
|
|
} |
727
|
1408 |
|
|
728
|
|
|
$iBak = $this->last; |
729
|
1408 |
|
$token = $this->str[$this->last] . $this->str[++$this->last] |
730
|
|
|
. $this->str[++$this->last] . $this->str[++$this->last]; // _TRUE_ or _FALS_e |
731
|
|
|
|
732
|
|
|
if (Context::isBool($token)) { |
733
|
|
|
return new Token($token, Token::TYPE_BOOL); |
734
|
|
|
} |
735
|
1392 |
|
|
736
|
|
|
if (++$this->last < $this->len) { |
737
|
1392 |
|
$token .= $this->str[$this->last]; // fals_E_ |
738
|
|
|
if (Context::isBool($token)) { |
739
|
|
|
return new Token($token, Token::TYPE_BOOL, 1); |
740
|
306 |
|
} |
741
|
|
|
} |
742
|
|
|
|
743
|
1392 |
|
$this->last = $iBak; |
744
|
1392 |
|
|
745
|
1392 |
|
return null; |
746
|
|
|
} |
747
|
1392 |
|
|
748
|
4 |
|
/** |
749
|
|
|
* Parses a number. |
750
|
|
|
*/ |
751
|
1392 |
|
public function parseNumber(): Token|null |
752
|
1390 |
|
{ |
753
|
1390 |
|
// A rudimentary state machine is being used to parse numbers due to |
754
|
6 |
|
// the various forms of their notation. |
755
|
|
|
// |
756
|
|
|
// Below are the states of the machines and the conditions to change |
757
|
|
|
// the state. |
758
|
1392 |
|
// |
759
|
|
|
// 1 --------------------[ + or - ]-------------------> 1 |
760
|
1392 |
|
// 1 -------------------[ 0x or 0X ]------------------> 2 |
761
|
|
|
// 1 --------------------[ 0 to 9 ]-------------------> 3 |
762
|
|
|
// 1 -----------------------[ . ]---------------------> 4 |
763
|
|
|
// 1 -----------------------[ b ]---------------------> 7 |
764
|
|
|
// |
765
|
|
|
// 2 --------------------[ 0 to F ]-------------------> 2 |
766
|
1408 |
|
// |
767
|
|
|
// 3 --------------------[ 0 to 9 ]-------------------> 3 |
768
|
|
|
// 3 -----------------------[ . ]---------------------> 4 |
769
|
|
|
// 3 --------------------[ e or E ]-------------------> 5 |
770
|
|
|
// |
771
|
|
|
// 4 --------------------[ 0 to 9 ]-------------------> 4 |
772
|
|
|
// 4 --------------------[ e or E ]-------------------> 5 |
773
|
|
|
// |
774
|
|
|
// 5 ---------------[ + or - or 0 to 9 ]--------------> 6 |
775
|
|
|
// |
776
|
|
|
// 7 -----------------------[ ' ]---------------------> 8 |
777
|
|
|
// |
778
|
|
|
// 8 --------------------[ 0 or 1 ]-------------------> 8 |
779
|
|
|
// 8 -----------------------[ ' ]---------------------> 9 |
780
|
|
|
// |
781
|
|
|
// State 1 may be reached by negative numbers. |
782
|
|
|
// State 2 is reached only by hex numbers. |
783
|
|
|
// State 4 is reached only by float numbers. |
784
|
|
|
// State 5 is reached only by numbers in approximate form. |
785
|
|
|
// State 7 is reached only by numbers in bit representation. |
786
|
|
|
// |
787
|
|
|
// Valid final states are: 2, 3, 4 and 6. Any parsing that finished in a |
788
|
|
|
// state other than these is invalid. |
789
|
|
|
// Also, negative states are invalid states. |
790
|
|
|
$iBak = $this->last; |
791
|
|
|
$token = ''; |
792
|
|
|
$flags = 0; |
793
|
|
|
$state = 1; |
794
|
|
|
for (; $this->last < $this->len; ++$this->last) { |
795
|
|
|
if ($state === 1) { |
796
|
|
|
if ($this->str[$this->last] === '-') { |
797
|
|
|
$flags |= Token::FLAG_NUMBER_NEGATIVE; |
798
|
|
|
} elseif ( |
799
|
|
|
$this->last + 1 < $this->len |
800
|
|
|
&& $this->str[$this->last] === '0' |
801
|
|
|
&& ( |
802
|
|
|
$this->str[$this->last + 1] === 'x' |
803
|
|
|
|| $this->str[$this->last + 1] === 'X' |
804
|
|
|
) |
805
|
1408 |
|
) { |
806
|
1408 |
|
$token .= $this->str[$this->last++]; |
807
|
1408 |
|
$state = 2; |
808
|
1408 |
|
} elseif ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9') { |
809
|
1408 |
|
$state = 3; |
810
|
1408 |
|
} elseif ($this->str[$this->last] === '.') { |
811
|
1408 |
|
$state = 4; |
812
|
70 |
|
} elseif ($this->str[$this->last] === 'b') { |
813
|
|
|
$state = 7; |
814
|
1408 |
|
} elseif ($this->str[$this->last] !== '+') { |
815
|
1408 |
|
// `+` is a valid character in a number. |
816
|
|
|
break; |
817
|
1408 |
|
} |
818
|
1408 |
|
} elseif ($state === 2) { |
819
|
|
|
$flags |= Token::FLAG_NUMBER_HEX; |
820
|
|
|
if ( |
821
|
4 |
|
! ( |
822
|
4 |
|
($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9') |
823
|
1408 |
|
|| ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'F') |
824
|
626 |
|
|| ($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'f') |
825
|
1408 |
|
) |
826
|
220 |
|
) { |
827
|
1408 |
|
break; |
828
|
110 |
|
} |
829
|
1408 |
|
} elseif ($state === 3) { |
830
|
|
|
if ($this->str[$this->last] === '.') { |
831
|
1408 |
|
$state = 4; |
832
|
|
|
} elseif ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') { |
833
|
726 |
|
$state = 5; |
834
|
4 |
|
} elseif ( |
835
|
|
|
($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'z') |
836
|
|
|
|| ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'Z') |
837
|
4 |
|
) { |
838
|
4 |
|
// A number can't be directly followed by a letter |
839
|
4 |
|
$state = -$state; |
840
|
|
|
} elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') { |
841
|
|
|
// Just digits and `.`, `e` and `E` are valid characters. |
842
|
4 |
|
break; |
843
|
|
|
} |
844
|
726 |
|
} elseif ($state === 4) { |
845
|
566 |
|
$flags |= Token::FLAG_NUMBER_FLOAT; |
846
|
12 |
|
if ($this->str[$this->last] === 'e' || $this->str[$this->last] === 'E') { |
847
|
564 |
|
$state = 5; |
848
|
2 |
|
} elseif ( |
849
|
|
|
($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'z') |
850
|
564 |
|
|| ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'Z') |
851
|
564 |
|
) { |
852
|
|
|
// A number can't be directly followed by a letter |
853
|
|
|
$state = -$state; |
854
|
6 |
|
} elseif ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') { |
855
|
562 |
|
// Just digits, `e` and `E` are valid characters. |
856
|
|
|
break; |
857
|
559 |
|
} |
858
|
|
|
} elseif ($state === 5) { |
859
|
314 |
|
$flags |= Token::FLAG_NUMBER_APPROXIMATE; |
860
|
230 |
|
if ( |
861
|
230 |
|
$this->str[$this->last] === '+' || $this->str[$this->last] === '-' |
862
|
14 |
|
|| ($this->str[$this->last] >= '0' && $this->str[$this->last] <= '9') |
863
|
|
|
) { |
864
|
230 |
|
$state = 6; |
865
|
230 |
|
} elseif ( |
866
|
|
|
($this->str[$this->last] >= 'a' && $this->str[$this->last] <= 'z') |
867
|
|
|
|| ($this->str[$this->last] >= 'A' && $this->str[$this->last] <= 'Z') |
868
|
172 |
|
) { |
869
|
90 |
|
// A number can't be directly followed by a letter |
870
|
|
|
$state = -$state; |
871
|
159 |
|
} else { |
872
|
|
|
break; |
873
|
264 |
|
} |
874
|
14 |
|
} elseif ($state === 6) { |
875
|
|
|
if ($this->str[$this->last] < '0' || $this->str[$this->last] > '9') { |
876
|
14 |
|
// Just digits are valid characters. |
877
|
14 |
|
break; |
878
|
|
|
} |
879
|
2 |
|
} elseif ($state === 7) { |
880
|
|
|
$flags |= Token::FLAG_NUMBER_BINARY; |
881
|
14 |
|
if ($this->str[$this->last] !== '\'') { |
882
|
14 |
|
break; |
883
|
|
|
} |
884
|
|
|
|
885
|
14 |
|
$state = 8; |
886
|
|
|
} elseif ($state === 8) { |
887
|
7 |
|
if ($this->str[$this->last] === '\'') { |
888
|
|
|
$state = 9; |
889
|
264 |
|
} elseif ($this->str[$this->last] !== '0' && $this->str[$this->last] !== '1') { |
890
|
2 |
|
break; |
891
|
|
|
} |
892
|
2 |
|
} elseif ($state === 9) { |
893
|
|
|
break; |
894
|
264 |
|
} |
895
|
106 |
|
|
896
|
106 |
|
$token .= $this->str[$this->last]; |
897
|
104 |
|
} |
898
|
|
|
|
899
|
|
|
if ($state === 2 || $state === 3 || ($token !== '.' && $state === 4) || $state === 6 || $state === 9) { |
900
|
2 |
|
--$this->last; |
901
|
178 |
|
|
902
|
2 |
|
return new Token($token, Token::TYPE_NUMBER, $flags); |
903
|
2 |
|
} |
904
|
2 |
|
|
905
|
2 |
|
$this->last = $iBak; |
906
|
|
|
|
907
|
178 |
|
return null; |
908
|
2 |
|
} |
909
|
|
|
|
910
|
|
|
/** |
911
|
810 |
|
* Parses a string. |
912
|
|
|
* |
913
|
|
|
* @param string $quote additional starting symbol |
914
|
1408 |
|
* |
915
|
626 |
|
* @throws LexerException |
916
|
|
|
*/ |
917
|
626 |
|
public function parseString($quote = ''): Token|null |
918
|
|
|
{ |
919
|
|
|
$token = $this->str[$this->last]; |
920
|
1408 |
|
$flags = Context::isString($token); |
|
|
|
|
921
|
|
|
|
922
|
1408 |
|
if (! $flags && $token !== $quote) { |
|
|
|
|
923
|
|
|
return null; |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
$quote = $token; |
927
|
|
|
|
928
|
|
|
while (++$this->last < $this->len) { |
929
|
|
|
if ( |
930
|
|
|
$this->last + 1 < $this->len |
931
|
|
|
&& ( |
932
|
1392 |
|
($this->str[$this->last] === $quote && $this->str[$this->last + 1] === $quote) |
933
|
|
|
|| ($this->str[$this->last] === '\\' && $quote !== '`') |
934
|
1392 |
|
) |
935
|
1392 |
|
) { |
936
|
|
|
$token .= $this->str[$this->last] . $this->str[++$this->last]; |
937
|
1392 |
|
} else { |
938
|
1392 |
|
if ($this->str[$this->last] === $quote) { |
939
|
|
|
break; |
940
|
|
|
} |
941
|
686 |
|
|
942
|
|
|
$token .= $this->str[$this->last]; |
943
|
686 |
|
} |
944
|
|
|
} |
945
|
686 |
|
|
946
|
|
|
if ($this->last >= $this->len || $this->str[$this->last] !== $quote) { |
947
|
686 |
|
$this->error( |
948
|
686 |
|
sprintf( |
949
|
|
|
Translator::gettext('Ending quote %1$s was expected.'), |
950
|
|
|
$quote |
951
|
30 |
|
), |
952
|
|
|
'', |
953
|
686 |
|
$this->last |
954
|
682 |
|
); |
955
|
|
|
} else { |
956
|
|
|
$token .= $this->str[$this->last]; |
957
|
680 |
|
} |
958
|
|
|
|
959
|
|
|
return new Token($token, Token::TYPE_STRING, $flags); |
960
|
|
|
} |
961
|
686 |
|
|
962
|
14 |
|
/** |
963
|
14 |
|
* Parses a symbol. |
964
|
14 |
|
* |
965
|
14 |
|
* @throws LexerException |
966
|
14 |
|
*/ |
967
|
14 |
|
public function parseSymbol(): Token|null |
968
|
14 |
|
{ |
969
|
14 |
|
$token = $this->str[$this->last]; |
970
|
|
|
$flags = Context::isSymbol($token); |
|
|
|
|
971
|
682 |
|
|
972
|
|
|
if (! $flags) { |
|
|
|
|
973
|
|
|
return null; |
974
|
686 |
|
} |
975
|
|
|
|
976
|
|
|
if ($flags & Token::FLAG_SYMBOL_VARIABLE) { |
977
|
|
|
if ($this->last + 1 < $this->len && $this->str[++$this->last] === '@') { |
978
|
|
|
// This is a system variable (e.g. `@@hostname`). |
979
|
|
|
$token .= $this->str[$this->last++]; |
980
|
|
|
$flags |= Token::FLAG_SYMBOL_SYSTEM; |
981
|
|
|
} |
982
|
1392 |
|
} elseif ($flags & Token::FLAG_SYMBOL_PARAMETER) { |
983
|
|
|
if ($token !== '?' && $this->last + 1 < $this->len) { |
984
|
1392 |
|
++$this->last; |
985
|
1392 |
|
} |
986
|
|
|
} else { |
987
|
1392 |
|
$token = ''; |
988
|
1390 |
|
} |
989
|
|
|
|
990
|
|
|
$str = null; |
991
|
452 |
|
|
992
|
122 |
|
if ($this->last < $this->len) { |
993
|
|
|
$str = $this->parseString('`'); |
994
|
26 |
|
|
995
|
74 |
|
if ($str === null) { |
996
|
|
|
$str = $this->parseUnknown(); |
997
|
362 |
|
|
998
|
6 |
|
if ($str === null) { |
999
|
5 |
|
$this->error('Variable name was expected.', $this->str[$this->last], $this->last); |
1000
|
|
|
} |
1001
|
|
|
} |
1002
|
358 |
|
} |
1003
|
|
|
|
1004
|
|
|
if ($str !== null) { |
1005
|
452 |
|
$token .= $str->token; |
1006
|
|
|
} |
1007
|
452 |
|
|
1008
|
452 |
|
return new Token($token, Token::TYPE_SYMBOL, $flags); |
1009
|
|
|
} |
1010
|
452 |
|
|
1011
|
88 |
|
/** |
1012
|
|
|
* Parses unknown parts of the query. |
1013
|
88 |
|
*/ |
1014
|
6 |
|
public function parseUnknown(): Token|null |
1015
|
|
|
{ |
1016
|
|
|
$token = $this->str[$this->last]; |
1017
|
|
|
if (Context::isSeparator($token)) { |
|
|
|
|
1018
|
|
|
return null; |
1019
|
452 |
|
} |
1020
|
448 |
|
|
1021
|
|
|
while (++$this->last < $this->len && ! Context::isSeparator($this->str[$this->last])) { |
1022
|
|
|
$token .= $this->str[$this->last]; |
1023
|
452 |
|
|
1024
|
|
|
// Test if end of token equals the current delimiter. If so, remove it from the token. |
1025
|
|
|
if (str_ends_with($token, $this->delimiter)) { |
1026
|
|
|
$token = substr($token, 0, -$this->delimiterLen); |
1027
|
|
|
$this->last -= $this->delimiterLen - 1; |
1028
|
|
|
break; |
1029
|
1074 |
|
} |
1030
|
|
|
} |
1031
|
1074 |
|
|
1032
|
1074 |
|
--$this->last; |
1033
|
10 |
|
|
1034
|
|
|
return new Token($token); |
1035
|
|
|
} |
1036
|
1072 |
|
|
1037
|
1040 |
|
/** |
1038
|
|
|
* Parses the delimiter of the query. |
1039
|
|
|
*/ |
1040
|
1040 |
|
public function parseDelimiter(): Token|null |
1041
|
4 |
|
{ |
1042
|
4 |
|
$idx = 0; |
1043
|
4 |
|
|
1044
|
|
|
while ($idx < $this->delimiterLen && $this->last + $idx < $this->len) { |
1045
|
|
|
if ($this->delimiter[$idx] !== $this->str[$this->last + $idx]) { |
1046
|
|
|
return null; |
1047
|
1072 |
|
} |
1048
|
|
|
|
1049
|
1072 |
|
++$idx; |
1050
|
|
|
} |
1051
|
|
|
|
1052
|
|
|
$this->last += $this->delimiterLen - 1; |
1053
|
|
|
|
1054
|
|
|
return new Token($this->delimiter, Token::TYPE_DELIMITER); |
1055
|
1408 |
|
} |
1056
|
|
|
} |
1057
|
|
|
|