1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RulerZ\Parser; |
6
|
|
|
|
7
|
|
|
use Hoa\Compiler; |
8
|
|
|
use Hoa\File; |
9
|
|
|
use Hoa\Ruler; |
10
|
|
|
use Hoa\Visitor; |
11
|
|
|
|
12
|
|
|
use RulerZ\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Parses a rule. |
16
|
|
|
* |
17
|
|
|
* A valid rule returns an AST: |
18
|
|
|
* ``` |
19
|
|
|
* $parser = new Parser; |
20
|
|
|
* $ast = $parser->parse('foo = 42'); |
21
|
|
|
* ``` |
22
|
|
|
* |
23
|
|
|
* And an invalid one throw an exception: |
24
|
|
|
* ```should_throw |
25
|
|
|
* $parser = new Parser; |
26
|
|
|
* $parser->parse('foo = '); |
27
|
|
|
* ``` |
28
|
|
|
*/ |
29
|
|
|
class Parser implements Visitor\Visit |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Parser. |
33
|
|
|
* |
34
|
|
|
* @var \Hoa\Compiler\Llk\Parser |
35
|
|
|
*/ |
36
|
|
|
private $parser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Root. |
40
|
|
|
* |
41
|
|
|
* @var \RulerZ\Model\Rule object |
42
|
|
|
*/ |
43
|
|
|
private $root; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Next positional parameter index. |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $nextParameterIndex = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Parses the rule into an equivalent AST. |
54
|
|
|
* |
55
|
|
|
* @param string $rule The rule represented as a string. |
56
|
|
|
* |
57
|
|
|
* @return \RulerZ\Model\Rule |
58
|
|
|
*/ |
59
|
|
|
public function parse($rule) |
60
|
|
|
{ |
61
|
|
|
if ($this->parser === null) { |
62
|
|
|
$this->parser = Compiler\Llk::load( |
63
|
|
|
new File\Read(__DIR__.'/../Grammar.pp') |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->nextParameterIndex = 0; |
68
|
|
|
|
69
|
|
|
return $this->visit($this->parser->parse($rule)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Visit an element. |
74
|
|
|
* |
75
|
|
|
* @param \Hoa\Visitor\Element $element Element to visit. |
76
|
|
|
* @param mixed &$handle Handle (reference). |
77
|
|
|
* @param mixed $eldnah Handle (not reference). |
78
|
|
|
* |
79
|
|
|
* @return \RulerZ\Model\Rule |
80
|
|
|
* |
81
|
|
|
* @throws \Hoa\Ruler\Exception\Interpreter |
82
|
|
|
*/ |
83
|
|
|
public function visit(Visitor\Element $element, &$handle = null, $eldnah = null) |
84
|
|
|
{ |
85
|
|
|
/** @var \Hoa\Compiler\Llk\TreeNode $element */ |
86
|
|
|
$id = $element->getId(); |
87
|
|
|
$variable = false !== $eldnah; |
88
|
|
|
|
89
|
|
|
switch ($id) { |
90
|
|
|
case '#expression': |
91
|
|
|
$this->root = new Model\Rule(); |
92
|
|
|
$this->root->expression = $element->getChild(0)->accept( |
|
|
|
|
93
|
|
|
$this, |
94
|
|
|
$handle, |
95
|
|
|
$eldnah |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $this->root; |
99
|
|
|
|
100
|
|
|
case '#operation': |
101
|
|
|
$children = $element->getChildren(); |
102
|
|
|
$left = $children[0]->accept($this, $handle, $eldnah); |
103
|
|
|
$right = $children[2]->accept($this, $handle, $eldnah); |
104
|
|
|
$name = $children[1]->accept($this, $handle, false); |
105
|
|
|
|
106
|
|
|
return $this->root->_operator( |
|
|
|
|
107
|
|
|
$name, |
108
|
|
|
[$left, $right], |
109
|
|
|
false |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
case '#variable_access': |
113
|
|
|
$children = $element->getChildren(); |
114
|
|
|
$name = $children[0]->accept($this, $handle, $eldnah); |
115
|
|
|
array_shift($children); |
116
|
|
|
|
117
|
|
|
foreach ($children as $child) { |
118
|
|
|
$_child = $child->accept($this, $handle, $eldnah); |
119
|
|
|
|
120
|
|
|
switch ($child->getId()) { |
121
|
|
|
case '#attribute_access': |
122
|
|
|
$name->attribute($_child); |
123
|
|
|
|
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $name; |
129
|
|
|
|
130
|
|
|
case '#attribute_access': |
131
|
|
|
return $element->getChild(0)->accept($this, $handle, false); |
132
|
|
|
|
133
|
|
|
case '#array_declaration': |
134
|
|
|
$out = []; |
135
|
|
|
|
136
|
|
|
foreach ($element->getChildren() as $child) { |
137
|
|
|
$out[] = $child->accept($this, $handle, $eldnah); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $out; |
|
|
|
|
141
|
|
|
|
142
|
|
|
case '#function_call': |
143
|
|
|
$children = $element->getChildren(); |
144
|
|
|
$name = $children[0]->accept($this, $handle, false); |
145
|
|
|
array_shift($children); |
146
|
|
|
|
147
|
|
|
$arguments = []; |
148
|
|
|
|
149
|
|
|
foreach ($children as $child) { |
150
|
|
|
$arguments[] = $child->accept($this, $handle, $eldnah); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->root->_operator( |
|
|
|
|
154
|
|
|
$name, |
155
|
|
|
$arguments, |
156
|
|
|
true |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
case '#and': |
160
|
|
|
case '#or': |
161
|
|
|
case '#xor': |
162
|
|
|
$name = substr($id, 1); |
163
|
|
|
$children = $element->getChildren(); |
164
|
|
|
$left = $children[0]->accept($this, $handle, $eldnah); |
165
|
|
|
$right = $children[1]->accept($this, $handle, $eldnah); |
166
|
|
|
|
167
|
|
|
return $this->root->operation($name, [$left, $right]); |
|
|
|
|
168
|
|
|
|
169
|
|
|
case '#not': |
170
|
|
|
return $this->root->operation( |
|
|
|
|
171
|
|
|
'not', |
172
|
|
|
[$element->getChild(0)->accept($this, $handle, $eldnah)] |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
case 'token': |
176
|
|
|
$token = $element->getValueToken(); |
177
|
|
|
$value = $element->getValueValue(); |
178
|
|
|
|
179
|
|
|
switch ($token) { |
180
|
|
|
case 'identifier': |
181
|
|
|
return true === $variable ? $this->root->variable($value) : $value; |
|
|
|
|
182
|
|
|
|
183
|
|
|
case 'named_parameter': |
184
|
|
|
|
185
|
|
|
return new Model\Parameter(substr($value, 1)); |
|
|
|
|
186
|
|
|
|
187
|
|
|
case 'positional_parameter': |
188
|
|
|
$index = $this->nextParameterIndex++; |
189
|
|
|
|
190
|
|
|
return new Model\Parameter($index); |
|
|
|
|
191
|
|
|
|
192
|
|
|
case 'true': |
193
|
|
|
return true; |
|
|
|
|
194
|
|
|
|
195
|
|
|
case 'false': |
196
|
|
|
return false; |
|
|
|
|
197
|
|
|
|
198
|
|
|
case 'null': |
199
|
|
|
return null; |
200
|
|
|
|
201
|
|
|
case 'float': |
202
|
|
|
return (float) $value; |
|
|
|
|
203
|
|
|
|
204
|
|
|
case 'integer': |
205
|
|
|
return (int) $value; |
|
|
|
|
206
|
|
|
|
207
|
|
|
case 'string': |
208
|
|
|
return str_replace( |
|
|
|
|
209
|
|
|
'\\'.$value[0], |
210
|
|
|
$value[0], |
211
|
|
|
substr($value, 1, -1) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
default: |
215
|
|
|
throw new Ruler\Exception\Interpreter('Token %s is unknown.', 0, $token); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
default: |
219
|
|
|
throw new Ruler\Exception\Interpreter('Element %s is unknown.', 1, $id); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.