Skip to content

Commit 45f6fe0

Browse files
committed
TASK: Implement ModuleParser
1 parent e9f7ad0 commit 45f6fe0

File tree

5 files changed

+542
-0
lines changed

5 files changed

+542
-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\Language\AST\Node\Import;
24+
25+
final class ImportNodes
26+
{
27+
/**
28+
* @var ImportNode[]
29+
*/
30+
public readonly array $items;
31+
32+
public function __construct(ImportNode ...$items)
33+
{
34+
$this->items = $items;
35+
}
36+
}
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) 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\Language\AST\Node\Module;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\Export\ExportNode;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\Import\ImportNodes;
27+
use PackageFactory\ComponentEngine\Language\AST\Node\Node;
28+
use PackageFactory\ComponentEngine\Parser\Source\Range;
29+
30+
final class ModuleNode extends Node
31+
{
32+
public function __construct(
33+
public readonly Range $rangeInSource,
34+
public readonly ImportNodes $imports,
35+
public readonly ExportNode $export,
36+
) {
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Module;
24+
25+
use PackageFactory\ComponentEngine\Language\Parser\ParserException;
26+
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
27+
28+
final class ModuleCouldNotBeParsed extends ParserException
29+
{
30+
public static function becauseOfUnexpectedExceedingToken(
31+
Token $exceedingToken
32+
): self {
33+
return new self(
34+
code: 1691235933,
35+
message: sprintf(
36+
'Module could not be parsed because of unexpected exceeding token %s.',
37+
$exceedingToken->toDebugString()
38+
),
39+
affectedRangeInSource: $exceedingToken->boundaries
40+
);
41+
}
42+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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\Module;
24+
25+
use PackageFactory\ComponentEngine\Language\AST\Node\Export\ExportNode;
26+
use PackageFactory\ComponentEngine\Language\AST\Node\Import\ImportNode;
27+
use PackageFactory\ComponentEngine\Language\AST\Node\Import\ImportNodes;
28+
use PackageFactory\ComponentEngine\Language\AST\Node\Module\ModuleNode;
29+
use PackageFactory\ComponentEngine\Language\Parser\Export\ExportParser;
30+
use PackageFactory\ComponentEngine\Language\Parser\Import\ImportParser;
31+
use PackageFactory\ComponentEngine\Parser\Source\Position;
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+
37+
final class ModuleParser
38+
{
39+
private readonly ImportParser $importParser;
40+
private readonly ExportParser $exportParser;
41+
42+
public function __construct()
43+
{
44+
$this->importParser = new ImportParser();
45+
$this->exportParser = new ExportParser();
46+
}
47+
48+
/**
49+
* @param \Iterator<mixed,Token> $tokens
50+
* @return ModuleNode
51+
*/
52+
public function parse(\Iterator &$tokens): ModuleNode
53+
{
54+
Scanner::skipSpaceAndComments($tokens);
55+
56+
$imports = $this->parseImports($tokens);
57+
$export = $this->parseExport($tokens);
58+
59+
if (!Scanner::isEnd($tokens)) {
60+
throw ModuleCouldNotBeParsed::becauseOfUnexpectedExceedingToken(
61+
exceedingToken: $tokens->current()
62+
);
63+
}
64+
65+
return new ModuleNode(
66+
rangeInSource: Range::from(
67+
new Position(0, 0),
68+
$export->rangeInSource->end
69+
),
70+
imports: $imports,
71+
export: $export
72+
);
73+
}
74+
75+
/**
76+
* @param \Iterator<mixed,Token> $tokens
77+
* @return ImportNodes
78+
*/
79+
private function parseImports(\Iterator &$tokens): ImportNodes
80+
{
81+
$items = [];
82+
while (Scanner::type($tokens) !== TokenType::KEYWORD_EXPORT) {
83+
$items[] = $this->parseImport($tokens);
84+
}
85+
86+
return new ImportNodes(...$items);
87+
}
88+
89+
/**
90+
* @param \Iterator<mixed,Token> $tokens
91+
* @return ImportNode
92+
*/
93+
private function parseImport(\Iterator &$tokens): ImportNode
94+
{
95+
$import = $this->importParser->parse($tokens);
96+
Scanner::skipSpaceAndComments($tokens);
97+
98+
return $import;
99+
}
100+
101+
/**
102+
* @param \Iterator<mixed,Token> $tokens
103+
* @return ExportNode
104+
*/
105+
private function parseExport(\Iterator &$tokens): ExportNode
106+
{
107+
$export = $this->exportParser->parse($tokens);
108+
Scanner::skipSpaceAndComments($tokens);
109+
110+
return $export;
111+
}
112+
}

0 commit comments

Comments
 (0)