Expand description
§token — the public seam on both sides
What a producer hands the compiler, and what a renderer hands back.
The private capture/ and generation/ homes own the two directions while this module keeps the established public token paths unchanged.
The shared rendered-path magnitude lives at this seam, while each declaring home retains its established public compatibility constant.
§Why it is typed
proc_macro is a proc-macro-crate-only API.
A crate that is not compiled as a proc macro cannot name its types at all, so the compiler — which is ordinary callable Rust and must stay so — can neither take a TokenStream nor hand one back.
The answer is not to fall back to strings. A string is a token stream with its structure thrown away, and everything the capture then has to do is re-derive structure the compiler already had.
So the seam is typed on both sides.
§Reading
CapturedTokenTree is one token of a declared input: a payload, a stable TokenPath naming exactly where it sits in the tree, and an opaque SpanHandle indexing the producer’s own table.
Delimited groups stay groups, including a compiler’s invisible group.
Compiler-host input is never re-lexed.
CaptureBuilder is the only mint of a complete captured input.
A producer supplies its own source position and a CapturedAtom, or opens a nested group; the builder issues the path and handle, spends every declared magnitude, retains positions in handle order, and derives the final denominator from that roster.
No producer can state the resulting capture facts as sibling arguments.
A capture operation consumes its open CaptureLevel, and only a successful operation returns the level, so a refused partial tree has no road to CaptureLevel::finish.
The refused attempt’s positions remain available for its diagnostic; opening a fresh level rolls back that attempt while preserving handles issued by earlier successful captures in the same table.
Where a producer refuses one token after issuance, the refusal retains both the declaration-local path and the producer-local handle.
A payload carries a literal’s value and never its spelling.
"x" and r"x" are one text, "a\nb" is three characters, and which prefix a producer read is not a fact the tree keeps.
capture_literal is where a lexed spelling becomes that value, and it refuses a form it has no row for rather than filing it under a neighbouring one.
Token distinctions that change a proc macro’s input remain distinct: ordinary and raw identifiers have different rows, punctuation retains whether it joins what follows, lifetimes remain quote-plus-identifier tokens, and invisible compiler groups remain groups.
Whitespace and ordinary comments do not enter the normalized declaration.
Doc comments enter as the doc attributes the compiler presents to a proc macro.
Every producer walks under the same captured-input magnitudes — depth, level, whole tree, and capture work — declared as plain constants beside the capacities they govern.
A producer that skips or backtracks charges that observation through CaptureLevel::examined, so work discarded before capture does not disappear from the budget.
A SpanHandle means “the token at this index of the table the producer built while capturing”.
The compiler never resolves one; it carries the handle into a diagnostic so that whoever produced the input can map it back to the exact compiler span, which is what puts a compile_error! on the offending token rather than on the first token of the declaration.
TextCapture::read is the third producer.
A compiler is one, a test is another, and text is the third — it exists so that the reproduction route a diagnostic names is a real road and not a promise.
It uses a pinned low-level compiler lexer for token boundaries and slices every spelling from the original source by the lexer-reported byte range.
Its source-byte magnitude is checked before lexing and stands independently of the capture tree and work magnitudes.
The CapturedInput it returns is the shared normalization boundary against which the compiler-token producer is observed.
CaptureCursor reads generic mechanical shapes from that boundary without interpreting them.
It leaves declaration vocabulary and diagnostic policy with the caller, and its typed refusals retain the exact token span available where a word, identifier, punctuation seat, group, separator, magnitude, or end condition disagrees.
This is the reusable parser battery for subject-owned grammars, not a grammar owned by Macroonz.
CapturedFragment is one borrowed exact run inside that same boundary.
A cursor can return the fragment consumed by a caller-owned structural read, so types, paths, signatures, expressions, patterns, generic clauses, bodies, and other exact Rust seats retain their captured tokens and source handles without becoming strings.
AuthoredItem is the shallow structural lens over one supported complete caller-authored item boundary.
It identifies the outer attributes, visibility, qualifiers, structural item family, optional name, generics, where clause, signature, and body group while the complete CapturedFragment remains the one preserved token reading.
The lens is not a Rust AST, and Rustc remains responsible for full syntax, type, lifetime, ownership, and coherence judgments.
§Writing
GeneratedTree is what a renderer produces.
A renderer states a literal’s value and never its spelling here too: the quoting, the escaping, and the absence of a suffix belong to the tree.
That is what keeps b"…" from being assembled out of a word and a quoted string — two tokens where the address reading it matches one — and what lets one count be written into a u32 seat, a u64 seat, and a usize seat, because an unsuffixed literal is typed by the position it lands in.
The private generation home’s composers are the rest of what a renderer needs: paths, calls, method chains, bindings, constants, functions, attributes, rosters.
A renderer states what it means and never assembles punctuation by hand.
The keyed slice projectors walk an informed KeyedRoster or KeyedRosterAssignment in its structural order while the renderer supplies every row’s tokens and the ordinary Rust item surrounding the slice.
Where generated output must repeat exact caller-authored Rust, CapturedFragment::generated projects the captured token structure directly into the generated vocabulary.
The road never builds and reparses source text, retains invisible compiler groups, carries each producer span as nonsemantic emission provenance, and uses a guarded GeneratedLiteral for numeric, character, byte, and C-string forms that require exact literal custody.
Producer spans do not enter equality, hashing, canonical bytes, or human inspection, and only the compiler host resolves them while materializing the declaration-site token stream.
The written roster grows only at its end. Each arm’s stable slot is one byte of the tree’s canonical encoding, and those bytes are what a rendered unit’s identity is derived over. An ordinary identifier and a raw identifier occupy distinct rows, while every pre-existing row keeps its occupied slot.
§What it is not
The Rust source text a person reads is GeneratedTree::inspected — a projection of the tree, produced for a person, never the artifact.
Nothing parses it back and no identity is derived from it.
Nothing here knows what a declaration means. The seam carries tokens with their structure and their spans intact; the grammar written in them is the caller’s.
Structs§
- Authored
Item - A checked structural lens into one supported complete caller-authored Rust item.
- Authored
Item Read Refusal - One refused authored-item lens with the exact available producer span.
- Capture
Builder - The only state that issues capture handles and retains the producer’s matching source positions.
- Capture
Cursor - A read cursor over one normalized captured-token sequence.
- Capture
Level - One nesting level borrowed from a
CaptureBuilder. - Capture
Read Refusal - One refused mechanical read with the exact producer span available at that site.
- Capture
Walk - The running state of one capture walk: what the walk has spent, and how much of the whole-tree magnitude it has taken.
- Captured
Fragment - One borrowed run of captured tokens under its original source boundary.
- Captured
Input - One captured declared input: the top-level token trees, and how many span handles the producer issued.
- Captured
Token Tree - One captured token: what it carries, where it sits, and how to reach the compiler span it came from.
- Fragment
Generation Refusal - One refused captured-fragment projection with the exact source span it belongs to.
- Generated
Literal - One exact literal form carried from declared Rust into generated Rust.
- Generated
RowRefusal - Why one flat keyed-row projection could not produce exactly one non-empty item run.
- Generated
Tree - One generated token tree: the artifact a renderer produces.
- Source
Coordinate - One compiler-local source position with its coordinate system stated.
- Span
Handle - An opaque index into the producer’s span table.
- Span
Resolution Refusal - Why one span table could not say where a handle sits.
- Text
Capture - One declared input read from text: the captured trees, and the byte offsets that resolve every handle the read issued.
- Text
Read Refusal - One refused text read: the established cause, and the byte it sits at.
- Token
Path - Where one captured token sits, as the index route from the root of the declared input.
Enums§
- Authored
Item Kind - The structural Rust item family one authored-item lens recognized.
- Authored
Item Read Issue - Why one captured input could not provide an authored-item structural lens.
- Capture
Bound - Which declared magnitude one capture ran past.
- Capture
Build Refusal - Why a checked capture was not completed.
- Capture
Expectation - The mechanical token shape one capture read asked for.
- Capture
Read Issue - Why generic captured-token grammar mechanics could not complete one read.
- Captured
Atom - One non-group value a capture producer offers to the checked builder.
- Captured
Delimiter - The delimiter one captured group is written with.
- Captured
Payload - What one captured token carries.
- Captured
Spacing - Whether one punctuation seat is joined to what follows or stands alone.
- Coordinate
Role - The coordinate system one source position is counted in.
- Fragment
Generation Issue - Why one preserved captured fragment could not become a generated tree.
- Generated
Delimiter - The delimiter one generated group is written with.
- Generated
Literal Refusal - Why an exact literal could not be admitted for generation.
- Generated
Spacing - Whether one generated punctuation mark joins the token after it.
- Generated
Token - One token a renderer writes.
- Literal
Read Cause - Why one literal spelling could not be read into the value it names.
- Span
Table - How a producer answers “where is the token this handle names?”.
- Text
Lexical Cause - Why the low-level lexer could not normalize one spelling.
- Text
Read Cause - Why one text read refused.
Constants§
- CAPTURED_
TOKEN_ LIMIT - Token trees one captured input may carry at any one nesting level.
- CAPTURED_
TREE_ TOKEN_ LIMIT - Tokens one captured input may retain across the whole tree, and positions one span table may hold.
- CAPTURE_
WORK_ LIMIT - Units of capture work one walk may spend, one unit per examined token.
- GENERATED_
TOKEN_ LIMIT - Tokens one generated tree may carry at any one nesting level.
- TEXT_
SOURCE_ BYTE_ LIMIT - Source bytes one text capture may read before tokenization.
- TOKEN_
PATH_ DEPTH_ LIMIT - Steps one token path may carry, and so how deeply a declared input may nest.
Functions§
- absolute_
path - The absolute path
::a::b::c. - and_all
- Every comparison joined by
&&. - associated_
constant - One associated constant declaration or definition.
- associated_
function - One associated function declaration or definition from an exact signature.
- associated_
type - One associated type declaration or definition.
- attribute
- One attribute
#[body]. - bound_
local - One statement
let name = expression;. - bound_
path - The path
root::a::b::c, rooted at a crate the caller named. - call
- One call
path(arguments). - capture_
literal - Read one literal token’s spelling into the atom it names.
- comma
- Two token runs separated by a comma.
- comma_
many - Every token run separated by a comma, with no trailing one.
- constant
- One item
const name: kind = value;. - consuming_
receiver - The conventional consuming receiver
self. - decorated
- Prefix one item with its declared attributes and visibility.
- documentation
- One documentation attribute over one sentence.
- enumeration
- One enum
enum Name<...> where ... { variants }. - equality
- One comparison
left == right. - exclusive_
receiver - The conventional exclusive receiver
&'a mut self, with an empty lifetime run producing&mut self. - function
- One conventional
fn name(parameters) -> result { body }item. - function_
item - One function or method item from an exact signature and exact body.
- function_
signature - One exact function signature without visibility, attributes, body or terminal semicolon.
- generic_
parameters - One generic-parameter list
<parameters>, or nothing when the roster is empty. - group
- One delimited group.
- implementation
- One inherent or trait implementation with exact qualifiers, parameters, target, predicates and items.
- inline_
module - One inline module
mod name { items }. - keyed_
assignment_ items - Projects one exact keyed assignment into a flat item run in denominator order.
- keyed_
assignment_ slice - Projects one exact keyed assignment into a borrowed Rust slice expression in denominator order.
- keyed_
roster_ items - Projects one keyed roster into a flat item run in retained order.
- keyed_
roster_ slice - Projects one keyed roster into a borrowed Rust slice expression in retained order.
- match_
arm - One match arm with an optional exact guard and a trailing comma.
- match_
expression - One match expression over exact subject and arm runs.
- metavariable
- One macro metavariable, as the two tokens that spell it.
- method_
call - One call
receiver.method(arguments). - method_
chain - One chain
receiver.first().second().third(), every method taking no argument. - named_
field - One named field
name: kind. - named_
struct - One named struct
struct Name<...> where ... { fields }. - named_
variant - One named variant.
- pinned_
receiver - The conventional pinned receiver
self: ::core::pin::Pin<&'a mut Self>. - rendered_
identifier - Whether one spelling is a single Rust identifier a rendering is willing to write.
- rendered_
name - Whether one spelling can NAME a rendered item: a single identifier the language has not already taken.
- result_
type - The type
::core::result::Result<ok, error>. - roster
- The
::std::vec![…]a roster-taking constructor is handed. - rust_
keyword - Whether one spelling is a Rust keyword no rendered item can be named by.
- shared_
receiver - The conventional shared receiver
&'a self, with an empty lifetime run producing&self. - text_
pair - Two spellings as the two comma-separated text literals a two-argument parser takes.
- trait_
declaration - One trait declaration with exact qualifiers, parameters, supertraits, predicates and associated items.
- tuple_
struct - One tuple struct
struct Name<...>(fields) where ...;. - tuple_
variant - One tuple variant.
- twin_
path - The path
$binding::a::b::c, rooted at a metavariable a macro shell will bind. - type_
alias - One type alias
type Name<...> = value where ...;. - typed_
parameter - One typed parameter
pattern: kind. - unit_
struct - One unit struct
struct Name<...> where ...;. - unit_
variant - One unit variant.
- use_
item - One import
use path [as alias];. - where_
clause - One
whereclause over exact caller-owned predicates, or nothing when the roster is empty.