Use $XXX syntax for metavariable/holes instead of :[xxx]
aryx opened this issue · 11 comments
One quick thing
If your feature request is about changing or updating :[hole]
syntax, please read some of the principles and guidelines on the syntax design before posting. If not, please proceed.
I read the rational, but we want to possibly use comby inside semgrep and it's better for the user
to have a consistent syntax for metavariables/hole, and semgrep uses $XXX.
Describe the solution you'd like
Ideally there would be a global flag somewhere, like -syntax_hole_dollar, accessible also via the API
to use $XXX instead of :[hole]
Yeah no worries. Things have evolved in a way that works out and that doc is a bit outdated :-) I've almost got things working in #255, just a few kinks to work through still.
The change will let you define syntax for whatever part of matching you want from comby. E.g., if you don't want any matching behavior besides :[hole]
or :[regex~foo]
, you can specify syntax just for that and omit definitions for the other current possibilities, and it won't be part of the matching engine.
Looks great!
In semgrep we actually impose the metavariable to be in uppercase, like $XXX, not $xxx, to be useful also on languages already using $ like PHP (you can have variables like $XXX in PHP but it's never used, so it was ok to use $XXX).
It would be simple to add that constraint for something like that in the library interface (like, define a function function | 'A' .. 'Z' -> true | _ -> false
), but not worth the effort exposing that to JSON or CLI flags. I'll add something like that for the internal API.
For disambiguating in Comby, there's a nice trick where if you want to match on the literal metasyntax (and the syntax might conflict) you would just use the metasyntax to specify the thing to match using regex. E.g., if the metasyntax is :[hole]
and you want to match :[hole]
, you just write the template :[var~:\[hole\]]
.
Similar, if you defined $XXX
to be a metavariable, and you define the syntax for matching regular expressions like $XXX~regex<some terminal>
then you can disambiguate and match $XXX by just doing $XXX~$XXX<some terminal>
. In Comby its possible to inline the regex (which is why you need to define some terminal to stop parsing a regex, like space, or whatever). Inlining the regex has some nice properties. I've been meaning to write a blog about that but no time yet 🤷 .
Hokay sorted out the kinks :-) will publish a release ~tomorrow
Aaaaand almost done with the .mli
documentation, looks like I'll need one more day to wrap things up.
FYI: ocaml/opam-repository#18367
Here's the updated mli. It's very much a first pass at making comby a proper library, so it'll improve over time, some parts of the interface are less than ideal :-)
Fantastic rijnard! This is great; I'll try to integrate comby in semgrep next week if I don't have too many other bugs to fix :)
It works! Super cool.
Ok found finally some time to try to integreate comby with semgrep: semgrep/semgrep#3248
Still experimental but it's a start.