Skip to content

Implement RecursiveIterator #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
felixfbecker opened this issue Jun 3, 2017 · 5 comments
Open

Implement RecursiveIterator #130

felixfbecker opened this issue Jun 3, 2017 · 5 comments
Labels

Comments

@felixfbecker
Copy link

felixfbecker commented Jun 3, 2017

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.

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.

@jens1o
Copy link
Contributor

jens1o commented Jun 3, 2017

Actually, that was my idea creating a pr, after the pr has been merged. But you are stealing my ideas constantly. :D

@felixfbecker
Copy link
Author

Well, go ahead. Don't let me keep you :)

@jens1o
Copy link
Contributor

jens1o commented Jun 9, 2017

@roblourens Are you fine with this change? I would start getting on this.

@felixfbecker
Copy link
Author

@jens1o Already almost finished ;)

@jens1o
Copy link
Contributor

jens1o commented Jun 9, 2017

Sorry, so you had implemented that already? Okay. Then I search for other things :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants