File tree Expand file tree Collapse file tree 7 files changed +44
-163
lines changed
Expand file tree Collapse file tree 7 files changed +44
-163
lines changed Original file line number Diff line number Diff line change @@ -34,13 +34,13 @@ if (!class_exists(Compiler::class)) {
3434$ grammar = (new Compiler ())
3535 ->load (new SplFileInfo (__DIR__ . '/../resources/grammar.pp2 ' ))
3636 ->build ()
37- ->withClassUsage ('TypeLang \\Parser \\Node ' )
38- ->withClassUsage ('TypeLang \\Parser \\Exception ' )
39- ->withClassUsage ('TypeLang \\Parser \\Exception \\SemanticException ' )
40- ->withClassUsage ('TypeLang \\Parser \\Exception \\FeatureNotAllowedException ' )
41- ;
37+ ->withClassReference ('TypeLang \\Parser \\Node ' )
38+ ->withClassReference ('TypeLang \\Parser \\Exception ' )
39+ ->withClassReference ('TypeLang \\Parser \\Exception \\SemanticException ' )
40+ ->withClassReference ('TypeLang \\Parser \\Exception \\FeatureNotAllowedException ' )
41+ -> generate () ;
4242
43- file_put_contents (__DIR__ . '/../resources/grammar.php ' , $ grammar-> generate () );
43+ file_put_contents (__DIR__ . '/../resources/grammar.php ' , $ grammar );
4444
4545//
4646// Postprocess and optimize output grammar
Original file line number Diff line number Diff line change 1010 },
1111 "require" : {
1212 "php" : " ^8.1" ,
13- "psr/cache" : " ^1.0|^2.0|^3.0" ,
14- "psr/simple-cache" : " ^1.0|^2.0|^3.0" ,
15- "psr/log" : " ^1.0|^2.0|^3.0" ,
1613 "phplrt/lexer" : " ^3.6" ,
1714 "phplrt/parser" : " ^3.6" ,
1815 "phplrt/source" : " ^3.6"
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace TypeLang \Parser ;
6+
7+ use JetBrains \PhpStorm \Language ;
8+ use Phplrt \Contracts \Source \SourceExceptionInterface ;
9+ use Phplrt \Contracts \Source \SourceFactoryInterface ;
10+ use Phplrt \Source \SourceFactory ;
11+ use TypeLang \Parser \Exception \ParserExceptionInterface ;
12+ use TypeLang \Parser \Node \Stmt \TypeStatement ;
13+
14+ final class InMemoryCachedParser implements ParserInterface
15+ {
16+ /**
17+ * @var array<non-empty-string, TypeStatement|null>
18+ */
19+ private array $ types = [];
20+
21+ public function __construct (
22+ private readonly ParserInterface $ parser = new Parser (),
23+ private readonly SourceFactoryInterface $ sources = new SourceFactory (),
24+ ) {}
25+
26+ /**
27+ * @throws ParserExceptionInterface
28+ * @throws SourceExceptionInterface
29+ * @throws \Throwable
30+ */
31+ public function parse (#[Language('PHP ' )] mixed $ source ): ?TypeStatement
32+ {
33+ $ instance = $ this ->sources ->create ($ source );
34+
35+ return $ this ->types [$ instance ->getHash ()] ??= $ this ->parser ->parse ($ source );
36+ }
37+ }
Original file line number Diff line number Diff line change @@ -67,14 +67,13 @@ final class Parser implements ParserInterface
6767 */
6868 public int $ lastProcessedTokenOffset = 0 ;
6969
70- private readonly SourceFactoryInterface $ sources ;
71-
7270 public function __construct (
7371 public readonly bool $ tolerant = false ,
7472 public readonly bool $ conditional = true ,
7573 public readonly bool $ shapes = true ,
7674 public readonly bool $ callables = true ,
7775 public readonly bool $ literals = true ,
76+ private readonly SourceFactoryInterface $ sources = new SourceFactory (),
7877 ) {
7978 /** @psalm-var GrammarConfigArray $grammar */
8079 $ grammar = require __DIR__ . '/../resources/grammar.php ' ;
@@ -85,7 +84,6 @@ public function __construct(
8584 /** @var \WeakMap<TokenInterface, IntLiteralNode> */
8685 $ this ->integerPool = new \WeakMap ();
8786
88- $ this ->sources = new SourceFactory ();
8987 $ this ->builder = new Builder ($ grammar ['reducers ' ]);
9088 $ this ->lexer = $ this ->createLexer ($ grammar );
9189 $ this ->parser = $ this ->createParser ($ this ->lexer , $ grammar );
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments