Skip to content

Commit 8c85541

Browse files
committed
TASK: Implement PropertyDeclarationParser
1 parent 2ab3f72 commit 8c85541

File tree

5 files changed

+458
-0
lines changed

5 files changed

+458
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Domain\PropertyName;
24+
25+
final class PropertyName
26+
{
27+
private function __construct(
28+
public readonly string $value
29+
) {
30+
}
31+
32+
public static function from(string $string): self
33+
{
34+
return new self($string);
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\AST\Node\PropertyDeclaration;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\TypeReference\TypeReferenceNode;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
29+
final class PropertyDeclarationNode extends Node
30+
{
31+
public function __construct(
32+
public readonly NodeAttributes $attributes,
33+
public readonly PropertyNameNode $name,
34+
public readonly TypeReferenceNode $type
35+
) {
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\AST\Node\PropertyDeclaration;
24+
25+
use PackageFactory\ComponentEngine\Domain\PropertyName\PropertyName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
29+
final class PropertyNameNode extends Node
30+
{
31+
public function __construct(
32+
public readonly NodeAttributes $attributes,
33+
public readonly PropertyName $value
34+
) {
35+
}
36+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)