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