[−][src]Macro syn::cond
macro_rules! cond { ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { ... }; ($i:expr, $cond:expr, $f:expr) => { ... }; }
Execute a parser only if a condition is met, otherwise return None.
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
#[macro_use] extern crate syn; use syn::{Ident, MacroDelimiter}; use syn::token::{Paren, Bracket, Brace}; use syn::synom::Synom; /// Parses a macro call with empty input. If the macro is written with /// parentheses or brackets, a trailing semicolon is required. /// /// Example: `my_macro!{}` or `my_macro!();` or `my_macro![];` struct EmptyMacroCall { name: Ident, bang_token: Token![!], empty_body: MacroDelimiter, semi_token: Option<Token![;]>, } fn requires_semi(delimiter: &MacroDelimiter) -> bool { match *delimiter { MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => true, MacroDelimiter::Brace(_) => false, } } impl Synom for EmptyMacroCall { named!(parse -> Self, do_parse!( name: syn!(Ident) >> bang_token: punct!(!) >> empty_body: alt!( parens!(epsilon!()) => { |d| MacroDelimiter::Paren(d.0) } | brackets!(epsilon!()) => { |d| MacroDelimiter::Bracket(d.0) } | braces!(epsilon!()) => { |d| MacroDelimiter::Brace(d.0) } ) >> semi_token: cond!(requires_semi(&empty_body), punct!(;)) >> (EmptyMacroCall { name, bang_token, empty_body, semi_token, }) )); }
This macro is available if Syn is built with the "parsing"
feature.