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