|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Frontend; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Frontend\Builder\Factory; |
|
15
|
|
|
use Railt\SDL\IR\Definition; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Builder |
|
19
|
|
|
*/ |
|
20
|
|
|
class Builder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Factory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $factory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Builder constructor. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->factory = new Factory(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Readable $readable |
|
37
|
|
|
* @param RuleInterface $ast |
|
38
|
|
|
* @return \Generator|RuleInterface[]|Definition[] |
|
39
|
|
|
* @throws \LogicException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function build(Readable $readable, RuleInterface $ast) |
|
42
|
|
|
{ |
|
43
|
|
|
$iterator = $this->forEach($readable, $ast); |
|
44
|
|
|
|
|
45
|
|
|
while ($iterator->valid()) { |
|
46
|
|
|
[$key, $value] = [$iterator->key(), $iterator->current()]; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if ($value instanceof RuleInterface) { |
|
49
|
|
|
yield from $children = $this->build($readable, $value); |
|
50
|
|
|
$iterator->send($children->getReturn()); |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$iterator->send(yield $key => $value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $iterator->getReturn(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Readable $readable |
|
62
|
|
|
* @param RuleInterface $ast |
|
63
|
|
|
* @return mixed|null |
|
64
|
|
|
* @throws \LogicException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function reduce(Readable $readable, RuleInterface $ast) |
|
67
|
|
|
{ |
|
68
|
|
|
$iterator = $this->build($readable, $ast); |
|
69
|
|
|
|
|
70
|
|
|
while ($iterator->valid()) { |
|
71
|
|
|
$iterator->send($iterator->current()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $iterator->getReturn(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Readable $readable |
|
79
|
|
|
* @param RuleInterface $ast |
|
80
|
|
|
* @return \Generator |
|
81
|
|
|
* @throws \LogicException |
|
82
|
|
|
*/ |
|
83
|
|
|
private function forEach(Readable $readable, RuleInterface $ast): \Generator |
|
84
|
|
|
{ |
|
85
|
|
|
$builder = $this->factory->resolve($readable, $ast); |
|
86
|
|
|
$result = $builder->build($readable, $ast); |
|
87
|
|
|
|
|
88
|
|
|
if (\is_iterable($result)) { |
|
89
|
|
|
yield from $result; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $result instanceof \Generator ? $result->getReturn() : $result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.