J-F-Liu / pom

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

Does Parser.collect() return discarded Parsers? #26

Closed chrisdotcode closed 5 years ago

chrisdotcode commented 5 years ago

Hello,

In the following code:

use pom::Parser;
use pom::parser::{sym, one_of};

fn num() -> Parser<u8, &'static [u8]> {
    (sym(b'{') * one_of(b"0123456789").repeat(1..) - sym(b'}'))
        .collect()
}

fn main() {
    println!("{:?}", num().parse(b"{123}"));
}

I get the output:

Ok([123, 49, 50, 51, 125])

As you can see, in the output, the leading { (ordinal 123) and the trailing } (ordinal 125) are being returned as well, even though I am using the * and - combinators to try and ignore them.

Is this a bug in the library, or am I just using it incorrectly?

Thank you for creating pom.


pom version 3.0.0 rust stable, 1.31.0 mac os

chrisdotcode commented 5 years ago

I am ultimately trying to create a parser that can parse number ranges surrounded by brackets in the form of: {1-999} {-999} {1-} {-}

But every time I try to create a more complex parser to parse any of the above, everytime I use .collect(), the { and } symbols keep also returning themselves in the result, no matter if I use * or -, or even .discard().

J-F-Liu commented 5 years ago

This is not a bug, * and - discard the output values, while collect applies to input symbols. You may write

sym(b'{') * one_of(b"0123456789").repeat(1..).collect() - sym(b'}')
chrisdotcode commented 5 years ago

I see. Thank you! (Merry Christmas 🎄)