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