Macro syn::switch [] [src]

macro_rules! switch {
    ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { ... };
}

Pattern-match the result of a parser to select which other parser to run.

#[macro_use]
extern crate syn;

use syn::Ident;
use syn::token::Brace;
use syn::synom::Synom;

/// Parse a unit struct or enum: either `struct S;` or `enum E { V }`.
enum UnitType {
    Struct {
        struct_token: Token![struct],
        name: Ident,
        semi_token: Token![;],
    },
    Enum {
        enum_token: Token![enum],
        name: Ident,
        brace_token: Brace,
        variant: Ident,
    },
}

enum StructOrEnum {
    Struct(Token![struct]),
    Enum(Token![enum]),
}

impl Synom for StructOrEnum {
    named!(parse -> Self, alt!(
        keyword!(struct) => { StructOrEnum::Struct }
        |
        keyword!(enum) => { StructOrEnum::Enum }
    ));
}

impl Synom for UnitType {
    named!(parse -> Self, do_parse!(
        which: syn!(StructOrEnum) >>
        name: syn!(Ident) >>
        item: switch!(value!(which),
            StructOrEnum::Struct(struct_token) => map!(
                punct!(;),
                |semi_token| UnitType::Struct {
                    struct_token,
                    name,
                    semi_token,
                }
            )
            |
            StructOrEnum::Enum(enum_token) => map!(
                braces!(syn!(Ident)),
                |(brace_token, variant)| UnitType::Enum {
                    enum_token,
                    name,
                    brace_token,
                    variant,
                }
            )
        ) >>
        (item)
    ));
}

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