Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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.

Code

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.

Code

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

References