Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

How Do Macros Work

To understand how macros work, it helps to look at the pipeline of how a compiler processes your code:

  1. Source Code: The raw text files written by the programmer, consisting of a sequence of characters in some character set (in Rust, Unicode characters in UTF-8).
  2. Lexical Analysis (Lexing): The compiler delimits the raw text into a sequence of tokens (e.g., fn, main, identifiers, punctuation, literals).
  3. Syntactic Analysis (Parsing): Based on the language’s grammar, the compiler organizes the sequence of tokens into an Abstract Syntax Tree (AST) that captures the hierarchical structure of the program.
Source CodeTokenized Rust (Approx.)AST Rust (Approx.)
fn main() {}Ident: fn, Ident: main, Group: (), Group: {}Fn: ident=main, block=...

Different macro systems hook into different parts of this pipeline, offering different tradeoffs between expressiveness and safety.

We’ll look at the macro system used in C and C++ via the C Preprocessor, which is well-known, and contrast this with Rust’s macro system.

This slide and its sub-slides should take about 20 minutes.
  • Explain that the AST is a structured tree that represents what the program actually means semantically.