|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * PackageFactory.ComponentEngine - Universal View Components for PHP |
| 5 | + * Copyright (C) 2023 Contributors of PackageFactory.ComponentEngine |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace PackageFactory\ComponentEngine\Language\Parser\PropertyDeclaration; |
| 24 | + |
| 25 | +use PackageFactory\ComponentEngine\Domain\PropertyName\PropertyName; |
| 26 | +use PackageFactory\ComponentEngine\Language\AST\Node\PropertyDeclaration\PropertyDeclarationNode; |
| 27 | +use PackageFactory\ComponentEngine\Language\AST\Node\PropertyDeclaration\PropertyNameNode; |
| 28 | +use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes; |
| 29 | +use PackageFactory\ComponentEngine\Language\Parser\TypeReference\TypeReferenceParser; |
| 30 | +use PackageFactory\ComponentEngine\Parser\Source\Range; |
| 31 | +use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner; |
| 32 | +use PackageFactory\ComponentEngine\Parser\Tokenizer\Token; |
| 33 | +use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType; |
| 34 | + |
| 35 | +final class PropertyDeclarationParser |
| 36 | +{ |
| 37 | + private readonly TypeReferenceParser $typeReferenceParser; |
| 38 | + |
| 39 | + public function __construct() |
| 40 | + { |
| 41 | + $this->typeReferenceParser = new TypeReferenceParser(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param \Iterator<mixed,Token> $tokens |
| 46 | + * @return PropertyDeclarationNode |
| 47 | + */ |
| 48 | + public function parse(\Iterator $tokens): PropertyDeclarationNode |
| 49 | + { |
| 50 | + Scanner::assertType($tokens, TokenType::STRING); |
| 51 | + $propertyNameToken = $tokens->current(); |
| 52 | + |
| 53 | + Scanner::skipOne($tokens); |
| 54 | + |
| 55 | + Scanner::assertType($tokens, TokenType::COLON); |
| 56 | + Scanner::skipOne($tokens); |
| 57 | + |
| 58 | + Scanner::skipSpace($tokens); |
| 59 | + |
| 60 | + $typeReferenceNode = $this->typeReferenceParser->parse($tokens); |
| 61 | + |
| 62 | + return new PropertyDeclarationNode( |
| 63 | + attributes: new NodeAttributes( |
| 64 | + rangeInSource: Range::from( |
| 65 | + $propertyNameToken->boundaries->start, |
| 66 | + $typeReferenceNode->attributes->rangeInSource->end |
| 67 | + ) |
| 68 | + ), |
| 69 | + name: new PropertyNameNode( |
| 70 | + attributes: new NodeAttributes( |
| 71 | + rangeInSource: $propertyNameToken->boundaries |
| 72 | + ), |
| 73 | + value: PropertyName::from($propertyNameToken->value) |
| 74 | + ), |
| 75 | + type: $typeReferenceNode |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
0 commit comments