|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Remorhaz\JSON\Path\Query; |
|
5
|
|
|
|
|
6
|
|
|
use function array_map; |
|
7
|
|
|
use function array_reverse; |
|
8
|
|
|
use PhpParser\BuilderFactory; |
|
9
|
|
|
use PhpParser\Node as PhpAstNode; |
|
10
|
|
|
use PhpParser\Node\Arg; |
|
11
|
|
|
use PhpParser\Node\Expr; |
|
12
|
|
|
use PhpParser\Node\Expr\Assign; |
|
13
|
|
|
use PhpParser\Node\Stmt\Expression; |
|
14
|
|
|
use PhpParser\Node\Stmt\Return_; |
|
15
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
16
|
|
|
use Remorhaz\JSON\Data\Value\NodeValueInterface; |
|
17
|
|
|
use Remorhaz\JSON\Path\Runtime\EvaluatorInterface; |
|
18
|
|
|
use Remorhaz\JSON\Path\Value\ValueListInterface; |
|
19
|
|
|
use Remorhaz\JSON\Path\Runtime\RuntimeInterface; |
|
20
|
|
|
use Remorhaz\UniLex\AST\AbstractTranslatorListener; |
|
21
|
|
|
use Remorhaz\UniLex\AST\Node as QueryAstNode; |
|
22
|
|
|
use Remorhaz\UniLex\Exception as UniLexException; |
|
23
|
|
|
use Remorhaz\UniLex\Stack\PushInterface; |
|
24
|
|
|
|
|
25
|
|
|
final class CallbackBuilder extends AbstractTranslatorListener implements CallbackBuilderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
private const ARG_RUNTIME = 'runtime'; |
|
29
|
|
|
|
|
30
|
|
|
private const ARG_EVALUATOR = 'evaluator'; |
|
31
|
|
|
|
|
32
|
|
|
private const ARG_INPUT = 'input'; |
|
33
|
|
|
|
|
34
|
|
|
private $php; |
|
35
|
|
|
|
|
36
|
|
|
private $references = []; |
|
37
|
|
|
|
|
38
|
|
|
private $runtime; |
|
39
|
|
|
|
|
40
|
|
|
private $evaluator; |
|
41
|
|
|
|
|
42
|
|
|
private $input; |
|
43
|
|
|
|
|
44
|
|
|
private $stmts = []; |
|
45
|
|
|
|
|
46
|
|
|
private $queryCallback; |
|
47
|
|
|
|
|
48
|
|
|
private $capabilities; |
|
49
|
|
|
|
|
50
|
9 |
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
9 |
|
$this->php = new BuilderFactory; |
|
53
|
9 |
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
public function getCallback(): callable |
|
56
|
|
|
{ |
|
57
|
4 |
|
if (isset($this->queryCallback)) { |
|
58
|
3 |
|
return $this->queryCallback; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
throw new Exception\QueryCallbackNotFoundException; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
public function getCapabilities(): CapabilitiesInterface |
|
65
|
|
|
{ |
|
66
|
3 |
|
if (isset($this->capabilities)) { |
|
67
|
2 |
|
return $this->capabilities; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
throw new Exception\CapabilitiesNotFoundException; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
public function onStart(QueryAstNode $node): void |
|
74
|
|
|
{ |
|
75
|
5 |
|
$this->runtime = $this->php->var(self::ARG_RUNTIME); |
|
76
|
5 |
|
$this->evaluator = $this->php->var(self::ARG_EVALUATOR); |
|
77
|
5 |
|
$this->input = $this->php->var(self::ARG_INPUT); |
|
78
|
5 |
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function onFinish(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$inputParam = $this |
|
83
|
3 |
|
->php |
|
84
|
3 |
|
->param(self::ARG_INPUT) |
|
85
|
3 |
|
->setType(NodeValueInterface::class) |
|
86
|
3 |
|
->getNode(); |
|
87
|
|
|
$runtimeParam = $this |
|
88
|
3 |
|
->php |
|
89
|
3 |
|
->param(self::ARG_RUNTIME) |
|
90
|
3 |
|
->setType(RuntimeInterface::class) |
|
91
|
3 |
|
->getNode(); |
|
92
|
|
|
$evaluatorParam = $this |
|
93
|
3 |
|
->php |
|
94
|
3 |
|
->param(self::ARG_EVALUATOR) |
|
95
|
3 |
|
->setType(EvaluatorInterface::class) |
|
96
|
3 |
|
->getNode(); |
|
97
|
3 |
|
$stmts = array_map( |
|
98
|
|
|
function (PhpAstNode $stmt): PhpAstNode { |
|
99
|
2 |
|
return $stmt instanceof Expr ? new Expression($stmt): $stmt; |
|
100
|
3 |
|
}, |
|
101
|
3 |
|
$this->stmts |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
3 |
|
$closure = new Expr\Closure( |
|
105
|
|
|
[ |
|
106
|
3 |
|
'stmts' => $stmts, |
|
107
|
|
|
'returnType' => ValueListInterface::class, |
|
108
|
3 |
|
'params' => [$inputParam, $runtimeParam, $evaluatorParam], |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
3 |
|
$return = new Return_($closure); |
|
112
|
|
|
|
|
113
|
3 |
|
$callbackCode = (new Standard)->prettyPrint([$return]); |
|
114
|
3 |
|
$this->queryCallback = eval($callbackCode); |
|
115
|
3 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function onBeginProduction(QueryAstNode $node, PushInterface $stack): void |
|
118
|
|
|
{ |
|
119
|
1 |
|
$stack->push(...array_reverse($node->getChildList())); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param QueryAstNode $node |
|
124
|
|
|
* @throws UniLexException |
|
125
|
|
|
*/ |
|
126
|
5 |
|
public function onFinishProduction(QueryAstNode $node): void |
|
127
|
|
|
{ |
|
128
|
5 |
|
if ($this->hasReference($node)) { |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
5 |
|
switch ($node->getName()) { |
|
132
|
|
|
case AstNodeType::GET_INPUT: |
|
133
|
4 |
|
$this->addRuntimeMethodCall( |
|
134
|
4 |
|
$node, |
|
135
|
4 |
|
'getInput', |
|
136
|
4 |
|
$this->input |
|
137
|
|
|
); |
|
138
|
4 |
|
break; |
|
139
|
|
|
|
|
140
|
|
|
case AstNodeType::SET_OUTPUT: |
|
141
|
5 |
|
$this->capabilities = new Capabilities( |
|
142
|
5 |
|
$node->getAttribute('is_definite'), |
|
143
|
5 |
|
$node->getAttribute('is_path') |
|
144
|
|
|
); |
|
145
|
5 |
|
$this->stmts[] = new Return_($this->getReference($node->getChild(0))); |
|
146
|
4 |
|
break; |
|
147
|
|
|
|
|
148
|
|
|
case AstNodeType::CREATE_FILTER_CONTEXT: |
|
149
|
|
|
$this->addRuntimeMethodCall( |
|
150
|
|
|
$node, |
|
151
|
|
|
'createFilterContext', |
|
152
|
|
|
$this->getReference($node->getChild(0)) |
|
153
|
|
|
); |
|
154
|
|
|
break; |
|
155
|
|
|
|
|
156
|
|
|
case AstNodeType::SPLIT: |
|
157
|
|
|
$this->addRuntimeMethodCall( |
|
158
|
|
|
$node, |
|
159
|
|
|
'split', |
|
160
|
|
|
$this->getReference($node->getChild(0)) |
|
161
|
|
|
); |
|
162
|
|
|
break; |
|
163
|
|
|
|
|
164
|
|
|
case AstNodeType::EVALUATE: |
|
165
|
|
|
$this->addEvaluatorMethodCall( |
|
166
|
|
|
$node, |
|
167
|
|
|
'evaluate', |
|
168
|
|
|
$this->getReference($node->getChild(0)), |
|
169
|
|
|
$this->getReference($node->getChild(1)) |
|
170
|
|
|
); |
|
171
|
|
|
break; |
|
172
|
|
|
|
|
173
|
|
|
case AstNodeType::FILTER: |
|
174
|
|
|
$this->addRuntimeMethodCall( |
|
175
|
|
|
$node, |
|
176
|
|
|
'filter', |
|
177
|
|
|
$this->getReference($node->getChild(0)), |
|
178
|
|
|
$this->getReference($node->getChild(1)) |
|
179
|
|
|
); |
|
180
|
|
|
break; |
|
181
|
|
|
|
|
182
|
|
|
case AstNodeType::EVALUATE_LOGICAL_OR: |
|
183
|
|
|
$this->addEvaluatorMethodCall( |
|
184
|
|
|
$node, |
|
185
|
|
|
'logicalOr', |
|
186
|
|
|
$this->getReference($node->getChild(0)), |
|
187
|
|
|
$this->getReference($node->getChild(1)) |
|
188
|
|
|
); |
|
189
|
|
|
break; |
|
190
|
|
|
|
|
191
|
|
|
case AstNodeType::EVALUATE_LOGICAL_AND: |
|
192
|
|
|
$this->addEvaluatorMethodCall( |
|
193
|
|
|
$node, |
|
194
|
|
|
'logicalAnd', |
|
195
|
|
|
$this->getReference($node->getChild(0)), |
|
196
|
|
|
$this->getReference($node->getChild(1)) |
|
197
|
|
|
); |
|
198
|
|
|
break; |
|
199
|
|
|
|
|
200
|
|
|
case AstNodeType::EVALUATE_LOGICAL_NOT: |
|
201
|
|
|
$this->addEvaluatorMethodCall( |
|
202
|
|
|
$node, |
|
203
|
|
|
'logicalNot', |
|
204
|
|
|
$this->getReference($node->getChild(0)) |
|
205
|
|
|
); |
|
206
|
|
|
break; |
|
207
|
|
|
|
|
208
|
|
|
case AstNodeType::CALCULATE_IS_EQUAL: |
|
209
|
|
|
$this->addEvaluatorMethodCall( |
|
210
|
|
|
$node, |
|
211
|
|
|
'isEqual', |
|
212
|
|
|
$this->getReference($node->getChild(0)), |
|
213
|
|
|
$this->getReference($node->getChild(1)) |
|
214
|
|
|
); |
|
215
|
|
|
break; |
|
216
|
|
|
|
|
217
|
|
|
case AstNodeType::CALCULATE_IS_GREATER: |
|
218
|
|
|
$this->addEvaluatorMethodCall( |
|
219
|
|
|
$node, |
|
220
|
|
|
'isGreater', |
|
221
|
|
|
$this->getReference($node->getChild(0)), |
|
222
|
|
|
$this->getReference($node->getChild(1)) |
|
223
|
|
|
); |
|
224
|
|
|
break; |
|
225
|
|
|
|
|
226
|
|
|
case AstNodeType::CALCULATE_IS_REGEXP: |
|
227
|
|
|
$this->addEvaluatorMethodCall( |
|
228
|
|
|
$node, |
|
229
|
|
|
'isRegExp', |
|
230
|
|
|
$this->php->val($node->getAttribute('pattern')), |
|
231
|
|
|
$this->getReference($node->getChild(0)) |
|
232
|
|
|
); |
|
233
|
|
|
break; |
|
234
|
|
|
|
|
235
|
|
|
case AstNodeType::FETCH_CHILDREN: |
|
236
|
|
|
$this->addRuntimeMethodCall( |
|
237
|
|
|
$node, |
|
238
|
|
|
'fetchChildren', |
|
239
|
|
|
$this->getReference($node->getChild(0)), |
|
240
|
|
|
new Arg( |
|
241
|
|
|
$this->getReference($node->getChild(1)), |
|
242
|
|
|
false, |
|
243
|
|
|
true |
|
244
|
|
|
) |
|
245
|
|
|
); |
|
246
|
|
|
break; |
|
247
|
|
|
|
|
248
|
|
|
case AstNodeType::FETCH_CHILDREN_DEEP: |
|
249
|
|
|
$this->addRuntimeMethodCall( |
|
250
|
|
|
$node, |
|
251
|
|
|
'fetchChildrenDeep', |
|
252
|
|
|
$this->getReference($node->getChild(0)), |
|
253
|
|
|
new Arg( |
|
254
|
|
|
$this->getReference($node->getChild(1)), |
|
255
|
|
|
false, |
|
256
|
|
|
true |
|
257
|
|
|
) |
|
258
|
|
|
); |
|
259
|
|
|
break; |
|
260
|
|
|
|
|
261
|
|
|
case AstNodeType::MATCH_ANY_CHILD: |
|
262
|
|
|
$this->addRuntimeMethodCall( |
|
263
|
|
|
$node, |
|
264
|
|
|
'matchAnyChild', |
|
265
|
|
|
$this->getReference($node->getChild(0)) |
|
266
|
|
|
); |
|
267
|
|
|
break; |
|
268
|
|
|
|
|
269
|
|
|
case AstNodeType::MATCH_PROPERTY_STRICTLY: |
|
270
|
|
|
$this->addRuntimeMethodCall( |
|
271
|
|
|
$node, |
|
272
|
|
|
'matchPropertyStrictly', |
|
273
|
|
|
$this->getReference($node->getChild(0)), |
|
274
|
|
|
); |
|
275
|
|
|
break; |
|
276
|
|
|
|
|
277
|
|
|
case AstNodeType::MATCH_ELEMENT_STRICTLY: |
|
278
|
|
|
$this->addRuntimeMethodCall( |
|
279
|
|
|
$node, |
|
280
|
|
|
'matchElementStrictly', |
|
281
|
|
|
$this->getReference($node->getChild(0)), |
|
282
|
|
|
); |
|
283
|
|
|
break; |
|
284
|
|
|
|
|
285
|
|
|
case AstNodeType::AGGREGATE: |
|
286
|
|
|
$this->addEvaluatorMethodCall( |
|
287
|
|
|
$node, |
|
288
|
|
|
'aggregate', |
|
289
|
|
|
$this->php->val($node->getAttribute('name')), |
|
290
|
|
|
$this->getReference($node->getChild(0)), |
|
291
|
|
|
); |
|
292
|
|
|
break; |
|
293
|
|
|
|
|
294
|
|
|
case AstNodeType::POPULATE_LITERAL: |
|
295
|
|
|
$this->addRuntimeMethodCall( |
|
296
|
|
|
$node, |
|
297
|
|
|
'populateLiteral', |
|
298
|
|
|
$this->getReference($node->getChild(0)), |
|
299
|
|
|
$this->getReference($node->getChild(1)) |
|
300
|
|
|
); |
|
301
|
|
|
break; |
|
302
|
|
|
|
|
303
|
|
|
case AstNodeType::POPULATE_ARRAY_ELEMENTS: |
|
304
|
|
|
$this->addRuntimeMethodCall( |
|
305
|
|
|
$node, |
|
306
|
|
|
'populateArrayElements', |
|
307
|
|
|
$this->getReference($node->getChild(0)), |
|
308
|
|
|
new Arg( |
|
309
|
|
|
$this->getReference($node->getChild(1)), |
|
310
|
|
|
false, |
|
311
|
|
|
true |
|
312
|
|
|
) |
|
313
|
|
|
); |
|
314
|
|
|
break; |
|
315
|
|
|
|
|
316
|
|
|
case AstNodeType::POPULATE_INDEX_LIST: |
|
317
|
|
|
$this->addRuntimeMethodCall( |
|
318
|
|
|
$node, |
|
319
|
|
|
'populateIndexList', |
|
320
|
|
|
$this->getReference($node->getChild(0)), |
|
321
|
|
|
...array_map( |
|
322
|
|
|
[$this->php, 'val'], |
|
323
|
|
|
$node->getAttribute('indexList') |
|
324
|
|
|
) |
|
325
|
|
|
); |
|
326
|
|
|
break; |
|
327
|
|
|
|
|
328
|
|
|
case AstNodeType::POPULATE_INDEX_SLICE: |
|
329
|
|
|
$attributes = $node->getAttributeList(); |
|
330
|
|
|
$this->addRuntimeMethodCall( |
|
331
|
|
|
$node, |
|
332
|
|
|
'populateIndexSlice', |
|
333
|
|
|
$this->getReference($node->getChild(0)), |
|
334
|
|
|
$this->php->val($attributes['start'] ?? null), |
|
335
|
|
|
$this->php->val($attributes['end'] ?? null), |
|
336
|
|
|
$this->php->val($attributes['step'] ?? null) |
|
337
|
|
|
); |
|
338
|
|
|
|
|
339
|
|
|
break; |
|
340
|
|
|
|
|
341
|
|
|
case AstNodeType::POPULATE_NAME_LIST: |
|
342
|
|
|
$this->addRuntimeMethodCall( |
|
343
|
|
|
$node, |
|
344
|
|
|
'populateNameList', |
|
345
|
|
|
$this->getReference($node->getChild(0)), |
|
346
|
|
|
...array_map( |
|
347
|
|
|
[$this->php, 'val'], |
|
348
|
|
|
$node->getAttribute('nameList') |
|
349
|
|
|
) |
|
350
|
|
|
); |
|
351
|
|
|
break; |
|
352
|
|
|
|
|
353
|
|
|
case AstNodeType::CREATE_SCALAR: |
|
354
|
|
|
$attributes = $node->getAttributeList(); |
|
355
|
|
|
// TODO: allow accessing null attributes |
|
356
|
|
|
$value = $this->php->val($attributes['value'] ?? null); |
|
357
|
|
|
$this->addRuntimeMethodCall( |
|
358
|
|
|
$node, |
|
359
|
|
|
'createScalar', |
|
360
|
|
|
$value |
|
361
|
|
|
); |
|
362
|
|
|
break; |
|
363
|
|
|
|
|
364
|
|
|
case AstNodeType::CREATE_ARRAY: |
|
365
|
|
|
// [ X:APPEND_TO_ARRAY ] |
|
366
|
|
|
$items = []; |
|
367
|
|
|
foreach ($node->getChildList() as $child) { |
|
368
|
|
|
$items[] = $this->getReference($child); |
|
369
|
|
|
} |
|
370
|
|
|
$this->stmts[] = new Assign( |
|
371
|
|
|
$this->createReference($node), |
|
372
|
|
|
$this->php->val($items) |
|
373
|
|
|
); |
|
374
|
|
|
break; |
|
375
|
|
|
|
|
376
|
|
|
case AstNodeType::APPEND_TO_ARRAY: |
|
377
|
|
|
// [ 0:<value> ] |
|
378
|
|
|
$this->setReference( |
|
379
|
|
|
$node, |
|
380
|
|
|
$this->getReference($node->getChild(0)) |
|
381
|
|
|
); |
|
382
|
|
|
break; |
|
383
|
|
|
|
|
384
|
|
|
case AstNodeType::CREATE_LITERAL_ARRAY: |
|
385
|
|
|
$this->addRuntimeMethodCall( |
|
386
|
|
|
$node, |
|
387
|
|
|
'createArray', |
|
388
|
|
|
$this->getReference($node->getChild(0)), |
|
389
|
|
|
new Arg( |
|
390
|
|
|
$this->getReference($node->getChild(1)), |
|
391
|
|
|
false, |
|
392
|
|
|
true |
|
393
|
|
|
) |
|
394
|
|
|
); |
|
395
|
|
|
break; |
|
396
|
|
|
} |
|
397
|
4 |
|
} |
|
398
|
|
|
|
|
399
|
4 |
|
private function getVarName(QueryAstNode $node): string |
|
400
|
|
|
{ |
|
401
|
4 |
|
return "var{$node->getId()}"; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
4 |
|
private function createReference(QueryAstNode $node): Expr |
|
405
|
|
|
{ |
|
406
|
4 |
|
$reference = $this->php->var($this->getVarName($node)); |
|
407
|
4 |
|
$this->setReference($node, $reference); |
|
408
|
|
|
|
|
409
|
4 |
|
return $reference; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
4 |
|
private function setReference(QueryAstNode $node, Expr $expr): void |
|
413
|
|
|
{ |
|
414
|
4 |
|
if (isset($this->references[$node->getId()])) { |
|
415
|
|
|
throw new Exception\ReferenceAlreadyExistsException($node->getId()); |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
4 |
|
$this->references[$node->getId()] = $expr; |
|
419
|
4 |
|
} |
|
420
|
|
|
|
|
421
|
5 |
|
private function hasReference(QueryAstNode $node): bool |
|
422
|
|
|
{ |
|
423
|
5 |
|
return isset($this->references[$node->getId()]); |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
5 |
|
private function getReference(QueryAstNode $node): Expr |
|
427
|
|
|
{ |
|
428
|
5 |
|
if (!isset($this->references[$node->getId()])) { |
|
429
|
1 |
|
throw new Exception\ReferenceNotFoundException($node->getId()); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
4 |
|
return $this->references[$node->getId()]; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
4 |
|
private function addRuntimeMethodCall(QueryAstNode $node, string $method, PhpAstNode ...$args): void |
|
436
|
|
|
{ |
|
437
|
|
|
$methodCall = $this |
|
438
|
4 |
|
->php |
|
439
|
4 |
|
->methodCall($this->runtime, $method, $args); |
|
440
|
4 |
|
$this->stmts[] = new Assign($this->createReference($node), $methodCall); |
|
441
|
4 |
|
} |
|
442
|
|
|
|
|
443
|
|
|
private function addEvaluatorMethodCall(QueryAstNode $node, string $method, PhpAstNode ...$args): void |
|
444
|
|
|
{ |
|
445
|
|
|
$methodCall = $this |
|
446
|
|
|
->php |
|
447
|
|
|
->methodCall($this->evaluator, $method, $args); |
|
448
|
|
|
$this->stmts[] = new Assign($this->createReference($node), $methodCall); |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|