microsoft / microsoft/tolerant-php-parser

Implement RecursiveIterator

Open
#130 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
PHP
Stars
894
Forks
85
Avg merge
2d 13h
Merged PRs (30d)
3

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the traversal code in src/Node.php around the Generator implementation linked in the issue, then review how the parser represents child nodes. Define what class-based RecursiveIterator support should cover, including hasChildren(), getChildren(), filtering, and skipping children; done means traversal can be composed through the PHP iterator APIs described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.