@@ -54,21 +54,16 @@ impl Context {
5454 /// Parses the arguments from the given list of tokens, returning None if
5555 /// there's a parse error so we can continue parsing other fmt! expressions.
5656 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 > ) {
6058 let p = rsparse:: new_parser_from_tts ( self . ecx . parse_sess ( ) ,
6159 self . ecx . cfg ( ) ,
6260 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+ }
7267
7368 if * p. token == token:: EOF {
7469 self . ecx . span_err ( sp, "requires at least a format string argument" ) ;
@@ -547,7 +542,7 @@ impl Context {
547542
548543 /// Actually builds the expression which the ifmt! block will be expanded
549544 /// 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 {
551546 let mut lets = ~[ ] ;
552547 let mut locals = ~[ ] ;
553548 let mut names = vec:: from_fn ( self . name_positions . len ( ) , |_| None ) ;
@@ -614,68 +609,33 @@ impl Context {
614609 self . ecx . expr_ident ( e. span , lname) ) ) ;
615610 }
616611
612+ // Now create the fmt::Arguments struct with all our locals we created.
617613 let args = names. move_iter ( ) . map ( |a| a. unwrap ( ) ) ;
618614 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) ] ) ;
679639 self . ecx . expr_block ( self . ecx . block ( self . fmtsp , lets,
680640 Some ( result) ) )
681641 }
@@ -740,29 +700,8 @@ impl Context {
740700 }
741701}
742702
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 {
766705 let mut cx = Context {
767706 ecx : ecx,
768707 args : ~[ ] ,
@@ -776,14 +715,13 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: Span, tts: &[ast::token_tree],
776715 method_statics : ~[ ] ,
777716 fmtsp : sp,
778717 } ;
779- let ( extra, efmt) = match cx. parse_args ( sp, leading_arg , tts) {
718+ let ( extra, efmt) = match cx. parse_args ( sp, tts) {
780719 ( extra, Some ( e) ) => ( extra, e) ,
781720 ( _, None ) => { return MRExpr ( ecx. expr_uint ( sp, 2 ) ) ; }
782721 } ;
783722 cx. fmtsp = efmt. span ;
784723 let fmt = expr_to_str ( ecx, efmt,
785724 "format argument must be a string literal." ) ;
786- let fmt = if append_newline { fmt + "\n " } else { fmt. to_owned ( ) } ;
787725
788726 let mut err = false ;
789727 do parse:: parse_error:: cond. trap ( |m| {
@@ -814,5 +752,5 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: Span, tts: &[ast::token_tree],
814752 }
815753 }
816754
817- MRExpr ( cx. to_expr ( extra, function ) )
755+ MRExpr ( cx. to_expr ( extra) )
818756}
0 commit comments