2013-10-06 04:15:46 +00:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
use std::char;
|
|
|
|
|
|
|
|
use ast;
|
|
|
|
use codemap;
|
|
|
|
use ext::base;
|
|
|
|
use ext::build::AstBuilder;
|
|
|
|
|
2013-12-29 05:06:22 +00:00
|
|
|
pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
|
2013-10-06 04:15:46 +00:00
|
|
|
sp: codemap::Span,
|
2014-01-09 13:05:33 +00:00
|
|
|
tts: &[ast::TokenTree]) -> base::MacResult {
|
2013-10-06 04:15:46 +00:00
|
|
|
let es = base::get_exprs_from_tts(cx, sp, tts);
|
|
|
|
let mut accumulator = ~"";
|
|
|
|
for e in es.move_iter() {
|
|
|
|
let e = cx.expand_expr(e);
|
|
|
|
match e.node {
|
|
|
|
ast::ExprLit(lit) => {
|
|
|
|
match lit.node {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitStr(s, _) | ast::LitFloat(s, _)
|
|
|
|
| ast::LitFloatUnsuffixed(s) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
accumulator.push_str(s);
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitChar(c) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
accumulator.push_char(char::from_u32(c).unwrap());
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
accumulator.push_str(format!("{}", i));
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitUint(u, _) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
accumulator.push_str(format!("{}", u));
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitNil => {}
|
|
|
|
ast::LitBool(b) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
accumulator.push_str(format!("{}", b));
|
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitBinary(..) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
cx.span_err(e.span, "cannot concatenate a binary literal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.span_err(e.span, "expected a literal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base::MRExpr(cx.expr_str(sp, accumulator.to_managed()));
|
|
|
|
}
|