1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Component; |
8
|
|
|
use PhpMyAdmin\SqlParser\Exceptions\ParserException; |
9
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
10
|
|
|
use PhpMyAdmin\SqlParser\Token; |
11
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
12
|
|
|
|
13
|
|
|
use function count; |
14
|
|
|
use function implode; |
15
|
|
|
use function preg_match; |
16
|
|
|
use function strlen; |
17
|
|
|
use function substr; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parses a list of expressions delimited by a comma. |
21
|
|
|
*/ |
22
|
|
|
final class ExpressionArray implements Component |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param Parser $parser the parser that serves as context |
26
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
27
|
|
|
* @param array<string, mixed> $options parameters for parsing |
28
|
|
|
* |
29
|
|
|
* @return Expression[] |
30
|
|
|
* |
31
|
|
|
* @throws ParserException |
32
|
|
|
*/ |
33
|
506 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
34
|
|
|
{ |
35
|
506 |
|
$ret = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The state of the parser. |
39
|
|
|
* |
40
|
|
|
* Below are the states of the parser. |
41
|
|
|
* |
42
|
|
|
* 0 ----------------------[ array ]---------------------> 1 |
43
|
|
|
* |
44
|
|
|
* 1 ------------------------[ , ]------------------------> 0 |
45
|
|
|
* 1 -----------------------[ else ]----------------------> (END) |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
506 |
|
$state = 0; |
50
|
|
|
|
51
|
506 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
52
|
|
|
/** |
53
|
|
|
* Token parsed at this moment. |
54
|
|
|
*/ |
55
|
506 |
|
$token = $list->tokens[$list->idx]; |
56
|
|
|
|
57
|
|
|
// End of statement. |
58
|
506 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
59
|
230 |
|
break; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Skipping whitespaces and comments. |
63
|
500 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
64
|
384 |
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ( |
68
|
500 |
|
($token->type === Token::TYPE_KEYWORD) |
69
|
500 |
|
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED) |
70
|
500 |
|
&& ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION)) |
71
|
500 |
|
&& ($token->value !== 'DUAL') |
72
|
500 |
|
&& ($token->value !== 'NULL') |
73
|
500 |
|
&& ($token->value !== 'CASE') |
74
|
500 |
|
&& ($token->value !== 'NOT') |
75
|
|
|
) { |
76
|
|
|
// No keyword is expected. |
77
|
420 |
|
break; |
78
|
|
|
} |
79
|
|
|
|
80
|
490 |
|
if ($state === 0) { |
81
|
490 |
|
if ($token->type === Token::TYPE_KEYWORD && $token->value === 'CASE') { |
82
|
38 |
|
$expr = CaseExpression::parse($parser, $list, $options); |
83
|
|
|
} else { |
84
|
488 |
|
$expr = Expression::parse($parser, $list, $options); |
85
|
|
|
} |
86
|
|
|
|
87
|
490 |
|
if ($expr === null) { |
88
|
2 |
|
break; |
89
|
|
|
} |
90
|
|
|
|
91
|
488 |
|
$ret[] = $expr; |
92
|
488 |
|
$state = 1; |
93
|
190 |
|
} elseif ($state === 1) { |
94
|
190 |
|
if ($token->value !== ',') { |
95
|
20 |
|
break; |
96
|
|
|
} |
97
|
|
|
|
98
|
180 |
|
$state = 0; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
506 |
|
if ($state === 0) { |
|
|
|
|
103
|
20 |
|
$parser->error('An expression was expected.', $list->tokens[$list->idx]); |
104
|
|
|
} |
105
|
|
|
|
106
|
506 |
|
--$list->idx; |
107
|
506 |
|
$retIndex = count($ret) - 1; |
108
|
506 |
|
if (isset($ret[$retIndex])) { |
109
|
488 |
|
$expr = $ret[$retIndex]->expr; |
110
|
488 |
|
if (preg_match('/\s*--\s.*$/', $expr, $matches)) { |
111
|
4 |
|
$found = $matches[0]; |
112
|
4 |
|
$ret[$retIndex]->expr = substr($expr, 0, strlen($expr) - strlen($found)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
506 |
|
return $ret; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Expression[] $component the component to be built |
121
|
|
|
* @param array<string, mixed> $options parameters for building |
122
|
|
|
*/ |
123
|
76 |
|
public static function build($component, array $options = []): string |
124
|
|
|
{ |
125
|
76 |
|
$ret = []; |
126
|
76 |
|
foreach ($component as $frag) { |
127
|
76 |
|
$ret[] = $frag::build($frag); |
128
|
|
|
} |
129
|
|
|
|
130
|
76 |
|
return implode(', ', $ret); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function __toString(): string |
134
|
|
|
{ |
135
|
|
|
return static::build($this); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|