2013-08-06 23:50:23 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-08-27 23:27:39 -05:00
|
|
|
/*
|
2012-10-12 14:32:36 -05:00
|
|
|
* The compiler code necessary to support the fmt! extension. Eventually this
|
2011-08-27 23:27:39 -05:00
|
|
|
* should all get sucked into either the standard library extfmt module or the
|
|
|
|
* compiler syntax extension plugin interface.
|
|
|
|
*/
|
2012-12-23 16:41:37 -06:00
|
|
|
|
|
|
|
use ast;
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::Span;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ext::base::*;
|
|
|
|
use ext::base;
|
2013-05-17 09:19:28 -05:00
|
|
|
use ext::build::AstBuilder;
|
2013-03-01 12:44:43 -06:00
|
|
|
|
2013-06-24 19:40:33 -05:00
|
|
|
use std::option;
|
|
|
|
use std::unstable::extfmt::ct::*;
|
2013-06-04 13:09:18 -05:00
|
|
|
use parse::token::{str_to_ident};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
|
2013-01-22 18:45:27 -06:00
|
|
|
-> base::MacResult {
|
2013-07-22 13:22:22 -05:00
|
|
|
let args = get_exprs_from_tts(cx, sp, tts);
|
2012-12-12 19:08:09 -06:00
|
|
|
if args.len() == 0 {
|
|
|
|
cx.span_fatal(sp, "fmt! takes at least 1 argument.");
|
|
|
|
}
|
2013-10-07 19:49:10 -05:00
|
|
|
let (fmt, _fmt_str_style) =
|
2011-08-27 23:27:39 -05:00
|
|
|
expr_to_str(cx, args[0],
|
2013-08-06 23:50:23 -05:00
|
|
|
"first argument to fmt! must be a string literal.");
|
2011-08-27 23:27:39 -05:00
|
|
|
let fmtspan = args[0].span;
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("Format string: {}", fmt);
|
2013-08-31 11:13:04 -05:00
|
|
|
fn parse_fmt_err_(cx: @ExtCtxt, sp: Span, msg: &str) -> ! {
|
2011-08-27 23:27:39 -05:00
|
|
|
cx.span_fatal(sp, msg);
|
|
|
|
}
|
2013-08-30 20:00:38 -05:00
|
|
|
let parse_fmt_err: &fn(&str) -> ! = |s| parse_fmt_err_(cx, fmtspan, s);
|
2011-08-27 23:27:39 -05:00
|
|
|
let pieces = parse_fmt_string(fmt, parse_fmt_err);
|
2013-01-22 18:45:27 -06:00
|
|
|
MRExpr(pieces_to_expr(cx, sp, pieces, args))
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2249): A lot of these functions for producing expressions can
|
|
|
|
// probably be factored out in common with other code that builds
|
|
|
|
// expressions. Also: Cleanup the naming of these functions.
|
2013-01-29 18:18:59 -06:00
|
|
|
// Note: Moved many of the common ones to build.rs --kevina
|
2013-08-31 11:13:04 -05:00
|
|
|
fn pieces_to_expr(cx: @ExtCtxt, sp: Span,
|
2013-09-01 20:45:37 -05:00
|
|
|
pieces: ~[Piece], args: ~[@ast::Expr])
|
|
|
|
-> @ast::Expr {
|
2013-09-01 19:50:59 -05:00
|
|
|
fn make_path_vec(ident: &str) -> ~[ast::Ident] {
|
2013-06-04 14:34:25 -05:00
|
|
|
return ~[str_to_ident("std"),
|
|
|
|
str_to_ident("unstable"),
|
|
|
|
str_to_ident("extfmt"),
|
|
|
|
str_to_ident("rt"),
|
|
|
|
str_to_ident(ident)];
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn make_rt_path_expr(cx: @ExtCtxt, sp: Span, nm: &str) -> @ast::Expr {
|
2013-06-04 17:14:56 -05:00
|
|
|
let path = make_path_vec(nm);
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_path(cx.path_global(sp, path))
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
// Produces an AST expression that represents a RT::conv record,
|
|
|
|
// which tells the RT::conv* functions how to perform the conversion
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
fn make_rt_conv_expr(cx: @ExtCtxt, sp: Span, cnv: &Conv) -> @ast::Expr {
|
|
|
|
fn make_flags(cx: @ExtCtxt, sp: Span, flags: &[Flag]) -> @ast::Expr {
|
2013-05-02 03:16:07 -05:00
|
|
|
let mut tmp_expr = make_rt_path_expr(cx, sp, "flag_none");
|
2013-08-03 11:45:23 -05:00
|
|
|
for f in flags.iter() {
|
2012-09-19 18:55:01 -05:00
|
|
|
let fstr = match *f {
|
2013-05-02 03:16:07 -05:00
|
|
|
FlagLeftJustify => "flag_left_justify",
|
|
|
|
FlagLeftZeroPad => "flag_left_zero_pad",
|
|
|
|
FlagSpaceForSign => "flag_space_for_sign",
|
|
|
|
FlagSignAlways => "flag_sign_always",
|
|
|
|
FlagAlternate => "flag_alternate"
|
2012-06-29 01:36:00 -05:00
|
|
|
};
|
2013-09-01 20:45:37 -05:00
|
|
|
tmp_expr = cx.expr_binary(sp, ast::BiBitOr, tmp_expr,
|
2013-05-19 00:53:42 -05:00
|
|
|
make_rt_path_expr(cx, sp, fstr));
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return tmp_expr;
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn make_count(cx: @ExtCtxt, sp: Span, cnt: Count) -> @ast::Expr {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cnt {
|
2012-09-11 21:37:29 -05:00
|
|
|
CountImplied => {
|
2013-05-02 03:16:07 -05:00
|
|
|
return make_rt_path_expr(cx, sp, "CountImplied");
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-09-11 21:37:29 -05:00
|
|
|
CountIs(c) => {
|
2013-05-19 00:53:42 -05:00
|
|
|
let count_lit = cx.expr_uint(sp, c as uint);
|
2013-06-04 17:14:56 -05:00
|
|
|
let count_is_path = make_path_vec("CountIs");
|
2012-06-29 18:26:56 -05:00
|
|
|
let count_is_args = ~[count_lit];
|
2013-05-19 00:53:42 -05:00
|
|
|
return cx.expr_call_global(sp, count_is_path, count_is_args);
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
_ => cx.span_unimpl(sp, "unimplemented fmt! conversion")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn make_ty(cx: @ExtCtxt, sp: Span, t: Ty) -> @ast::Expr {
|
2013-05-02 03:16:07 -05:00
|
|
|
let rt_type = match t {
|
2012-09-11 21:37:29 -05:00
|
|
|
TyHex(c) => match c {
|
2013-05-02 03:16:07 -05:00
|
|
|
CaseUpper => "TyHexUpper",
|
|
|
|
CaseLower => "TyHexLower"
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2013-05-02 03:16:07 -05:00
|
|
|
TyBits => "TyBits",
|
|
|
|
TyOctal => "TyOctal",
|
|
|
|
_ => "TyDefault"
|
|
|
|
};
|
|
|
|
return make_rt_path_expr(cx, sp, rt_type);
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn make_conv_struct(cx: @ExtCtxt, sp: Span, flags_expr: @ast::Expr,
|
|
|
|
width_expr: @ast::Expr, precision_expr: @ast::Expr,
|
|
|
|
ty_expr: @ast::Expr) -> @ast::Expr {
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_struct(
|
2013-01-24 12:33:20 -06:00
|
|
|
sp,
|
2013-06-04 17:14:56 -05:00
|
|
|
cx.path_global(sp, make_path_vec("Conv")),
|
2013-01-24 12:33:20 -06:00
|
|
|
~[
|
2013-06-04 14:34:25 -05:00
|
|
|
cx.field_imm(sp, str_to_ident("flags"), flags_expr),
|
|
|
|
cx.field_imm(sp, str_to_ident("width"), width_expr),
|
|
|
|
cx.field_imm(sp, str_to_ident("precision"), precision_expr),
|
|
|
|
cx.field_imm(sp, str_to_ident("ty"), ty_expr)
|
2013-01-24 12:33:20 -06:00
|
|
|
]
|
|
|
|
)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
let rt_conv_flags = make_flags(cx, sp, cnv.flags);
|
|
|
|
let rt_conv_width = make_count(cx, sp, cnv.width);
|
|
|
|
let rt_conv_precision = make_count(cx, sp, cnv.precision);
|
|
|
|
let rt_conv_ty = make_ty(cx, sp, cnv.ty);
|
2013-01-24 12:33:20 -06:00
|
|
|
make_conv_struct(cx, sp, rt_conv_flags, rt_conv_width,
|
|
|
|
rt_conv_precision, rt_conv_ty)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-08-31 11:13:04 -05:00
|
|
|
fn make_conv_call(cx: @ExtCtxt, sp: Span, conv_type: &str, cnv: &Conv,
|
2013-09-01 20:45:37 -05:00
|
|
|
arg: @ast::Expr, buf: @ast::Expr) -> @ast::Expr {
|
2012-07-14 00:57:48 -05:00
|
|
|
let fname = ~"conv_" + conv_type;
|
2013-06-04 17:14:56 -05:00
|
|
|
let path = make_path_vec(fname);
|
2011-08-27 23:27:39 -05:00
|
|
|
let cnv_expr = make_rt_conv_expr(cx, sp, cnv);
|
2013-03-20 22:33:10 -05:00
|
|
|
let args = ~[cnv_expr, arg, buf];
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_call_global(arg.span, path, args)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-06-28 18:23:12 -05:00
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
fn make_new_conv(cx: @ExtCtxt, sp: Span, cnv: &Conv,
|
2013-09-01 20:45:37 -05:00
|
|
|
arg: @ast::Expr, buf: @ast::Expr) -> @ast::Expr {
|
2013-03-19 20:24:01 -05:00
|
|
|
fn is_signed_type(cnv: &Conv) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cnv.ty {
|
2012-09-11 21:37:29 -05:00
|
|
|
TyInt(s) => match s {
|
|
|
|
Signed => return true,
|
|
|
|
Unsigned => return false
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-09-11 21:37:29 -05:00
|
|
|
TyFloat => return true,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => return false
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 14:32:36 -05:00
|
|
|
let unsupported = ~"conversion not supported in fmt! string";
|
2012-08-06 14:34:08 -05:00
|
|
|
match cnv.param {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cx.span_unimpl(sp, unsupported)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for f in cnv.flags.iter() {
|
2012-09-19 18:55:01 -05:00
|
|
|
match *f {
|
2012-09-11 21:37:29 -05:00
|
|
|
FlagLeftJustify => (),
|
|
|
|
FlagSignAlways => {
|
2011-08-27 23:27:39 -05:00
|
|
|
if !is_signed_type(cnv) {
|
|
|
|
cx.span_fatal(sp,
|
2013-05-23 11:09:11 -05:00
|
|
|
"+ flag only valid in \
|
|
|
|
signed fmt! conversion");
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-11 21:37:29 -05:00
|
|
|
FlagSpaceForSign => {
|
2011-08-27 23:27:39 -05:00
|
|
|
if !is_signed_type(cnv) {
|
|
|
|
cx.span_fatal(sp,
|
2013-05-23 11:09:11 -05:00
|
|
|
"space flag only valid in \
|
|
|
|
signed fmt! conversions");
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-11 21:37:29 -05:00
|
|
|
FlagLeftZeroPad => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cx.span_unimpl(sp, unsupported)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match cnv.width {
|
2012-09-11 21:37:29 -05:00
|
|
|
CountImplied => (),
|
|
|
|
CountIs(_) => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cx.span_unimpl(sp, unsupported)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match cnv.precision {
|
2012-09-11 21:37:29 -05:00
|
|
|
CountImplied => (),
|
|
|
|
CountIs(_) => (),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => cx.span_unimpl(sp, unsupported)
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-03-20 22:33:10 -05:00
|
|
|
let (name, actual_arg) = match cnv.ty {
|
|
|
|
TyStr => ("str", arg),
|
|
|
|
TyInt(Signed) => ("int", arg),
|
|
|
|
TyBool => ("bool", arg),
|
|
|
|
TyChar => ("char", arg),
|
|
|
|
TyBits | TyOctal | TyHex(_) | TyInt(Unsigned) => ("uint", arg),
|
|
|
|
TyFloat => ("float", arg),
|
2013-07-27 22:21:39 -05:00
|
|
|
TyPointer => ("pointer", arg),
|
2013-05-19 00:53:42 -05:00
|
|
|
TyPoly => ("poly", cx.expr_addr_of(sp, arg))
|
2013-03-20 22:33:10 -05:00
|
|
|
};
|
|
|
|
return make_conv_call(cx, arg.span, name, cnv, actual_arg,
|
2013-05-19 00:53:42 -05:00
|
|
|
cx.expr_mut_addr_of(arg.span, buf));
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-03-19 20:24:01 -05:00
|
|
|
fn log_conv(c: &Conv) {
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("Building conversion:");
|
2012-08-06 14:34:08 -05:00
|
|
|
match c.param {
|
2013-09-27 23:01:58 -05:00
|
|
|
Some(p) => { debug2!("param: {}", p.to_str()); }
|
|
|
|
_ => debug2!("param: none")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for f in c.flags.iter() {
|
2012-09-19 18:55:01 -05:00
|
|
|
match *f {
|
2013-09-27 23:01:58 -05:00
|
|
|
FlagLeftJustify => debug2!("flag: left justify"),
|
|
|
|
FlagLeftZeroPad => debug2!("flag: left zero pad"),
|
|
|
|
FlagSpaceForSign => debug2!("flag: left space pad"),
|
|
|
|
FlagSignAlways => debug2!("flag: sign always"),
|
|
|
|
FlagAlternate => debug2!("flag: alternate")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match c.width {
|
2013-03-08 14:39:42 -06:00
|
|
|
CountIs(i) =>
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("width: count is {}", i.to_str()),
|
2013-03-08 14:39:42 -06:00
|
|
|
CountIsParam(i) =>
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("width: count is param {}", i.to_str()),
|
|
|
|
CountIsNextParam => debug2!("width: count is next param"),
|
|
|
|
CountImplied => debug2!("width: count is implied")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match c.precision {
|
2013-03-08 14:39:42 -06:00
|
|
|
CountIs(i) =>
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("prec: count is {}", i.to_str()),
|
2013-03-08 14:39:42 -06:00
|
|
|
CountIsParam(i) =>
|
2013-09-27 23:01:58 -05:00
|
|
|
debug2!("prec: count is param {}", i.to_str()),
|
|
|
|
CountIsNextParam => debug2!("prec: count is next param"),
|
|
|
|
CountImplied => debug2!("prec: count is implied")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match c.ty {
|
2013-09-27 23:01:58 -05:00
|
|
|
TyBool => debug2!("type: bool"),
|
|
|
|
TyStr => debug2!("type: str"),
|
|
|
|
TyChar => debug2!("type: char"),
|
2012-09-11 21:37:29 -05:00
|
|
|
TyInt(s) => match s {
|
2013-09-27 23:01:58 -05:00
|
|
|
Signed => debug2!("type: signed"),
|
|
|
|
Unsigned => debug2!("type: unsigned")
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2013-09-27 23:01:58 -05:00
|
|
|
TyBits => debug2!("type: bits"),
|
2012-09-11 21:37:29 -05:00
|
|
|
TyHex(cs) => match cs {
|
2013-09-27 23:01:58 -05:00
|
|
|
CaseUpper => debug2!("type: uhex"),
|
|
|
|
CaseLower => debug2!("type: lhex"),
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2013-09-27 23:01:58 -05:00
|
|
|
TyOctal => debug2!("type: octal"),
|
|
|
|
TyFloat => debug2!("type: float"),
|
|
|
|
TyPointer => debug2!("type: pointer"),
|
|
|
|
TyPoly => debug2!("type: poly")
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-20 12:01:58 -05:00
|
|
|
|
2013-06-19 01:16:44 -05:00
|
|
|
/* Short circuit an easy case up front (won't work otherwise) */
|
|
|
|
if pieces.len() == 0 {
|
|
|
|
return cx.expr_str_uniq(args[0].span, @"");
|
|
|
|
}
|
|
|
|
|
2011-08-27 23:27:39 -05:00
|
|
|
let fmt_sp = args[0].span;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut n = 0u;
|
2012-06-28 18:23:12 -05:00
|
|
|
let nargs = args.len();
|
2013-03-20 22:33:10 -05:00
|
|
|
|
|
|
|
/* 'ident' is the local buffer building up the result of fmt! */
|
2013-06-04 14:34:25 -05:00
|
|
|
let ident = str_to_ident("__fmtbuf");
|
2013-05-19 00:53:42 -05:00
|
|
|
let buf = || cx.expr_ident(fmt_sp, ident);
|
2013-06-04 14:34:25 -05:00
|
|
|
let core_ident = str_to_ident("std");
|
|
|
|
let str_ident = str_to_ident("str");
|
|
|
|
let push_ident = str_to_ident("push_str");
|
2013-03-20 22:33:10 -05:00
|
|
|
let mut stms = ~[];
|
|
|
|
|
|
|
|
/* Translate each piece (portion of the fmt expression) by invoking the
|
2013-06-24 19:40:33 -05:00
|
|
|
corresponding function in std::unstable::extfmt. Each function takes a
|
2013-03-20 22:33:10 -05:00
|
|
|
buffer to insert data into along with the data being formatted. */
|
2013-04-12 00:10:31 -05:00
|
|
|
let npieces = pieces.len();
|
2013-08-07 21:21:36 -05:00
|
|
|
for (i, pc) in pieces.move_iter().enumerate() {
|
2013-03-20 12:01:58 -05:00
|
|
|
match pc {
|
2013-03-20 22:33:10 -05:00
|
|
|
/* Raw strings get appended via str::push_str */
|
|
|
|
PieceString(s) => {
|
|
|
|
/* If this is the first portion, then initialize the local
|
2013-04-12 00:10:31 -05:00
|
|
|
buffer with it directly. If it's actually the only piece,
|
|
|
|
then there's no need for it to be mutable */
|
2013-03-20 22:33:10 -05:00
|
|
|
if i == 0 {
|
2013-05-19 00:53:42 -05:00
|
|
|
stms.push(cx.stmt_let(fmt_sp, npieces > 1,
|
2013-06-12 12:02:55 -05:00
|
|
|
ident, cx.expr_str_uniq(fmt_sp, s.to_managed())));
|
2013-03-20 22:33:10 -05:00
|
|
|
} else {
|
2013-06-10 02:42:24 -05:00
|
|
|
// we call the push_str function because the
|
|
|
|
// bootstrap doesnt't seem to work if we call the
|
|
|
|
// method.
|
2013-06-12 12:02:55 -05:00
|
|
|
let args = ~[cx.expr_mut_addr_of(fmt_sp, buf()),
|
|
|
|
cx.expr_str(fmt_sp, s.to_managed())];
|
2013-05-19 00:53:42 -05:00
|
|
|
let call = cx.expr_call_global(fmt_sp,
|
2013-05-24 21:35:29 -05:00
|
|
|
~[core_ident,
|
|
|
|
str_ident,
|
|
|
|
push_ident],
|
2013-05-19 00:53:42 -05:00
|
|
|
args);
|
|
|
|
stms.push(cx.stmt_expr(call));
|
2013-03-20 22:33:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invoke the correct conv function in extfmt */
|
2013-03-20 12:01:58 -05:00
|
|
|
PieceConv(ref conv) => {
|
|
|
|
n += 1u;
|
|
|
|
if n >= nargs {
|
|
|
|
cx.span_fatal(sp,
|
2013-05-23 11:09:11 -05:00
|
|
|
"not enough arguments to fmt! \
|
|
|
|
for the given format string");
|
2013-03-20 12:01:58 -05:00
|
|
|
}
|
2013-03-20 22:33:10 -05:00
|
|
|
|
2013-03-20 12:01:58 -05:00
|
|
|
log_conv(conv);
|
2013-03-20 22:33:10 -05:00
|
|
|
/* If the first portion is a conversion, then the local buffer
|
|
|
|
must be initialized as an empty string */
|
|
|
|
if i == 0 {
|
2013-05-19 00:53:42 -05:00
|
|
|
stms.push(cx.stmt_let(fmt_sp, true, ident,
|
2013-06-12 12:02:55 -05:00
|
|
|
cx.expr_str_uniq(fmt_sp, @"")));
|
2013-03-20 22:33:10 -05:00
|
|
|
}
|
2013-05-19 00:53:42 -05:00
|
|
|
stms.push(cx.stmt_expr(make_new_conv(cx, fmt_sp, conv,
|
|
|
|
args[n], buf())));
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-20 22:33:10 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 23:27:39 -05:00
|
|
|
let expected_nargs = n + 1u; // n conversions + the fmt string
|
|
|
|
if expected_nargs < nargs {
|
2011-09-12 05:39:38 -05:00
|
|
|
cx.span_fatal
|
2013-09-27 23:01:58 -05:00
|
|
|
(sp, format!("too many arguments to fmt!. found {}, expected {}",
|
2012-08-22 19:24:52 -05:00
|
|
|
nargs, expected_nargs));
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|
2012-06-28 18:23:12 -05:00
|
|
|
|
2013-08-01 10:34:59 -05:00
|
|
|
cx.expr_block(cx.block(fmt_sp, stms, Some(buf())))
|
2011-08-27 23:27:39 -05:00
|
|
|
}
|