Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorthand syntax for selecting a return value from a sequence #267

Open
kevinmehall opened this issue Aug 8, 2021 · 0 comments
Open

Shorthand syntax for selecting a return value from a sequence #267

kevinmehall opened this issue Aug 8, 2021 · 0 comments
Labels
feature Something not supported that perhaps should be

Comments

@kevinmehall
Copy link
Owner

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 kevinmehall added the feature Something not supported that perhaps should be label Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Something not supported that perhaps should be
Projects
None yet
Development

No branches or pull requests

1 participant