liveBook · Manning

Tokenization

As explained in Write Powerful Rust Macros

Lexing is the process of converting raw source text into a sequence of lexical tokens.

Tokenization, or lexing, converts raw source text into a stream of lexical tokens. In Rust, procedural macros receive code as a TokenStream and return another TokenStream for generated code. Parsing follows tokenization by organizing tokens into an abstract syntax tree (AST); spans connect tokens to their original source for diagnostics. section 3.2

From expression to tokens

Tokenizing `1 + 11` produces three tokens: an integer literal, the `+` punctuation token, and another integer literal. Parsing can then interpret `+` as the AST root with the two literals as branches. section 3.2

TokenStream [Literal { kind: Integer, symbol: "1" }, Punct { ch: '+', spacing: Alone }, Literal { kind: Integer, symbol: "11" }]

Inspect the concrete TokenStream representation produced from `1 + 11`, including literal kinds and punctuation spacing. section 3.2

How Write Powerful Rust Macros covers it

How deep it goes
Defines tokenization and contrasts it with parsing, but stops short of detailed compiler internals.
Examples
Uses `1 + 11` and its TokenStream representation to demonstrate literals and punctuation.
What it assumes
Assumes Rust source-code context while teaching lexing and parsing without requiring compiler internals.
What you can do after
Evaluate/understand how Rust represents macro input before parsing and code generation.
How it connects
Connects tokenization with parsing, abstract syntax trees, procedural macros, TokenStream, and spans.

Also explained in

Compare how 25 books explain tokenization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Write Powerful Rust Macros. The section links point to the parts of the book it draws on.