home ~ projects ~ socials

The many1 nom Parser Panics With multispace0 in Rust

Still learning how all this stuff works, but it seems like multispace0 won't throw an error that many1 needs to stop trying to process. I thought many1 would stop on it's own error, but I don't think that's the case. I think it only stops it if gets an error from a child parser.

use nom::character::complete::multispace0;
use nom::multi::many1;
use nom::IResult;

fn main() {
    let source = "  -- alfa\n-- bravo\n\ncharlie\ndelta\n\necho\n\n-- foxtrot\n\ngolf\nhotel";
    dbg!(sections(source).unwrap());
}

fn sections(source: &str) -> IResult<&str, Vec<&str>> {
    dbg!(&source);
    let (source, sections) = many1(section)(source)?;
    Ok((source, sections))
}

fn section(source: &str) -> IResult<&str, &str> {
    dbg!(&source);
    let (source, section) = multispace0(source)?;
    Ok((source, section))
}

If you add something like this below the multispace line, the output shows two dashes and returns the rest of the content.

let (source, section) = tag("-")(source)?;
-- end of line --

References