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\Delegate; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Grammar\LookaheadIterator; |
13
|
|
|
use Railt\Lexer\Result\Eoi; |
14
|
|
|
use Railt\Lexer\Result\Token; |
15
|
|
|
use Railt\Lexer\TokenInterface; |
16
|
|
|
use Railt\Parser\Ast\Delegate; |
17
|
|
|
use Railt\Parser\Ast\LeafInterface; |
18
|
|
|
use Railt\Parser\Ast\NodeInterface; |
19
|
|
|
use Railt\Parser\Ast\Rule; |
20
|
|
|
use Railt\Parser\Ast\RuleInterface; |
21
|
|
|
use Railt\Parser\Environment; |
22
|
|
|
use Railt\Parser\Grammar; |
23
|
|
|
use Railt\Parser\GrammarInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class RuleDelegate |
27
|
|
|
*/ |
28
|
|
|
class RuleDelegate extends Rule |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return iterable|TokenInterface[]|LookaheadIterator |
32
|
|
|
*/ |
33
|
|
|
public function getInnerTokens(): iterable |
34
|
|
|
{ |
35
|
|
|
return new LookaheadIterator((function() { |
36
|
|
|
yield from $this->getTokens($this->first('RuleProduction')); |
|
|
|
|
37
|
|
|
yield new Eoi(0); |
38
|
|
|
})->call($this)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RuleInterface|NodeInterface $rule |
43
|
|
|
* @return \Traversable |
44
|
|
|
*/ |
45
|
|
|
private function getTokens(RuleInterface $rule): \Traversable |
46
|
|
|
{ |
47
|
|
|
/** @var LeafInterface $child */ |
48
|
|
|
foreach ($rule->getChildren() as $child) { |
49
|
|
|
if ($child instanceof RuleInterface) { |
50
|
|
|
yield from $this->getTokens($child); |
51
|
|
|
} else { |
52
|
|
|
yield new Token($child->getName(), $child->getValues(), $child->getOffset()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getRuleName(): string |
61
|
|
|
{ |
62
|
|
|
return $this |
|
|
|
|
63
|
|
|
->first('RuleName') |
64
|
|
|
->first('T_NAME') |
65
|
|
|
->getValue(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isKept(): bool |
72
|
|
|
{ |
73
|
|
|
return (bool)$this->first('ShouldKeep'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return null|string |
78
|
|
|
*/ |
79
|
|
|
public function getDelegate(): ?string |
80
|
|
|
{ |
81
|
|
|
$delegate = $this->first('RuleDelegate'); |
82
|
|
|
|
83
|
|
|
if ($delegate instanceof RuleInterface) { |
84
|
|
|
return $delegate->first('T_NAME')->getValue(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: