]> git.proxmox.com Git - rustc.git/blame - src/doc/trpl/advanced-macros.md
Imported Upstream version 1.0.0~beta
[rustc.git] / src / doc / trpl / advanced-macros.md
CommitLineData
85aaf69f
SL
1% Advanced macros
2
3This chapter picks up where the [introductory macro chapter](macros.html) left
4off.
5
6# Syntactic requirements
7
8Even when Rust code contains un-expanded macros, it can be parsed as a full
c34b1796
AL
9[syntax tree][ast]. This property can be very useful for editors and other
10tools that process code. It also has a few consequences for the design of
11Rust's macro system.
12
13[ast]: glossary.html#abstract-syntax-tree
85aaf69f
SL
14
15One consequence is that Rust must determine, when it parses a macro invocation,
16whether the macro stands in for
17
18* zero or more items,
19* zero or more methods,
20* an expression,
21* a statement, or
22* a pattern.
23
24A macro invocation within a block could stand for some items, or for an
25expression / statement. Rust uses a simple rule to resolve this ambiguity. A
26macro invocation that stands for items must be either
27
28* delimited by curly braces, e.g. `foo! { ... }`, or
29* terminated by a semicolon, e.g. `foo!(...);`
30
31Another consequence of pre-expansion parsing is that the macro invocation must
32consist of valid Rust tokens. Furthermore, parentheses, brackets, and braces
33must be balanced within a macro invocation. For example, `foo!([)` is
34forbidden. This allows Rust to know where the macro invocation ends.
35
36More formally, the macro invocation body must be a sequence of *token trees*.
37A token tree is defined recursively as either
38
39* a sequence of token trees surrounded by matching `()`, `[]`, or `{}`, or
40* any other single token.
41
42Within a matcher, each metavariable has a *fragment specifier*, identifying
43which syntactic form it matches.
44
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.
56
57There are additional rules regarding the next token after a metavariable:
58
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.
63
64These rules provide some flexibility for Rust's syntax to evolve without
65breaking existing macros.
66
67The macro system does not deal with parse ambiguity at all. For example, the
68grammar `$($t:ty)* $e:expr` will always fail to parse, because the parser would
69be forced to choose between parsing `$t` and parsing `$e`. Changing the
70invocation syntax to put a distinctive token in front can solve the problem. In
71this case, you can write `$(T $t:ty)* E $e:exp`.
72
73[item]: ../reference.html#items
74
75# Scoping and macro import/export
76
77Macros are expanded at an early stage in compilation, before name resolution.
78One downside is that scoping works differently for macros, compared to other
79constructs in the language.
80
81Definition and expansion of macros both happen in a single depth-first,
82lexical-order traversal of a crate's source. So a macro defined at module scope
83is visible to any subsequent code in the same module, which includes the body
84of any subsequent child `mod` items.
85
86A macro defined within the body of a single `fn`, or anywhere else not at
87module scope, is visible only within that item.
88
89If a module has the `macro_use` attribute, its macros are also visible in its
90parent module after the child's `mod` item. If the parent also has `macro_use`
91then the macros will be visible in the grandparent after the parent's `mod`
92item, and so forth.
93
94The `macro_use` attribute can also appear on `extern crate`. In this context
95it controls which macros are loaded from the external crate, e.g.
96
97```rust,ignore
98#[macro_use(foo, bar)]
99extern crate baz;
100```
101
102If the attribute is given simply as `#[macro_use]`, all macros are loaded. If
103there is no `#[macro_use]` attribute then no macros are loaded. Only macros
104defined with the `#[macro_export]` attribute may be loaded.
105
106To load a crate's macros *without* linking it into the output, use `#[no_link]`
107as well.
108
109An example:
110
111```rust
112macro_rules! m1 { () => (()) }
113
114// visible here: m1
115
116mod foo {
117 // visible here: m1
118
119 #[macro_export]
120 macro_rules! m2 { () => (()) }
121
122 // visible here: m1, m2
123}
124
125// visible here: m1
126
127macro_rules! m3 { () => (()) }
128
129// visible here: m1, m3
130
131#[macro_use]
132mod bar {
133 // visible here: m1, m3
134
135 macro_rules! m4 { () => (()) }
136
137 // visible here: m1, m3, m4
138}
139
140// visible here: m1, m3, m4
141# fn main() { }
142```
143
144When this library is loaded with `#[macro_use] extern crate`, only `m2` will
145be imported.
146
147The Rust Reference has a [listing of macro-related
148attributes](../reference.html#macro--and-plugin-related-attributes).
149
150# The variable `$crate`
151
152A further difficulty occurs when a macro is used in multiple crates. Say that
153`mylib` defines
154
155```rust
156pub fn increment(x: u32) -> u32 {
157 x + 1
158}
159
160#[macro_export]
161macro_rules! inc_a {
162 ($x:expr) => ( ::increment($x) )
163}
164
165#[macro_export]
166macro_rules! inc_b {
167 ($x:expr) => ( ::mylib::increment($x) )
168}
169# fn main() { }
170```
171
172`inc_a` only works within `mylib`, while `inc_b` only works outside the
173library. Furthermore, `inc_b` will break if the user imports `mylib` under
174another name.
175
176Rust does not (yet) have a hygiene system for crate references, but it does
177provide a simple workaround for this problem. Within a macro imported from a
178crate named `foo`, the special macro variable `$crate` will expand to `::foo`.
179By contrast, when a macro is defined and then used in the same crate, `$crate`
180will expand to nothing. This means we can write
181
182```rust
183#[macro_export]
184macro_rules! inc {
185 ($x:expr) => ( $crate::increment($x) )
186}
187# fn main() { }
188```
189
190to define a single macro that works both inside and outside our library. The
191function name will expand to either `::increment` or `::mylib::increment`.
192
193To keep this system simple and correct, `#[macro_use] extern crate ...` may
194only appear at the root of your crate, not inside `mod`. This ensures that
195`$crate` is a single identifier.
196
c34b1796
AL
197# The deep end
198
199The introductory chapter mentioned recursive macros, but it did not give the
200full story. Recursive macros are useful for another reason: Each recursive
201invocation gives you another opportunity to pattern-match the macro's
202arguments.
203
204As an extreme example, it is possible, though hardly advisable, to implement
205the [Bitwise Cyclic Tag](http://esolangs.org/wiki/Bitwise_Cyclic_Tag) automaton
206within Rust's macro system.
207
208```rust
209macro_rules! bct {
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),*));
215
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));
221
222 // cmd 1p: 0 ... => 0 ...
223 (1, $p:tt, $($ps:tt),* ; $($ds:tt),*)
224 => (bct!($($ps),*, 1, $p ; $($ds),*));
225
226 // halt on empty data string
227 ( $($ps:tt),* ; )
228 => (());
229}
230```
231
232Exercise: use macros to reduce duplication in the above definition of the
233`bct!` macro.
85aaf69f 234
c34b1796 235# Procedural macros
85aaf69f
SL
236
237If 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!`
239macros, this is significantly more work, the interfaces are much less stable,
c34b1796 240and bugs can be much harder to track down. In exchange you get the
85aaf69f
SL
241flexibility of running arbitrary Rust code within the compiler. Syntax
242extension plugins are sometimes called *procedural macros* for this reason.