Named pattern groups
Created by: natefaubion
This is something that I've wanted on several occasions. Generally I'd like to be able to match on a literal keyword or syntax, but retain the original token in a pattern var for context (like with throwSyntaxError
). Right now we have to match on it with a generic pattern var, then verify it with a case body, which is something I do not enjoy. So I propose a simple extension that allows you to name literal groups and pattern groups.
macro func {
rule { $keyword:[function] $name:ident ($args:ident (,) ...) { $body ... } } => {
// ...
}
}
So a normal literal group is $[...]
, I think a named literal group could easily be $name:[...]
. If the match in the braces is successful, the tokens would be bound to the pattern var. This doesn't clobber any existing syntax since we generally treat $name:foo
as reserved.
We can extend this to pattern groups with sub matches:
macro obj {
rule { $def:($name:ident $eq:[=] $lhs:expr) (,) ... } => {
{
$($def$name : $def$lhs) (,) ...
}
}
}
This does the same thing as macroclass
does with concatenating the names. So in this case, its much like an ad-hoc macroclass
.
This should be a very simple implementation. I'd just like to get some feedback on it to make sure its something others want and the syntax is obvious enough.