rust-peg icon indicating copy to clipboard operation
rust-peg copied to clipboard

Shorthand syntax for selecting a return value from a sequence

Open kevinmehall opened this issue 2 years ago • 2 comments

A common pattern in rust-peg grammars is to use a block at the end of a parenthesized subexpression to choose a return value inside of a * or ? expression, for example when handling an optional type after a Rust-style let expression:

"let" name:ident() ty:(":" t:type_expr() { t })? 

This requires introducing an extra variable t and a lot of noise just to pick out the part of the expression you want to return.

This proposes a syntactic sugar

"let" name:ident() ty:(":" :type_expr())? 

to allow :-tagged arguments without labels and without an action block, where the sequence would evaluate to the tagged labels.

This could also build a tuple if there are multiple, making (:x(), :y()) equivalent to (a:x() b:y() {(a, b)}).

I think the :expr syntax is unambiguous now that parentheses are required on rule invocations, but will need to verify. It requires a little bit of lookahead and may be confusing for humans as well. >:expr or ^:expr could be alternatives, but PEG is already quite symbol-heavy.

kevinmehall avatar Aug 08 '21 04:08 kevinmehall