gogrep icon indicating copy to clipboard operation
gogrep copied to clipboard

$?<name> to match a single optional node

Open quasilyte opened this issue 5 years ago • 1 comments

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.

quasilyte avatar Jan 08 '20 09:01 quasilyte

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 avatar Jan 09 '20 09:01 mvdan