13
13
/// }
14
14
/// ```
15
15
///
16
+ /// To specify a different name for the resulting Rust item, a name can be
17
+ /// provided:
18
+ ///
19
+ /// ```
20
+ /// syn::custom_keyword!(whatever as Whatever);
21
+ /// ```
22
+ ///
16
23
/// The generated syntax tree node supports the following operations just like
17
24
/// any built-in keyword token.
18
25
///
89
96
#[ macro_export]
90
97
macro_rules! custom_keyword {
91
98
( $ident: ident) => {
99
+ $crate:: custom_keyword!{ $ident as $ident}
100
+ } ;
101
+ ( $keyword: ident as $ident: ident) => {
92
102
#[ allow( non_camel_case_types) ]
93
103
pub struct $ident {
94
104
#[ allow( dead_code) ]
@@ -114,10 +124,10 @@ macro_rules! custom_keyword {
114
124
}
115
125
}
116
126
117
- $crate:: impl_parse_for_custom_keyword!( $ident) ;
118
- $crate:: impl_to_tokens_for_custom_keyword!( $ident) ;
127
+ $crate:: impl_parse_for_custom_keyword!( $keyword as $ ident) ;
128
+ $crate:: impl_to_tokens_for_custom_keyword!( $keyword as $ ident) ;
119
129
$crate:: impl_clone_for_custom_keyword!( $ident) ;
120
- $crate:: impl_extra_traits_for_custom_keyword!( $ident) ;
130
+ $crate:: impl_extra_traits_for_custom_keyword!( $keyword as $ ident) ;
121
131
} ;
122
132
} ;
123
133
}
@@ -127,33 +137,33 @@ macro_rules! custom_keyword {
127
137
#[ doc( hidden) ]
128
138
#[ macro_export]
129
139
macro_rules! impl_parse_for_custom_keyword {
130
- ( $ident: ident) => {
140
+ ( $keyword : ident as $ ident: ident) => {
131
141
// For peek.
132
142
impl $crate:: __private:: CustomToken for $ident {
133
143
fn peek( cursor: $crate:: buffer:: Cursor ) -> $crate:: __private:: bool {
134
144
if let $crate:: __private:: Some ( ( ident, _rest) ) = cursor. ident( ) {
135
- ident == $crate:: __private:: stringify!( $ident )
145
+ ident == $crate:: __private:: stringify!( $keyword )
136
146
} else {
137
147
false
138
148
}
139
149
}
140
150
141
151
fn display( ) -> & ' static $crate:: __private:: str {
142
- $crate:: __private:: concat!( "`" , $crate:: __private:: stringify!( $ident ) , "`" )
152
+ $crate:: __private:: concat!( "`" , $crate:: __private:: stringify!( $keyword ) , "`" )
143
153
}
144
154
}
145
155
146
156
impl $crate:: parse:: Parse for $ident {
147
157
fn parse( input: $crate:: parse:: ParseStream ) -> $crate:: parse:: Result <$ident> {
148
158
input. step( |cursor| {
149
159
if let $crate:: __private:: Some ( ( ident, rest) ) = cursor. ident( ) {
150
- if ident == $crate:: __private:: stringify!( $ident ) {
160
+ if ident == $crate:: __private:: stringify!( $keyword ) {
151
161
return $crate:: __private:: Ok ( ( $ident { span: ident. span( ) } , rest) ) ;
152
162
}
153
163
}
154
164
$crate:: __private:: Err ( cursor. error( $crate:: __private:: concat!(
155
165
"expected `" ,
156
- $crate:: __private:: stringify!( $ident ) ,
166
+ $crate:: __private:: stringify!( $keyword ) ,
157
167
"`" ,
158
168
) ) )
159
169
} )
@@ -167,18 +177,18 @@ macro_rules! impl_parse_for_custom_keyword {
167
177
#[ doc( hidden) ]
168
178
#[ macro_export]
169
179
macro_rules! impl_parse_for_custom_keyword {
170
- ( $ident: ident) => { } ;
180
+ ( $keyword : ident as $ ident: ident) => { } ;
171
181
}
172
182
173
183
// Not public API.
174
184
#[ cfg( feature = "printing" ) ]
175
185
#[ doc( hidden) ]
176
186
#[ macro_export]
177
187
macro_rules! impl_to_tokens_for_custom_keyword {
178
- ( $ident: ident) => {
188
+ ( $keyword : ident as $ ident: ident) => {
179
189
impl $crate:: __private:: ToTokens for $ident {
180
190
fn to_tokens( & self , tokens: & mut $crate:: __private:: TokenStream2 ) {
181
- let ident = $crate:: Ident :: new( $crate:: __private:: stringify!( $ident ) , self . span) ;
191
+ let ident = $crate:: Ident :: new( $crate:: __private:: stringify!( $keyword ) , self . span) ;
182
192
$crate:: __private:: TokenStreamExt :: append( tokens, ident) ;
183
193
}
184
194
}
@@ -190,7 +200,7 @@ macro_rules! impl_to_tokens_for_custom_keyword {
190
200
#[ doc( hidden) ]
191
201
#[ macro_export]
192
202
macro_rules! impl_to_tokens_for_custom_keyword {
193
- ( $ident: ident) => { } ;
203
+ ( $keyword : ident as $ ident: ident) => { } ;
194
204
}
195
205
196
206
// Not public API.
@@ -223,14 +233,14 @@ macro_rules! impl_clone_for_custom_keyword {
223
233
#[ doc( hidden) ]
224
234
#[ macro_export]
225
235
macro_rules! impl_extra_traits_for_custom_keyword {
226
- ( $ident: ident) => {
236
+ ( $keyword : ident as $ ident: ident) => {
227
237
impl $crate:: __private:: Debug for $ident {
228
238
fn fmt( & self , f: & mut $crate:: __private:: Formatter ) -> $crate:: __private:: FmtResult {
229
239
$crate:: __private:: Formatter :: write_str(
230
240
f,
231
241
$crate:: __private:: concat!(
232
242
"Keyword [" ,
233
- $crate:: __private:: stringify!( $ident ) ,
243
+ $crate:: __private:: stringify!( $keyword ) ,
234
244
"]" ,
235
245
) ,
236
246
)
@@ -256,5 +266,5 @@ macro_rules! impl_extra_traits_for_custom_keyword {
256
266
#[ doc( hidden) ]
257
267
#[ macro_export]
258
268
macro_rules! impl_extra_traits_for_custom_keyword {
259
- ( $ident: ident) => { } ;
269
+ ( $keyword : ident as $ ident: ident) => { } ;
260
270
}
0 commit comments