Function combine::parser [] [src]

pub fn parser<I, O, F>(f: F) -> FnParser<I, F> where
    I: Stream,
    F: FnMut(I) -> ParseResult<O, I>, 

Wraps a function, turning it into a parser.

Mainly needed to turn closures into parsers as function types can be casted to function pointers to make them usable as a parser.

extern crate combine;
let mut even_digit = parser(|input| {
    // Help type inference out
    let _: easy::Stream<&str> = input;
    let position = input.position();
    let (char_digit, input) = try!(digit().parse_stream(input));
    let d = (char_digit as i32) - ('0' as i32);
    if d % 2 == 0 {
        Ok((d, input))
    }
    else {
        //Return an empty error since we only tested the first token of the stream
        let errors = easy::Errors::new(
            position,
            StreamError::expected(From::from("even number"))
        );
        Err(Consumed::Empty(errors.into()))
    }
});
let result = even_digit
    .easy_parse("8")
    .map(|x| x.0);
assert_eq!(result, Ok(8));