Writing Procedural Macros
Writing procedural macros is basically about learning how to work with the standard helper libraries designed specifically to parse and generate Rust code.
Because dealing with raw, low-level TokenStreams manually is tedious and
difficult, we use a standard suite of specialized helper libraries:
proc_macro: Provided by the compiler to interface token streams.proc_macro2: Wrapsproc_macroto allow unit testing and out-of-compiler executions.syn: A complete parser library that parses Rust token streams into structured Abstract Syntax Tree (AST) structures.quote: A templated code generation library that turns your Rust structures back into token streams.
We will look at how each of these crates builds on top of one another.
This slide and its sub-slides should take about 56 minutes.
- Consider how writing a proc macro is essentially writing a mini-compiler: you parse a string of tokens (lexical analysis), match its structure (parsing), and generate a new string of tokens (transformation and code emission).
- Reassure students that you rarely ever work with raw tokens directly. The
synandquotecrates do 95% of the heavy parsing and formatting work for you, allowing you to write more-readable macro code.