Function chomp::combinators::or [] [src]

pub fn or<'a, I, T, E, F, G>(i: Input<'a, I>, f: F, g: G) -> ParseResult<'a, I, T, E> where F: FnOnce(Input<'a, I>) -> ParseResult<'a, I, T, E>, G: FnOnce(Input<'a, I>) -> ParseResult<'a, I, T, E>

Tries to match the parser f, if f fails it tries g. Returns the success value of the first match, otherwise the error of the last one if both fail.

Incomplete state is propagated from the first one to report incomplete.

use chomp::{Input, Error, or, token};

let p1 = Input::new(b"abc");
let p2 = Input::new(b"bbc");
let p3 = Input::new(b"cbc");

assert_eq!(or(p1, |i| token(i, b'a'), |i| token(i, b'b')).unwrap(), b'a');
assert_eq!(or(p2, |i| token(i, b'a'), |i| token(i, b'b')).unwrap(), b'b');
assert_eq!(or(p3, |i| token(i, b'a'), |i| token(i, b'b')).unwrap_err(), Error::Expected(b'b'));