@@ -271,12 +271,72 @@ fn invoke4() {}
271271// out: item: "fn invoke4() {}"
272272```
273273
274+ ### Declarative macro tokens and procedural macro tokens
275+
276+ Declarative ` macro_rules ` macros and procedural macros use similar, but
277+ different definitions for tokens (or rather [ ` TokenTree ` s] .)
278+
279+ Token trees in ` macro_rules ` (corresponding to ` tt ` matchers) are defined as
280+ - Delimited groups (` (...) ` , ` {...} ` , etc)
281+ - All operators supported by the language, both single-character and
282+ multi-character ones (` + ` , ` += ` ).
283+ - Note that this set doesn't include the single quote ` ' ` .
284+ - Literals (` "string" ` , ` 1 ` , etc)
285+ - Note that negation (e.g. ` -1 ` ) is never a part of such literal tokens,
286+ but a separate operator token.
287+ - Identifiers (` ident ` , ` r#ident ` )
288+ - Lifetimes (` 'ident ` )
289+ - Metavariable substitutions in ` macro_rules ` (e.g. ` $my_expr ` in
290+ ` macro_rules! mac { ($my_expr: expr) => { $my_expr } } ` after the ` mac ` 's
291+ expansion, which will be considered a single token tree regardless of the
292+ passed expression)
293+
294+ Token trees in procedural macros are defined as
295+ - Delimited groups (` (...) ` , ` {...} ` , etc)
296+ - All punctuation characters used in operators supported by the language (` + ` ,
297+ but not ` += ` ), and also the single quote ` ' ` character
298+ - Literals (` "string" ` , ` 1 ` , etc)
299+ - Negation (e.g. ` -1 ` ) is supported as a part of integer
300+ and floating point literals.
301+ - Identifiers (` ident ` , ` r#ident ` )
302+
303+ Mismatches between these two definitions are accounted for when token streams
304+ are passed to and from procedural macros. \
305+ Note that the conversions below may happen lazily, so they might not happen if
306+ the tokens are not actually inspected.
307+
308+ When passed to a proc-macro
309+ - All multi-character operators are broken into single characters.
310+ - Lifetimes are broken into a ` ' ` character and an identifier.
311+ - All metavariable substitutions are represented as their underlying token
312+ streams.
313+ - Such token streams may be wrapped into delimited groups ([ ` Group ` ] ) with
314+ implicit delimiters ([ ` Delimiter::None ` ] ) when it's necessary for
315+ preserving parsing priorities.
316+ - ` tt ` and ` ident ` substitutions are never wrapped into such groups and
317+ always represented as their underlying token trees.
318+
319+ When passed from a proc macro
320+ - Punctuation characters are glued into multi-character operators
321+ when applicable.
322+ - Single quotes ` ' ` joined with identifiers are glued into lifetimes.
323+ - Negative literals are converted into two tokens (the ` - ` and the literal)
324+ possibly wrapped into a delimited group ([ ` Group ` ] ) with implicit delimiters
325+ ([ ` Delimiter::None ` ] ) when it's necessary for preserving parsing priorities.
326+
327+ Note that neither declarative nor procedural macros support doc comment tokens
328+ (e.g. ` /// Doc ` ), so they are always converted to token streams representing
329+ their equivalent ` #[doc = "str"] ` attributes when passed to macros.
330+
274331[ Attribute macros ] : #attribute-macros
275332[ Cargo's build scripts ] : ../cargo/reference/build-scripts.html
276333[ Derive macros ] : #derive-macros
277334[ Function-like macros ] : #function-like-procedural-macros
335+ [ `Delimiter::None` ] : ../proc_macro/enum.Delimiter.html#variant.None
336+ [ `Group` ] : ../proc_macro/struct.Group.html
278337[ `TokenStream` ] : ../proc_macro/struct.TokenStream.html
279338[ `TokenStream`s ] : ../proc_macro/struct.TokenStream.html
339+ [ `TokenTree`s ] : ../proc_macro/enum.TokenTree.html
280340[ `compile_error` ] : ../std/macro.compile_error.html
281341[ `derive` attribute ] : attributes/derive.md
282342[ `extern` blocks ] : items/external-blocks.md
0 commit comments