@@ -26,7 +26,7 @@ use syntax::parse::parser::Parser;
26
26
use syntax:: parse:: token:: { BinOpToken , DelimToken , Token } ;
27
27
use syntax:: print:: pprust;
28
28
use syntax:: source_map:: { BytePos , Span } ;
29
- use syntax:: symbol;
29
+ use syntax:: symbol:: keywords ;
30
30
use syntax:: tokenstream:: { Cursor , TokenStream , TokenTree } ;
31
31
use syntax:: ThinVec ;
32
32
use syntax:: { ast, parse, ptr} ;
@@ -64,6 +64,7 @@ pub enum MacroArg {
64
64
Ty ( ptr:: P < ast:: Ty > ) ,
65
65
Pat ( ptr:: P < ast:: Pat > ) ,
66
66
Item ( ptr:: P < ast:: Item > ) ,
67
+ Keyword ( ast:: Ident , Span ) ,
67
68
}
68
69
69
70
impl MacroArg {
@@ -92,6 +93,7 @@ impl Rewrite for MacroArg {
92
93
MacroArg :: Ty ( ref ty) => ty. rewrite ( context, shape) ,
93
94
MacroArg :: Pat ( ref pat) => pat. rewrite ( context, shape) ,
94
95
MacroArg :: Item ( ref item) => item. rewrite ( context, shape) ,
96
+ MacroArg :: Keyword ( ident, _) => Some ( ident. to_string ( ) ) ,
95
97
}
96
98
}
97
99
}
@@ -156,7 +158,7 @@ fn rewrite_macro_name(
156
158
format ! ( "{}!" , path)
157
159
} ;
158
160
match extra_ident {
159
- Some ( ident) if ident != symbol :: keywords:: Invalid . ident ( ) => format ! ( "{} {}" , name, ident) ,
161
+ Some ( ident) if ident != keywords:: Invalid . ident ( ) => format ! ( "{} {}" , name, ident) ,
160
162
_ => name,
161
163
}
162
164
}
@@ -224,6 +226,23 @@ pub fn rewrite_macro(
224
226
result
225
227
}
226
228
229
+ fn check_keyword < ' a , ' b : ' a > ( parser : & ' a mut Parser < ' b > ) -> Option < MacroArg > {
230
+ for & keyword in RUST_KEYWORDS . iter ( ) {
231
+ if parser. token . is_keyword ( keyword)
232
+ && parser. look_ahead ( 1 , |t| {
233
+ * t == Token :: Eof
234
+ || * t == Token :: Comma
235
+ || * t == Token :: CloseDelim ( DelimToken :: NoDelim )
236
+ } )
237
+ {
238
+ let macro_arg = MacroArg :: Keyword ( keyword. ident ( ) , parser. span ) ;
239
+ parser. bump ( ) ;
240
+ return Some ( macro_arg) ;
241
+ }
242
+ }
243
+ None
244
+ }
245
+
227
246
pub fn rewrite_macro_inner (
228
247
mac : & ast:: Mac ,
229
248
extra_ident : Option < ast:: Ident > ,
@@ -276,11 +295,12 @@ pub fn rewrite_macro_inner(
276
295
277
296
if DelimToken :: Brace != style {
278
297
loop {
279
- match parse_macro_arg ( & mut parser) {
280
- Some ( arg) => arg_vec. push ( arg) ,
281
- None => {
282
- return return_macro_parse_failure_fallback ( context, shape. indent , mac. span ) ;
283
- }
298
+ if let Some ( arg) = parse_macro_arg ( & mut parser) {
299
+ arg_vec. push ( arg) ;
300
+ } else if let Some ( arg) = check_keyword ( & mut parser) {
301
+ arg_vec. push ( arg) ;
302
+ } else {
303
+ return return_macro_parse_failure_fallback ( context, shape. indent , mac. span ) ;
284
304
}
285
305
286
306
match parser. token {
@@ -1373,8 +1393,8 @@ fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream)
1373
1393
while parser. token != Token :: Eof {
1374
1394
// Parse a `lazy_static!` item.
1375
1395
let vis = crate :: utils:: format_visibility ( context, & parse_or ! ( parse_visibility, false ) ) ;
1376
- parser. eat_keyword ( symbol :: keywords:: Static ) ;
1377
- parser. eat_keyword ( symbol :: keywords:: Ref ) ;
1396
+ parser. eat_keyword ( keywords:: Static ) ;
1397
+ parser. eat_keyword ( keywords:: Ref ) ;
1378
1398
let id = parse_or ! ( parse_ident) ;
1379
1399
parser. eat ( & Token :: Colon ) ;
1380
1400
let ty = parse_or ! ( parse_ty) ;
@@ -1449,3 +1469,66 @@ fn rewrite_macro_with_items(
1449
1469
result. push_str ( trailing_semicolon) ;
1450
1470
Some ( result)
1451
1471
}
1472
+
1473
+ const RUST_KEYWORDS : [ keywords:: Keyword ; 60 ] = [
1474
+ keywords:: PathRoot ,
1475
+ keywords:: DollarCrate ,
1476
+ keywords:: Underscore ,
1477
+ keywords:: As ,
1478
+ keywords:: Box ,
1479
+ keywords:: Break ,
1480
+ keywords:: Const ,
1481
+ keywords:: Continue ,
1482
+ keywords:: Crate ,
1483
+ keywords:: Else ,
1484
+ keywords:: Enum ,
1485
+ keywords:: Extern ,
1486
+ keywords:: False ,
1487
+ keywords:: Fn ,
1488
+ keywords:: For ,
1489
+ keywords:: If ,
1490
+ keywords:: Impl ,
1491
+ keywords:: In ,
1492
+ keywords:: Let ,
1493
+ keywords:: Loop ,
1494
+ keywords:: Match ,
1495
+ keywords:: Mod ,
1496
+ keywords:: Move ,
1497
+ keywords:: Mut ,
1498
+ keywords:: Pub ,
1499
+ keywords:: Ref ,
1500
+ keywords:: Return ,
1501
+ keywords:: SelfLower ,
1502
+ keywords:: SelfUpper ,
1503
+ keywords:: Static ,
1504
+ keywords:: Struct ,
1505
+ keywords:: Super ,
1506
+ keywords:: Trait ,
1507
+ keywords:: True ,
1508
+ keywords:: Type ,
1509
+ keywords:: Unsafe ,
1510
+ keywords:: Use ,
1511
+ keywords:: Where ,
1512
+ keywords:: While ,
1513
+ keywords:: Abstract ,
1514
+ keywords:: Become ,
1515
+ keywords:: Do ,
1516
+ keywords:: Final ,
1517
+ keywords:: Macro ,
1518
+ keywords:: Override ,
1519
+ keywords:: Priv ,
1520
+ keywords:: Typeof ,
1521
+ keywords:: Unsized ,
1522
+ keywords:: Virtual ,
1523
+ keywords:: Yield ,
1524
+ keywords:: Dyn ,
1525
+ keywords:: Async ,
1526
+ keywords:: Try ,
1527
+ keywords:: UnderscoreLifetime ,
1528
+ keywords:: StaticLifetime ,
1529
+ keywords:: Auto ,
1530
+ keywords:: Catch ,
1531
+ keywords:: Default ,
1532
+ keywords:: Existential ,
1533
+ keywords:: Union ,
1534
+ ] ;
0 commit comments