Macro syn::named [] [src]

macro_rules! named {
    ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { ... };
    (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { ... };
    ($name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { ... };
    (pub $name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { ... };
}

Define a parser function with the signature expected by syn parser combinators.

The function may be the parse function of the Synom trait, or it may be a free-standing function with an arbitrary name. When implementing the Synom trait, the function name is parse and the return type is Self.

  • Syntax: named!(NAME -> TYPE, PARSER) or named!(pub NAME -> TYPE, PARSER)
#[macro_use]
extern crate syn;

use syn::Type;
use syn::punctuated::Punctuated;
use syn::synom::Synom;

/// Parses one or more Rust types separated by commas.
///
/// Example: `String, Vec<T>, [u8; LEN + 1]`
named!(pub comma_separated_types -> Punctuated<Type, Token![,]>,
    call!(Punctuated::parse_separated_nonempty)
);

/// The same function as a `Synom` implementation.
struct CommaSeparatedTypes {
    types: Punctuated<Type, Token![,]>,
}

impl Synom for CommaSeparatedTypes {
    /// As the default behavior, we want there to be at least 1 type.
    named!(parse -> Self, do_parse!(
        types: call!(Punctuated::parse_separated_nonempty) >>
        (CommaSeparatedTypes { types })
    ));
}

impl CommaSeparatedTypes {
    /// A separate parser that the user can invoke explicitly which allows
    /// for parsing 0 or more types, rather than the default 1 or more.
    named!(pub parse0 -> Self, do_parse!(
        types: call!(Punctuated::parse_separated) >>
        (CommaSeparatedTypes { types })
    ));
}

This macro is available if Syn is built with the "parsing" feature.