You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP has native interfaces and classes for Iterators (Generator implements Iterator). Iterators can be traversed with foreach and composed easily, for example by filtering through CallbackFilterIterator.
In particular, it has the RecursiveIterator interface, that adds two more methods to implement: hasChildren() and getChildren().
This allows it to be traversed with a RecursiveIteratorIterator, which has countless options for emitting all nodes or just leaves, using level order, etc. ParentIterator can be used to visit just nodes with child nodes, RecursiveLimitIterator to limit the nodes traversed, AppendIterator to concatenate Iterators, RecursiveTreeIterator can be used to draw an ASCII tree of an AST and RecursiveCallbackFilterIterator to filter nodes (including skipping children). Really a lot of awesome stuff in there.
Uh oh!
There was an error while loading. Please reload this page.
PHP has native interfaces and classes for Iterators (
Generator
implementsIterator
). Iterators can be traversed withforeach
and composed easily, for example by filtering throughCallbackFilterIterator
.In particular, it has the
RecursiveIterator
interface, that adds two more methods to implement:hasChildren()
andgetChildren()
.This allows it to be traversed with a
RecursiveIteratorIterator
, which has countless options for emitting all nodes or just leaves, using level order, etc.ParentIterator
can be used to visit just nodes with child nodes,RecursiveLimitIterator
to limit the nodes traversed,AppendIterator
to concatenate Iterators,RecursiveTreeIterator
can be used to draw an ASCII tree of an AST andRecursiveCallbackFilterIterator
to filter nodes (including skipping children). Really a lot of awesome stuff in there.The Parser already uses Generators for traversal, which is great, but it only has limited options for filtering and skipping certain children through a callback: https://sourcegraph.com/github.com/Microsoft/tolerant-php-parser/-/blob/src/Node.php#L158-159
I think providing class-based implementations for Iterators would give greater flexibility.
The text was updated successfully, but these errors were encountered: