8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- pub use self :: Invocation :: * ;
12
11
use self :: ArgumentType :: * ;
13
12
use self :: Position :: * ;
14
13
@@ -68,54 +67,33 @@ struct Context<'a, 'b:'a> {
68
67
next_arg : uint ,
69
68
}
70
69
71
- pub enum Invocation {
72
- Call ( P < ast:: Expr > ) ,
73
- MethodCall ( P < ast:: Expr > , ast:: Ident ) ,
74
- }
75
-
76
70
/// Parses the arguments from the given list of tokens, returning None
77
71
/// if there's a parse error so we can continue parsing other format!
78
72
/// expressions.
79
73
///
80
- /// If parsing succeeds, the second return value is:
74
+ /// If parsing succeeds, the return value is:
81
75
///
82
76
/// Some((fmtstr, unnamed arguments, ordering of named arguments,
83
77
/// named arguments))
84
- fn parse_args ( ecx : & mut ExtCtxt , sp : Span , allow_method : bool ,
85
- tts : & [ ast:: TokenTree ] )
86
- -> ( Invocation , Option < ( P < ast:: Expr > , Vec < P < ast:: Expr > > , Vec < String > ,
87
- HashMap < String , P < ast:: Expr > > ) > ) {
78
+ fn parse_args ( ecx : & mut ExtCtxt , sp : Span , tts : & [ ast:: TokenTree ] )
79
+ -> Option < ( P < ast:: Expr > , Vec < P < ast:: Expr > > , Vec < String > ,
80
+ HashMap < String , P < ast:: Expr > > ) > {
88
81
let mut args = Vec :: new ( ) ;
89
82
let mut names = HashMap :: < String , P < ast:: Expr > > :: new ( ) ;
90
83
let mut order = Vec :: new ( ) ;
91
84
92
85
let mut p = ecx. new_parser_from_tts ( tts) ;
93
- // Parse the leading function expression (maybe a block, maybe a path)
94
- let invocation = if allow_method {
95
- let e = p. parse_expr ( ) ;
96
- if !p. eat ( & token:: Comma ) {
97
- ecx. span_err ( sp, "expected token: `,`" ) ;
98
- return ( Call ( e) , None ) ;
99
- }
100
- MethodCall ( e, p. parse_ident ( ) )
101
- } else {
102
- Call ( p. parse_expr ( ) )
103
- } ;
104
- if !p. eat ( & token:: Comma ) {
105
- ecx. span_err ( sp, "expected token: `,`" ) ;
106
- return ( invocation, None ) ;
107
- }
108
86
109
87
if p. token == token:: Eof {
110
88
ecx. span_err ( sp, "requires at least a format string argument" ) ;
111
- return ( invocation , None ) ;
89
+ return None ;
112
90
}
113
91
let fmtstr = p. parse_expr ( ) ;
114
92
let mut named = false ;
115
93
while p. token != token:: Eof {
116
94
if !p. eat ( & token:: Comma ) {
117
95
ecx. span_err ( sp, "expected token: `,`" ) ;
118
- return ( invocation , None ) ;
96
+ return None ;
119
97
}
120
98
if p. token == token:: Eof { break } // accept trailing commas
121
99
if named || ( p. token . is_ident ( ) && p. look_ahead ( 1 , |t| * t == token:: Eq ) ) {
@@ -129,13 +107,13 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool,
129
107
ecx. span_err ( p. span ,
130
108
"expected ident, positional arguments \
131
109
cannot follow named arguments") ;
132
- return ( invocation , None ) ;
110
+ return None ;
133
111
}
134
112
_ => {
135
113
ecx. span_err ( p. span ,
136
114
format ! ( "expected ident for named argument, found `{}`" ,
137
115
p. this_token_to_string( ) ) [ ] ) ;
138
- return ( invocation , None ) ;
116
+ return None ;
139
117
}
140
118
} ;
141
119
let interned_name = token:: get_ident ( ident) ;
@@ -158,7 +136,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool,
158
136
args. push ( p. parse_expr ( ) ) ;
159
137
}
160
138
}
161
- return ( invocation , Some ( ( fmtstr, args, order, names) ) ) ;
139
+ Some ( ( fmtstr, args, order, names) )
162
140
}
163
141
164
142
impl < ' a , ' b > Context < ' a , ' b > {
@@ -497,7 +475,7 @@ impl<'a, 'b> Context<'a, 'b> {
497
475
498
476
/// Actually builds the expression which the iformat! block will be expanded
499
477
/// to
500
- fn to_expr ( mut self , invocation : Invocation ) -> P < ast:: Expr > {
478
+ fn into_expr ( mut self ) -> P < ast:: Expr > {
501
479
let mut locals = Vec :: new ( ) ;
502
480
let mut names = Vec :: from_fn ( self . name_positions . len ( ) , |_| None ) ;
503
481
let mut pats = Vec :: new ( ) ;
@@ -615,26 +593,11 @@ impl<'a, 'b> Context<'a, 'b> {
615
593
( "with_placeholders" , vec ! [ pieces, fmt, args_slice] )
616
594
} ;
617
595
618
- let body = self . ecx . expr_call_global ( self . fmtsp , vec ! (
596
+ self . ecx . expr_call_global ( self . fmtsp , vec ! (
619
597
self . ecx. ident_of( "std" ) ,
620
598
self . ecx. ident_of( "fmt" ) ,
621
599
self . ecx. ident_of( "Arguments" ) ,
622
- self . ecx. ident_of( fn_name) ) , fn_args) ;
623
-
624
- match invocation {
625
- Call ( e) => {
626
- let span = e. span ;
627
- self . ecx . expr_call ( span, e, vec ! [
628
- self . ecx. expr_addr_of( span, body)
629
- ] )
630
- }
631
- MethodCall ( e, m) => {
632
- let span = e. span ;
633
- self . ecx . expr_method_call ( span, e, m, vec ! [
634
- self . ecx. expr_addr_of( span, body)
635
- ] )
636
- }
637
- }
600
+ self . ecx. ident_of( fn_name) ) , fn_args)
638
601
}
639
602
640
603
fn format_arg ( ecx : & ExtCtxt , sp : Span ,
@@ -684,24 +647,22 @@ pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, sp: Span,
684
647
tts : & [ ast:: TokenTree ] )
685
648
-> Box < base:: MacResult +' cx > {
686
649
687
- match parse_args ( ecx, sp, false , tts) {
688
- ( invocation , Some ( ( efmt, args, order, names) ) ) => {
689
- MacExpr :: new ( expand_preparsed_format_args ( ecx, sp, invocation , efmt,
650
+ match parse_args ( ecx, sp, tts) {
651
+ Some ( ( efmt, args, order, names) ) => {
652
+ MacExpr :: new ( expand_preparsed_format_args ( ecx, sp, efmt,
690
653
args, order, names) )
691
654
}
692
- ( _ , None ) => MacExpr :: new ( ecx . expr_uint ( sp , 2 ) )
655
+ None => DummyResult :: expr ( sp )
693
656
}
694
657
}
695
658
696
- /// Take the various parts of `format_args!(extra, efmt, args...,
697
- /// name=names...)` and construct the appropriate formatting
698
- /// expression.
659
+ /// Take the various parts of `format_args!(efmt, args..., name=names...)`
660
+ /// and construct the appropriate formatting expression.
699
661
pub fn expand_preparsed_format_args ( ecx : & mut ExtCtxt , sp : Span ,
700
- invocation : Invocation ,
701
662
efmt : P < ast:: Expr > ,
702
663
args : Vec < P < ast:: Expr > > ,
703
- name_ordering : Vec < string :: String > ,
704
- names : HashMap < string :: String , P < ast:: Expr > > )
664
+ name_ordering : Vec < String > ,
665
+ names : HashMap < String , P < ast:: Expr > > )
705
666
-> P < ast:: Expr > {
706
667
let arg_types = Vec :: from_fn ( args. len ( ) , |_| None ) ;
707
668
let mut cx = Context {
@@ -722,8 +683,8 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
722
683
} ;
723
684
cx. fmtsp = efmt. span ;
724
685
let fmt = match expr_to_string ( cx. ecx ,
725
- efmt,
726
- "format argument must be a string literal." ) {
686
+ efmt,
687
+ "format argument must be a string literal." ) {
727
688
Some ( ( fmt, _) ) => fmt,
728
689
None => return DummyResult :: raw_expr ( sp)
729
690
} ;
@@ -771,5 +732,5 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span,
771
732
}
772
733
}
773
734
774
- cx. to_expr ( invocation )
735
+ cx. into_expr ( )
775
736
}
0 commit comments