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

Syntax suggestion for select! #234

Closed
muja opened this issue Dec 18, 2022 · 1 comment
Closed

Syntax suggestion for select! #234

muja opened this issue Dec 18, 2022 · 1 comment

Comments

@muja
Copy link
Contributor

muja commented Dec 18, 2022

I've seen you updated select to optionally accept a span in master. However with the way it is implemented right now, span must be defined on every enum path. I've used the following implementation quite successfully:

macro_rules! select {
    (|$span:ident| $($p:pat $(if $guard:expr)? => $out:expr),+ $(,)?) => ({
        chumsky::primitive::filter_map(move |$span, x| match x {
            $($p $(if $guard)? => ::core::result::Result::Ok($out)),+,
            _ => ::core::result::Result::Err(chumsky::error::Error::expected_input_found($span, ::core::option::Option::None, ::core::option::Option::Some(x))),
        })
    });

    ($($p:pat $(if $guard:expr)? => $out:expr),+ $(,)?) => (select!(|_span| $($p $(if $guard)? => $out),+));
}

This way span can only be specified once: select!(|span| Token::Literal(s) => (span, Value::Literal(s)))

I think this also easier to read, but that's probably a matter of opinion. If you want I can create a PR

@zesterer
Copy link
Owner

I think this would be acceptable. IIRC the span feature isn't yet in a release, so this wouldn't be a breaking change. If you want to open a PR, feel free!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants