J-F-Liu / pom

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

Can't discard trailing symbols #13

Closed cetra3 closed 7 years ago

cetra3 commented 7 years ago

Not sure if I'm doing things wrong here:

extern crate pom;

use pom::DataInput;
use pom::parser::*;

fn main() {

    let mut input = DataInput::new(b" an example string ");

    let parser = untilspace() + untilspace() + untilspace();

    let output = parser.parse(&mut input);

    println!("{:?}", output);

    assert_eq!(output, Ok(((String::from("an"), String::from("example")), String::from("string"))));

}

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

fn untilspace<'a>() -> Parser<'a, u8, String> {

    let value = space() * none_of(b" ").repeat(0..);

    value.collect().convert(String::from_utf8)
}

Error I get is:

thread 'main' panicked at 'assertion failed: `(left == right)` (left: `Ok(((" an", " example"), " string"))`, right: `Ok((("an", "example"), "string"))`)', src/main.rs:16
cetra3 commented 7 years ago

Appears to be related to the collect() function, that returns ALL symbols, including discarded ones. Is this desired?

J-F-Liu commented 7 years ago

Yes, collect() returns all symbols. Try

fn untilspace<'a>() -> Parser<'a, u8, String> {
    space() * none_of(b" ").repeat(0..).convert(String::from_utf8)
}