Macro syn::value [] [src]

macro_rules! value {
    ($i:expr, $res:expr) => { ... };
}

Produce the given value without parsing anything.

This can be needed where you have an existing parsed value but a parser macro's syntax expects you to provide a submacro, such as in the first argument of switch! or one of the branches of alt!.

  • Syntax: value!(VALUE)
  • Output: VALUE
#[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.