-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathExpressionParser.php
More file actions
694 lines (603 loc) · 25.4 KB
/
ExpressionParser.php
File metadata and controls
694 lines (603 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
<?php
namespace Vimeo\MysqlEngine\Parser;
use Vimeo\MysqlEngine\Query\CreateColumn;
use Vimeo\MysqlEngine\Query\Expression\BetweenOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\BinaryOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\CaseOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\CastExpression;
use Vimeo\MysqlEngine\Query\Expression\ColumnExpression;
use Vimeo\MysqlEngine\Query\Expression\ConstantExpression;
use Vimeo\MysqlEngine\Query\Expression\ExistsOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\Expression;
use Vimeo\MysqlEngine\Query\Expression\FunctionExpression;
use Vimeo\MysqlEngine\Query\Expression\InOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\IntervalOperatorExpression;
use Vimeo\MysqlEngine\Query\Expression\StubExpression;
use Vimeo\MysqlEngine\Query\Expression\NamedPlaceholderExpression;
use Vimeo\MysqlEngine\Query\Expression\QuestionMarkPlaceholderExpression;
use Vimeo\MysqlEngine\Query\Expression\PositionExpression;
use Vimeo\MysqlEngine\Query\Expression\RowExpression;
use Vimeo\MysqlEngine\Query\Expression\SubqueryExpression;
use Vimeo\MysqlEngine\Query\Expression\UnaryExpression;
use Vimeo\MysqlEngine\Query\Expression\VariableExpression;
use Vimeo\MysqlEngine\TokenType;
final class ExpressionParser
{
const OPERATOR_PRECEDENCE = [
'INTERVAL' => 17,
'BINARY' => 16,
'COLLATE' => 16,
'!' => 15,
'UNARY_MINUS' => 14,
'UNARY_PLUS' => 14,
'~' => 14,
'^' => 13,
'*' => 12,
'/' => 12,
'DIV' => 12,
'%' => 12,
'MOD' => 12,
'-' => 11,
'+' => 11,
'<<' => 10,
'>>' => 10,
'&' => 9,
'|' => 8,
'=' => 7,
'<=>' => 7,
'>=' => 7,
'>' => 7,
'<=' => 7,
'<' => 7,
'<>' => 7,
'!=' => 7,
'IS' => 7,
'LIKE' => 7,
'NOT LIKE' => 7,
'REGEXP' => 7,
'NOT REGEXP' => 7,
'IN' => 7,
'NOT IN' => 7,
'EXISTS' => 7,
'NOT EXISTS' => 7,
'BETWEEN' => 6,
'CASE' => 6,
'WHEN' => 6,
'THEN' => 6,
'ELSE' => 6,
'END' => 6,
'NOT' => 5,
'AND' => 4,
'&&' => 4,
'XOR' => 3,
'OR' => 2,
'||' => 2,
':=' => 1,
];
/**
* @var array<int, Expression>|null
*/
private $selectExpressions = null;
/**
* @var array<int, Token>
*/
private $tokens;
/**
* @var int
*/
private $pointer = -1;
/**
* @var Expression
*/
private $expression;
/**
* @var int
*/
public $min_precedence = 0;
/**
* @var bool
*/
private $is_child = false;
/**
* @param array<int, Token> $tokens
*/
public function __construct(
array $tokens,
int $pointer = -1,
?Expression $expression = null,
int $min_precedence = 0,
bool $is_child = false
) {
$this->tokens = $tokens;
$this->pointer = $pointer;
$this->expression = $expression ?: new StubExpression();
$this->min_precedence = $min_precedence;
$this->is_child = $is_child;
}
/**
* @param array<int, Token> $tokens
*
* @return array{
* 0: bool,
* 1: list<Expression>,
* 2: ?array<int, array{direction: 'ASC'|'DESC', expression: Expression}>,
* 3: ?Expression
* }
*/
private function getListExpression(array $tokens)
{
$distinct = false;
$pos = 0;
$token_count = \count($tokens);
$needs_comma = false;
$args = [];
$order_by = null;
$separator = null;
if (isset($tokens[0]) && $tokens[0]->value == "DISTINCT") {
$distinct = true;
$pos++;
}
while ($pos < $token_count) {
$arg = $tokens[$pos];
if ($arg->value === ',') {
if ($needs_comma) {
$needs_comma = false;
$pos++;
continue;
} else {
throw new ParserException("Unexpected comma in SQL query");
}
}
if ($arg->value === 'ORDER') {
$p = new OrderByParser($pos, $tokens);
[$pos, $order_by] = $p->parse();
$pos++; // ORDER BY の次の式の先頭に position を合わせる
continue;
}
if ($arg->value === 'SEPARATOR') {
$p = new ExpressionParser($tokens, $pos);
list(, $expr) = $p->buildWithPointer();
$separator = $expr;
break;
}
$p = new ExpressionParser($tokens, $pos - 1);
list($pos, $expr) = $p->buildWithPointer();
$args[] = $expr;
$pos++;
$needs_comma = true;
}
return [$distinct, $args, $order_by, $separator];
}
/**
* @param array<int, Token> $tokens
*
* @return array{Expression, non-empty-list<Token>}
*/
private function getCastAsExpression(array $tokens)
{
$pos = 0;
$token_count = \count($tokens);
$needs_as = 0;
$expr = null;
$as_type_tokens = [];
while ($pos < $token_count) {
$arg = $tokens[$pos];
if ($arg->value === 'AS') {
if ($needs_as === 1) {
$needs_as = 2;
$pos++;
continue;
} else {
throw new ParserException("Unexpected AS in SQL query");
}
}
if ($needs_as === 0) {
$p = new ExpressionParser($tokens, $pos - 1);
list($pos, $expr) = $p->buildWithPointer();
$pos++;
$needs_as = 1;
} elseif ($needs_as === 2) {
$as_type_tokens[] = $arg;
$pos++;
}
}
if (!$expr || !$as_type_tokens) {
throw new ParserException("Expecting two parts to CAST query");
}
return [$expr, $as_type_tokens];
}
/**
* @param Token $token
*
* @return Expression
*/
public function tokenToExpression(Token $token)
{
switch ($token->type) {
case TokenType::NUMERIC_CONSTANT:
case TokenType::STRING_CONSTANT:
case TokenType::NULL_CONSTANT:
return new ConstantExpression($token);
case TokenType::IDENTIFIER:
if ($this->selectExpressions !== null) {
foreach ($this->selectExpressions as $expr) {
if ($expr->name === $token->value) {
return $expr;
}
}
}
if ($token->value === '?') {
if ($token->parameterOffset !== null) {
return new QuestionMarkPlaceholderExpression($token, $token->parameterOffset);
}
if ($token->parameterName !== null) {
return new NamedPlaceholderExpression($token, $token->parameterName);
}
throw new ParserException('? encountered with unknown offset');
}
if ($token->value[0] === '@') {
return new VariableExpression($token);
}
return new ColumnExpression($token);
case TokenType::SQLFUNCTION:
$next = ($__tmp1__ = $this->nextToken()) !== null ? $__tmp1__ : (function () {
throw new \TypeError('Failed assertion');
})();
\assert($next->type === TokenType::PAREN, 'function is be followed by parentheses');
$closing_paren_pointer = SQLParser::findMatchingParen($this->pointer, $this->tokens);
$arg_tokens = \array_slice(
$this->tokens,
$this->pointer + 1,
$closing_paren_pointer - $this->pointer - 1
);
if ($token->value === 'CAST') {
[$expr, $as_type_tokens] = $this->getCastAsExpression($arg_tokens);
$as_type_token_values = array_map(
function ($token) {
return $token->value;
},
$as_type_tokens
);
$type = CreateTableParser::parseFieldType($as_type_token_values, true);
$fn = new CastExpression($token, $expr, $type);
} else {
list($distinct, $args, $order, $separator) = $this->getListExpression($arg_tokens);
$fn = new FunctionExpression($token, $args, $distinct, $order, $separator);
}
$this->pointer = $closing_paren_pointer;
return $fn;
default:
throw new SQLFakeNotImplementedException("Not implemented: {$token->value}");
}
}
/**
* @return Expression
*/
public function build()
{
$token = $this->nextToken();
while ($token !== null) {
switch ($token->type) {
case TokenType::PAREN:
$close = SQLParser::findMatchingParen($this->pointer, $this->tokens);
$arg_tokens = \array_slice($this->tokens, $this->pointer + 1, $close - $this->pointer - 1);
if (!\count($arg_tokens)) {
throw new ParserException("Empty parentheses found");
}
$this->pointer = $close;
if ($arg_tokens[0]->value === 'SELECT') {
$subquery_sql = \implode(
' ',
\array_map(
function ($token) {
return $token->value;
},
$arg_tokens
)
);
$parser = new SelectParser(0, $arg_tokens, $subquery_sql);
$select = $parser->parse();
$expr = new SubqueryExpression($select, '');
} else {
if ($this->expression instanceof InOperatorExpression) {
$pointer = -1;
$in_list = [];
$token_count = \count($arg_tokens);
while ($pointer < $token_count) {
$p = new ExpressionParser($arg_tokens, $pointer);
list($pointer, $expr) = $p->buildWithPointer();
$in_list[] = $expr;
if ($pointer + 1 >= $token_count) {
break;
}
$pointer++;
$next = $arg_tokens[$pointer];
if ($next->value !== ',') {
throw new ParserException("Expected , in IN () list");
}
}
($__tmp2__ = $this->expression) instanceof InOperatorExpression ? $__tmp2__ : (function () {
throw new \TypeError('Failed assertion');
})();
$this->expression->setInList($in_list);
break;
}
$second_token = $arg_tokens[1] ?? null;
if ($second_token !== null && $second_token->type === TokenType::SEPARATOR) {
list($distinct, $elements) = $this->getListExpression($arg_tokens);
if ($distinct) {
throw new ParserException("Unexpected DISTINCT in row expression");
}
$expr = new RowExpression($elements, $token);
} else {
$p = new ExpressionParser($arg_tokens, -1);
$expr = $p->build();
}
}
if ($this->expression instanceof StubExpression) {
$this->expression = new BinaryOperatorExpression($expr);
} else {
$this->expression->setNextChild($expr);
}
break;
case TokenType::NUMERIC_CONSTANT:
case TokenType::NULL_CONSTANT:
case TokenType::STRING_CONSTANT:
case TokenType::SQLFUNCTION:
case TokenType::IDENTIFIER:
$expr = $this->tokenToExpression($token);
if ($this->expression instanceof StubExpression) {
$this->expression = new BinaryOperatorExpression($expr);
} elseif ($this->expression instanceof IntervalOperatorExpression
&& $token->type === TokenType::IDENTIFIER
&& $this->expression->number
) {
$this->expression->setUnit($token->value);
} else {
if (($this->expression->operator === null || $this->expression->operator === '')
&& $this->expression instanceof BinaryOperatorExpression
&& $token->type === TokenType::IDENTIFIER
) {
$this->pointer--;
return $this->expression->left;
}
$this->expression->setNextChild($expr);
}
break;
case TokenType::OPERATOR:
$operator = $token->value;
if ($operator === 'CASE') {
$close = SQLParser::findMatchingEnd($this->pointer, $this->tokens);
$arg_tokens = \array_slice($this->tokens, $this->pointer + 1, $close - $this->pointer);
$p = new ExpressionParser($arg_tokens, -1, new CaseOperatorExpression($token));
$expr = $p->build();
$this->pointer = $close;
if ($this->expression instanceof StubExpression) {
$this->expression = new BinaryOperatorExpression($expr);
} else {
$this->expression->setNextChild($expr);
}
break;
}
if ($operator === 'INTERVAL') {
if (!$this->expression instanceof StubExpression) {
$this->pointer = $this->expression->addRecursiveExpression(
$this->tokens,
$this->pointer - 1
);
break;
}
$this->expression = new IntervalOperatorExpression($token);
break;
}
if ($operator === 'WHEN' || $operator === 'THEN' || $operator === 'ELSE' || $operator === 'END') {
if (!$this->expression instanceof CaseOperatorExpression) {
if ($this->expression instanceof BinaryOperatorExpression) {
$this->pointer--;
if ($this->expression->right) {
return $this->expression;
}
return $this->expression->left;
}
throw new ParserException("Unexpected {$operator}");
}
$this->expression->setKeyword($operator);
if ($operator !== 'END') {
$this->pointer = $this->expression->addRecursiveExpression(
$this->tokens,
$this->pointer
);
}
break;
}
if ($this->expression->operator !== null && $this->expression->operator !== '') {
if ($operator === 'AND'
&& $this->expression->operator === 'BETWEEN'
&& !$this->expression->isWellFormed()
) {
if (!$this->expression instanceof BetweenOperatorExpression) {
throw new \TypeError('Failed assertion');
}
$this->expression->foundAnd();
} else {
if ($operator === 'NOT') {
if ($this->expression->operator !== 'IS') {
$next = $this->peekNext();
if ($next !== null
&& (($next->type === TokenType::OPERATOR
&& (\strtoupper($next->value) === 'IN'
|| \strtoupper($next->value) === 'EXISTS'))
|| $next->type === TokenType::PAREN)
) {
$this->pointer = $this->expression->addRecursiveExpression(
$this->tokens,
$this->pointer,
true
);
break;
}
throw new ParserException("Unexpected NOT");
}
$this->expression->negate();
} else {
$current_op_precedence = $this->expression->precedence;
$new_op_precedence = $this->getPrecedence($operator);
if ($current_op_precedence < $new_op_precedence) {
$this->pointer = $this->expression->addRecursiveExpression(
$this->tokens,
$this->pointer - 1
);
} else {
if ($operator === 'BETWEEN') {
$this->expression = new BetweenOperatorExpression(
$this->expression
);
} else {
if ($operator === 'IN') {
$this->expression = new InOperatorExpression(
$this->expression,
$this->expression->negated
);
} elseif ($operator === 'NOT IN') {
$this->expression = new InOperatorExpression(
$this->expression,
!$this->expression->negated
);
} else {
$this->expression = new BinaryOperatorExpression(
$this->expression,
false,
$operator
);
}
}
}
}
}
} else {
if ($operator === 'BETWEEN') {
if (!$this->expression instanceof BinaryOperatorExpression) {
throw new ParserException('Unexpected keyword BETWEEN');
}
$this->expression = new BetweenOperatorExpression($this->expression->left);
} elseif ($operator === 'NOT') {
$this->expression->negate();
} elseif ($operator === 'IN' || $operator === 'NOT IN') {
if (!$this->expression instanceof BinaryOperatorExpression) {
throw new ParserException('Unexpected keyword IN');
}
$this->expression = new InOperatorExpression(
$this->expression->left,
$operator === 'NOT IN' || $this->expression->negated
);
} elseif ($operator === 'EXISTS' || $operator === 'NOT EXISTS') {
$this->expression = new ExistsOperatorExpression(
$operator === 'NOT EXISTS' || $this->expression->negated,
$token
);
} elseif ($operator === 'UNARY_MINUS'
|| $operator === 'UNARY_PLUS'
|| $operator === '~'
|| $operator === '!'
) {
if (!$this->expression instanceof StubExpression) {
throw new \TypeError('Failed assertion');
}
$this->expression = new UnaryExpression($operator, $token);
} else {
if (!$this->expression instanceof BinaryOperatorExpression) {
throw new \TypeError(
'Expecting BinaryOperatorExpression but saw ' . \get_class($this->expression)
);
}
$this->expression->setOperator($operator);
}
}
break;
default:
throw new ParserException("Expression parse error: unexpected {$token->value}");
}
$nextToken = $this->peekNext();
if (!$nextToken) {
break;
}
if ($nextToken->type === TokenType::CLAUSE
|| $nextToken->type === TokenType::RESERVED
|| $nextToken->type === TokenType::SEPARATOR
) {
if ($nextToken->value === 'VALUES' && !$this->expression->isWellFormed()) {
$this->tokens[$this->pointer + 1]->type = TokenType::SQLFUNCTION;
} else {
break;
}
}
if ($this->expression->isWellFormed()) {
if ($nextToken->type === TokenType::IDENTIFIER) {
break;
}
if ($nextToken->value === 'ELSE'
|| $nextToken->value === 'THEN'
|| $nextToken->value === 'END'
) {
break;
}
if ($nextToken->type !== TokenType::OPERATOR) {
throw new ParserException("Unexpected token {$nextToken->value}");
}
if ($this->is_child) {
$next_operator_precedence = $this->getPrecedence($nextToken->value);
if ($next_operator_precedence <= $this->min_precedence) {
break;
}
}
}
$token = $this->nextToken();
}
if (!$this->expression->isWellFormed()) {
if ($this->expression instanceof BinaryOperatorExpression && $this->expression->operator === '') {
return $this->expression->left;
}
throw new ParserException('Parse error, unexpected end of input for ' . \get_class($this->expression));
}
return $this->expression;
}
/**
* @return array{0: int, 1: Expression}
*/
public function buildWithPointer()
{
$expr = $this->build();
return [$this->pointer, $expr];
}
/**
* @return Token|null
*/
private function nextToken()
{
$this->pointer++;
return $this->tokens[$this->pointer] ?? null;
}
/**
* @return Token|null
*/
private function peekNext()
{
return $this->tokens[$this->pointer + 1] ?? null;
}
/**
* @return int
*/
private function getPrecedence(string $operator)
{
return self::OPERATOR_PRECEDENCE[$operator] ?? 0;
}
/**
* @param array<int, Expression> $expressions
*
* @return void
*/
public function setSelectExpressions(array $expressions)
{
$this->selectExpressions = $expressions;
}
}