@@ -49,6 +49,8 @@ the matcher and the transcriber must be surrounded by delimiters. Macros can
49
49
expand to expressions, statements, items (including traits, impls, and foreign
50
50
items), types, or patterns.
51
51
52
+ ## Transcription
53
+
52
54
When a macro is invoked, the macro expander looks up macro invocations by name,
53
55
and tries each macro rule in turn. It transcribes the first successful match; if
54
56
this results in an error, then future matches are not tried. When matching, no
@@ -60,19 +62,56 @@ invocation unambiguously:
60
62
61
63
``` rust,compile_fail
62
64
macro_rules! ambiguity {
63
- ($($i:ident)* $j:ident) => { ($($i)-*) * $j };
65
+ ($($i:ident)* $j:ident) => { };
64
66
}
65
67
66
68
ambiguity!(error); // Error: local ambiguity
67
69
```
68
70
69
71
In both the matcher and the transcriber, the ` $ ` token is used to invoke special
70
- behaviours from the macro engine. Tokens that aren't part of such an invocation
71
- are matched and transcribed literally, with one exception. The exception is that
72
- the outer delimiters for the matcher will match any pair of delimiters. Thus,
73
- for instance, the matcher ` (()) ` will match ` {()} ` but not ` {{}} ` . The character
72
+ behaviours from the macro engine (described below in [ Metavariables] and
73
+ [ Repetitions] ). Tokens that aren't part of such an invocation are matched and
74
+ transcribed literally, with one exception. The exception is that the outer
75
+ delimiters for the matcher will match any pair of delimiters. Thus, for
76
+ instance, the matcher ` (()) ` will match ` {()} ` but not ` {{}} ` . The character
74
77
` $ ` cannot be matched or transcribed literally.
75
78
79
+ When forwarding a matched fragment to another macro-by-example, matchers in
80
+ the second macro will see an opaque AST of the fragment type. The second macro
81
+ can't use literal tokens to match the fragments in the matcher, only a
82
+ fragment specifier of the same type. The ` ident ` , ` lifetime ` , and ` tt `
83
+ fragment types are an exception, and can be matched by literal tokens. The
84
+ following illustrates this restriction:
85
+
86
+ ``` rust,compile_fail
87
+ macro_rules! foo {
88
+ ($l:expr) => { bar!($l); }
89
+ // ERROR: ^^ no rules expected this token in macro call
90
+ }
91
+
92
+ macro_rules! bar {
93
+ (3) => {}
94
+ }
95
+
96
+ foo!(3);
97
+ ```
98
+
99
+ The following illustrates how tokens can be directly matched after matching a
100
+ ` tt ` fragment:
101
+
102
+ ``` rust
103
+ // compiles OK
104
+ macro_rules! foo {
105
+ ($ l : tt ) => { bar! ($ l ); }
106
+ }
107
+
108
+ macro_rules! bar {
109
+ (3 ) => {}
110
+ }
111
+
112
+ foo! (3 );
113
+ ```
114
+
76
115
## Metavariables
77
116
78
117
In the matcher, ` $ ` _ name_ ` : ` _ fragment-specifier_ matches a Rust syntax
@@ -94,40 +133,44 @@ fragment specifiers are:
94
133
* ` vis ` : a possibly empty [ _ Visibility_ ] qualifier
95
134
* ` literal ` : matches ` - ` <sup >?</sup >[ _ LiteralExpression_ ]
96
135
97
- In the transcriber, metavariables are referred to simply by $` _name_ ` , since
136
+ In the transcriber, metavariables are referred to simply by ` $ ` _ name_ , since
98
137
the fragment kind is specified in the matcher. Metavariables are replaced with
99
138
the syntax element that matched them. The keyword metavariable ` $crate ` can be
100
139
used to refer to the current crate; see [ Hygiene] below. Metavariables can be
101
140
transcribed more than once or not at all.
102
141
103
- ## Repititions
142
+ ## Repetitions
104
143
105
144
In both the matcher and transcriber, repetitions are indicated by placing the
106
- tokens to be repeated inside ` $( ... ) ` , followed by a repetition operator,
145
+ tokens to be repeated inside ` $( ` … ` ) ` , followed by a repetition operator,
107
146
optionally with a separator token between. The separator token can be any token
108
147
other than a delimiter or one of the repetition operators, but ` ; ` and ` , ` are
109
148
the most common. For instance, ` $( $i:ident ),* ` represents any number of
110
- identifiers separated by commas. Nested repititions are permitted.
149
+ identifiers separated by commas. Nested repetitions are permitted.
150
+
151
+ The repetition operators are:
111
152
112
- The repetition operators are ` * ` , which indicates any number of repetitions,
113
- ` + ` , which indicates any number but at least one, and ` ? ` which indicates an
114
- optional fragment with zero or one occurrences. Since ` ? ` represents at most one
115
- occurrence, it cannot be used with a separator.
153
+ - ` * ` — indicates any number of repetitions.
154
+ - ` + ` — indicates any number but at least one.
155
+ - ` ? ` — indicates an optional fragment with zero or one occurrences.
156
+
157
+ Since ` ? ` represents at most one occurrence, it cannot be used with a
158
+ separator.
116
159
117
160
The repeated fragment both matches and transcribes to the specified number of
118
161
the fragment, separated by the separator token. Metavariables are matched to
119
162
every repetition of their corresponding fragment. For instance, the `$( $i: ident
120
163
),* ` example above matches ` $i` to all of the identifiers in the list.
121
164
122
- During transcription, additional restrictions apply to repititions so that the
165
+ During transcription, additional restrictions apply to repetitions so that the
123
166
compiler knows how to expand them properly:
124
167
125
168
1 . A metavariable must appear in exactly the same number, kind, and nesting
126
169
order of repetitions in the transcriber as it did in the matcher. So for the
127
- matcher ` $( $i:ident ),* ` , the transcribers ` => $i ` , ` => $( $( $i)* )* ` , and
128
- ` => $( $i )+ ` are all illegal, but ` => { $( $i );* } ` is correct and
129
- replaces a comma-separated list of identifiers with a semicolon -separated
130
- list.
170
+ matcher ` $( $i:ident ),* ` , the transcribers ` => { $i } ` ,
171
+ ` => { $( $( $i)* )* } ` , and ` => { $( $i )+ } ` are all illegal, but
172
+ ` => { $( $i );* } ` is correct and replaces a comma -separated list of
173
+ identifiers with a semicolon-separated list.
131
174
1 . Second, each repetition in the transcriber must contain at least one
132
175
metavariable to decide now many times to expand it. If multiple
133
176
metavariables appear in the same repetition, they must be bound to the same
@@ -147,12 +190,12 @@ compiler knows how to expand them properly:
147
190
For historical reasons, the scoping of macros by example does not work entirely like
148
191
items. Macros have two forms of scope: textual scope, and path-based scope.
149
192
Textual scope is based on the order that things appear in source files, or even
150
- across multiple files, and is the default scoping. It's explained further below.
193
+ across multiple files, and is the default scoping. It is explained further below.
151
194
Path-based scope works exactly the same way that item scoping does. The scoping,
152
195
exporting, and importing of macros is controlled largely by attributes.
153
196
154
197
When a macro is invoked by an unqualified identifier (not part of a multi-part
155
- path), it's first looked up in textual scoping. If this does not yield any
198
+ path), it is first looked up in textual scoping. If this does not yield any
156
199
results, then it is looked up in path-based scoping. If the macro's name is
157
200
qualified with a path, then it is only looked up in path-based scoping.
158
201
@@ -194,7 +237,7 @@ mod has_macro {
194
237
195
238
//// src/has_macro/uses_macro.rs
196
239
197
- m!{} // OK: appears after delcaration of m in src/lib.rs
240
+ m!{} // OK: appears after declaration of m in src/lib.rs
198
241
```
199
242
200
243
It is not an error to define a macro multiple times; the most recent declaration
@@ -241,7 +284,9 @@ fn foo() {
241
284
// m!(); // Error: m is not in scope.
242
285
```
243
286
244
- The ` #[macro_use] ` attribute has two purposes. First, it can be used to make a
287
+ ### The ` macro_use ` attribute
288
+
289
+ The * ` macro_use ` attribute* has two purposes. First, it can be used to make a
245
290
module's macro scope not end when the module is closed, by applying it to a
246
291
module:
247
292
@@ -257,7 +302,7 @@ m!();
257
302
```
258
303
259
304
Second, it can be used to import macros from another crate, by attaching it to
260
- an ` extern crate ` declaration appearing in the crate's root module. Macros
305
+ an ` extern crate ` declaration appearing in the crate's root module. Macros
261
306
imported this way are imported into the prelude of the crate, not textually,
262
307
which means that they can be shadowed by any other name. While macros imported
263
308
by ` #[macro_use] ` can be used before the import statement, in case of a
@@ -270,7 +315,7 @@ module.
270
315
extern crate lazy_static;
271
316
272
317
lazy_static!{};
273
- // self::lazy_static!{} // Error: lazy_static is not defined inself
318
+ // self::lazy_static!{} // Error: lazy_static is not defined in `self`
274
319
```
275
320
276
321
Macros to be imported with ` #[macro_use] ` must be exported with
@@ -302,7 +347,7 @@ mod mac {
302
347
Macros labeled with ` #[macro_export] ` are always ` pub ` and can be referred to
303
348
by other crates, either by path or by ` #[macro_use] ` as described above.
304
349
305
- ## Hygiene
350
+ ## Hygiene
306
351
307
352
By default, all identifiers referred to in a macro are expanded as-is, and are
308
353
looked up at the macro's invocation site. This can lead to issues if a macro
@@ -418,22 +463,24 @@ When repetitions are involved, then the rules apply to every possible number of
418
463
expansions, taking separators into account. This means:
419
464
420
465
* If the repetition includes a separator, that separator must be able to
421
- follow the contents of the repitition .
422
- * If the repitition can repeat multiple times (` * ` or ` + ` ), then the contents
466
+ follow the contents of the repetition .
467
+ * If the repetition can repeat multiple times (` * ` or ` + ` ), then the contents
423
468
must be able to follow themselves.
424
469
* The contents of the repetition must be able to follow whatever comes
425
470
before, and whatever comes after must be able to follow the contents of the
426
- repitition .
427
- * If the repitition can match zero times (` * ` or ` ? ` ), then whatever comes
471
+ repetition .
472
+ * If the repetition can match zero times (` * ` or ` ? ` ), then whatever comes
428
473
after must be able to follow whatever comes before.
429
474
430
475
431
476
For more detail, see the [ formal specification] .
432
477
478
+ [ Hygiene ] : #hygiene
433
479
[ IDENTIFIER ] : identifiers.html
434
480
[ IDENTIFIER_OR_KEYWORD ] : identifiers.html
435
481
[ LIFETIME_TOKEN ] : tokens.html#lifetimes-and-loop-labels
436
- [ formal specification ] : macro-ambiguity.html
482
+ [ Metavariables ] : #metavariables
483
+ [ Repetitions ] : #repetitions
437
484
[ _BlockExpression_ ] : expressions/block-expr.html
438
485
[ _DelimTokenTree_ ] : macros.html
439
486
[ _Expression_ ] : expressions.html
@@ -447,5 +494,5 @@ For more detail, see the [formal specification].
447
494
[ _TypePath_ ] : paths.html#paths-in-types
448
495
[ _Type_ ] : types.html#type-expressions
449
496
[ _Visibility_ ] : visibility-and-privacy.html
497
+ [ formal specification ] : macro-ambiguity.html
450
498
[ token ] : tokens.html
451
- [ Hygiene ] : #hygiene
0 commit comments