]> git.proxmox.com Git - rustc.git/blame - src/libsyntax_ext/concat.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / libsyntax_ext / concat.rs
CommitLineData
1a4d82fc
JJ
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
9cc50fc6 11use syntax::ast;
9cc50fc6
SL
12use syntax::ext::base;
13use syntax::ext::build::AstBuilder;
476ff2be 14use syntax::symbol::Symbol;
3157f602
XL
15use syntax_pos;
16use syntax::tokenstream;
1a4d82fc
JJ
17
18use std::string::String;
19
20pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
3157f602
XL
21 sp: syntax_pos::Span,
22 tts: &[tokenstream::TokenTree])
9e0c209e 23 -> Box<base::MacResult + 'static> {
1a4d82fc
JJ
24 let es = match base::get_exprs_from_tts(cx, sp, tts) {
25 Some(e) => e,
9e0c209e 26 None => return base::DummyResult::expr(sp),
1a4d82fc
JJ
27 };
28 let mut accumulator = String::new();
85aaf69f 29 for e in es {
1a4d82fc 30 match e.node {
7453a54e 31 ast::ExprKind::Lit(ref lit) => {
1a4d82fc 32 match lit.node {
7453a54e
SL
33 ast::LitKind::Str(ref s, _) |
34 ast::LitKind::Float(ref s, _) |
35 ast::LitKind::FloatUnsuffixed(ref s) => {
476ff2be 36 accumulator.push_str(&s.as_str());
1a4d82fc 37 }
7453a54e 38 ast::LitKind::Char(c) => {
1a4d82fc
JJ
39 accumulator.push(c);
40 }
7453a54e
SL
41 ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) |
42 ast::LitKind::Int(i, ast::LitIntType::Signed(_)) |
43 ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
c34b1796 44 accumulator.push_str(&format!("{}", i));
1a4d82fc 45 }
7453a54e 46 ast::LitKind::Bool(b) => {
c34b1796 47 accumulator.push_str(&format!("{}", b));
1a4d82fc 48 }
7453a54e
SL
49 ast::LitKind::Byte(..) |
50 ast::LitKind::ByteStr(..) => {
e9174d1e 51 cx.span_err(e.span, "cannot concatenate a byte string literal");
1a4d82fc
JJ
52 }
53 }
54 }
55 _ => {
56 cx.span_err(e.span, "expected a literal");
57 }
58 }
59 }
476ff2be 60 base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
1a4d82fc 61}