Declarative (macro_rules!) macro improvements #252

Not sure whom to answer, as this is a very diverse RFC and I have diverse ideas about it:

Language Expressivess

It is not possible to parse some valid Rust syntax like if expr block, because what may come after expr is very limited. This may not be generally possible, because an expr can confusingly end with SomeStruct { field } or an if expression. A solution might be medium greedy expr, which stops before the next item cannot be matched. I.e. if x == SomeStruct { field } and if x == SomeStruct { field } {} would both match upto before the last block.

Generalising this is also ruined by the syntax choice for let chains, where suddenly && is not part of the preceding expr. A solution might be if let $lhs:pat = $value:min_expr && … where min_expr stops capturing before where it first sees the next token.

Type Granularity

You can have a general tt or be more specific. In the same way I often want to be more specific on other fragment specs.

Strings are syntactically different from numbers. Integers are different from floats. One should be able to choose between literal, string, number and then again integer, float (which needs to check for either dot or alas also the four float suffixes like 1f16.)

Likewise adding _ to expr really ruined it, because in most contexts that is not a valid expression! Therefore we now need to be able to further distinguish lhs_expr and rhs_expr by whatever better name.

Alternatives

I variously find I have to repeat almost the same pattern. We would need $(…$|…$|…) in which case a repeat suffix would be optional.

Repeat Count

There would need to be a suffix to represent a precise count, e.g. {min, max} like in regex.

Capture Verbatim Things

This $var:(foo$|bar$|baz) would put into $var whichever of the three words (or other things) it found. Otherwise $(x)? is purely decorative, without being able to know whether it matched.

1 Like