]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/concat.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libsyntax / ext / concat.rs
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::string::String;
18
19 pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
20                          sp: codemap::Span,
21                          tts: &[ast::TokenTree])
22                          -> Box<base::MacResult+'static> {
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 = String::new();
28     for e in es {
29         match e.node {
30             ast::ExprLit(ref 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);
36                     }
37                     ast::LitChar(c) => {
38                         accumulator.push(c);
39                     }
40                     ast::LitInt(i, ast::UnsignedIntLit(_)) |
41                     ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) |
42                     ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => {
43                         accumulator.push_str(&format!("{}", i)[]);
44                     }
45                     ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) |
46                     ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => {
47                         accumulator.push_str(&format!("-{}", i)[]);
48                     }
49                     ast::LitBool(b) => {
50                         accumulator.push_str(&format!("{}", b)[]);
51                     }
52                     ast::LitByte(..) |
53                     ast::LitBinary(..) => {
54                         cx.span_err(e.span, "cannot concatenate a binary literal");
55                     }
56                 }
57             }
58             _ => {
59                 cx.span_err(e.span, "expected a literal");
60             }
61         }
62     }
63     base::MacExpr::new(cx.expr_str(
64             sp,
65             token::intern_and_get_ident(&accumulator[..])))
66 }