winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
572 stars 44 forks source link

dispatch shouldn't require comma after {} arm #344

Open adamchalmers opened 1 year ago

adamchalmers commented 1 year ago

Please complete the following tasks

rust version

1.73

winnow version

0.5.16

Minimal reproducible code

use winnow::prelude::*;
use winnow::token::any;
use winnow::combinator::{success, fail, dispatch, preceded};

fn escaped(input: &mut &str) -> PResult<char> {
    preceded('\\', escape_seq_char).parse_next(input)
}

fn escape_seq_char(input: &mut &str) -> PResult<char> {
    dispatch! {any;
        'b' => {
            success('\u{8}')
        }
        'f' => success('\u{c}'),
        _ => fail::<_, char, _>,
    }
    .parse_next(input)
}

Steps to reproduce the bug with the above code

cargo check

Actual Behaviour

Program does not compile, because the arm with 'b' doesn't have a trailing comma.

error: no rules expected the token `'f'`
  --> src/main.rs:14:9
   |
13 |         }
   |          - help: missing comma here
14 |         'f' => success('\u{c}'),
   |         ^^^ no rules expected this token in macro call
   |
   = note: while trying to match sequence start

If you add a trailing comma after, it compiles:

        'b' => {
            success('\u{8}')
        },

Expected Behaviour

dispatch! should work like Rust's standard match construct, which requires a trailing comma after non-{} arms, but allows omitting the trailing comma after {} arms.

This way dispatch! will be more familiar to new users of the library

Additional Context

This is a very minor low-priority issue, but the current macro behaviour surprised me. I figured it was worth tracking.

epage commented 1 year ago

Thanks for reporting! macro-rules can be a pain to get right in all of these corner cases.