1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PhpMyAdmin\SqlParser\Parsers; |
||
6 | |||
7 | use PhpMyAdmin\SqlParser\Components\Expression; |
||
8 | use PhpMyAdmin\SqlParser\Exceptions\ParserException; |
||
9 | use PhpMyAdmin\SqlParser\Parseable; |
||
10 | use PhpMyAdmin\SqlParser\Parser; |
||
11 | use PhpMyAdmin\SqlParser\Token; |
||
12 | use PhpMyAdmin\SqlParser\TokensList; |
||
13 | use PhpMyAdmin\SqlParser\TokenType; |
||
14 | |||
15 | use function count; |
||
16 | use function implode; |
||
17 | use function preg_match; |
||
18 | use function strlen; |
||
19 | use function substr; |
||
20 | |||
21 | /** |
||
22 | * Parses a list of expressions delimited by a comma. |
||
23 | */ |
||
24 | final class ExpressionArray implements Parseable |
||
25 | { |
||
26 | /** |
||
27 | * @param Parser $parser the parser that serves as context |
||
28 | * @param TokensList $list the list of tokens that are being parsed |
||
29 | * @param array<string, mixed> $options parameters for parsing |
||
30 | * |
||
31 | * @return Expression[] |
||
32 | * |
||
33 | * @throws ParserException |
||
34 | */ |
||
35 | 524 | public static function parse(Parser $parser, TokensList $list, array $options = []): array |
|
36 | { |
||
37 | 524 | $ret = []; |
|
38 | |||
39 | /** |
||
40 | * The state of the parser. |
||
41 | * |
||
42 | * Below are the states of the parser. |
||
43 | * |
||
44 | * 0 ----------------------[ array ]---------------------> 1 |
||
45 | * |
||
46 | * 1 ------------------------[ , ]------------------------> 0 |
||
47 | * 1 -----------------------[ else ]----------------------> (END) |
||
48 | */ |
||
49 | 524 | $state = 0; |
|
50 | |||
51 | 524 | for (; $list->idx < $list->count; ++$list->idx) { |
|
52 | /** |
||
53 | * Token parsed at this moment. |
||
54 | */ |
||
55 | 524 | $token = $list->tokens[$list->idx]; |
|
56 | |||
57 | // End of statement. |
||
58 | 524 | if ($token->type === TokenType::Delimiter) { |
|
59 | 236 | break; |
|
60 | } |
||
61 | |||
62 | // Skipping whitespaces and comments. |
||
63 | 518 | if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
|
64 | 394 | continue; |
|
65 | } |
||
66 | |||
67 | if ( |
||
68 | 518 | ($token->type === TokenType::Keyword) |
|
69 | 518 | && ($token->flags & Token::FLAG_KEYWORD_RESERVED) |
|
70 | 518 | && ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION)) |
|
71 | 518 | && ($token->value !== 'DUAL') |
|
72 | 518 | && ($token->value !== 'NULL') |
|
73 | 518 | && ($token->value !== 'CASE') |
|
74 | 518 | && ($token->value !== 'NOT') |
|
75 | ) { |
||
76 | // No keyword is expected. |
||
77 | 436 | break; |
|
78 | } |
||
79 | |||
80 | 506 | if ($state === 0) { |
|
81 | 506 | if ($token->type === TokenType::Keyword && $token->value === 'CASE') { |
|
82 | 38 | $expr = CaseExpressions::parse($parser, $list, $options); |
|
83 | } else { |
||
84 | 504 | $expr = Expressions::parse($parser, $list, $options); |
|
85 | } |
||
86 | |||
87 | 506 | if ($expr === null) { |
|
88 | 2 | break; |
|
89 | } |
||
90 | |||
91 | 504 | $ret[] = $expr; |
|
92 | 504 | $state = 1; |
|
93 | 192 | } elseif ($state === 1) { |
|
94 | 192 | if ($token->value !== ',') { |
|
95 | 18 | break; |
|
96 | } |
||
97 | |||
98 | 184 | $state = 0; |
|
99 | } |
||
100 | } |
||
101 | |||
102 | 524 | if ($state === 0) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
103 | 22 | $parser->error('An expression was expected.', $list->tokens[$list->idx]); |
|
104 | } |
||
105 | |||
106 | 524 | --$list->idx; |
|
107 | 524 | $retIndex = count($ret) - 1; |
|
108 | 524 | if (isset($ret[$retIndex])) { |
|
109 | 504 | $expr = $ret[$retIndex]->expr; |
|
110 | 504 | 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 | 524 | return $ret; |
|
117 | } |
||
118 | |||
119 | /** @param Expression[] $component the component to be built */ |
||
120 | 66 | public static function buildAll(array $component): string |
|
121 | { |
||
122 | 66 | return implode(', ', $component); |
|
123 | } |
||
124 | } |
||
125 |