J-F-Liu / pom

PEG parser combinators using operator overloading without macros.
MIT License
496 stars 30 forks source link

Pom doesn't recognize line breaks #34

Closed anasbarg closed 5 years ago

anasbarg commented 5 years ago

Hello .. I have this piece of code that is trying to make sure that constant ends with a line break.

pub fn constant<'a>() -> Parser<'a, u8, Constant> {
    let parsed_constant = (space() * seq(b"const") - space())
        * ((ident() - space() - sym(b'='))
            + (space() * (regex() | string() | float() | integer()) - space()))
        - linebreak().repeat(1..).discard();
    parsed_constant.map(|term| Constant {
        identifier: String::from(term.0),
        value: term.1,
    })
}
fn linebreak<'a>() -> Parser<'a, u8, ()> {
    sym(b'\r').opt() * sym(b'\n').discard()
}

But it's not seeing the line break at all. I'm loading the text from a file as a String then I convert it to &[u8] using .as_bytes(). The linebreak parser is copied as is from the whitespace example. Am I doing something wrong?

Thank you

anasbarg commented 5 years ago

That was my bad. I had a conflicting definition of space that matches before reaching linebreak.

pub fn space<'a>() -> Parser<'a, u8, ()> {
    one_of(b" \n\t\r").repeat(0..).discard()
}