Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

$?<name> to match a single optional node #52

Open
quasilyte opened this issue Jan 8, 2020 · 1 comment
Open

$?<name> to match a single optional node #52

quasilyte opened this issue Jan 8, 2020 · 1 comment

Comments

@quasilyte
Copy link
Contributor

quasilyte commented Jan 8, 2020

Right now we only have $*<name> to match optional parts.

Sometimes we want to match exactly 1 optional node inside a pattern. It's either nil or it matches exactly 1 node.

It could be used to match optional parts inside a pattern that are expected to represent a single expression (or none).

I don't think that we need more general {min,max} form from regexps, but ? seems useful.

My use case: "find f() function call that is not followed by a return statement".
It could be expressed via f(); $x and then checking that $x is a return statement. The only problem is that f() could be the last statement inside a block.

We could solve that by using f(); $?x query that should in theory match these cases without return as well.

@mvdan
Copy link
Owner

mvdan commented Jan 9, 2020

Good idea - I agree this is useful.

I also agree that {min,max} is probably too much for the dollar syntax. If someone needs that much power, they should just use #32.

@mvdan mvdan changed the title Proposal: $?<name> to match a single optional node $?<name> to match a single optional node Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants