facde9206e00290943b58443a7aebcea526a9e59
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Type\Parser\Factory;
6
7 use CuyZ\Valinor\Type\Parser\CachedParser;
8 use CuyZ\Valinor\Type\Parser\Factory\Specifications\TypeParserSpecification;
9 use CuyZ\Valinor\Type\Parser\Lexer\AdvancedClassLexer;
10 use CuyZ\Valinor\Type\Parser\Lexer\NativeLexer;
11 use CuyZ\Valinor\Type\Parser\LexingParser;
12 use CuyZ\Valinor\Type\Parser\TypeParser;
13
14 /** @internal */
15 final class LexingTypeParserFactory implements TypeParserFactory
16 {
17 private TypeParser $nativeParser;
18
19 public function get(TypeParserSpecification ...$specifications): TypeParser
20 {
21 if (empty($specifications)) {
22 return $this->nativeParser ??= $this->nativeParser();
23 }
24
25 $lexer = new NativeLexer();
26 $lexer = new AdvancedClassLexer($lexer, $this);
27
28 foreach ($specifications as $specification) {
29 $lexer = $specification->transform($lexer);
30 }
31
32 return new LexingParser($lexer);
33 }
34
35 private function nativeParser(): TypeParser
36 {
37 $lexer = new NativeLexer();
38 $lexer = new AdvancedClassLexer($lexer, $this);
39 $parser = new LexingParser($lexer);
40
41 return new CachedParser($parser);
42 }
43 }