Function parser

Source
pub fn parser<I, O, F>(f: F) -> FnParser<I, F>
where I: Stream, F: FnMut(State<I>) -> ParseResult<O, I, I::Item>,
Expand description

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 parser_combinators as pc;
use pc::*;
use pc::primitives::{Consumed, Error};
let mut even_digit = parser(|input| {
    let position = input.position;
    let (char_digit, input) = try!(digit().parse_state(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
        Err(Consumed::Empty(ParseError::new(position, Error::Expected(From::from("even number")))))
    }
});
let result = even_digit
    .parse("8")
    .map(|x| x.0);
assert_eq!(result, Ok(8));