1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Hoa |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* |
11
|
|
|
* BSD 3-Clause License |
12
|
|
|
* |
13
|
|
|
* Copyright © 2007-2017, Hoa community. All rights reserved. |
14
|
|
|
* |
15
|
|
|
* Redistribution and use in source and binary forms, with or without |
16
|
|
|
* modification, are permitted provided that the following conditions are met: |
17
|
|
|
* |
18
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
19
|
|
|
* list of conditions and the following disclaimer. |
20
|
|
|
* |
21
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
22
|
|
|
* this list of conditions and the following disclaimer in the documentation |
23
|
|
|
* and/or other materials provided with the distribution. |
24
|
|
|
* |
25
|
|
|
* 3. Neither the name of the copyright holder nor the names of its |
26
|
|
|
* contributors may be used to endorse or promote products derived from |
27
|
|
|
* this software without specific prior written permission. |
28
|
|
|
* |
29
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
30
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
31
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
32
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
33
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
34
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
35
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
36
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
37
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
namespace JMS\Serializer\Type\Compiler\Llk; |
42
|
|
|
|
43
|
|
|
use Hoa\Visitor; |
44
|
|
|
use Hoa\Visitor\Visit; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class \JMS\Serializer\Type\Compiler\Llk\TreeNode. |
48
|
|
|
* |
49
|
|
|
* Provide a generic node for the AST produced by LL(k) parser. |
50
|
|
|
*/ |
51
|
|
|
final class TreeNode implements Visitor\Element |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* ID (should be something like #ruleName or token). |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $_id = ''; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Value of the node (non-null for token nodes). |
62
|
|
|
* |
63
|
|
|
* @var array|null |
64
|
|
|
*/ |
65
|
|
|
protected $_value = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Children. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $_children = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Parent. |
76
|
|
|
* |
77
|
|
|
* @var \JMS\Serializer\Type\Compiler\Llk\TreeNode|null |
78
|
|
|
*/ |
79
|
|
|
protected $_parent = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Attached data. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $_data = []; |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id ID. |
92
|
|
|
* @param array $value Value. |
93
|
|
|
* @param array $children Children. |
94
|
|
|
* @param \JMS\Serializer\Type\Compiler\Llk\TreeNode $parent Parent. |
95
|
|
|
*/ |
96
|
|
|
public function __construct( |
97
|
|
|
string $id, |
98
|
|
|
?array $value = null, |
99
|
|
|
array $children = [], |
100
|
|
|
?self $parent = null |
101
|
|
|
) { |
102
|
|
|
$this->setId($id); |
103
|
|
|
|
104
|
|
|
if (!empty($value)) { |
105
|
|
|
$this->setValue($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->setChildren($children); |
109
|
|
|
|
110
|
|
|
if (null !== $parent) { |
111
|
|
|
$this->setParent($parent); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set ID. |
119
|
|
|
* |
120
|
|
|
* @param string $id ID. |
121
|
|
|
*/ |
122
|
|
|
public function setId(string $id): string |
123
|
|
|
{ |
124
|
|
|
$old = $this->_id; |
125
|
|
|
$this->_id = $id; |
126
|
|
|
|
127
|
|
|
return $old; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get ID. |
132
|
|
|
*/ |
133
|
|
|
public function getId(): string |
134
|
|
|
{ |
135
|
|
|
return $this->_id; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set value. |
140
|
|
|
* |
141
|
|
|
* @param array $value Value (token & value). |
142
|
|
|
* |
143
|
|
|
* @return array|null |
144
|
|
|
*/ |
145
|
|
|
public function setValue(array $value): ?array |
146
|
|
|
{ |
147
|
|
|
$old = $this->_value; |
148
|
|
|
$this->_value = $value; |
149
|
|
|
|
150
|
|
|
return $old; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get value. |
155
|
|
|
* |
156
|
|
|
* @return array|null |
157
|
|
|
*/ |
158
|
|
|
public function getValue(): ?array |
159
|
|
|
{ |
160
|
|
|
return $this->_value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get value token. |
165
|
|
|
*/ |
166
|
|
|
public function getValueToken(): string |
167
|
|
|
{ |
168
|
|
|
return $this->_value['token'] ?? null; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get value value. |
173
|
|
|
*/ |
174
|
|
|
public function getValueValue(): string |
175
|
|
|
{ |
176
|
|
|
return $this->_value['value'] ?? null; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get token offset. |
181
|
|
|
*/ |
182
|
|
|
public function getOffset(): int |
183
|
|
|
{ |
184
|
|
|
return $this->_value['offset'] ?? 0; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Check if the node represents a token or not. |
189
|
|
|
*/ |
190
|
|
|
public function isToken(): bool |
191
|
|
|
{ |
192
|
|
|
return !empty($this->_value); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Prepend a child. |
197
|
|
|
* |
198
|
|
|
* @param \JMS\Serializer\Type\Compiler\Llk\TreeNode $child Child. |
199
|
|
|
*/ |
200
|
|
|
public function prependChild(self $child): \JMS\Serializer\Type\Compiler\Llk\TreeNode |
201
|
|
|
{ |
202
|
|
|
array_unshift($this->_children, $child); |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Append a child. |
209
|
|
|
* |
210
|
|
|
* @param \JMS\Serializer\Type\Compiler\Llk\TreeNode $child Child. |
211
|
|
|
*/ |
212
|
|
|
public function appendChild(self $child): \JMS\Serializer\Type\Compiler\Llk\TreeNode |
213
|
|
|
{ |
214
|
|
|
$this->_children[] = $child; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set children. |
221
|
|
|
* |
222
|
|
|
* @param array $children Children. |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function setChildren(array $children): array |
227
|
|
|
{ |
228
|
|
|
$old = $this->_children; |
229
|
|
|
$this->_children = $children; |
230
|
|
|
|
231
|
|
|
return $old; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get child. |
236
|
|
|
* |
237
|
|
|
* @param int $i Index. |
238
|
|
|
*/ |
239
|
|
|
public function getChild(int $i): \JMS\Serializer\Type\Compiler\Llk\TreeNode |
240
|
|
|
{ |
241
|
|
|
return true === $this->childExists($i) |
|
|
|
|
242
|
|
|
? $this->_children[$i] |
243
|
|
|
: null; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get children. |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getChildren(): array |
252
|
|
|
{ |
253
|
|
|
return $this->_children; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get number of children. |
258
|
|
|
*/ |
259
|
|
|
public function getChildrenNumber(): int |
260
|
|
|
{ |
261
|
|
|
return count($this->_children); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Check if a child exists. |
266
|
|
|
* |
267
|
|
|
* @param int $i Index. |
268
|
|
|
*/ |
269
|
|
|
public function childExists(int $i): bool |
270
|
|
|
{ |
271
|
|
|
return array_key_exists($i, $this->_children); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Set parent. |
276
|
|
|
* |
277
|
|
|
* @param \JMS\Serializer\Type\Compiler\Llk\TreeNode $parent Parent. |
278
|
|
|
*/ |
279
|
|
|
public function setParent(self $parent): ?\JMS\Serializer\Type\Compiler\Llk\TreeNode |
280
|
|
|
{ |
281
|
|
|
$old = $this->_parent; |
282
|
|
|
$this->_parent = $parent; |
283
|
|
|
|
284
|
|
|
return $old; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get parent. |
289
|
|
|
*/ |
290
|
|
|
public function getParent(): ?\JMS\Serializer\Type\Compiler\Llk\TreeNode |
291
|
|
|
{ |
292
|
|
|
return $this->_parent; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get data. |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
|
|
public function &getData(): array |
301
|
|
|
{ |
302
|
|
|
return $this->_data; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Accept a visitor. |
307
|
|
|
* |
308
|
|
|
* @param Visit $visitor Visitor. |
309
|
|
|
* @param mixed &$handle Handle (reference). |
310
|
|
|
* @param mixed $eldnah Handle (no reference). |
311
|
|
|
* |
312
|
|
|
* @return mixed |
313
|
|
|
*/ |
314
|
|
|
public function accept( |
315
|
|
|
Visitor\Visit $visitor, |
316
|
|
|
&$handle = null, |
317
|
|
|
$eldnah = null |
318
|
|
|
) { |
319
|
|
|
return $visitor->visit($this, $handle, $eldnah); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Remove circular reference to the parent (help the garbage collector). |
324
|
|
|
* |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
public function __destruct() |
328
|
|
|
{ |
329
|
|
|
unset($this->_parent); |
330
|
|
|
|
331
|
|
|
return; |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|