-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
Visitor.php
507 lines (450 loc) · 17 KB
/
Visitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php declare(strict_types=1);
namespace GraphQL\Language;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
use GraphQL\Utils\TypeInfo;
use GraphQL\Utils\Utils;
/**
* Utility for efficient AST traversal and modification.
*
* `visit()` will walk through an AST using a depth first traversal, calling
* the visitor's enter function at each node in the traversal, and calling the
* leave function after visiting that node and all of its child nodes.
*
* By returning different values from the `enter` and `leave` functions, the
* behavior of the visitor can be altered.
* - no return (`void`) or return `null`: no action
* - `Visitor::skipNode()`: skips over the subtree at the current node of the AST
* - `Visitor::stop()`: stop the Visitor completely
* - `Visitor::removeNode()`: remove the current node
* - return any other value: replace this node with the returned value
*
* When using `visit()` to edit an AST, the original AST will not be modified, and
* a new version of the AST with the changes applied will be returned from the
* visit function.
*
* $editedAST = Visitor::visit($ast, [
* 'enter' => function ($node, $key, $parent, $path, $ancestors) {
* // ...
* },
* 'leave' => function ($node, $key, $parent, $path, $ancestors) {
* // ...
* }
* ]);
*
* Alternatively to providing `enter` and `leave` functions, a visitor can
* instead provide functions named the same as the [kinds of AST nodes](class-reference.md#graphqllanguageastnodekind),
* or enter/leave visitors at a named key, leading to four permutations of
* visitor API:
*
* 1. Named visitors triggered when entering a node a specific kind.
*
* Visitor::visit($ast, [
* 'Kind' => function ($node) {
* // enter the "Kind" node
* }
* ]);
*
* 2. Named visitors that trigger upon entering and leaving a node of
* a specific kind.
*
* Visitor::visit($ast, [
* 'Kind' => [
* 'enter' => function ($node) {
* // enter the "Kind" node
* }
* 'leave' => function ($node) {
* // leave the "Kind" node
* }
* ]
* ]);
*
* 3. Generic visitors that trigger upon entering and leaving any node.
*
* Visitor::visit($ast, [
* 'enter' => function ($node) {
* // enter any node
* },
* 'leave' => function ($node) {
* // leave any node
* }
* ]);
*
* 4. Parallel visitors for entering and leaving nodes of a specific kind.
*
* Visitor::visit($ast, [
* 'enter' => [
* 'Kind' => function($node) {
* // enter the "Kind" node
* }
* },
* 'leave' => [
* 'Kind' => function ($node) {
* // leave the "Kind" node
* }
* ]
* ]);
*
* @phpstan-type NodeVisitor callable(Node): (VisitorOperation|null|false|void)
* @phpstan-type VisitorArray array<string, NodeVisitor>|array<string, array<string, NodeVisitor>>
*
* @see \GraphQL\Tests\Language\VisitorTest
*/
class Visitor
{
public const VISITOR_KEYS = [
NodeKind::NAME => [],
NodeKind::DOCUMENT => ['definitions'],
NodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'],
NodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue', 'directives'],
NodeKind::VARIABLE => ['name'],
NodeKind::SELECTION_SET => ['selections'],
NodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
NodeKind::ARGUMENT => ['name', 'value'],
NodeKind::FRAGMENT_SPREAD => ['name', 'directives'],
NodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'],
NodeKind::FRAGMENT_DEFINITION => [
'name',
// Note: fragment variable definitions are experimental and may be changed
// or removed in the future.
'variableDefinitions',
'typeCondition',
'directives',
'selectionSet',
],
NodeKind::INT => [],
NodeKind::FLOAT => [],
NodeKind::STRING => [],
NodeKind::BOOLEAN => [],
NodeKind::NULL => [],
NodeKind::ENUM => [],
NodeKind::LST => ['values'],
NodeKind::OBJECT => ['fields'],
NodeKind::OBJECT_FIELD => ['name', 'value'],
NodeKind::DIRECTIVE => ['name', 'arguments'],
NodeKind::NAMED_TYPE => ['name'],
NodeKind::LIST_TYPE => ['type'],
NodeKind::NON_NULL_TYPE => ['type'],
NodeKind::SCHEMA_DEFINITION => ['directives', 'operationTypes'],
NodeKind::OPERATION_TYPE_DEFINITION => ['type'],
NodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'],
NodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'],
NodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'],
NodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'],
NodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'],
NodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'],
NodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'],
NodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'],
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'],
NodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'],
NodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'],
NodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'],
NodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'],
NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'],
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'],
NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'],
NodeKind::SCHEMA_EXTENSION => ['directives', 'operationTypes'],
];
/**
* Visit the AST (see class description for details).
*
* @param NodeList<Node>|Node $root
* @param VisitorArray $visitor
* @param array<string, mixed>|null $keyMap
*
* @throws \Exception
*
* @return mixed
*
* @api
*/
public static function visit(object $root, array $visitor, array $keyMap = null)
{
$visitorKeys = $keyMap ?? self::VISITOR_KEYS;
/**
* @var list<array{
* inList: bool,
* index: int,
* keys: Node|NodeList|mixed,
* edits: array<int, array{mixed, mixed}>,
* }> $stack */
$stack = [];
$inList = $root instanceof NodeList;
$keys = [$root];
$index = -1;
$edits = [];
$parent = null;
$path = [];
$ancestors = [];
do {
++$index;
$isLeaving = $index === \count($keys);
$key = null;
$node = null;
$isEdited = $isLeaving && $edits !== [];
if ($isLeaving) {
$key = $ancestors === []
? null
: $path[\count($path) - 1];
$node = $parent;
$parent = \array_pop($ancestors);
if ($isEdited) {
if ($node instanceof Node || $node instanceof NodeList) {
$node = $node->cloneDeep();
}
$editOffset = 0;
foreach ($edits as [$editKey, $editValue]) {
if ($inList) {
$editKey -= $editOffset;
}
if ($inList && $editValue === null) {
assert($node instanceof NodeList, 'Follows from $inList');
$node->splice($editKey, 1);
++$editOffset;
} elseif ($node instanceof NodeList) {
if (! $editValue instanceof Node) {
$notNode = Utils::printSafe($editValue);
throw new \Exception("Can only add Node to NodeList, got: {$notNode}.");
}
$node[$editKey] = $editValue;
} else {
$node->{$editKey} = $editValue;
}
}
}
// @phpstan-ignore-next-line the stack is guaranteed to be non-empty at this point
[
'index' => $index,
'keys' => $keys,
'edits' => $edits,
'inList' => $inList,
] = \array_pop($stack);
} elseif ($parent === null) {
$node = $root;
} else {
$key = $inList
? $index
: $keys[$index];
$node = $parent instanceof NodeList
? $parent[$key]
: $parent->{$key};
if ($node === null) {
continue;
}
$path[] = $key;
}
$result = null;
if (! $node instanceof NodeList) {
if (! ($node instanceof Node)) {
$notNode = Utils::printSafe($node);
throw new \Exception("Invalid AST Node: {$notNode}.");
}
$visitFn = self::extractVisitFn($visitor, $node->kind, $isLeaving);
if ($visitFn !== null) {
$result = $visitFn($node, $key, $parent, $path, $ancestors);
if ($result !== null) {
if ($result instanceof VisitorStop) {
break;
}
if ($result instanceof VisitorSkipNode) {
if (! $isLeaving) {
\array_pop($path);
}
continue;
}
$editValue = $result instanceof VisitorRemoveNode
? null
: $result;
$edits[] = [$key, $editValue];
if (! $isLeaving) {
if (! ($editValue instanceof Node)) {
\array_pop($path);
continue;
}
$node = $editValue;
}
}
}
}
if ($result === null && $isEdited) {
$edits[] = [$key, $node];
}
if ($isLeaving) {
\array_pop($path);
} else {
$stack[] = [
'inList' => $inList,
'index' => $index,
'keys' => $keys,
'edits' => $edits,
];
$inList = $node instanceof NodeList;
$keys = ($inList ? $node : $visitorKeys[$node->kind]) ?? [];
$index = -1;
$edits = [];
if ($parent !== null) {
$ancestors[] = $parent;
}
$parent = $node;
}
} while ($stack !== []);
return $edits === []
? $root
: $edits[0][1];
}
/**
* Returns marker for stopping.
*
* @api
*/
public static function stop(): VisitorStop
{
static $stop;
return $stop ??= new VisitorStop();
}
/**
* Returns marker for skipping the subtree at the current node.
*
* @api
*/
public static function skipNode(): VisitorSkipNode
{
static $skipNode;
return $skipNode ??= new VisitorSkipNode();
}
/**
* Returns marker for removing the current node.
*
* @api
*/
public static function removeNode(): VisitorRemoveNode
{
static $removeNode;
return $removeNode ??= new VisitorRemoveNode();
}
/**
* Combines the given visitors to run in parallel.
*
* @phpstan-param array<int, VisitorArray> $visitors
*
* @return VisitorArray
*/
public static function visitInParallel(array $visitors): array
{
$visitorsCount = \count($visitors);
$skipping = new \SplFixedArray($visitorsCount);
return [
'enter' => static function (Node $node) use ($visitors, $skipping, $visitorsCount) {
for ($i = 0; $i < $visitorsCount; ++$i) {
if ($skipping[$i] !== null) {
continue;
}
$fn = self::extractVisitFn(
$visitors[$i],
$node->kind,
false
);
if ($fn === null) {
continue;
}
$result = $fn(...\func_get_args());
if ($result instanceof VisitorSkipNode) {
$skipping[$i] = $node;
} elseif ($result instanceof VisitorStop) {
$skipping[$i] = $result;
} elseif ($result instanceof VisitorRemoveNode) {
return $result;
} elseif ($result !== null) {
return $result;
}
}
return null;
},
'leave' => static function (Node $node) use ($visitors, $skipping, $visitorsCount) {
for ($i = 0; $i < $visitorsCount; ++$i) {
if ($skipping[$i] === null) {
$fn = self::extractVisitFn(
$visitors[$i],
$node->kind,
true
);
if ($fn !== null) {
$result = $fn(...\func_get_args());
if ($result instanceof VisitorStop) {
$skipping[$i] = $result;
} elseif ($result instanceof VisitorRemoveNode) {
return $result;
} elseif ($result !== null) {
return $result;
}
}
} elseif ($skipping[$i] === $node) {
$skipping[$i] = null;
}
}
return null;
},
];
}
/**
* Creates a new visitor that updates TypeInfo and delegates to the given visitor.
*
* @phpstan-param VisitorArray $visitor
*
* @phpstan-return VisitorArray
*/
public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): array
{
return [
'enter' => static function (Node $node) use ($typeInfo, $visitor) {
$typeInfo->enter($node);
$fn = self::extractVisitFn($visitor, $node->kind, false);
if ($fn === null) {
return null;
}
$result = $fn(...\func_get_args());
if ($result === null) {
return null;
}
$typeInfo->leave($node);
if ($result instanceof Node) {
$typeInfo->enter($result);
}
return $result;
},
'leave' => static function (Node $node) use ($typeInfo, $visitor) {
$fn = self::extractVisitFn($visitor, $node->kind, true);
$result = $fn !== null
? $fn(...\func_get_args())
: null;
$typeInfo->leave($node);
return $result;
},
];
}
/**
* @phpstan-param VisitorArray $visitor
*
* @return callable(Node $node, string $key, Node|NodeList $parent, array<int, int|string $path, array<int, Node|NodeList> $ancestors): VisitorOperation|Node|null
*/
protected static function extractVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
{
$kindVisitor = $visitor[$kind] ?? null;
if (\is_array($kindVisitor)) {
return $isLeaving
? $kindVisitor['leave'] ?? null
: $kindVisitor['enter'] ?? null;
}
if ($kindVisitor !== null && ! $isLeaving) {
return $kindVisitor;
}
$specificVisitor = $isLeaving
? $visitor['leave'] ?? null
: $visitor['enter'] ?? null;
if (\is_array($specificVisitor)) {
return $specificVisitor[$kind] ?? null;
}
return $specificVisitor;
}
}