-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.php
301 lines (267 loc) · 10.4 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
declare(strict_types=1);
namespace TypeLang\Parser;
use Phplrt\Contracts\Lexer\LexerInterface;
use Phplrt\Contracts\Lexer\TokenInterface;
use Phplrt\Contracts\Parser\ParserRuntimeExceptionInterface;
use Phplrt\Contracts\Source\ReadableInterface;
use Phplrt\Contracts\Source\SourceExceptionInterface;
use Phplrt\Contracts\Source\SourceFactoryInterface;
use Phplrt\Lexer\Config\PassthroughHandler;
use Phplrt\Lexer\Lexer;
use Phplrt\Parser\BuilderInterface;
use Phplrt\Parser\Context;
use Phplrt\Parser\Exception\UnexpectedTokenException;
use Phplrt\Parser\Exception\UnrecognizedTokenException;
use Phplrt\Parser\Grammar\RuleInterface;
use Phplrt\Parser\Parser as ParserCombinator;
use Phplrt\Parser\ParserConfigsInterface;
use Phplrt\Source\SourceFactory;
use TypeLang\Parser\Exception\ParseException;
use TypeLang\Parser\Exception\SemanticException;
use TypeLang\Parser\Node\Literal\IntLiteralNode;
use TypeLang\Parser\Node\Literal\StringLiteralNode;
use TypeLang\Parser\Node\Node;
use TypeLang\Parser\Node\Stmt\TypeStatement;
/**
* @phpstan-type GrammarConfigArray array{
* initial: array-key,
* tokens: array{
* default: array<non-empty-string, non-empty-string>,
* ...
* },
* skip: list<non-empty-string>,
* grammar: array<array-key, RuleInterface>,
* reducers: array<int<0, max>|non-empty-string, callable(Context, mixed): mixed>,
* transitions?: array<array-key, mixed>
* }
*/
final class Parser implements ParserInterface
{
/**
* @var ParserCombinator<Node>
*/
private readonly ParserCombinator $parser;
private readonly Lexer $lexer;
/**
* In-memory string literal pool.
*
* @var \WeakMap<TokenInterface, StringLiteralNode>
*
* @api an annotation for another PhpStorm bug fix...
*
* @internal ...but it is truly an internal property
*/
protected readonly \WeakMap $stringPool;
/**
* In-memory integer literal pool.
*
* @var \WeakMap<TokenInterface, IntLiteralNode>
*
* @api an annotation for another PhpStorm bug fix...
*
* @internal ...but it is truly an internal property
*/
protected readonly \WeakMap $integerPool;
private readonly BuilderInterface $builder;
/**
* @var int<0, max>
*/
public int $lastProcessedTokenOffset = 0;
/**
* @param bool $tolerant Enables or disables tolerant type recognition. If
* the option is {@see true}, the parser allows arbitrary text after
* the type definition. This mode allows you to recognize types
* specified in DocBlocks.
* @param bool $conditional enables or disables support for
* dependent/conditional types such as `T ? U : V`
* @param bool $shapes enables or disables support for type shapes
* such as `T{key: U}`
* @param bool $callables enables or disables support for callable types
* such as `(T, U): V`
* @param bool $literals enables or disables support for literal types such
* as `42` or `"string"`
* @param bool $generics enables or disables support for template arguments
* such as `T<U, V>`
* @param bool $union enables or disables support for logical union types
* such as `T | U`
* @param bool $intersection enables or disables support for logical
* intersection types such as `T & U`
* @param bool $list enables or disables support for square bracket list
* types such as `T[]`
* @param bool $offsets enables or disables support for square bracket
* offset access types such as `T[U]`
* @param bool $hints enables or disables support for template argument
* hints such as `T<out U, in V>`
* @param bool $attributes enables or disables support for attributes
* such as `#[attr]`
*/
public function __construct(
public readonly bool $tolerant = false,
public readonly bool $conditional = true,
public readonly bool $shapes = true,
public readonly bool $callables = true,
public readonly bool $literals = true,
public readonly bool $generics = true,
public readonly bool $union = true,
public readonly bool $intersection = true,
public readonly bool $list = true,
public readonly bool $offsets = true,
public readonly bool $hints = true,
public readonly bool $attributes = true,
private readonly SourceFactoryInterface $sources = new SourceFactory(),
) {
/** @phpstan-var GrammarConfigArray $grammar */
$grammar = require __DIR__ . '/../resources/grammar.php';
$this->stringPool = new \WeakMap();
$this->integerPool = new \WeakMap();
$this->builder = $this->createBuilder($grammar['reducers']);
$this->lexer = $this->createLexer($grammar);
$this->parser = $this->createParser($this->lexer, $grammar);
}
/**
* @param array<int<0, max>|non-empty-string, callable(Context, mixed):mixed> $reducers
*/
private function createBuilder(array $reducers): BuilderInterface
{
return new class ($reducers) implements BuilderInterface {
/**
* @param array<int<0, max>|non-empty-string, callable(Context, mixed):mixed> $reducers
*/
public function __construct(
private readonly array $reducers,
) {}
public function build(Context $context, mixed $result): mixed
{
if (!isset($this->reducers[$context->state])) {
return $result;
}
$result = ($this->reducers[$context->state])($context, $result);
if ($result instanceof Node && $result->offset === 0) {
$result->offset = $context->lastProcessedToken->getOffset();
}
return $result;
}
};
}
/**
* @phpstan-param GrammarConfigArray $grammar
*
* @return ParserCombinator<Node>
*/
private function createParser(LexerInterface $lexer, array $grammar): ParserCombinator
{
/** @var ParserCombinator<Node> */
return new ParserCombinator(
lexer: $lexer,
grammar: $grammar['grammar'],
options: [
ParserConfigsInterface::CONFIG_INITIAL_RULE => $grammar['initial'],
ParserConfigsInterface::CONFIG_AST_BUILDER => $this->builder,
ParserConfigsInterface::CONFIG_ALLOW_TRAILING_TOKENS => $this->tolerant,
],
);
}
/**
* @phpstan-param GrammarConfigArray $grammar
*/
private function createLexer(array $grammar): Lexer
{
return new Lexer(
tokens: $grammar['tokens']['default'],
skip: $grammar['skip'],
onUnknownToken: new PassthroughHandler(),
);
}
public function parse(mixed $source): TypeStatement
{
$this->lastProcessedTokenOffset = 0;
try {
$instance = $this->sources->create($source);
try {
foreach ($this->parser->parse($instance) as $stmt) {
if ($stmt instanceof TypeStatement) {
$context = $this->parser->getLastExecutionContext();
if ($context !== null) {
$token = $context->buffer->current();
$this->lastProcessedTokenOffset = $token->getOffset();
}
return $stmt;
}
}
throw new ParseException(
message: 'Could not read type statement',
code: ParseException::ERROR_CODE_INTERNAL_ERROR,
);
} catch (UnexpectedTokenException $e) {
throw $this->unexpectedTokenError($e, $instance);
} catch (UnrecognizedTokenException $e) {
throw $this->unrecognizedTokenError($e, $instance);
} catch (ParserRuntimeExceptionInterface $e) {
throw $this->parserRuntimeError($e, $instance);
} catch (SemanticException $e) {
throw $this->semanticError($e, $instance);
} catch (\Throwable $e) {
throw $this->internalError($e, $instance);
}
} catch (SourceExceptionInterface $e) {
throw new ParseException(
message: $e->getMessage(),
code: ParseException::ERROR_CODE_INTERNAL_ERROR,
previous: $e,
);
}
}
/**
* @throws SourceExceptionInterface in case of source content reading error
*/
private function unexpectedTokenError(UnexpectedTokenException $e, ReadableInterface $source): ParseException
{
$token = $e->getToken();
return ParseException::fromUnexpectedToken(
token: $token->getValue(),
statement: $source->getContents(),
offset: $token->getOffset(),
);
}
/**
* @throws SourceExceptionInterface in case of source content reading error
*/
private function unrecognizedTokenError(UnrecognizedTokenException $e, ReadableInterface $source): ParseException
{
$token = $e->getToken();
return ParseException::fromUnrecognizedToken(
token: $token->getValue(),
statement: $source->getContents(),
offset: $token->getOffset(),
);
}
/**
* @throws SourceExceptionInterface in case of source content reading error
*/
private function semanticError(SemanticException $e, ReadableInterface $source): ParseException
{
return ParseException::fromSemanticError($e, $source);
}
/**
* @throws SourceExceptionInterface in case of source content reading error
*/
private function parserRuntimeError(ParserRuntimeExceptionInterface $e, ReadableInterface $source): ParseException
{
$token = $e->getToken();
return ParseException::fromUnrecognizedSyntaxError(
statement: $source->getContents(),
offset: $token->getOffset(),
);
}
/**
* @throws SourceExceptionInterface in case of source content reading error
*/
private function internalError(\Throwable $e, ReadableInterface $source): ParseException
{
return ParseException::fromInternalError(
statement: $source->getContents(),
e: $e,
);
}
}