Skip to content
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

Matching any child with NodePattern #6841

Closed
marcandre opened this issue Mar 16, 2019 · 1 comment · Fixed by #6965
Closed

Matching any child with NodePattern #6841

marcandre opened this issue Mar 16, 2019 · 1 comment · Fixed by #6965

Comments

@marcandre
Copy link
Contributor

marcandre commented Mar 16, 2019

From @Drenmi :

there's a long standing request to add an "any child" matcher. This would be especially helpful when matching on hashes. Consider:

ruby-parse -e '{ foo: 1, bar: 2 }'

(hash
  (pair
    (sym :foo)
    (int 1))
  (pair
    (sym :bar)
    (int 2)))

We may be interested in hashes that have a foo: 1 pair, but the pairs can appear in any order, so we can't reliably match that in a pattern. 🙂

What is needed is a suggested extension to the syntax, and the actual implementation.

Suggested syntax extension would be <...> as it seems the easiest remaining "brackety" symbol left.

Simple:

(hash <(pair (sym :foo) (int 1)) (pair (sym :bar) (int 2))>)

This would match {bar: 2, foo: 1} and {foo: 1, bar: 2} but nothing else. To match hashes containing these two value-key pairs, use ...:

Ellipsis:

(hash <(pair (sym :foo) (int 1)) ...>)

This would match any literal hash with a foo: 1 key-value pair. Ellipsis must be at end of <> sequence.

Note: Patterns are attempted to be matched in the order given. <(pair _ (int 1)) (pair (sym :foo) _)> would match {bar: 1, foo: 1} but not {foo: 1, bar: 1}. Captures are always returned in the order of the pattern.

Comments?

@marcandre
Copy link
Contributor Author

Description edited to match #6965

marcandre added a commit to marcandre/rubocop that referenced this issue Apr 25, 2019
…rder.

These can be used multiple times in the same sequence as well as
nested.

Limitations: A sequence can only contain a single ellipsis (contained
or not within a <> sequence).

Other implementation changes:
- Captures are now an array (e.g. `capture0` is now `captures[0]`)
- Temporary variables like `temp1` were renamed to `node1`.
- Reading tokens until a delimiter was factorized into `tokens_until`
bbatsov pushed a commit that referenced this issue Apr 25, 2019
These can be used multiple times in the same sequence as well as
nested.

Limitations: A sequence can only contain a single ellipsis (contained
or not within a <> sequence).

Other implementation changes:
- Captures are now an array (e.g. `capture0` is now `captures[0]`)
- Temporary variables like `temp1` were renamed to `node1`.
- Reading tokens until a delimiter was factorized into `tokens_until`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant