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\Grammar\PP2\Delegate; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
13
|
|
|
use Railt\Parser\Ast\Rule; |
14
|
|
|
use Railt\Parser\Ast\RuleInterface; |
15
|
|
|
use Railt\Parser\Rule\Repetition; |
16
|
|
|
use Railt\Parser\Rule\Symbol; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class RepetitionDelegate |
20
|
|
|
*/ |
21
|
|
|
class RepetitionDelegate extends BaseRuleDelegate implements ProvidesChildrenSymbol |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @return Symbol |
25
|
|
|
*/ |
26
|
|
|
public function getRule(): Symbol |
27
|
|
|
{ |
28
|
|
|
[$from, $to] = $this->getInterval($this->first('RepetitionInterval')); |
|
|
|
|
29
|
|
|
|
30
|
|
|
return new Repetition($this->getId(), $from, $to, $this->getChildrenRuleIds()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RuleInterface|NodeInterface $repetition |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
private function getInterval(RuleInterface $repetition): array |
38
|
|
|
{ |
39
|
|
|
switch (true) { |
40
|
|
|
case (bool)$repetition->first('T_ZERO_OR_MORE'): |
41
|
|
|
return [0, Repetition::INF_MAX_VALUE]; |
42
|
|
|
|
43
|
|
|
case (bool)$repetition->first('T_ONE_OR_MORE'): |
44
|
|
|
return [1, Repetition::INF_MAX_VALUE]; |
45
|
|
|
|
46
|
|
|
case (bool)$repetition->first('T_ZERO_OR_ONE'): |
47
|
|
|
return [0, 1]; |
48
|
|
|
|
49
|
|
|
case (bool)($repeat = $repetition->first('Repeat')): |
50
|
|
|
$value = (int)$repeat->first('T_NUMBER')->getValue(); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return [$value, $value]; |
53
|
|
|
break; |
|
|
|
|
54
|
|
|
|
55
|
|
|
default: |
56
|
|
|
$from = $to = Repetition::INF_MAX_VALUE; |
57
|
|
|
|
58
|
|
|
if ($nodeFrom = $repetition->first('From')) { |
59
|
|
|
$from = (int)$nodeFrom->first('T_NUMBER')->getValue(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($nodeTo = $repetition->first('To')) { |
63
|
|
|
$to = (int)$nodeTo->first('T_NUMBER')->getValue(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [$from, $to]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.