@@ -90,8 +90,8 @@ use codemap::Spanned;
90
90
use errors:: FatalError ;
91
91
use ext:: tt:: quoted:: { self , TokenTree } ;
92
92
use parse:: { Directory , ParseSess } ;
93
- use parse:: parser:: { PathStyle , Parser } ;
94
- use parse:: token:: { self , DocComment , Token , Nonterminal } ;
93
+ use parse:: parser:: { Parser , PathStyle } ;
94
+ use parse:: token:: { self , DocComment , Nonterminal , Token } ;
95
95
use print:: pprust;
96
96
use symbol:: keywords;
97
97
use tokenstream:: TokenStream ;
@@ -100,7 +100,7 @@ use util::small_vector::SmallVector;
100
100
use std:: mem;
101
101
use std:: rc:: Rc ;
102
102
use std:: collections:: HashMap ;
103
- use std:: collections:: hash_map:: Entry :: { Vacant , Occupied } ;
103
+ use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
104
104
105
105
// To avoid costly uniqueness checks, we require that `MatchSeq` always has
106
106
// a nonempty body.
@@ -182,7 +182,7 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
182
182
match_lo : 0 ,
183
183
match_cur : 0 ,
184
184
match_hi : match_idx_hi,
185
- sp_lo : lo
185
+ sp_lo : lo,
186
186
} )
187
187
}
188
188
@@ -206,25 +206,27 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
206
206
#[ derive( Debug , Clone ) ]
207
207
pub enum NamedMatch {
208
208
MatchedSeq ( Rc < Vec < NamedMatch > > , syntax_pos:: Span ) ,
209
- MatchedNonterminal ( Rc < Nonterminal > )
209
+ MatchedNonterminal ( Rc < Nonterminal > ) ,
210
210
}
211
211
212
- fn nameize < I : Iterator < Item =NamedMatch > > ( sess : & ParseSess , ms : & [ TokenTree ] , mut res : I )
213
- -> NamedParseResult {
214
- fn n_rec < I : Iterator < Item =NamedMatch > > ( sess : & ParseSess , m : & TokenTree , res : & mut I ,
215
- ret_val : & mut HashMap < Ident , Rc < NamedMatch > > )
216
- -> Result < ( ) , ( syntax_pos:: Span , String ) > {
212
+ fn nameize < I : Iterator < Item = NamedMatch > > (
213
+ sess : & ParseSess ,
214
+ ms : & [ TokenTree ] ,
215
+ mut res : I ,
216
+ ) -> NamedParseResult {
217
+ fn n_rec < I : Iterator < Item = NamedMatch > > (
218
+ sess : & ParseSess ,
219
+ m : & TokenTree ,
220
+ res : & mut I ,
221
+ ret_val : & mut HashMap < Ident , Rc < NamedMatch > > ,
222
+ ) -> Result < ( ) , ( syntax_pos:: Span , String ) > {
217
223
match * m {
218
- TokenTree :: Sequence ( _, ref seq) => {
219
- for next_m in & seq. tts {
220
- n_rec ( sess, next_m, res. by_ref ( ) , ret_val) ?
221
- }
222
- }
223
- TokenTree :: Delimited ( _, ref delim) => {
224
- for next_m in & delim. tts {
225
- n_rec ( sess, next_m, res. by_ref ( ) , ret_val) ?;
226
- }
227
- }
224
+ TokenTree :: Sequence ( _, ref seq) => for next_m in & seq. tts {
225
+ n_rec ( sess, next_m, res. by_ref ( ) , ret_val) ?
226
+ } ,
227
+ TokenTree :: Delimited ( _, ref delim) => for next_m in & delim. tts {
228
+ n_rec ( sess, next_m, res. by_ref ( ) , ret_val) ?;
229
+ } ,
228
230
TokenTree :: MetaVarDecl ( span, _, id) if id. name == keywords:: Invalid . name ( ) => {
229
231
if sess. missing_fragment_specifiers . borrow_mut ( ) . remove ( & span) {
230
232
return Err ( ( span, "missing fragment specifier" . to_string ( ) ) ) ;
@@ -250,7 +252,7 @@ fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut
250
252
let mut ret_val = HashMap :: new ( ) ;
251
253
for m in ms {
252
254
match n_rec ( sess, m, res. by_ref ( ) , & mut ret_val) {
253
- Ok ( _) => { } ,
255
+ Ok ( _) => { }
254
256
Err ( ( sp, msg) ) => return Error ( sp, msg) ,
255
257
}
256
258
}
@@ -265,18 +267,21 @@ pub enum ParseResult<T> {
265
267
/// indicates that no rules expected the given token.
266
268
Failure ( syntax_pos:: Span , Token ) ,
267
269
/// Fatal error (malformed macro?). Abort compilation.
268
- Error ( syntax_pos:: Span , String )
270
+ Error ( syntax_pos:: Span , String ) ,
269
271
}
270
272
271
273
pub fn parse_failure_msg ( tok : Token ) -> String {
272
274
match tok {
273
275
token:: Eof => "unexpected end of macro invocation" . to_string ( ) ,
274
- _ => format ! ( "no rules expected the token `{}`" , pprust:: token_to_string( & tok) ) ,
276
+ _ => format ! (
277
+ "no rules expected the token `{}`" ,
278
+ pprust:: token_to_string( & tok)
279
+ ) ,
275
280
}
276
281
}
277
282
278
283
/// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
279
- fn token_name_eq ( t1 : & Token , t2 : & Token ) -> bool {
284
+ fn token_name_eq ( t1 : & Token , t2 : & Token ) -> bool {
280
285
if let ( Some ( id1) , Some ( id2) ) = ( t1. ident ( ) , t2. ident ( ) ) {
281
286
id1. name == id2. name
282
287
} else if let ( & token:: Lifetime ( id1) , & token:: Lifetime ( id2) ) = ( t1, t2) {
@@ -290,14 +295,15 @@ fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
290
295
( 0 ..len) . into_iter ( ) . map ( |_| Rc :: new ( Vec :: new ( ) ) ) . collect ( )
291
296
}
292
297
293
- fn inner_parse_loop ( sess : & ParseSess ,
294
- cur_items : & mut SmallVector < Box < MatcherPos > > ,
295
- next_items : & mut Vec < Box < MatcherPos > > ,
296
- eof_items : & mut SmallVector < Box < MatcherPos > > ,
297
- bb_items : & mut SmallVector < Box < MatcherPos > > ,
298
- token : & Token ,
299
- span : syntax_pos:: Span )
300
- -> ParseResult < ( ) > {
298
+ fn inner_parse_loop (
299
+ sess : & ParseSess ,
300
+ cur_items : & mut SmallVector < Box < MatcherPos > > ,
301
+ next_items : & mut Vec < Box < MatcherPos > > ,
302
+ eof_items : & mut SmallVector < Box < MatcherPos > > ,
303
+ bb_items : & mut SmallVector < Box < MatcherPos > > ,
304
+ token : & Token ,
305
+ span : syntax_pos:: Span ,
306
+ ) -> ParseResult < ( ) > {
301
307
while let Some ( mut item) = cur_items. pop ( ) {
302
308
// When unzipped trees end, remove them
303
309
while item. idx >= item. top_elts . len ( ) {
@@ -306,7 +312,7 @@ fn inner_parse_loop(sess: &ParseSess,
306
312
item. top_elts = elts;
307
313
item. idx = idx + 1 ;
308
314
}
309
- None => break
315
+ None => break ,
310
316
}
311
317
}
312
318
@@ -341,11 +347,16 @@ fn inner_parse_loop(sess: &ParseSess,
341
347
// Check if we need a separator
342
348
if idx == len && item. sep . is_some ( ) {
343
349
// We have a separator, and it is the current token.
344
- if item. sep . as_ref ( ) . map ( |sep| token_name_eq ( token, sep) ) . unwrap_or ( false ) {
350
+ if item. sep
351
+ . as_ref ( )
352
+ . map ( |sep| token_name_eq ( token, sep) )
353
+ . unwrap_or ( false )
354
+ {
345
355
item. idx += 1 ;
346
356
next_items. push ( item) ;
347
357
}
348
- } else { // we don't need a separator
358
+ } else {
359
+ // we don't need a separator
349
360
item. match_cur = item. match_lo ;
350
361
item. idx = 0 ;
351
362
cur_items. push ( item) ;
@@ -418,12 +429,13 @@ fn inner_parse_loop(sess: &ParseSess,
418
429
Success ( ( ) )
419
430
}
420
431
421
- pub fn parse ( sess : & ParseSess ,
422
- tts : TokenStream ,
423
- ms : & [ TokenTree ] ,
424
- directory : Option < Directory > ,
425
- recurse_into_modules : bool )
426
- -> NamedParseResult {
432
+ pub fn parse (
433
+ sess : & ParseSess ,
434
+ tts : TokenStream ,
435
+ ms : & [ TokenTree ] ,
436
+ directory : Option < Directory > ,
437
+ recurse_into_modules : bool ,
438
+ ) -> NamedParseResult {
427
439
let mut parser = Parser :: new ( sess, tts, directory, recurse_into_modules, true ) ;
428
440
let mut cur_items = SmallVector :: one ( initial_matcher_pos ( ms. to_owned ( ) , parser. span . lo ( ) ) ) ;
429
441
let mut next_items = Vec :: new ( ) ; // or proceed normally
@@ -433,9 +445,16 @@ pub fn parse(sess: &ParseSess,
433
445
let mut eof_items = SmallVector :: new ( ) ;
434
446
assert ! ( next_items. is_empty( ) ) ;
435
447
436
- match inner_parse_loop ( sess, & mut cur_items, & mut next_items, & mut eof_items, & mut bb_items,
437
- & parser. token , parser. span ) {
438
- Success ( _) => { } ,
448
+ match inner_parse_loop (
449
+ sess,
450
+ & mut cur_items,
451
+ & mut next_items,
452
+ & mut eof_items,
453
+ & mut bb_items,
454
+ & parser. token ,
455
+ parser. span ,
456
+ ) {
457
+ Success ( _) => { }
439
458
Failure ( sp, tok) => return Failure ( sp, tok) ,
440
459
Error ( sp, msg) => return Error ( sp, msg) ,
441
460
}
@@ -446,43 +465,56 @@ pub fn parse(sess: &ParseSess,
446
465
/* error messages here could be improved with links to orig. rules */
447
466
if token_name_eq ( & parser. token , & token:: Eof ) {
448
467
if eof_items. len ( ) == 1 {
449
- let matches = eof_items[ 0 ] . matches . iter_mut ( ) . map ( |dv| {
450
- Rc :: make_mut ( dv) . pop ( ) . unwrap ( )
451
- } ) ;
468
+ let matches = eof_items[ 0 ]
469
+ . matches
470
+ . iter_mut ( )
471
+ . map ( |dv| Rc :: make_mut ( dv) . pop ( ) . unwrap ( ) ) ;
452
472
return nameize ( sess, ms, matches) ;
453
473
} else if eof_items. len ( ) > 1 {
454
- return Error ( parser. span , "ambiguity: multiple successful parses" . to_string ( ) ) ;
474
+ return Error (
475
+ parser. span ,
476
+ "ambiguity: multiple successful parses" . to_string ( ) ,
477
+ ) ;
455
478
} else {
456
479
return Failure ( parser. span , token:: Eof ) ;
457
480
}
458
481
} else if ( !bb_items. is_empty ( ) && !next_items. is_empty ( ) ) || bb_items. len ( ) > 1 {
459
- let nts = bb_items. iter ( ) . map ( |item| match item. top_elts . get_tt ( item. idx ) {
460
- TokenTree :: MetaVarDecl ( _, bind, name) => {
461
- format ! ( "{} ('{}')" , name, bind)
462
- }
463
- _ => panic ! ( )
464
- } ) . collect :: < Vec < String > > ( ) . join ( " or " ) ;
465
-
466
- return Error ( parser. span , format ! (
467
- "local ambiguity: multiple parsing options: {}" ,
468
- match next_items. len( ) {
469
- 0 => format!( "built-in NTs {}." , nts) ,
470
- 1 => format!( "built-in NTs {} or 1 other option." , nts) ,
471
- n => format!( "built-in NTs {} or {} other options." , nts, n) ,
472
- }
473
- ) ) ;
482
+ let nts = bb_items
483
+ . iter ( )
484
+ . map ( |item| match item. top_elts . get_tt ( item. idx ) {
485
+ TokenTree :: MetaVarDecl ( _, bind, name) => format ! ( "{} ('{}')" , name, bind) ,
486
+ _ => panic ! ( ) ,
487
+ } )
488
+ . collect :: < Vec < String > > ( )
489
+ . join ( " or " ) ;
490
+
491
+ return Error (
492
+ parser. span ,
493
+ format ! (
494
+ "local ambiguity: multiple parsing options: {}" ,
495
+ match next_items. len( ) {
496
+ 0 => format!( "built-in NTs {}." , nts) ,
497
+ 1 => format!( "built-in NTs {} or 1 other option." , nts) ,
498
+ n => format!( "built-in NTs {} or {} other options." , nts, n) ,
499
+ }
500
+ ) ,
501
+ ) ;
474
502
} else if bb_items. is_empty ( ) && next_items. is_empty ( ) {
475
503
return Failure ( parser. span , parser. token ) ;
476
504
} else if !next_items. is_empty ( ) {
477
505
/* Now process the next token */
478
506
cur_items. extend ( next_items. drain ( ..) ) ;
479
507
parser. bump ( ) ;
480
- } else /* bb_items.len() == 1 */ {
508
+ } else
509
+ /* bb_items.len() == 1 */
510
+ {
481
511
let mut item = bb_items. pop ( ) . unwrap ( ) ;
482
512
if let TokenTree :: MetaVarDecl ( span, _, ident) = item. top_elts . get_tt ( item. idx ) {
483
513
let match_cur = item. match_cur ;
484
- item. push_match ( match_cur,
485
- MatchedNonterminal ( Rc :: new ( parse_nt ( & mut parser, span, & ident. name . as_str ( ) ) ) ) ) ;
514
+ item. push_match (
515
+ match_cur,
516
+ MatchedNonterminal ( Rc :: new ( parse_nt ( & mut parser, span, & ident. name . as_str ( ) ) ) ) ,
517
+ ) ;
486
518
item. idx += 1 ;
487
519
item. match_cur += 1 ;
488
520
} else {
@@ -512,20 +544,21 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
512
544
"expr" => token. can_begin_expr ( ) ,
513
545
"ty" => token. can_begin_type ( ) ,
514
546
"ident" => token. is_ident ( ) ,
515
- "vis" => match * token { // The follow-set of :vis + "priv" keyword + interpolated
547
+ "vis" => match * token {
548
+ // The follow-set of :vis + "priv" keyword + interpolated
516
549
Token :: Comma | Token :: Ident ( _) | Token :: Interpolated ( _) => true ,
517
550
_ => token. can_begin_type ( ) ,
518
551
} ,
519
552
"block" => match * token {
520
553
Token :: OpenDelim ( token:: Brace ) => true ,
521
554
Token :: Interpolated ( ref nt) => match nt. 0 {
522
- token:: NtItem ( _) |
523
- token:: NtPat ( _) |
524
- token:: NtTy ( _) |
525
- token:: NtIdent ( _) |
526
- token:: NtMeta ( _) |
527
- token:: NtPath ( _) |
528
- token:: NtVis ( _) => false , // none of these may start with '{'.
555
+ token:: NtItem ( _)
556
+ | token:: NtPat ( _)
557
+ | token:: NtTy ( _)
558
+ | token:: NtIdent ( _)
559
+ | token:: NtMeta ( _)
560
+ | token:: NtPath ( _)
561
+ | token:: NtVis ( _) => false , // none of these may start with '{'.
529
562
_ => true ,
530
563
} ,
531
564
_ => false ,
@@ -591,12 +624,15 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
591
624
"ident" => match p. token {
592
625
token:: Ident ( sn) => {
593
626
p. bump ( ) ;
594
- token:: NtIdent ( Spanned :: < Ident > { node : sn, span : p. prev_span } )
627
+ token:: NtIdent ( Spanned :: < Ident > {
628
+ node : sn,
629
+ span : p. prev_span ,
630
+ } )
595
631
}
596
632
_ => {
597
633
let token_str = pprust:: token_to_string ( & p. token ) ;
598
- p. fatal ( & format ! ( "expected ident, found {}" ,
599
- & token_str [ .. ] ) ) . emit ( ) ;
634
+ p. fatal ( & format ! ( "expected ident, found {}" , & token_str [ .. ] ) )
635
+ . emit ( ) ;
600
636
FatalError . raise ( )
601
637
}
602
638
} ,
@@ -606,6 +642,6 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
606
642
"lifetime" => token:: NtLifetime ( p. expect_lifetime ( ) ) ,
607
643
// this is not supposed to happen, since it has been checked
608
644
// when compiling the macro.
609
- _ => p. span_bug ( sp, "invalid fragment specifier" )
645
+ _ => p. span_bug ( sp, "invalid fragment specifier" ) ,
610
646
}
611
647
}
0 commit comments