@@ -42,7 +42,7 @@ the pattern in the above code:
42
42
# let input_1 = T::SpecialA(0);
43
43
# let input_2 = T::SpecialA(0);
44
44
macro_rules! early_return {
45
- ($inp:expr, $sp:path) => ( // invoke it like `(input_5 SpecialE)`
45
+ ($inp:expr, $sp:path) => ( // invoke it like `(input_5, SpecialE)`
46
46
match $inp {
47
47
$sp(x) => { return x; }
48
48
_ => {}
@@ -59,7 +59,7 @@ early_return!(input_2, T::SpecialB);
59
59
~~~~
60
60
61
61
Macros are defined in pattern-matching style: in the above example, the text
62
- ` ($inp:expr $sp:ident ) ` that appears on the left-hand side of the ` => ` is the
62
+ ` ($inp:expr, $sp:path ) ` that appears on the left-hand side of the ` => ` is the
63
63
* macro invocation syntax* , a pattern denoting how to write a call to the
64
64
macro. The text on the right-hand side of the ` => ` , beginning with `match
65
65
$inp`, is the * macro transcription syntax* : what the macro expands to.
@@ -74,6 +74,8 @@ conforms to the following rules:
74
74
2 . ` $ ` has special meaning (described below).
75
75
3 . The ` () ` s, ` [] ` s, and ` {} ` s it contains must balance. For example, ` ([) ` is
76
76
forbidden.
77
+ 4 . Some arguments can be followed only by a limited set of separators, to
78
+ avoid ambiguity (described below).
77
79
78
80
Otherwise, the invocation syntax is free-form.
79
81
@@ -86,7 +88,8 @@ To take a fragment of Rust code as an argument, write `$` followed by a name
86
88
` foo ` .)
87
89
* ` expr ` (an expression. Examples: ` 2 + 2 ` ; ` if true then { 1 } else { 2 } ` ;
88
90
` f(42) ` .)
89
- * ` ty ` (a type. Examples: ` int ` , ` Vec<(char, String)> ` , ` &T ` .)
91
+ * ` ty ` (a type. Examples: ` i32 ` , ` Vec<(char, String)> ` , ` &T ` .)
92
+ * ` path ` (a path to struct or enum variant. Example: ` T::SpecialA ` )
90
93
* ` pat ` (a pattern, usually appearing in a ` match ` or on the left-hand side of
91
94
a declaration. Examples: ` Some(t) ` ; ` (17, 'a') ` ; ` _ ` .)
92
95
* ` block ` (a sequence of actions. Example: ` { log(error, "hi"); return 12; } ` )
@@ -97,6 +100,12 @@ rules of tokenization apply,
97
100
So ` ($x:ident -> (($e:expr))) ` , though excessively fancy, would designate a macro
98
101
that could be invoked like: ` my_macro!(i->(( 2+2 ))) ` .
99
102
103
+ To avoid ambiguity, macro invocation syntax must conform to the following rules:
104
+ * ` expr ` must be followed by ` => ` , ` , ` or ` ; ` .
105
+ * ` ty ` and ` path ` must be followed by ` => ` , ` , ` , ` : ` , ` = ` , ` > ` or ` as ` .
106
+ * ` pat ` must be followed by ` => ` , ` , ` or ` = ` .
107
+ * ` ident ` and ` block ` can be followed by any token.
108
+
100
109
## Invocation location
101
110
102
111
A macro invocation may take the place of (and therefore expand to) an
0 commit comments