Macro synom::cond
[−]
[src]
macro_rules! cond { ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { ... }; ($i:expr, $cond:expr, $f:expr) => { ... }; }
Conditionally execute the given parser.
If you are familiar with nom, this is nom's cond_with_error
parser.
- Syntax:
cond!(CONDITION, THING)
- Output:
Some(THING)
if the condition is true, elseNone
extern crate syn; #[macro_use] extern crate synom; use syn::parse::boolean; // Parses a tuple of booleans like `(true, false, false)`, possibly with a // dotdot indicating omitted values like `(true, true, .., true)`. Returns // separate vectors for the booleans before and after the dotdot. The second // vector is None if there was no dotdot. named!(bools_with_dotdot -> (Vec<bool>, Option<Vec<bool>>), do_parse!( punct!("(") >> before: separated_list!(punct!(","), boolean) >> after: option!(do_parse!( // Only allow comma if there are elements before dotdot, i.e. cannot // be `(, .., true)`. cond!(!before.is_empty(), punct!(",")) >> punct!("..") >> after: many0!(preceded!(punct!(","), boolean)) >> // Only allow trailing comma if there are elements after dotdot, // i.e. cannot be `(true, .., )`. cond!(!after.is_empty(), option!(punct!(","))) >> (after) )) >> // Allow trailing comma if there is no dotdot but there are elements. cond!(!before.is_empty() && after.is_none(), option!(punct!(","))) >> punct!(")") >> (before, after) )); fn main() { let input = "(true, false, false)"; let parsed = bools_with_dotdot(input).expect("bools with dotdot"); assert_eq!(parsed, (vec![true, false, false], None)); let input = "(true, true, .., true)"; let parsed = bools_with_dotdot(input).expect("bools with dotdot"); assert_eq!(parsed, (vec![true, true], Some(vec![true]))); let input = "(.., true)"; let parsed = bools_with_dotdot(input).expect("bools with dotdot"); assert_eq!(parsed, (vec![], Some(vec![true]))); let input = "(true, true, ..)"; let parsed = bools_with_dotdot(input).expect("bools with dotdot"); assert_eq!(parsed, (vec![true, true], Some(vec![]))); let input = "(..)"; let parsed = bools_with_dotdot(input).expect("bools with dotdot"); assert_eq!(parsed, (vec![], Some(vec![]))); }