Macro cpp_synom::opt_vec [] [src]

macro_rules! opt_vec {
    ($i:expr, $submac:ident!( $($args:tt)* )) => { ... };
}

Turn a failed parse into an empty vector. The argument parser must itself return a vector.

This is often more convenient than option!(...) when the argument produces a vector.

  • Syntax: opt_vec!(THING)
  • Output: THING, which must be Vec<T>
extern crate syn;
#[macro_use] extern crate synom;

use syn::{Lifetime, Ty};
use syn::parse::{lifetime, ty};

named!(bound_lifetimes -> (Vec<Lifetime>, Ty), tuple!(
    opt_vec!(do_parse!(
        keyword!("for") >>
        punct!("<") >>
        lifetimes: terminated_list!(punct!(","), lifetime) >>
        punct!(">") >>
        (lifetimes)
    )),
    ty
));

fn main() {
    let input = "for<'a, 'b> fn(&'a A) -> &'b B";
    let parsed = bound_lifetimes(input).expect("bound lifetimes");
    assert_eq!(parsed.0, [Lifetime::new("'a"), Lifetime::new("'b")]);
    println!("{:?}", parsed);

    let input = "From<String>";
    let parsed = bound_lifetimes(input).expect("bound lifetimes");
    assert!(parsed.0.is_empty());
    println!("{:?}", parsed);
}