Skip to content

Commit 99bb80a

Browse files
committed
TASK: Implement ExportParser
1 parent e7aed5a commit 99bb80a

File tree

4 files changed

+412
-0
lines changed

4 files changed

+412
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Export;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\ComponentDeclaration\ComponentDeclarationNode;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\EnumDeclaration\EnumDeclarationNode;
27+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
28+
use PackageFactory\ComponentEngine\Language\AST\Node\StructDeclaration\StructDeclarationNode;
29+
use PackageFactory\ComponentEngine\Parser\Source\Range;
30+
31+
final class ExportNode extends Node
32+
{
33+
public function __construct(
34+
public readonly Range $rangeInSource,
35+
public readonly ComponentDeclarationNode | EnumDeclarationNode | StructDeclarationNode $declaration
36+
) {
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Export;
24+
25+
use PackageFactory\ComponentEngine\Language\Parser\ParserException;
26+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
27+
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenTypes;
28+
29+
final class ExportCouldNotBeParsed extends ParserException
30+
{
31+
public static function becauseOfUnexpectedToken(
32+
TokenTypes $expectedTokenTypes,
33+
Token $actualToken
34+
): self {
35+
return new self(
36+
code: 1691184282,
37+
message: sprintf(
38+
'Export could not be parsed because of unexpected token %s. '
39+
. 'Expected %s instead.',
40+
$actualToken->toDebugString(),
41+
$expectedTokenTypes->toDebugString()
42+
),
43+
affectedRangeInSource: $actualToken->boundaries
44+
);
45+
}
46+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\Export;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\ComponentDeclaration\ComponentDeclarationNode;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\EnumDeclaration\EnumDeclarationNode;
27+
use PackageFactory\ComponentEngine\Language\AST\Node\Export\ExportNode;
28+
use PackageFactory\ComponentEngine\Language\AST\Node\StructDeclaration\StructDeclarationNode;
29+
use PackageFactory\ComponentEngine\Language\Parser\ComponentDeclaration\ComponentDeclarationParser;
30+
use PackageFactory\ComponentEngine\Language\Parser\EnumDeclaration\EnumDeclarationParser;
31+
use PackageFactory\ComponentEngine\Language\Parser\StructDeclaration\StructDeclarationParser;
32+
use PackageFactory\ComponentEngine\Parser\Source\Range;
33+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner;
34+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
35+
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType;
36+
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenTypes;
37+
38+
final class ExportParser
39+
{
40+
private readonly ComponentDeclarationParser $componentDeclarationParser;
41+
private readonly EnumDeclarationParser $enumDeclarationParser;
42+
private readonly StructDeclarationParser $structDeclarationParser;
43+
44+
public function __construct()
45+
{
46+
$this->componentDeclarationParser = new ComponentDeclarationParser();
47+
$this->enumDeclarationParser = new EnumDeclarationParser();
48+
$this->structDeclarationParser = new StructDeclarationParser();
49+
}
50+
51+
/**
52+
* @param \Iterator<mixed,Token> $tokens
53+
* @return ExportNode
54+
*/
55+
public function parse(\Iterator &$tokens): ExportNode
56+
{
57+
$exportKeywordToken = $this->extractToken($tokens, TokenType::KEYWORD_EXPORT);
58+
$declaration = match (Scanner::type($tokens)) {
59+
TokenType::KEYWORD_COMPONENT => $this->parseComponentDeclaration($tokens),
60+
TokenType::KEYWORD_ENUM => $this->parseEnumDeclaration($tokens),
61+
TokenType::KEYWORD_STRUCT => $this->parseStructDeclaration($tokens),
62+
default => throw ExportCouldNotBeParsed::becauseOfUnexpectedToken(
63+
expectedTokenTypes: TokenTypes::from(
64+
TokenType::KEYWORD_COMPONENT,
65+
TokenType::KEYWORD_ENUM,
66+
TokenType::KEYWORD_STRUCT
67+
),
68+
actualToken: $tokens->current()
69+
)
70+
};
71+
72+
return new ExportNode(
73+
rangeInSource: Range::from(
74+
$exportKeywordToken->boundaries->start,
75+
$declaration->rangeInSource->end
76+
),
77+
declaration: $declaration
78+
);
79+
}
80+
81+
/**
82+
* @param \Iterator<mixed,Token> $tokens
83+
* @param TokenType $tokenType
84+
* @return Token
85+
*/
86+
private function extractToken(\Iterator &$tokens, TokenType $tokenType): Token
87+
{
88+
Scanner::assertType($tokens, $tokenType);
89+
$token = $tokens->current();
90+
Scanner::skipOne($tokens);
91+
Scanner::skipSpace($tokens);
92+
93+
return $token;
94+
}
95+
96+
/**
97+
* @param \Iterator<mixed,Token> $tokens
98+
* @return ComponentDeclarationNode
99+
*/
100+
private function parseComponentDeclaration(\Iterator &$tokens): ComponentDeclarationNode
101+
{
102+
return $this->componentDeclarationParser->parse($tokens);
103+
}
104+
105+
/**
106+
* @param \Iterator<mixed,Token> $tokens
107+
* @return EnumDeclarationNode
108+
*/
109+
private function parseEnumDeclaration(\Iterator &$tokens): EnumDeclarationNode
110+
{
111+
return $this->enumDeclarationParser->parse($tokens);
112+
}
113+
114+
/**
115+
* @param \Iterator<mixed,Token> $tokens
116+
* @return StructDeclarationNode
117+
*/
118+
private function parseStructDeclaration(\Iterator &$tokens): StructDeclarationNode
119+
{
120+
return $this->structDeclarationParser->parse($tokens);
121+
}
122+
}

0 commit comments

Comments
 (0)