Skip to main content

grammar

Macro grammar 

Source
grammar!() { /* proc-macro */ }
Expand description

Generates a zero-cost grammar with compile-time dispatch.

Produces a grammar struct, a symbol enum, and a GrammarDef implementation with all dispatch resolved via match arms (no allocations, no HashMap lookups).

§Syntax

grammar! {
    grammar MyGrammar;
    symbol MySymbol;
    start Expr;

    Expr => [Expr, Expr, BinOp] | [Val];
    BinOp => [Add] | [Sub] | [Mul];
    Val => [X] | [One];
}
  • grammar <name> — name of the generated struct implementing GrammarDef
  • symbol <name> — name of the generated enum with all grammar symbols
  • start <rule> — the start rule
  • Rules: <name> => [symbols...] | [symbols...];

Symbols appearing on the left side of => are non-terminals. All other symbols are terminals.