-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Parser.php
127 lines (107 loc) · 4.51 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace Neos\Fusion\Core;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Fusion;
use Neos\Fusion\Core\Cache\ParserCache;
use Neos\Fusion\Core\ObjectTreeParser\Ast\FusionFile;
use Neos\Fusion\Core\ObjectTreeParser\FilePatternResolver;
use Neos\Fusion\Core\ObjectTreeParser\MergedArrayTree;
use Neos\Fusion\Core\ObjectTreeParser\MergedArrayTreeVisitor;
use Neos\Fusion\Core\ObjectTreeParser\ObjectTreeParser;
use Neos\Flow\Annotations as Flow;
/**
* The Fusion Parser
*
* @api
*/
class Parser implements ParserInterface
{
/**
* Reserved parse tree keys for internal usage.
*/
public static array $reservedParseTreeKeys = ['__meta', '__prototypes', '__stopInheritanceChain', '__prototypeObjectName', '__prototypeChain', '__value', '__objectType', '__eelExpression'];
/**
* @Flow\Inject
* @var DslFactory
*/
protected $dslFactory;
/**
* @Flow\Inject
* @var ParserCache
*/
protected $parserCache;
/**
* Parses the given Fusion source code, resolves includes and returns a merged array tree
* as the result.
*
* @param string $sourceCode The Fusion source code to parse
* @param string|null $contextPathAndFilename An optional path and filename used for relative Fusion file includes
* @param array $mergedArrayTreeUntilNow Used internally for keeping track of the built merged array tree
* @return array The merged array tree for the Fusion runtime, generated from the source code
* @throws Fusion\Exception
* @api
*/
public function parse(string $sourceCode, ?string $contextPathAndFilename = null, array $mergedArrayTreeUntilNow = []): array
{
$fusionFile = $this->getFusionFile($sourceCode, $contextPathAndFilename);
$mergedArrayTree = new MergedArrayTree($mergedArrayTreeUntilNow);
$mergedArrayTree = $this->getMergedArrayTreeVisitor($mergedArrayTree)->visitFusionFile($fusionFile);
$mergedArrayTree->buildPrototypeHierarchy();
return $mergedArrayTree->getTree();
}
protected function handleFileInclude(MergedArrayTree $mergedArrayTree, string $filePattern, ?string $contextPathAndFilename): void
{
$filesToInclude = FilePatternResolver::resolveFilesByPattern($filePattern, $contextPathAndFilename, '.fusion');
foreach ($filesToInclude as $file) {
if (is_readable($file) === false) {
throw new Fusion\Exception("Could not read file '$file' of pattern '$filePattern'.", 1347977017);
}
// Check if not trying to recursively include the current file via globbing
if ($contextPathAndFilename === null
|| stat($contextPathAndFilename) !== stat($file)) {
$fusionFile = $this->getFusionFile(file_get_contents($file), $file);
$this->getMergedArrayTreeVisitor($mergedArrayTree)->visitFusionFile($fusionFile);
}
}
}
protected function handleDslTranspile(string $identifier, string $code)
{
return $this->parserCache->cacheForDsl(
$identifier,
$code,
function () use ($identifier, $code) {
$dslObject = $this->dslFactory->create($identifier);
$transpiledFusion = $dslObject->transpile($code);
$fusionFile = ObjectTreeParser::parse('value = ' . $transpiledFusion);
$mergedArrayTree = $this->getMergedArrayTreeVisitor(new MergedArrayTree())->visitFusionFile($fusionFile);
$temporaryAst = $mergedArrayTree->getTree();
$dslValue = $temporaryAst['value'];
return $dslValue;
}
);
}
protected function getMergedArrayTreeVisitor(MergedArrayTree $mergedArrayTree): MergedArrayTreeVisitor
{
return new MergedArrayTreeVisitor(
$mergedArrayTree,
fn (...$args) => $this->handleFileInclude(...$args),
fn (...$args) => $this->handleDslTranspile(...$args)
);
}
protected function getFusionFile(string $sourceCode, ?string $contextPathAndFilename): FusionFile
{
return $this->parserCache->cacheForFusionFile(
$contextPathAndFilename,
fn () => ObjectTreeParser::parse($sourceCode, $contextPathAndFilename)
);
}
}