Skip to content

Commit

Permalink
Fix text in TRPL macros guide to match the example
Browse files Browse the repository at this point in the history
Also, update description of macro invocation syntax:
after rust-lang#20563 there is
a number of additional limitations on macro syntax.
  • Loading branch information
alkor committed Jan 12, 2015
1 parent 0aec4db commit a3d87fa
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/doc/trpl/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ the pattern in the above code:
# let input_1 = T::SpecialA(0);
# let input_2 = T::SpecialA(0);
macro_rules! early_return {
($inp:expr, $sp:path) => ( // invoke it like `(input_5 SpecialE)`
($inp:expr, $sp:path) => ( // invoke it like `(input_5, SpecialE)`
match $inp {
$sp(x) => { return x; }
_ => {}
Expand All @@ -59,7 +59,7 @@ early_return!(input_2, T::SpecialB);
~~~~

Macros are defined in pattern-matching style: in the above example, the text
`($inp:expr $sp:ident)` that appears on the left-hand side of the `=>` is the
`($inp:expr, $sp:path)` that appears on the left-hand side of the `=>` is the
*macro invocation syntax*, a pattern denoting how to write a call to the
macro. The text on the right-hand side of the `=>`, beginning with `match
$inp`, is the *macro transcription syntax*: what the macro expands to.
Expand All @@ -74,6 +74,8 @@ conforms to the following rules:
2. `$` has special meaning (described below).
3. The `()`s, `[]`s, and `{}`s it contains must balance. For example, `([)` is
forbidden.
4. Some arguments can be followed only by a limited set of separators, to
avoid ambiguity (described below).

Otherwise, the invocation syntax is free-form.

Expand All @@ -86,7 +88,8 @@ To take a fragment of Rust code as an argument, write `$` followed by a name
`foo`.)
* `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`;
`f(42)`.)
* `ty` (a type. Examples: `int`, `Vec<(char, String)>`, `&T`.)
* `ty` (a type. Examples: `i32`, `Vec<(char, String)>`, `&T`.)
* `path` (a path to struct or enum variant. Example: `T::SpecialA`)
* `pat` (a pattern, usually appearing in a `match` or on the left-hand side of
a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.)
* `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`)
Expand All @@ -97,6 +100,12 @@ rules of tokenization apply,
So `($x:ident -> (($e:expr)))`, though excessively fancy, would designate a macro
that could be invoked like: `my_macro!(i->(( 2+2 )))`.

To avoid ambiguity, macro invocation syntax must conform to the following rules:
* `expr` must be followed by `=>`, `,` or `;`.
* `ty` and `path` must be followed by `=>`, `,`, `:`, `=`, `>` or `as`.
* `pat` must be followed by `=>`, `,` or `=`.
* `ident` and `block` can be followed by any token.

## Invocation location

A macro invocation may take the place of (and therefore expand to) an
Expand Down

1 comment on commit a3d87fa

@steveklabnik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+ rollup

Please sign in to comment.