winnow-rs / winnow

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

`seq!` accept constructor with multiple segments #531

Closed TennyZhuang closed 5 months ago

TennyZhuang commented 5 months ago

Please complete the following tasks

winnow version

0.6.9

Describe your use case

If we have an enum like this:

enum Expr {
    Cast {
        expr: Box<Expr>,
        data_type: DataType,
    }
}

And we want to construct it during parsing, currently we must explicit use Expr::Cast.

use Expr:Cast;
seq! {
    Cast {
        _: Token::LParen,
        expr: expr.map(Box::new),
        _: Keyword::AS,
        data_type: data_type,
        _: Token::RParen,
    }
}

There is no reason to disallow we use Expr::Cast here directly:

seq! {
    Expr::Cast {
        _: Token::LParen,
        expr: expr.map(Box::new),
        _: Keyword::AS,
        data_type: data_type,
        _: Token::RParen,
    }
}

Describe the solution you'd like

Change $name: ident to $name: path here.

Alternatives, if applicable

No response

Additional Context

No response

TennyZhuang commented 5 months ago

When I try to do that, I know why we have to use ident here now :(

https://internals.rust-lang.org/t/allow-and-to-follow-path-fragment-in-macro/7796/2

If we don't want to rewrite the macro using proc-macro, we can't really support the feature.

Finally, I found it's doable by $($name: ident)::*