@@ -54,21 +54,16 @@ impl Context {
54
54
/// Parses the arguments from the given list of tokens, returning None if
55
55
/// there's a parse error so we can continue parsing other fmt! expressions.
56
56
fn parse_args ( & mut self , sp : Span ,
57
- leading_expr : bool ,
58
- tts : & [ ast:: token_tree ] ) -> ( Option < @ast:: Expr > ,
59
- Option < @ast:: Expr > ) {
57
+ tts : & [ ast:: token_tree ] ) -> ( @ast:: Expr , Option < @ast:: Expr > ) {
60
58
let p = rsparse:: new_parser_from_tts ( self . ecx . parse_sess ( ) ,
61
59
self . ecx . cfg ( ) ,
62
60
tts. to_owned ( ) ) ;
63
- // If we want a leading expression, parse it here
64
- let extra = if leading_expr {
65
- let e = Some ( p. parse_expr ( ) ) ;
66
- if !p. eat ( & token:: COMMA ) {
67
- self . ecx . span_err ( sp, "expected token: `,`" ) ;
68
- return ( e, None ) ;
69
- }
70
- e
71
- } else { None } ;
61
+ // Parse the leading function expression (maybe a block, maybe a path)
62
+ let extra = p. parse_expr ( ) ;
63
+ if !p. eat ( & token:: COMMA ) {
64
+ self . ecx . span_err ( sp, "expected token: `,`" ) ;
65
+ return ( extra, None ) ;
66
+ }
72
67
73
68
if * p. token == token:: EOF {
74
69
self . ecx . span_err ( sp, "requires at least a format string argument" ) ;
@@ -547,7 +542,7 @@ impl Context {
547
542
548
543
/// Actually builds the expression which the ifmt! block will be expanded
549
544
/// to
550
- fn to_expr ( & self , extra : Option < @ast:: Expr > , f : Option < & str > ) -> @ast:: Expr {
545
+ fn to_expr ( & self , extra : @ast:: Expr ) -> @ast:: Expr {
551
546
let mut lets = ~[ ] ;
552
547
let mut locals = ~[ ] ;
553
548
let mut names = vec:: from_fn ( self . name_positions . len ( ) , |_| None ) ;
@@ -614,68 +609,33 @@ impl Context {
614
609
self . ecx . expr_ident ( e. span , lname) ) ) ;
615
610
}
616
611
612
+ // Now create the fmt::Arguments struct with all our locals we created.
617
613
let args = names. move_iter ( ) . map ( |a| a. unwrap ( ) ) ;
618
614
let mut args = locals. move_iter ( ) . chain ( args) ;
619
-
620
- let result = match f {
621
- // Invocation of write!()/format!(), call the function and we're
622
- // done.
623
- Some ( f) => {
624
- let mut fmt_args = match extra {
625
- Some ( e) => ~[ e] , None => ~[ ]
626
- } ;
627
- fmt_args. push ( self . ecx . expr_ident ( self . fmtsp , static_name) ) ;
628
- fmt_args. push ( self . ecx . expr_vec_slice ( self . fmtsp ,
629
- args. collect ( ) ) ) ;
630
-
631
- let result = self . ecx . expr_call_global ( self . fmtsp , ~[
632
- self . ecx . ident_of ( "std" ) ,
633
- self . ecx . ident_of ( "fmt" ) ,
634
- self . ecx . ident_of ( f) ,
635
- ] , fmt_args) ;
636
-
637
- // sprintf is unsafe, but we just went through a lot of work to
638
- // validate that our call is save, so inject the unsafe block
639
- // for the user.
640
- self . ecx . expr_block ( ast:: Block {
641
- view_items : ~[ ] ,
642
- stmts : ~[ ] ,
643
- expr : Some ( result) ,
644
- id : ast:: DUMMY_NODE_ID ,
645
- rules : ast:: UnsafeBlock ( ast:: CompilerGenerated ) ,
646
- span : self . fmtsp ,
647
- } )
648
- }
649
-
650
- // Invocation of format_args!()
651
- None => {
652
- let fmt = self . ecx . expr_ident ( self . fmtsp , static_name) ;
653
- let args = self . ecx . expr_vec_slice ( self . fmtsp , args. collect ( ) ) ;
654
- let result = self . ecx . expr_call_global ( self . fmtsp , ~[
655
- self . ecx . ident_of ( "std" ) ,
656
- self . ecx . ident_of ( "fmt" ) ,
657
- self . ecx . ident_of ( "Arguments" ) ,
658
- self . ecx . ident_of ( "new" ) ,
659
- ] , ~[ fmt, args] ) ;
660
-
661
- // We did all the work of making sure that the arguments
662
- // structure is safe, so we can safely have an unsafe block.
663
- let result = self . ecx . expr_block ( ast:: Block {
664
- view_items : ~[ ] ,
665
- stmts : ~[ ] ,
666
- expr : Some ( result) ,
667
- id : ast:: DUMMY_NODE_ID ,
668
- rules : ast:: UnsafeBlock ( ast:: CompilerGenerated ) ,
669
- span : self . fmtsp ,
670
- } ) ;
671
- let extra = extra. unwrap ( ) ;
672
- let resname = self . ecx . ident_of ( "__args" ) ;
673
- lets. push ( self . ecx . stmt_let ( self . fmtsp , false , resname, result) ) ;
674
- let res = self . ecx . expr_ident ( self . fmtsp , resname) ;
675
- self . ecx . expr_call ( extra. span , extra, ~[
676
- self . ecx . expr_addr_of ( extra. span , res) ] )
677
- }
678
- } ;
615
+ let fmt = self . ecx . expr_ident ( self . fmtsp , static_name) ;
616
+ let args = self . ecx . expr_vec_slice ( self . fmtsp , args. collect ( ) ) ;
617
+ let result = self . ecx . expr_call_global ( self . fmtsp , ~[
618
+ self . ecx . ident_of ( "std" ) ,
619
+ self . ecx . ident_of ( "fmt" ) ,
620
+ self . ecx . ident_of ( "Arguments" ) ,
621
+ self . ecx . ident_of ( "new" ) ,
622
+ ] , ~[ fmt, args] ) ;
623
+
624
+ // We did all the work of making sure that the arguments
625
+ // structure is safe, so we can safely have an unsafe block.
626
+ let result = self . ecx . expr_block ( ast:: Block {
627
+ view_items : ~[ ] ,
628
+ stmts : ~[ ] ,
629
+ expr : Some ( result) ,
630
+ id : ast:: DUMMY_NODE_ID ,
631
+ rules : ast:: UnsafeBlock ( ast:: CompilerGenerated ) ,
632
+ span : self . fmtsp ,
633
+ } ) ;
634
+ let resname = self . ecx . ident_of ( "__args" ) ;
635
+ lets. push ( self . ecx . stmt_let ( self . fmtsp , false , resname, result) ) ;
636
+ let res = self . ecx . expr_ident ( self . fmtsp , resname) ;
637
+ let result = self . ecx . expr_call ( extra. span , extra, ~[
638
+ self . ecx . expr_addr_of ( extra. span , res) ] ) ;
679
639
self . ecx . expr_block ( self . ecx . block ( self . fmtsp , lets,
680
640
Some ( result) ) )
681
641
}
@@ -740,29 +700,8 @@ impl Context {
740
700
}
741
701
}
742
702
743
- pub fn expand_format ( ecx : @ExtCtxt , sp : Span ,
744
- tts : & [ ast:: token_tree ] ) -> base:: MacResult {
745
- expand_ifmt ( ecx, sp, tts, false , false , Some ( "format_unsafe" ) )
746
- }
747
-
748
- pub fn expand_write ( ecx : @ExtCtxt , sp : Span ,
749
- tts : & [ ast:: token_tree ] ) -> base:: MacResult {
750
- expand_ifmt ( ecx, sp, tts, true , false , Some ( "write_unsafe" ) )
751
- }
752
-
753
- pub fn expand_writeln ( ecx : @ExtCtxt , sp : Span ,
754
- tts : & [ ast:: token_tree ] ) -> base:: MacResult {
755
- expand_ifmt ( ecx, sp, tts, true , true , Some ( "write_unsafe" ) )
756
- }
757
-
758
- pub fn expand_format_args ( ecx : @ExtCtxt , sp : Span ,
759
- tts : & [ ast:: token_tree ] ) -> base:: MacResult {
760
- expand_ifmt ( ecx, sp, tts, true , false , None )
761
- }
762
-
763
- fn expand_ifmt ( ecx : @ExtCtxt , sp : Span , tts : & [ ast:: token_tree ] ,
764
- leading_arg : bool , append_newline : bool ,
765
- function : Option < & str > ) -> base:: MacResult {
703
+ pub fn expand_args ( ecx : @ExtCtxt , sp : Span ,
704
+ tts : & [ ast:: token_tree ] ) -> base:: MacResult {
766
705
let mut cx = Context {
767
706
ecx : ecx,
768
707
args : ~[ ] ,
@@ -776,14 +715,13 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: Span, tts: &[ast::token_tree],
776
715
method_statics : ~[ ] ,
777
716
fmtsp : sp,
778
717
} ;
779
- let ( extra, efmt) = match cx. parse_args ( sp, leading_arg , tts) {
718
+ let ( extra, efmt) = match cx. parse_args ( sp, tts) {
780
719
( extra, Some ( e) ) => ( extra, e) ,
781
720
( _, None ) => { return MRExpr ( ecx. expr_uint ( sp, 2 ) ) ; }
782
721
} ;
783
722
cx. fmtsp = efmt. span ;
784
723
let fmt = expr_to_str ( ecx, efmt,
785
724
"format argument must be a string literal." ) ;
786
- let fmt = if append_newline { fmt + "\n " } else { fmt. to_owned ( ) } ;
787
725
788
726
let mut err = false ;
789
727
do parse:: parse_error:: cond. trap ( |m| {
@@ -814,5 +752,5 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: Span, tts: &[ast::token_tree],
814
752
}
815
753
}
816
754
817
- MRExpr ( cx. to_expr ( extra, function ) )
755
+ MRExpr ( cx. to_expr ( extra) )
818
756
}
0 commit comments