]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/concat.rs
Auto merge of #38835 - alexcrichton:less-overlapped, r=brson
[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 syntax::ast;
12 use syntax::ext::base;
13 use syntax::ext::build::AstBuilder;
14 use syntax::symbol::Symbol;
15 use syntax_pos;
16 use syntax::tokenstream;
17
18 use std::string::String;
19
20 pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
21                          sp: syntax_pos::Span,
22                          tts: &[tokenstream::TokenTree])
23                          -> Box<base::MacResult + 'static> {
24     let es = match base::get_exprs_from_tts(cx, sp, tts) {
25         Some(e) => e,
26         None => return base::DummyResult::expr(sp),
27     };
28     let mut accumulator = String::new();
29     for e in es {
30         match e.node {
31             ast::ExprKind::Lit(ref lit) => {
32                 match lit.node {
33                     ast::LitKind::Str(ref s, _) |
34                     ast::LitKind::Float(ref s, _) |
35                     ast::LitKind::FloatUnsuffixed(ref s) => {
36                         accumulator.push_str(&s.as_str());
37                     }
38                     ast::LitKind::Char(c) => {
39                         accumulator.push(c);
40                     }
41                     ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) |
42                     ast::LitKind::Int(i, ast::LitIntType::Signed(_)) |
43                     ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
44                         accumulator.push_str(&format!("{}", i));
45                     }
46                     ast::LitKind::Bool(b) => {
47                         accumulator.push_str(&format!("{}", b));
48                     }
49                     ast::LitKind::Byte(..) |
50                     ast::LitKind::ByteStr(..) => {
51                         cx.span_err(e.span, "cannot concatenate a byte string literal");
52                     }
53                 }
54             }
55             _ => {
56                 cx.span_err(e.span, "expected a literal");
57             }
58         }
59     }
60     base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
61 }