Macro keyword

Source
macro_rules! keyword {
    ($(#[$attribute:meta])* $pub:vis $name:ident = $str:literal $(;$($cont:tt)*)?) => { ... };
    () => { ... };
}
Expand description

Define types matching keywords.

keyword!{ pub Name = "identifier", ...}

  • A optional pub defines the keyword public, default is private
  • Name is the name for the struct to be generated
  • "identifier" is the case sensitive keyword

Name::parse() will then only match the defined identifier. It will implement Debug and Clone for keywords. Additionally AsRef<str> is implemented for each Keyword to access the identifier string from rust code.

The unsynn! macro supports defining keywords by using keyword Name = "ident";, the pub specification has to come before keyword then.

ยงExample

keyword!{
    /// Optional documentation for `If`
    If = "if";
    Else = "else";
}

let mut tokens = "if else".to_token_iter();
let if_kw = If::parse(&mut tokens).unwrap();
assert_eq!(if_kw.as_ref(), "if");
let else_kw = Else::parse(&mut tokens).unwrap();
assert_eq!(else_kw.as_ref(), "else");