pub fn parser<I, O, F>(f: F) -> FnParser<I, F>
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 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) = try!(digit().parse_stream(input));
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(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));