macro_rules! keyword {
($name:ident = $str:literal; ...) => { ... };
($name:ident = $group:path; ...) => { ... };
($name:ident = [$($keywords:tt),+]; ...) => { ... };
($name:ident != $str:literal; ...) => { ... };
($name:ident != $group:path; ...) => { ... };
($name:ident != [$($keywords:tt),+]; ...) => { ... };
}Expand description
Define types matching keywords.
- Any number of attributes (
#[...]), including documentation comments. Keyword documentation is automatically extended by a small auto generated doc comment listing what a keyword definition will match. - A optional
pubdeclaration. Nameis the name for the struct to be generated."identifier"is the case sensitive keyword.groupcan be a non empty bracketed list of"identifier"or any an other keyword definition.- By using
=the keyword must match the given definition while!=negates the output and matches any identifier that is not in the definition.
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. See the
unsynn! keyword documentation for details.
In case a invalid keyword is defined (not an identifier) the compilation will panic. But because the actual matching function is optimized and lazy evaluated this will only happen on the first use of the invalid keyword definition.
Keywords implement AsRef<str>, AsRef<Ident> and Keyword::as_str(&self) -> &str. For
Keywords that are defined with a single literal string (keyword!{ Foo = "foo"}) the
Default trait is implemented. Thus they can be created and inserted statically.
ยงExample
keyword!{
/// Optional documentation for `If`
pub If = "if";
pub Else = "else";
// keywords can be grouped from existing keywords
IfElse = [If, Else,];
// or contain identifiers in double quotes
IfElseThen = [IfElse, "then"];
// matching can be negated with `!=`
NotIfElseThen != [IfElse, "then"];
}
let mut tokens = "if".to_token_iter();
let if_kw = If::parse(&mut tokens).unwrap();
assert_eq!(if_kw.as_str(), "if");