1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* @link https://github.com/nicoSWD |
8
|
|
|
* @author Nicolas Oelgart <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace nicoSWD\Rule\TokenStream; |
11
|
|
|
|
12
|
|
|
use Closure; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use nicoSWD\Rule\Parser\Exception\ParserException; |
15
|
|
|
use nicoSWD\Rule\Grammar\CallableUserFunction; |
16
|
|
|
use nicoSWD\Rule\Tokenizer\TokenizerInterface; |
17
|
|
|
use nicoSWD\Rule\TokenStream\Exception\UndefinedVariableException; |
18
|
|
|
use nicoSWD\Rule\TokenStream\Token\BaseToken; |
19
|
|
|
use nicoSWD\Rule\TokenStream\Token\TokenFactory; |
20
|
|
|
|
21
|
|
|
class AST |
22
|
|
|
{ |
23
|
|
|
/** @var TokenizerInterface */ |
24
|
|
|
private $tokenizer; |
25
|
|
|
/** @var TokenFactory */ |
26
|
|
|
public $tokenFactory; |
27
|
|
|
/** @var TokenStream */ |
28
|
|
|
private $tokenStream; |
29
|
|
|
/** @var Closure[] */ |
30
|
|
|
private $functions = []; |
31
|
|
|
/** @var mixed[] */ |
32
|
|
|
private $variables = []; |
33
|
|
|
/** @var string[] */ |
34
|
|
|
private $methods = []; |
35
|
|
|
|
36
|
224 |
|
public function __construct( |
37
|
|
|
TokenizerInterface $tokenizer, |
38
|
|
|
TokenFactory $tokenFactory, |
39
|
|
|
TokenStream $tokenStream |
40
|
|
|
) { |
41
|
224 |
|
$this->tokenizer = $tokenizer; |
42
|
224 |
|
$this->tokenFactory = $tokenFactory; |
43
|
224 |
|
$this->tokenStream = $tokenStream; |
44
|
224 |
|
} |
45
|
|
|
|
46
|
220 |
|
public function getStream(string $rule): TokenStream |
47
|
|
|
{ |
48
|
220 |
|
return $this->tokenStream->create($this->tokenizer->tokenize($rule), $this); |
49
|
|
|
} |
50
|
|
|
|
51
|
26 |
|
public function getFunction(string $name): Closure |
52
|
|
|
{ |
53
|
26 |
|
if (empty($this->functions)) { |
54
|
26 |
|
$this->registerFunctions(); |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
if (!isset($this->functions[$name])) { |
58
|
4 |
|
throw ParserException::undefinedFunction($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
22 |
|
return $this->functions[$name]; |
62
|
|
|
} |
63
|
|
|
|
64
|
108 |
|
public function getMethod(string $methodName, BaseToken $token): CallableUserFunction |
65
|
|
|
{ |
66
|
108 |
|
if (empty($this->methods)) { |
67
|
108 |
|
$this->registerMethods(); |
68
|
|
|
} |
69
|
|
|
|
70
|
108 |
|
if (!isset($this->methods[$methodName])) { |
71
|
4 |
|
throw new Exception\UndefinedMethodException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
104 |
|
return new $this->methods[$methodName]($token); |
75
|
|
|
} |
76
|
|
|
|
77
|
220 |
|
public function setVariables(array $variables) |
78
|
|
|
{ |
79
|
220 |
|
$this->variables = $variables; |
80
|
220 |
|
} |
81
|
|
|
|
82
|
90 |
|
public function getVariable(string $name): BaseToken |
83
|
|
|
{ |
84
|
90 |
|
if (!$this->variableExists($name)) { |
85
|
4 |
|
throw new UndefinedVariableException(); |
86
|
|
|
} |
87
|
|
|
|
88
|
86 |
|
return $this->tokenFactory->createFromPHPType($this->variables[$name]); |
89
|
|
|
} |
90
|
|
|
|
91
|
90 |
|
public function variableExists(string $name): bool |
92
|
|
|
{ |
93
|
90 |
|
return array_key_exists($name, $this->variables); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function registerFunctionClass(string $functionName, string $className) |
97
|
|
|
{ |
98
|
22 |
|
$this->functions[$functionName] = function (...$args) use ($className): BaseToken { |
99
|
22 |
|
$function = new $className(); |
100
|
|
|
|
101
|
22 |
|
if (!$function instanceof CallableUserFunction) { |
102
|
|
|
throw new InvalidArgumentException( |
103
|
|
|
sprintf( |
104
|
|
|
"%s must be an instance of %s", |
105
|
|
|
$className, |
106
|
|
|
CallableUserFunction::class |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
22 |
|
return $function->call(...$args); |
|
|
|
|
112
|
|
|
}; |
113
|
26 |
|
} |
114
|
|
|
|
115
|
26 |
|
private function registerFunctions() |
116
|
|
|
{ |
117
|
26 |
|
foreach ($this->tokenizer->getGrammar()->getInternalFunctions() as $functionName => $className) { |
118
|
26 |
|
$this->registerFunctionClass($functionName, $className); |
119
|
|
|
} |
120
|
26 |
|
} |
121
|
|
|
|
122
|
108 |
|
private function registerMethods() |
123
|
|
|
{ |
124
|
108 |
|
$this->methods = $this->tokenizer->getGrammar()->getInternalMethods(); |
125
|
108 |
|
} |
126
|
|
|
} |
127
|
|
|
|
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: