@@ -16,6 +16,7 @@ use ext::placeholders;
16
16
use ext:: tt:: macro_parser:: { Success , Error , Failure } ;
17
17
use ext:: tt:: macro_parser:: { MatchedSeq , MatchedNonterminal } ;
18
18
use ext:: tt:: macro_parser:: parse;
19
+ use parse:: ParseSess ;
19
20
use parse:: lexer:: new_tt_reader;
20
21
use parse:: parser:: { Parser , Restrictions } ;
21
22
use parse:: token:: { self , gensym_ident, NtTT , Token } ;
@@ -204,7 +205,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
204
205
_ => cx. span_bug ( sp, "malformed macro rhs" ) ,
205
206
} ;
206
207
// rhs has holes ( `$id` and `$(...)` that need filled)
207
- let trncbr = new_tt_reader ( & cx. parse_sess ( ) . span_diagnostic ,
208
+ let trncbr = new_tt_reader ( & cx. parse_sess . span_diagnostic ,
208
209
Some ( named_matches) ,
209
210
imported_from,
210
211
rhs) ;
@@ -278,9 +279,7 @@ impl IdentMacroExpander for MacroRulesExpander {
278
279
// Holy self-referential!
279
280
280
281
/// Converts a `macro_rules!` invocation into a syntax extension.
281
- pub fn compile < ' cx > ( cx : & ' cx mut ExtCtxt ,
282
- def : & ast:: MacroDef ) -> SyntaxExtension {
283
-
282
+ pub fn compile ( sess : & ParseSess , def : & ast:: MacroDef ) -> SyntaxExtension {
284
283
let lhs_nm = gensym_ident ( "lhs" ) ;
285
284
let rhs_nm = gensym_ident ( "rhs" ) ;
286
285
@@ -312,19 +311,12 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
312
311
] ;
313
312
314
313
// Parse the macro_rules! invocation (`none` is for no interpolations):
315
- let arg_reader = new_tt_reader ( & cx. parse_sess ( ) . span_diagnostic ,
316
- None ,
317
- None ,
318
- def. body . clone ( ) ) ;
319
-
320
- let argument_map = match parse ( cx. parse_sess ( ) ,
321
- cx. cfg ( ) ,
322
- arg_reader,
323
- & argument_gram) {
314
+ let arg_reader = new_tt_reader ( & sess. span_diagnostic , None , None , def. body . clone ( ) ) ;
315
+
316
+ let argument_map = match parse ( sess, Vec :: new ( ) , arg_reader, & argument_gram) {
324
317
Success ( m) => m,
325
318
Failure ( sp, str) | Error ( sp, str) => {
326
- panic ! ( cx. parse_sess( ) . span_diagnostic
327
- . span_fatal( sp. substitute_dummy( def. span) , & str [ ..] ) ) ;
319
+ panic ! ( sess. span_diagnostic. span_fatal( sp. substitute_dummy( def. span) , & str ) ) ;
328
320
}
329
321
} ;
330
322
@@ -335,27 +327,27 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
335
327
MatchedSeq ( ref s, _) => {
336
328
s. iter ( ) . map ( |m| match * * m {
337
329
MatchedNonterminal ( NtTT ( ref tt) ) => {
338
- valid &= check_lhs_nt_follows ( cx , tt) ;
330
+ valid &= check_lhs_nt_follows ( sess , tt) ;
339
331
( * * tt) . clone ( )
340
332
}
341
- _ => cx . span_bug ( def. span , "wrong-structured lhs" )
333
+ _ => sess . span_diagnostic . span_bug ( def. span , "wrong-structured lhs" )
342
334
} ) . collect ( )
343
335
}
344
- _ => cx . span_bug ( def. span , "wrong-structured lhs" )
336
+ _ => sess . span_diagnostic . span_bug ( def. span , "wrong-structured lhs" )
345
337
} ;
346
338
347
339
let rhses = match * * argument_map. get ( & rhs_nm) . unwrap ( ) {
348
340
MatchedSeq ( ref s, _) => {
349
341
s. iter ( ) . map ( |m| match * * m {
350
342
MatchedNonterminal ( NtTT ( ref tt) ) => ( * * tt) . clone ( ) ,
351
- _ => cx . span_bug ( def. span , "wrong-structured rhs" )
343
+ _ => sess . span_diagnostic . span_bug ( def. span , "wrong-structured rhs" )
352
344
} ) . collect ( )
353
345
}
354
- _ => cx . span_bug ( def. span , "wrong-structured rhs" )
346
+ _ => sess . span_diagnostic . span_bug ( def. span , "wrong-structured rhs" )
355
347
} ;
356
348
357
349
for rhs in & rhses {
358
- valid &= check_rhs ( cx , rhs) ;
350
+ valid &= check_rhs ( sess , rhs) ;
359
351
}
360
352
361
353
let exp: Box < _ > = Box :: new ( MacroRulesMacroExpander {
@@ -369,35 +361,35 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
369
361
NormalTT ( exp, Some ( def. span ) , def. allow_internal_unstable )
370
362
}
371
363
372
- fn check_lhs_nt_follows ( cx : & mut ExtCtxt , lhs : & TokenTree ) -> bool {
364
+ fn check_lhs_nt_follows ( sess : & ParseSess , lhs : & TokenTree ) -> bool {
373
365
// lhs is going to be like TokenTree::Delimited(...), where the
374
366
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
375
367
match lhs {
376
- & TokenTree :: Delimited ( _, ref tts) => check_matcher ( cx , & tts. tts ) ,
368
+ & TokenTree :: Delimited ( _, ref tts) => check_matcher ( sess , & tts. tts ) ,
377
369
_ => {
378
- cx . span_err ( lhs . get_span ( ) , "invalid macro matcher; matchers must \
379
- be contained in balanced delimiters" ) ;
370
+ let msg = "invalid macro matcher; matchers must be contained in balanced delimiters" ;
371
+ sess . span_diagnostic . span_err ( lhs . get_span ( ) , msg ) ;
380
372
false
381
373
}
382
374
}
383
375
// we don't abort on errors on rejection, the driver will do that for us
384
376
// after parsing/expansion. we can report every error in every macro this way.
385
377
}
386
378
387
- fn check_rhs ( cx : & mut ExtCtxt , rhs : & TokenTree ) -> bool {
379
+ fn check_rhs ( sess : & ParseSess , rhs : & TokenTree ) -> bool {
388
380
match * rhs {
389
381
TokenTree :: Delimited ( ..) => return true ,
390
- _ => cx . span_err ( rhs. get_span ( ) , "macro rhs must be delimited" )
382
+ _ => sess . span_diagnostic . span_err ( rhs. get_span ( ) , "macro rhs must be delimited" )
391
383
}
392
384
false
393
385
}
394
386
395
- fn check_matcher ( cx : & mut ExtCtxt , matcher : & [ TokenTree ] ) -> bool {
387
+ fn check_matcher ( sess : & ParseSess , matcher : & [ TokenTree ] ) -> bool {
396
388
let first_sets = FirstSets :: new ( matcher) ;
397
389
let empty_suffix = TokenSet :: empty ( ) ;
398
- let err = cx . parse_sess . span_diagnostic . err_count ( ) ;
399
- check_matcher_core ( cx , & first_sets, matcher, & empty_suffix) ;
400
- err == cx . parse_sess . span_diagnostic . err_count ( )
390
+ let err = sess . span_diagnostic . err_count ( ) ;
391
+ check_matcher_core ( sess , & first_sets, matcher, & empty_suffix) ;
392
+ err == sess . span_diagnostic . err_count ( )
401
393
}
402
394
403
395
// The FirstSets for a matcher is a mapping from subsequences in the
@@ -635,7 +627,7 @@ impl TokenSet {
635
627
//
636
628
// Requires that `first_sets` is pre-computed for `matcher`;
637
629
// see `FirstSets::new`.
638
- fn check_matcher_core ( cx : & mut ExtCtxt ,
630
+ fn check_matcher_core ( sess : & ParseSess ,
639
631
first_sets : & FirstSets ,
640
632
matcher : & [ TokenTree ] ,
641
633
follow : & TokenSet ) -> TokenSet {
@@ -667,7 +659,8 @@ fn check_matcher_core(cx: &mut ExtCtxt,
667
659
TokenTree :: Token ( sp, ref tok) => {
668
660
let can_be_followed_by_any;
669
661
if let Err ( bad_frag) = has_legal_fragment_specifier ( tok) {
670
- cx. struct_span_err ( sp, & format ! ( "invalid fragment specifier `{}`" , bad_frag) )
662
+ let msg = format ! ( "invalid fragment specifier `{}`" , bad_frag) ;
663
+ sess. span_diagnostic . struct_span_err ( sp, & msg)
671
664
. help ( "valid fragment specifiers are `ident`, `block`, \
672
665
`stmt`, `expr`, `pat`, `ty`, `path`, `meta`, `tt` \
673
666
and `item`")
@@ -692,7 +685,7 @@ fn check_matcher_core(cx: &mut ExtCtxt,
692
685
}
693
686
TokenTree :: Delimited ( _, ref d) => {
694
687
let my_suffix = TokenSet :: singleton ( ( d. close_span , Token :: CloseDelim ( d. delim ) ) ) ;
695
- check_matcher_core ( cx , first_sets, & d. tts , & my_suffix) ;
688
+ check_matcher_core ( sess , first_sets, & d. tts , & my_suffix) ;
696
689
// don't track non NT tokens
697
690
last. replace_with_irrelevant ( ) ;
698
691
@@ -724,7 +717,7 @@ fn check_matcher_core(cx: &mut ExtCtxt,
724
717
// At this point, `suffix_first` is built, and
725
718
// `my_suffix` is some TokenSet that we can use
726
719
// for checking the interior of `seq_rep`.
727
- let next = check_matcher_core ( cx , first_sets, & seq_rep. tts , my_suffix) ;
720
+ let next = check_matcher_core ( sess , first_sets, & seq_rep. tts , my_suffix) ;
728
721
if next. maybe_empty {
729
722
last. add_all ( & next) ;
730
723
} else {
@@ -744,9 +737,9 @@ fn check_matcher_core(cx: &mut ExtCtxt,
744
737
' each_last: for & ( _sp, ref t) in & last. tokens {
745
738
if let MatchNt ( ref name, ref frag_spec) = * t {
746
739
for & ( sp, ref next_token) in & suffix_first. tokens {
747
- match is_in_follow ( cx , next_token, & frag_spec. name . as_str ( ) ) {
740
+ match is_in_follow ( next_token, & frag_spec. name . as_str ( ) ) {
748
741
Err ( ( msg, help) ) => {
749
- cx . struct_span_err ( sp, & msg) . help ( help) . emit ( ) ;
742
+ sess . span_diagnostic . struct_span_err ( sp, & msg) . help ( help) . emit ( ) ;
750
743
// don't bother reporting every source of
751
744
// conflict for a particular element of `last`.
752
745
continue ' each_last;
@@ -761,7 +754,7 @@ fn check_matcher_core(cx: &mut ExtCtxt,
761
754
"may be"
762
755
} ;
763
756
764
- cx . span_err (
757
+ sess . span_diagnostic . span_err (
765
758
sp,
766
759
& format ! ( "`${name}:{frag}` {may_be} followed by `{next}`, which \
767
760
is not allowed for `{frag}` fragments",
@@ -818,7 +811,7 @@ fn frag_can_be_followed_by_any(frag: &str) -> bool {
818
811
/// break macros that were relying on that binary operator as a
819
812
/// separator.
820
813
// when changing this do not forget to update doc/book/macros.md!
821
- fn is_in_follow ( _ : & ExtCtxt , tok : & Token , frag : & str ) -> Result < bool , ( String , & ' static str ) > {
814
+ fn is_in_follow ( tok : & Token , frag : & str ) -> Result < bool , ( String , & ' static str ) > {
822
815
if let & CloseDelim ( _) = tok {
823
816
// closing a token tree can never be matched by any fragment;
824
817
// iow, we always require that `(` and `)` match, etc.
0 commit comments