[][src]Macro egg::define_language

macro_rules! define_language {
    ($(#[$meta:meta])* $vis:vis enum $name:ident $variants:tt) => { ... };
}

A macro to easily create a Language.

define_language derives Debug, PartialEq, Eq, PartialOrd, Ord, Hash, and Clone on the given enum so it can implement Language. The macro also implements Language::from_op_str and Language::display_op for the enum based on either the data of variants or the provided strings.

The final variant must have a trailing comma; this is due to limitations in macro parsing.

Note that you can always implement Language yourself by just not using this macro.

Presently, the macro does not support data variant with children, but that may be added later.

Example

The following macro invocation shows all the accepted forms of variants:

define_language! {
    enum SimpleLanguage {
        // string variant with no children
        "pi" = Pi,

        // string variants with an array of child `Id`s (any static size)
        "+" = Add([Id; 2]),
        "-" = Sub([Id; 2]),
        "*" = Mul([Id; 2]),

        // string variants with a single child `Id`
        // note that this is distinct from `Sub`, even though it has the same
        // string, because it has a different number of children
        "-"  = Neg(Id),

        // data variants with a single field
        // this field must implement `FromStr` and `Display`
        Num(i32),
        // language items are parsed in order, and we want symbol to
        // be a fallback, so we put it last
        Symbol(String),
    }
}