3 This chapter picks up where the [introductory macro chapter](macros.html) left
6 # Syntactic requirements
8 Even when Rust code contains un-expanded macros, it can be parsed as a full
9 [syntax tree][ast]. This property can be very useful for editors and other
10 tools that process code. It also has a few consequences for the design of
13 [ast]: glossary.html#abstract-syntax-tree
15 One consequence is that Rust must determine, when it parses a macro invocation,
16 whether the macro stands in for
19 * zero or more methods,
24 A macro invocation within a block could stand for some items, or for an
25 expression / statement. Rust uses a simple rule to resolve this ambiguity. A
26 macro invocation that stands for items must be either
28 * delimited by curly braces, e.g. `foo! { ... }`, or
29 * terminated by a semicolon, e.g. `foo!(...);`
31 Another consequence of pre-expansion parsing is that the macro invocation must
32 consist of valid Rust tokens. Furthermore, parentheses, brackets, and braces
33 must be balanced within a macro invocation. For example, `foo!([)` is
34 forbidden. This allows Rust to know where the macro invocation ends.
36 More formally, the macro invocation body must be a sequence of *token trees*.
37 A token tree is defined recursively as either
39 * a sequence of token trees surrounded by matching `()`, `[]`, or `{}`, or
40 * any other single token.
42 Within a matcher, each metavariable has a *fragment specifier*, identifying
43 which syntactic form it matches.
45 * `ident`: an identifier. Examples: `x`; `foo`.
46 * `path`: a qualified name. Example: `T::SpecialA`.
47 * `expr`: an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`; `f(42)`.
48 * `ty`: a type. Examples: `i32`; `Vec<(char, String)>`; `&T`.
49 * `pat`: a pattern. Examples: `Some(t)`; `(17, 'a')`; `_`.
50 * `stmt`: a single statement. Example: `let x = 3`.
51 * `block`: a brace-delimited sequence of statements. Example:
52 `{ log(error, "hi"); return 12; }`.
53 * `item`: an [item][]. Examples: `fn foo() { }`; `struct Bar;`.
54 * `meta`: a "meta item", as found in attributes. Example: `cfg(target_os = "windows")`.
55 * `tt`: a single token tree.
57 There are additional rules regarding the next token after a metavariable:
59 * `expr` variables must be followed by one of: `=> , ;`
60 * `ty` and `path` variables must be followed by one of: `=> , : = > as`
61 * `pat` variables must be followed by one of: `=> , =`
62 * Other variables may be followed by any token.
64 These rules provide some flexibility for Rust's syntax to evolve without
65 breaking existing macros.
67 The macro system does not deal with parse ambiguity at all. For example, the
68 grammar `$($t:ty)* $e:expr` will always fail to parse, because the parser would
69 be forced to choose between parsing `$t` and parsing `$e`. Changing the
70 invocation syntax to put a distinctive token in front can solve the problem. In
71 this case, you can write `$(T $t:ty)* E $e:exp`.
73 [item]: ../reference.html#items
75 # Scoping and macro import/export
77 Macros are expanded at an early stage in compilation, before name resolution.
78 One downside is that scoping works differently for macros, compared to other
79 constructs in the language.
81 Definition and expansion of macros both happen in a single depth-first,
82 lexical-order traversal of a crate's source. So a macro defined at module scope
83 is visible to any subsequent code in the same module, which includes the body
84 of any subsequent child `mod` items.
86 A macro defined within the body of a single `fn`, or anywhere else not at
87 module scope, is visible only within that item.
89 If a module has the `macro_use` attribute, its macros are also visible in its
90 parent module after the child's `mod` item. If the parent also has `macro_use`
91 then the macros will be visible in the grandparent after the parent's `mod`
94 The `macro_use` attribute can also appear on `extern crate`. In this context
95 it controls which macros are loaded from the external crate, e.g.
98 #[macro_use(foo, bar)]
102 If the attribute is given simply as `#[macro_use]`, all macros are loaded. If
103 there is no `#[macro_use]` attribute then no macros are loaded. Only macros
104 defined with the `#[macro_export]` attribute may be loaded.
106 To load a crate's macros *without* linking it into the output, use `#[no_link]`
112 macro_rules! m1 { () => (()) }
120 macro_rules! m2 { () => (()) }
122 // visible here: m1, m2
127 macro_rules! m3 { () => (()) }
129 // visible here: m1, m3
133 // visible here: m1, m3
135 macro_rules! m4 { () => (()) }
137 // visible here: m1, m3, m4
140 // visible here: m1, m3, m4
144 When this library is loaded with `#[macro_use] extern crate`, only `m2` will
147 The Rust Reference has a [listing of macro-related
148 attributes](../reference.html#macro--and-plugin-related-attributes).
150 # The variable `$crate`
152 A further difficulty occurs when a macro is used in multiple crates. Say that
156 pub fn increment(x: u32) -> u32 {
162 ($x:expr) => ( ::increment($x) )
167 ($x:expr) => ( ::mylib::increment($x) )
172 `inc_a` only works within `mylib`, while `inc_b` only works outside the
173 library. Furthermore, `inc_b` will break if the user imports `mylib` under
176 Rust does not (yet) have a hygiene system for crate references, but it does
177 provide a simple workaround for this problem. Within a macro imported from a
178 crate named `foo`, the special macro variable `$crate` will expand to `::foo`.
179 By contrast, when a macro is defined and then used in the same crate, `$crate`
180 will expand to nothing. This means we can write
185 ($x:expr) => ( $crate::increment($x) )
190 to define a single macro that works both inside and outside our library. The
191 function name will expand to either `::increment` or `::mylib::increment`.
193 To keep this system simple and correct, `#[macro_use] extern crate ...` may
194 only appear at the root of your crate, not inside `mod`. This ensures that
195 `$crate` is a single identifier.
199 The introductory chapter mentioned recursive macros, but it did not give the
200 full story. Recursive macros are useful for another reason: Each recursive
201 invocation gives you another opportunity to pattern-match the macro's
204 As an extreme example, it is possible, though hardly advisable, to implement
205 the [Bitwise Cyclic Tag](http://esolangs.org/wiki/Bitwise_Cyclic_Tag) automaton
206 within Rust's macro system.
210 // cmd 0: d ... => ...
211 (0, $($ps:tt),* ; $_d:tt)
212 => (bct!($($ps),*, 0 ; ));
213 (0, $($ps:tt),* ; $_d:tt, $($ds:tt),*)
214 => (bct!($($ps),*, 0 ; $($ds),*));
216 // cmd 1p: 1 ... => 1 ... p
217 (1, $p:tt, $($ps:tt),* ; 1)
218 => (bct!($($ps),*, 1, $p ; 1, $p));
219 (1, $p:tt, $($ps:tt),* ; 1, $($ds:tt),*)
220 => (bct!($($ps),*, 1, $p ; 1, $($ds),*, $p));
222 // cmd 1p: 0 ... => 0 ...
223 (1, $p:tt, $($ps:tt),* ; $($ds:tt),*)
224 => (bct!($($ps),*, 1, $p ; $($ds),*));
226 // halt on empty data string
232 Exercise: use macros to reduce duplication in the above definition of the
237 If Rust's macro system can't do what you need, you may want to write a
238 [compiler plugin](plugins.html) instead. Compared to `macro_rules!`
239 macros, this is significantly more work, the interfaces are much less stable,
240 and bugs can be much harder to track down. In exchange you get the
241 flexibility of running arbitrary Rust code within the compiler. Syntax
242 extension plugins are sometimes called *procedural macros* for this reason.