1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Remorhaz\JSON\Path\Query; |
6
|
|
|
|
7
|
|
|
use Remorhaz\UniLex\AST\Tree; |
8
|
|
|
use Remorhaz\UniLex\Exception as UniLexException; |
9
|
|
|
|
10
|
|
|
final class AstBuilder implements AstBuilderInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $inputId; |
14
|
|
|
|
15
|
|
|
private $tree; |
16
|
|
|
|
17
|
|
|
public function __construct(Tree $tree) |
18
|
|
|
{ |
19
|
|
|
$this->tree = $tree; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return int |
24
|
|
|
*/ |
25
|
|
|
public function getInput(): int |
26
|
|
|
{ |
27
|
|
|
if (!isset($this->inputId)) { |
28
|
|
|
$this->inputId = $this |
29
|
|
|
->tree |
30
|
|
|
->createNode(AstNodeType::GET_INPUT) |
31
|
|
|
->getId(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->inputId; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $id |
39
|
|
|
* @param bool $isDefinite |
40
|
|
|
* @param bool $isAddressable |
41
|
|
|
* @throws UniLexException |
42
|
|
|
*/ |
43
|
|
|
public function setOutput(int $id, bool $isDefinite, bool $isAddressable): void |
44
|
|
|
{ |
45
|
|
|
$setOutputNode = $this |
46
|
|
|
->tree |
47
|
|
|
->createNode(AstNodeType::SET_OUTPUT) |
48
|
|
|
->addChild($this->tree->getNode($id)) |
49
|
|
|
->setAttribute('is_definite', $isDefinite) |
50
|
|
|
->setAttribute('is_addressable', $isAddressable); |
51
|
|
|
$this |
52
|
|
|
->tree |
53
|
|
|
->setRootNode($setOutputNode); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $id |
58
|
|
|
* @return int |
59
|
|
|
* @throws UniLexException |
60
|
|
|
*/ |
61
|
|
|
public function fetchFilterContext(int $id): int |
62
|
|
|
{ |
63
|
|
|
return $this |
64
|
|
|
->tree |
65
|
|
|
->createNode(AstNodeType::FETCH_FILTER_CONTEXT) |
66
|
|
|
->addChild($this->tree->getNode($id)) |
67
|
|
|
->getId(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $id |
72
|
|
|
* @return int |
73
|
|
|
* @throws UniLexException |
74
|
|
|
*/ |
75
|
|
|
public function splitFilterContext(int $id): int |
76
|
|
|
{ |
77
|
|
|
return $this |
78
|
|
|
->tree |
79
|
|
|
->createNode(AstNodeType::SPLIT_FILTER_CONTEXT) |
80
|
|
|
->addChild($this->tree->getNode($id)) |
81
|
|
|
->getId(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $evaluatedId |
86
|
|
|
* @param int $contextId |
87
|
|
|
* @return int |
88
|
|
|
* @throws UniLexException |
89
|
|
|
*/ |
90
|
|
|
public function joinFilterResults(int $evaluatedId, int $contextId): int |
91
|
|
|
{ |
92
|
|
|
return $this |
93
|
|
|
->tree |
94
|
|
|
->createNode(AstNodeType::JOIN_FILTER_RESULTS) |
95
|
|
|
->addChild($this->tree->getNode($evaluatedId)) |
96
|
|
|
->addChild($this->tree->getNode($contextId)) |
97
|
|
|
->getId(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $sourceId |
102
|
|
|
* @param int $id |
103
|
|
|
* @return int |
104
|
|
|
* @throws UniLexException |
105
|
|
|
*/ |
106
|
|
|
public function evaluate(int $sourceId, int $id): int |
107
|
|
|
{ |
108
|
|
|
return $this |
109
|
|
|
->tree |
110
|
|
|
->createNode(AstNodeType::EVALUATE) |
111
|
|
|
->addChild($this->tree->getNode($sourceId)) |
112
|
|
|
->addChild($this->tree->getNode($id)) |
113
|
|
|
->getId(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $contextId |
118
|
|
|
* @param int $evaluatedId |
119
|
|
|
* @return int |
120
|
|
|
* @throws UniLexException |
121
|
|
|
*/ |
122
|
|
|
public function filter(int $contextId, int $evaluatedId): int |
123
|
|
|
{ |
124
|
|
|
return $this |
125
|
|
|
->tree |
126
|
|
|
->createNode(AstNodeType::FILTER) |
127
|
|
|
->addChild($this->tree->getNode($contextId)) |
128
|
|
|
->addChild($this->tree->getNode($evaluatedId)) |
129
|
|
|
->getId(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int $leftEvaluatedId |
134
|
|
|
* @param int $rightEvaluatedId |
135
|
|
|
* @return int |
136
|
|
|
* @throws UniLexException |
137
|
|
|
*/ |
138
|
|
|
public function evaluateLogicalOr(int $leftEvaluatedId, int $rightEvaluatedId): int |
139
|
|
|
{ |
140
|
|
|
return $this |
141
|
|
|
->tree |
142
|
|
|
->createNode(AstNodeType::EVALUATE_LOGICAL_OR) |
143
|
|
|
->addChild($this->tree->getNode($leftEvaluatedId)) |
144
|
|
|
->addChild($this->tree->getNode($rightEvaluatedId)) |
145
|
|
|
->getId(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param int $leftEvaluatedId |
150
|
|
|
* @param int $rightEvaluatedId |
151
|
|
|
* @return int |
152
|
|
|
* @throws UniLexException |
153
|
|
|
*/ |
154
|
|
|
public function evaluateLogicalAnd(int $leftEvaluatedId, int $rightEvaluatedId): int |
155
|
|
|
{ |
156
|
|
|
return $this |
157
|
|
|
->tree |
158
|
|
|
->createNode(AstNodeType::EVALUATE_LOGICAL_AND) |
159
|
|
|
->addChild($this->tree->getNode($leftEvaluatedId)) |
160
|
|
|
->addChild($this->tree->getNode($rightEvaluatedId)) |
161
|
|
|
->getId(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param int $evaluatedId |
166
|
|
|
* @return int |
167
|
|
|
* @throws UniLexException |
168
|
|
|
*/ |
169
|
|
|
public function evaluateLogicalNot(int $evaluatedId): int |
170
|
|
|
{ |
171
|
|
|
return $this |
172
|
|
|
->tree |
173
|
|
|
->createNode(AstNodeType::EVALUATE_LOGICAL_NOT) |
174
|
|
|
->addChild($this->tree->getNode($evaluatedId)) |
175
|
|
|
->getId(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int $leftId |
180
|
|
|
* @param int $rightId |
181
|
|
|
* @return int |
182
|
|
|
* @throws UniLexException |
183
|
|
|
*/ |
184
|
|
|
public function calculateIsEqual(int $leftId, int $rightId): int |
185
|
|
|
{ |
186
|
|
|
return $this |
187
|
|
|
->tree |
188
|
|
|
->createNode(AstNodeType::CALCULATE_IS_EQUAL) |
189
|
|
|
->addChild($this->tree->getNode($leftId)) |
190
|
|
|
->addChild($this->tree->getNode($rightId)) |
191
|
|
|
->getId(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param int $leftId |
196
|
|
|
* @param int $rightId |
197
|
|
|
* @return int |
198
|
|
|
* @throws UniLexException |
199
|
|
|
*/ |
200
|
|
|
public function calculateIsGreater(int $leftId, int $rightId): int |
201
|
|
|
{ |
202
|
|
|
return $this |
203
|
|
|
->tree |
204
|
|
|
->createNode(AstNodeType::CALCULATE_IS_GREATER) |
205
|
|
|
->addChild($this->tree->getNode($leftId)) |
206
|
|
|
->addChild($this->tree->getNode($rightId)) |
207
|
|
|
->getId(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $pattern |
212
|
|
|
* @param int $id |
213
|
|
|
* @return int |
214
|
|
|
* @throws UniLexException |
215
|
|
|
*/ |
216
|
|
|
public function calculateIsRegExp(string $pattern, int $id): int |
217
|
|
|
{ |
218
|
|
|
return $this |
219
|
|
|
->tree |
220
|
|
|
->createNode(AstNodeType::CALCULATE_IS_REGEXP) |
221
|
|
|
->addChild($this->tree->getNode($id)) |
222
|
|
|
->setAttribute('pattern', $pattern) |
223
|
|
|
->getId(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param int $id |
228
|
|
|
* @param int $matcherId |
229
|
|
|
* @return int |
230
|
|
|
* @throws UniLexException |
231
|
|
|
*/ |
232
|
|
|
public function fetchChildren(int $id, int $matcherId): int |
233
|
|
|
{ |
234
|
|
|
return $this |
235
|
|
|
->tree |
236
|
|
|
->createNode(AstNodeType::FETCH_CHILDREN) |
237
|
|
|
->addChild($this->tree->getNode($id)) |
238
|
|
|
->addChild($this->tree->getNode($matcherId)) |
239
|
|
|
->getId(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param int $id |
244
|
|
|
* @param int $matcherId |
245
|
|
|
* @return int |
246
|
|
|
* @throws UniLexException |
247
|
|
|
*/ |
248
|
|
|
public function fetchChildrenDeep(int $id, int $matcherId): int |
249
|
|
|
{ |
250
|
|
|
return $this |
251
|
|
|
->tree |
252
|
|
|
->createNode(AstNodeType::FETCH_CHILDREN_DEEP) |
253
|
|
|
->addChild($this->tree->getNode($id)) |
254
|
|
|
->addChild($this->tree->getNode($matcherId)) |
255
|
|
|
->getId(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param int ...$idList |
260
|
|
|
* @return int |
261
|
|
|
* @throws UniLexException |
262
|
|
|
*/ |
263
|
|
|
public function merge(int ...$idList): int |
264
|
|
|
{ |
265
|
|
|
$node = $this |
266
|
|
|
->tree |
267
|
|
|
->createNode(AstNodeType::MERGE); |
268
|
|
|
foreach ($idList as $id) { |
269
|
|
|
$node->addChild($this->tree->getNode($id)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $node->getId(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return int |
277
|
|
|
*/ |
278
|
|
|
public function matchAnyChild(): int |
279
|
|
|
{ |
280
|
|
|
return $this |
281
|
|
|
->tree |
282
|
|
|
->createNode(AstNodeType::MATCH_ANY_CHILD) |
283
|
|
|
->getId(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string ...$names |
288
|
|
|
* @return int |
289
|
|
|
* @throws UniLexException |
290
|
|
|
*/ |
291
|
|
|
public function matchPropertyStrictly(string ...$names): int |
292
|
|
|
{ |
293
|
|
|
return $this |
294
|
|
|
->tree |
295
|
|
|
->createNode(AstNodeType::MATCH_PROPERTY_STRICTLY) |
296
|
|
|
->setAttribute('names', $names) |
297
|
|
|
->getId(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param int ...$indexes |
302
|
|
|
* @return int |
303
|
|
|
* @throws UniLexException |
304
|
|
|
*/ |
305
|
|
|
public function matchElementStrictly(int ...$indexes): int |
306
|
|
|
{ |
307
|
|
|
return $this |
308
|
|
|
->tree |
309
|
|
|
->createNode(AstNodeType::MATCH_ELEMENT_STRICTLY) |
310
|
|
|
->setAttribute('indexes', $indexes) |
311
|
|
|
->getId(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param int|null $start |
316
|
|
|
* @param int|null $end |
317
|
|
|
* @param int|null $step |
318
|
|
|
* @return int |
319
|
|
|
* @throws UniLexException |
320
|
|
|
*/ |
321
|
|
|
public function matchElementSlice(?int $start, ?int $end, ?int $step): int |
322
|
|
|
{ |
323
|
|
|
return $this |
324
|
|
|
->tree |
325
|
|
|
->createNode(AstNodeType::MATCH_ELEMENT_SLICE) |
326
|
|
|
->setAttribute('hasStart', isset($start)) |
327
|
|
|
->setAttribute('start', $start) |
328
|
|
|
->setAttribute('hasEnd', isset($end)) |
329
|
|
|
->setAttribute('end', $end) |
330
|
|
|
->setAttribute('step', $step ?? 1) |
331
|
|
|
->getId(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param string $name |
336
|
|
|
* @param int $id |
337
|
|
|
* @return int |
338
|
|
|
* @throws UniLexException |
339
|
|
|
*/ |
340
|
|
|
public function aggregate(string $name, int $id): int |
341
|
|
|
{ |
342
|
|
|
return $this |
343
|
|
|
->tree |
344
|
|
|
->createNode(AstNodeType::AGGREGATE) |
345
|
|
|
->addChild($this->tree->getNode($id)) |
346
|
|
|
->setAttribute('name', $name) |
347
|
|
|
->getId(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param int $sourceId |
352
|
|
|
* @param mixed $value |
353
|
|
|
* @return int |
354
|
|
|
* @throws UniLexException |
355
|
|
|
*/ |
356
|
|
|
public function createScalar(int $sourceId, $value): int |
357
|
|
|
{ |
358
|
|
|
return $this |
359
|
|
|
->tree |
360
|
|
|
->createNode(AstNodeType::CREATE_LITERAL_SCALAR) |
361
|
|
|
->setAttribute('value', $value) |
362
|
|
|
->addChild($this->tree->getNode($sourceId)) |
363
|
|
|
->getId(); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param int $sourceId |
368
|
|
|
* @param int $arrayId |
369
|
|
|
* @return int |
370
|
|
|
* @throws UniLexException |
371
|
|
|
*/ |
372
|
|
|
public function createLiteralArray(int $sourceId, int $arrayId): int |
373
|
|
|
{ |
374
|
|
|
return $this |
375
|
|
|
->tree |
376
|
|
|
->createNode(AstNodeType::CREATE_LITERAL_ARRAY) |
377
|
|
|
->addChild($this->tree->getNode($sourceId)) |
378
|
|
|
->addChild($this->tree->getNode($arrayId)) |
379
|
|
|
->getId(); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function createArray(): int |
383
|
|
|
{ |
384
|
|
|
return $this |
385
|
|
|
->tree |
386
|
|
|
->createNode(AstNodeType::CREATE_ARRAY) |
387
|
|
|
->getId(); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param int $arrayId |
392
|
|
|
* @param int $valueId |
393
|
|
|
* @return int |
394
|
|
|
* @throws UniLexException |
395
|
|
|
*/ |
396
|
|
|
public function appendToArray(int $arrayId, int $valueId): int |
397
|
|
|
{ |
398
|
|
|
$appendNode = $this |
399
|
|
|
->tree |
400
|
|
|
->createNode(AstNodeType::APPEND_TO_ARRAY) |
401
|
|
|
->addChild($this->tree->getNode($valueId)); |
402
|
|
|
|
403
|
|
|
return $this |
404
|
|
|
->tree |
405
|
|
|
->getNode($arrayId) |
406
|
|
|
->addChild($appendNode) |
407
|
|
|
->getId(); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|