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\Compiler\Parser\Ast; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Rule |
14
|
|
|
*/ |
15
|
|
|
class Rule extends Node implements RuleInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array|iterable|\Traversable |
19
|
|
|
*/ |
20
|
|
|
protected $children; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Rule constructor. |
24
|
|
|
* @param string $name |
25
|
|
|
* @param iterable $children |
26
|
|
|
*/ |
27
|
54 |
|
public function __construct(string $name, iterable $children = [], int $offset = 0) |
28
|
|
|
{ |
29
|
54 |
|
parent::__construct($name, $offset); |
30
|
54 |
|
$this->children = $children; |
31
|
54 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
54 |
|
public function getChildren(): array |
37
|
|
|
{ |
38
|
54 |
|
if ($this->children instanceof \Traversable) { |
39
|
|
|
$this->children = \iterator_to_array($this->children); |
40
|
|
|
} |
41
|
|
|
|
42
|
54 |
|
return $this->children; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $index |
47
|
|
|
* @return null|NodeInterface |
48
|
|
|
*/ |
49
|
|
|
public function getChild(int $index): ?NodeInterface |
50
|
|
|
{ |
51
|
|
|
return $this->getChildren()[$index] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function hasChildren(): bool |
58
|
|
|
{ |
59
|
|
|
return \count($this->getChildren()) > 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param NodeInterface $node |
64
|
|
|
*/ |
65
|
|
|
public function append(NodeInterface $node): void |
66
|
|
|
{ |
67
|
|
|
$this->getChildren(); |
68
|
|
|
|
69
|
|
|
$this->children[] = $node; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return null|NodeInterface |
74
|
|
|
*/ |
75
|
|
|
public function pop(): ?NodeInterface |
76
|
|
|
{ |
77
|
|
|
return \count($this->children) ? \array_pop($this->children) : null; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
public function count(): int |
84
|
|
|
{ |
85
|
|
|
return \count($this->getChildren()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \Traversable |
90
|
|
|
*/ |
91
|
|
|
public function getIterator(): \Traversable |
92
|
|
|
{ |
93
|
|
|
yield from $this->getChildren(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $name |
98
|
|
|
* @param int|null $depth |
99
|
|
|
* @return null|NodeInterface |
100
|
|
|
*/ |
101
|
|
|
public function find(string $name, int $depth = null): ?NodeInterface |
102
|
|
|
{ |
103
|
|
|
foreach ($this->getChildren() as $child) { |
104
|
|
|
if ($child->is($name)) { |
105
|
|
|
return $child; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (\is_int($depth) && $depth > 0 && $child instanceof RuleInterface) { |
109
|
|
|
return $child->find($name, $depth - 1); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string[]|\Traversable |
118
|
|
|
*/ |
119
|
|
|
public function getValue() |
120
|
|
|
{ |
121
|
|
|
foreach ($this->children as $child) { |
122
|
|
|
if ($child instanceof RuleInterface) { |
123
|
|
|
yield from $child->getValue(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($child instanceof LeafInterface) { |
127
|
|
|
yield $child => $child->getValue(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|