Skip to content

Commit 93dea79

Browse files
committed
TASK: Implement StructDeclarationParser
1 parent 8c85541 commit 93dea79

6 files changed

Lines changed: 971 additions & 0 deletions

File tree

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) 2022 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\StructName;
24+
25+
final class StructName
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
final class PropertyDeclarationNodes
26+
{
27+
/**
28+
* @var array<string,PropertyDeclarationNode>
29+
*/
30+
public readonly array $items;
31+
32+
public function __construct(PropertyDeclarationNode ...$items)
33+
{
34+
$itemsAsHashMap = [];
35+
foreach ($items as $item) {
36+
$itemsAsHashMap[$item->name->value->value] = $item;
37+
}
38+
39+
$this->items = $itemsAsHashMap;
40+
}
41+
}
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\StructDeclaration;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\PropertyDeclaration\PropertyDeclarationNodes;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
29+
final class StructDeclarationNode extends Node
30+
{
31+
public function __construct(
32+
public readonly NodeAttributes $attributes,
33+
public readonly StructNameNode $name,
34+
public readonly PropertyDeclarationNodes $properties
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\StructDeclaration;
24+
25+
use PackageFactory\ComponentEngine\Domain\StructName\StructName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
27+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
28+
29+
final class StructNameNode extends Node
30+
{
31+
public function __construct(
32+
public readonly NodeAttributes $attributes,
33+
public readonly StructName $value
34+
) {
35+
}
36+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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\StructDeclaration;
24+
25+
use PackageFactory\ComponentEngine\Domain\StructName\StructName;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\PropertyDeclaration\PropertyDeclarationNodes;
27+
use PackageFactory\ComponentEngine\Language\AST\Node\StructDeclaration\StructDeclarationNode;
28+
use PackageFactory\ComponentEngine\Language\AST\Node\StructDeclaration\StructNameNode;
29+
use PackageFactory\ComponentEngine\Language\AST\NodeAttributes\NodeAttributes;
30+
use PackageFactory\ComponentEngine\Language\Parser\PropertyDeclaration\PropertyDeclarationParser;
31+
use PackageFactory\ComponentEngine\Parser\Source\Range;
32+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner;
33+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
34+
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType;
35+
36+
final class StructDeclarationParser
37+
{
38+
private readonly PropertyDeclarationParser $propertyDeclarationParser;
39+
40+
public function __construct()
41+
{
42+
$this->propertyDeclarationParser = new PropertyDeclarationParser();
43+
}
44+
45+
/**
46+
* @param \Iterator<mixed,Token> $tokens
47+
* @return StructDeclarationNode
48+
*/
49+
public function parse(\Iterator $tokens): StructDeclarationNode
50+
{
51+
$structKeywordToken = $this->extractStructKeywordToken($tokens);
52+
$structNameNode = $this->parseStructName($tokens);
53+
$this->skipOpeningBracketToken($tokens);
54+
$propertyDeclarationNodes = $this->parsePropertyDeclarations($tokens);
55+
$closingBracketToken = $this->extractClosingBracketToken($tokens);
56+
57+
return new StructDeclarationNode(
58+
attributes: new NodeAttributes(
59+
rangeInSource: Range::from(
60+
$structKeywordToken->boundaries->start,
61+
$closingBracketToken->boundaries->end
62+
)
63+
),
64+
name: $structNameNode,
65+
properties: $propertyDeclarationNodes
66+
);
67+
}
68+
69+
/**
70+
* @param \Iterator<mixed,Token> $tokens
71+
* @return Token
72+
*/
73+
public function extractStructKeywordToken(\Iterator $tokens): Token
74+
{
75+
Scanner::assertType($tokens, TokenType::KEYWORD_STRUCT);
76+
77+
$structKeywordToken = $tokens->current();
78+
79+
Scanner::skipOne($tokens);
80+
Scanner::skipSpace($tokens);
81+
82+
return $structKeywordToken;
83+
}
84+
85+
/**
86+
* @param \Iterator<mixed,Token> $tokens
87+
* @return StructNameNode
88+
*/
89+
public function parseStructName(\Iterator $tokens): StructNameNode
90+
{
91+
Scanner::assertType($tokens, TokenType::STRING);
92+
93+
$structNameToken = $tokens->current();
94+
95+
Scanner::skipOne($tokens);
96+
Scanner::skipSpaceAndComments($tokens);
97+
98+
return new StructNameNode(
99+
attributes: new NodeAttributes(
100+
rangeInSource: $structNameToken->boundaries
101+
),
102+
value: StructName::from($structNameToken->value)
103+
);
104+
}
105+
106+
/**
107+
* @param \Iterator<mixed,Token> $tokens
108+
* @return void
109+
*/
110+
public function skipOpeningBracketToken(\Iterator $tokens): void
111+
{
112+
Scanner::assertType($tokens, TokenType::BRACKET_CURLY_OPEN);
113+
Scanner::skipOne($tokens);
114+
Scanner::skipSpaceAndComments($tokens);
115+
}
116+
117+
/**
118+
* @param \Iterator<mixed,Token> $tokens
119+
* @return PropertyDeclarationNodes
120+
*/
121+
public function parsePropertyDeclarations(\Iterator $tokens): PropertyDeclarationNodes
122+
{
123+
$items = [];
124+
while (Scanner::type($tokens) === TokenType::STRING) {
125+
$items[] = $this->propertyDeclarationParser->parse($tokens);
126+
Scanner::skipSpaceAndComments($tokens);
127+
}
128+
129+
return new PropertyDeclarationNodes(...$items);
130+
}
131+
132+
/**
133+
* @param \Iterator<mixed,Token> $tokens
134+
* @return Token
135+
*/
136+
public function extractClosingBracketToken(\Iterator $tokens): Token
137+
{
138+
Scanner::assertType($tokens, TokenType::BRACKET_CURLY_CLOSE);
139+
140+
$closingBracketToken = $tokens->current();
141+
142+
Scanner::skipOne($tokens);
143+
144+
return $closingBracketToken;
145+
}
146+
}

0 commit comments

Comments
 (0)