Function chomp::combinators::choice [] [src]

pub fn choice<I, T, E, R>(i: I, parsers: R) -> ParseResult<I, T, E> where
    I: Input,
    R: IntoIterator<Item = Box<FnMut(I) -> ParseResult<I, T, E>>>, 

Attempts each parser yielded by an iterator in order, returning the result of the first successful parser. This combinator requires boxing of all the parsers returned from the iterator.

Panics if the list/iteartor is empty.

NOTE: Since we are supporting stable, FnMut is required here since FnBox cannot be used yet.

use chomp::combinators::choice;
use chomp::parsers::token;
use chomp::parse_only;

let v: Vec<Box<FnMut(_) -> _>> = vec![
    Box::new(|i| token(i, b'b')),
    Box::new(|i| token(i, b'a')),
];
assert_eq!(parse_only(|i| choice(i, v), &b"abc"[..]), Ok(b'a'));
use chomp::combinators::choice;
use chomp::parsers::{Error, token};
use chomp::parse_only;

let v: Vec<Box<FnMut(_) -> _>> = vec![
    Box::new(|i| token(i, b'b')),
    Box::new(|i| token(i, b'a')),
];
assert_eq!(parse_only(|i| choice(i, v), &b"c"[..]), Err((&b"c"[..], Error::expected(b'a'))));