[][src]Function combine::parser

pub fn parser<Input, O, F>(f: F) -> FnParser<Input, F> where
    Input: Stream,
    F: FnMut(&mut Input) -> StdParseResult<O, Input>, 

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 _: &mut easy::Stream<&str> = input;
    let position = input.position();
    let (char_digit, consumed) = digit().parse_stream(input).into_result()?;
    let d = (char_digit as i32) - ('0' as i32);
    if d % 2 == 0 {
        Ok((d, consumed))
    }
    else {
        //Return an empty error since we only tested the first token of the stream
        let errors = easy::Errors::new(
            position,
            StreamError::expected("even number")
        );
        Err(Consumed::Empty(errors.into()))
    }
});
let result = even_digit
    .easy_parse("8")
    .map(|x| x.0);
assert_eq!(result, Ok(8));