1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast;
12 use codemap;
13 use ext::base;
14 use ext::build::AstBuilder;
15 use parse::token;
16
17 use std::strbuf::StrBuf;
18
19 pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
20 sp: codemap::Span,
21 tts: &[ast::TokenTree])
22 -> Box<base::MacResult> {
23 let es = match base::get_exprs_from_tts(cx, sp, tts) {
24 Some(e) => e,
25 None => return base::DummyResult::expr(sp)
26 };
27 let mut accumulator = StrBuf::new();
28 for e in es.move_iter() {
29 match e.node {
30 ast::ExprLit(lit) => {
31 match lit.node {
32 ast::LitStr(ref s, _) |
33 ast::LitFloat(ref s, _) |
34 ast::LitFloatUnsuffixed(ref s) => {
35 accumulator.push_str(s.get());
36 }
37 ast::LitChar(c) => {
38 accumulator.push_char(c);
39 }
40 ast::LitInt(i, _) | ast::LitIntUnsuffixed(i) => {
41 accumulator.push_str(format!("{}", i));
42 }
43 ast::LitUint(u, _) => {
44 accumulator.push_str(format!("{}", u));
45 }
46 ast::LitNil => {}
47 ast::LitBool(b) => {
48 accumulator.push_str(format!("{}", b));
49 }
50 ast::LitBinary(..) => {
51 cx.span_err(e.span, "cannot concatenate a binary literal");
52 }
53 }
54 }
55 _ => {
56 cx.span_err(e.span, "expected a literal");
57 }
58 }
59 }
60 base::MacExpr::new(cx.expr_str(
61 sp,
62 token::intern_and_get_ident(accumulator.into_owned())))
63 }