|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Type; |
|
6
|
|
|
|
|
7
|
|
|
use JMS\Serializer\Type\Exception\SyntaxError; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal |
|
11
|
|
|
* |
|
12
|
|
|
* @phpstan-import-type TypeArray from Type |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Parser implements ParserInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Lexer |
|
18
|
|
|
*/ |
|
19
|
448 |
|
private $lexer; |
|
20
|
|
|
|
|
21
|
448 |
|
/** |
|
22
|
448 |
|
* @var bool |
|
23
|
448 |
|
*/ |
|
24
|
|
|
private $root = true; |
|
25
|
320 |
|
|
|
26
|
|
|
public function parse(string $string): array |
|
27
|
|
|
{ |
|
28
|
320 |
|
$this->lexer = new Lexer(); |
|
29
|
|
|
$this->lexer->setInput($string); |
|
30
|
314 |
|
$this->lexer->moveNext(); |
|
31
|
6 |
|
|
|
32
|
6 |
|
return $this->visit(); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
private function visit() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->lexer->moveNext(); |
|
41
|
|
|
|
|
42
|
|
|
if (!$this->lexer->token) { |
|
43
|
|
|
throw new SyntaxError( |
|
44
|
|
|
'Syntax error, unexpected end of stream', |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (Lexer::T_FLOAT === $this->lexer->token->type) { |
|
|
|
|
|
|
49
|
|
|
return floatval($this->lexer->token->value); |
|
50
|
|
|
} elseif (Lexer::T_INTEGER === $this->lexer->token->type) { |
|
|
|
|
|
|
51
|
|
|
return intval($this->lexer->token->value); |
|
52
|
|
|
} elseif (Lexer::T_NULL === $this->lexer->token->type) { |
|
|
|
|
|
|
53
|
|
|
return null; |
|
54
|
|
|
} elseif (Lexer::T_STRING === $this->lexer->token->type) { |
|
|
|
|
|
|
55
|
|
|
return $this->lexer->token->value; |
|
56
|
|
|
} elseif (Lexer::T_IDENTIFIER === $this->lexer->token->type) { |
|
|
|
|
|
|
57
|
|
|
if ($this->lexer->isNextToken(Lexer::T_TYPE_START)) { |
|
58
|
|
|
return $this->visitCompoundType(); |
|
59
|
|
|
} elseif ($this->lexer->isNextToken(Lexer::T_ARRAY_START)) { |
|
60
|
|
|
return $this->visitArrayType(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->visitSimpleType(); |
|
64
|
|
|
} elseif (!$this->root && Lexer::T_ARRAY_START === $this->lexer->token->type) { |
|
|
|
|
|
|
65
|
|
|
return $this->visitArrayType(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
throw new SyntaxError(sprintf( |
|
69
|
|
|
'Syntax error, unexpected "%s" (%s)', |
|
70
|
|
|
$this->lexer->token->value, |
|
71
|
|
|
$this->getConstant($this->lexer->token->type), |
|
|
|
|
|
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return TypeArray |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
private function visitSimpleType(): array |
|
79
|
|
|
{ |
|
80
|
|
|
$value = $this->lexer->token->value; |
|
81
|
|
|
|
|
82
|
|
|
return ['name' => $value, 'params' => []]; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return TypeArray |
|
87
|
|
|
*/ |
|
88
|
|
|
private function visitCompoundType(): array |
|
89
|
|
|
{ |
|
90
|
|
|
$this->root = false; |
|
91
|
|
|
$name = $this->lexer->token->value; |
|
92
|
|
|
$this->match(Lexer::T_TYPE_START); |
|
93
|
|
|
|
|
94
|
|
|
$params = []; |
|
95
|
|
|
if (!$this->lexer->isNextToken(Lexer::T_TYPE_END)) { |
|
|
|
|
|
|
96
|
|
|
while (true) { |
|
97
|
|
|
$params[] = $this->visit(); |
|
98
|
|
|
|
|
99
|
|
|
if ($this->lexer->isNextToken(Lexer::T_TYPE_END)) { |
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->match(Lexer::T_COMMA); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->match(Lexer::T_TYPE_END); |
|
108
|
|
|
|
|
109
|
|
|
return [ |
|
|
|
|
|
|
110
|
|
|
'name' => $name, |
|
111
|
|
|
'params' => $params, |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function visitArrayType(): array |
|
116
|
|
|
{ |
|
117
|
|
|
/* |
|
118
|
|
|
* Here we should call $this->match(Lexer::T_ARRAY_START); to make it clean |
|
119
|
|
|
* but the token has already been consumed by moveNext() in visit() |
|
120
|
|
|
*/ |
|
121
|
|
|
|
|
122
|
|
|
$params = []; |
|
123
|
|
|
if (!$this->lexer->isNextToken(Lexer::T_ARRAY_END)) { |
|
|
|
|
|
|
124
|
|
|
while (true) { |
|
125
|
|
|
$params[] = $this->visit(); |
|
126
|
|
|
if ($this->lexer->isNextToken(Lexer::T_ARRAY_END)) { |
|
127
|
|
|
break; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->match(Lexer::T_COMMA); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->match(Lexer::T_ARRAY_END); |
|
135
|
|
|
|
|
136
|
|
|
return $params; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function match(int $token): void |
|
140
|
|
|
{ |
|
141
|
|
|
if (!$this->lexer->lookahead) { |
|
142
|
|
|
throw new SyntaxError( |
|
143
|
|
|
sprintf('Syntax error, unexpected end of stream, expected %s', $this->getConstant($token)), |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($this->lexer->lookahead->type === $token) { |
|
|
|
|
|
|
148
|
|
|
$this->lexer->moveNext(); |
|
149
|
|
|
|
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
throw new SyntaxError(sprintf( |
|
154
|
|
|
'Syntax error, unexpected "%s" (%s), expected was %s', |
|
155
|
|
|
$this->lexer->lookahead->value, |
|
156
|
|
|
$this->getConstant($this->lexer->lookahead->type), |
|
|
|
|
|
|
157
|
|
|
$this->getConstant($token), |
|
158
|
|
|
)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function getConstant(int $value): string |
|
162
|
|
|
{ |
|
163
|
|
|
$oClass = new \ReflectionClass(Lexer::class); |
|
164
|
|
|
|
|
165
|
|
|
return array_search($value, $oClass->getConstants()); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|