Skip to content

Is it possible to declare parser function like nom's named macro? #391

Answered by epage
FantasyCheese asked this question in Q&A
Discussion options

You must be logged in to vote

FYI the macro parsers, like named!, aren't in nom anymore, not just one being preferred.

If you want to use let a_or_b within a function, that should work. The following compiles

use winnow::prelude::*;
use winnow::combinator::alt;

fn some_parser(input: &mut &str) -> PResult<u32> {
    let prefix = alt(("0o", "0b"));
    (prefix, winnow::ascii::digit1).recognize().parse_to().parse_next(input)
}

fn main() {
    some_parser.parse("0b101010").unwrap();
}

The challenge with using it in a global scope is that parsers are inherently mutable. This works with Rust's type system in most cases but there are some exceptions and I'm assuming a global is one.

For global parsers, is there a concern …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@FantasyCheese
Comment options

Answer selected by FantasyCheese
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants