Skip to content

Commit

Permalink
Simplify visitor (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Feb 24, 2023
1 parent b9cfc95 commit 88f8856
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
8 changes: 2 additions & 6 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,15 +1117,11 @@ visitor API:
/**
* Visit the AST (see class description for details).
*
* @template TNode of Node
*
* @param NodeList<TNode>|Node $root
* @param NodeList<Node>|Node $root
* @param VisitorArray $visitor
* @param array<string, mixed>|null $keyMap
*
* @throws \Exception
*
* @return Node|mixed
* @return mixed
*
* @api
*/
Expand Down
45 changes: 18 additions & 27 deletions src/Language/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,11 @@ class Visitor
/**
* Visit the AST (see class description for details).
*
* @template TNode of Node
*
* @param NodeList<TNode>|Node $root
* @param NodeList<Node>|Node $root
* @param VisitorArray $visitor
* @param array<string, mixed>|null $keyMap
*
* @throws \Exception
*
* @return Node|mixed
* @return mixed
*
* @api
*/
Expand All @@ -187,7 +183,6 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
$parent = null;
$path = [];
$ancestors = [];
$newRoot = $root;

do {
++$index;
Expand Down Expand Up @@ -240,20 +235,17 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
] = \array_pop($stack);
} else {
if ($parent === null) {
$node = $newRoot;
$node = $root;
} else {
$key = $inList
? $index
: $keys[$index];
$node = $parent instanceof NodeList
? $parent[$key]
: $parent->{$key};
}
if ($node === null) {
continue;
}

if ($parent !== null) {
if ($node === null) {
continue;
}
$path[] = $key;
}
}
Expand All @@ -264,7 +256,7 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
throw new \Exception('Invalid AST Node: ' . \json_encode($node));
}

$visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving);
$visitFn = self::extractVisitFn($visitor, $node->kind, $isLeaving);

if ($visitFn !== null) {
$result = $visitFn($node, $key, $parent, $path, $ancestors);
Expand Down Expand Up @@ -324,11 +316,9 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
}
} while (\count($stack) > 0);

if (\count($edits) > 0) {
$newRoot = $edits[0][1];
}

return $newRoot;
return \count($edits) > 0
? $edits[0][1]
: $root;
}

/**
Expand Down Expand Up @@ -368,6 +358,8 @@ public static function removeNode(): VisitorRemoveNode
}

/**
* Combines the given visitors to run in parallel.
*
* @phpstan-param array<int, VisitorArray> $visitors
*
* @return VisitorArray
Expand All @@ -384,7 +376,7 @@ public static function visitInParallel(array $visitors): array
continue;
}

$fn = self::getVisitFn(
$fn = self::extractVisitFn(
$visitors[$i],
$node->kind,
false
Expand Down Expand Up @@ -412,7 +404,7 @@ public static function visitInParallel(array $visitors): array
'leave' => static function (Node $node) use ($visitors, $skipping, $visitorsCount) {
for ($i = 0; $i < $visitorsCount; ++$i) {
if ($skipping[$i] === null) {
$fn = self::getVisitFn(
$fn = self::extractVisitFn(
$visitors[$i],
$node->kind,
true
Expand Down Expand Up @@ -440,8 +432,7 @@ public static function visitInParallel(array $visitors): array
}

/**
* Creates a new visitor instance which maintains a provided TypeInfo instance
* along with visiting visitor.
* Creates a new visitor that updates TypeInfo and delegates to the given visitor.
*
* @phpstan-param VisitorArray $visitor
*
Expand All @@ -452,7 +443,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
return [
'enter' => static function (Node $node) use ($typeInfo, $visitor) {
$typeInfo->enter($node);
$fn = self::getVisitFn($visitor, $node->kind, false);
$fn = self::extractVisitFn($visitor, $node->kind, false);

if ($fn === null) {
return null;
Expand All @@ -471,7 +462,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
return $result;
},
'leave' => static function (Node $node) use ($typeInfo, $visitor) {
$fn = self::getVisitFn($visitor, $node->kind, true);
$fn = self::extractVisitFn($visitor, $node->kind, true);
$result = $fn !== null
? $fn(...\func_get_args())
: null;
Expand All @@ -488,7 +479,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
*
* @return callable(Node $node, string $key, Node|NodeList $parent, array<int, int|string $path, array<int, Node|NodeList> $ancestors): VisitorOperation|Node|null
*/
public static function getVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
protected static function extractVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
{
$kindVisitor = $visitor[$kind] ?? null;

Expand Down

0 comments on commit 88f8856

Please sign in to comment.