Macro combine::dispatch

source ·
macro_rules! dispatch {
    ($match_expr: expr; $( $($pat: pat)|+ $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { ... };
}
Expand description

dispatch! allows a parser to be constructed depending on earlier input, without forcing each branch to have the same type of parser

use combine::{dispatch, any, token, satisfy, EasyParser, Parser};

let mut parser = any().then(|e| {
    dispatch!(e;
        'a' => token('a'),
        'b' => satisfy(|b| b == 'b'),
        t if t == 'c' => any(),
        _ => token('d')
    )
});
assert_eq!(parser.easy_parse("aa"), Ok(('a', "")));
assert_eq!(parser.easy_parse("cc"), Ok(('c', "")));
assert_eq!(parser.easy_parse("cd"), Ok(('d', "")));
assert!(parser.easy_parse("ab").is_err());