3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Type\Parser\Factory;
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;
15 final class LexingTypeParserFactory implements TypeParserFactory
17 private TypeParser $nativeParser;
19 public function get(TypeParserSpecification ...$specifications): TypeParser
21 if (empty($specifications)) {
22 return $this->nativeParser ??= $this->nativeParser();
25 $lexer = new NativeLexer();
26 $lexer = new AdvancedClassLexer($lexer, $this);
28 foreach ($specifications as $specification) {
29 $lexer = $specification->transform($lexer);
32 return new LexingParser($lexer);
35 private function nativeParser(): TypeParser
37 $lexer = new NativeLexer();
38 $lexer = new AdvancedClassLexer($lexer, $this);
39 $parser = new LexingParser($lexer);
41 return new CachedParser($parser);