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