|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Components\Parsers; |
|
6
|
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Components\DataType; |
|
8
|
|
|
use PhpMyAdmin\SqlParser\Components\ParameterDefinition; |
|
9
|
|
|
use PhpMyAdmin\SqlParser\Parseable; |
|
10
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
11
|
|
|
use PhpMyAdmin\SqlParser\TokensList; |
|
12
|
|
|
use PhpMyAdmin\SqlParser\TokenType; |
|
13
|
|
|
|
|
14
|
|
|
use function implode; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The definition of a parameter of a function or procedure. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class ParameterDefinitions implements Parseable |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param Parser $parser the parser that serves as context |
|
23
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
|
24
|
|
|
* @param array<string, mixed> $options parameters for parsing |
|
25
|
|
|
* |
|
26
|
|
|
* @return ParameterDefinition[] |
|
27
|
|
|
*/ |
|
28
|
52 |
|
public static function parse(Parser $parser, TokensList $list, array $options = []): array |
|
29
|
|
|
{ |
|
30
|
52 |
|
$ret = []; |
|
31
|
|
|
|
|
32
|
52 |
|
$expr = new ParameterDefinition(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The state of the parser. |
|
36
|
|
|
* |
|
37
|
|
|
* Below are the states of the parser. |
|
38
|
|
|
* |
|
39
|
|
|
* 0 -----------------------[ ( ]------------------------> 1 |
|
40
|
|
|
* |
|
41
|
|
|
* 1 ----------------[ IN / OUT / INOUT ]----------------> 1 |
|
42
|
|
|
* 1 ----------------------[ name ]----------------------> 2 |
|
43
|
|
|
* |
|
44
|
|
|
* 2 -------------------[ data type ]--------------------> 3 |
|
45
|
|
|
* |
|
46
|
|
|
* 3 ------------------------[ , ]-----------------------> 1 |
|
47
|
|
|
* 3 ------------------------[ ) ]-----------------------> (END) |
|
48
|
|
|
*/ |
|
49
|
52 |
|
$state = 0; |
|
50
|
|
|
|
|
51
|
52 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
|
52
|
|
|
/** |
|
53
|
|
|
* Token parsed at this moment. |
|
54
|
|
|
*/ |
|
55
|
52 |
|
$token = $list->tokens[$list->idx]; |
|
56
|
|
|
|
|
57
|
|
|
// End of statement. |
|
58
|
52 |
|
if ($token->type === TokenType::Delimiter) { |
|
59
|
4 |
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Skipping whitespaces and comments. |
|
63
|
52 |
|
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) { |
|
64
|
44 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
52 |
|
if ($state === 0) { |
|
68
|
52 |
|
if (($token->type === TokenType::Operator) && ($token->value === '(')) { |
|
69
|
51 |
|
$state = 1; |
|
70
|
|
|
} |
|
71
|
50 |
|
} elseif ($state === 1) { |
|
72
|
50 |
|
if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) { |
|
73
|
22 |
|
$expr->inOut = $token->value; |
|
74
|
22 |
|
++$list->idx; |
|
75
|
50 |
|
} elseif ($token->value === ')') { |
|
76
|
6 |
|
++$list->idx; |
|
77
|
6 |
|
break; |
|
78
|
|
|
} else { |
|
79
|
44 |
|
$expr->name = $token->value; |
|
80
|
44 |
|
$state = 2; |
|
81
|
|
|
} |
|
82
|
44 |
|
} elseif ($state === 2) { |
|
83
|
44 |
|
$expr->type = DataType::parse($parser, $list); |
|
84
|
44 |
|
$state = 3; |
|
85
|
|
|
} else { |
|
86
|
44 |
|
$ret[] = $expr; |
|
87
|
44 |
|
$expr = new ParameterDefinition(); |
|
88
|
44 |
|
if ($token->value === ',') { |
|
89
|
10 |
|
$state = 1; |
|
90
|
42 |
|
} elseif ($token->value === ')') { |
|
91
|
42 |
|
++$list->idx; |
|
92
|
42 |
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Last iteration was not saved. |
|
98
|
52 |
|
if (isset($expr->name) && ($expr->name !== '')) { |
|
99
|
2 |
|
$ret[] = $expr; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
52 |
|
--$list->idx; |
|
103
|
|
|
|
|
104
|
52 |
|
return $ret; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** @param ParameterDefinition[] $component the component to be built */ |
|
108
|
6 |
|
public static function buildAll(array $component): string |
|
109
|
|
|
{ |
|
110
|
6 |
|
return '(' . implode(', ', $component) . ')'; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|