]>
Commit | Line | Data |
---|---|---|
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 | 11 | use syntax::ast; |
9cc50fc6 SL |
12 | use syntax::ext::base; |
13 | use syntax::ext::build::AstBuilder; | |
14 | use syntax::parse::token; | |
3157f602 XL |
15 | use syntax_pos; |
16 | use syntax::tokenstream; | |
1a4d82fc JJ |
17 | |
18 | use std::string::String; | |
19 | ||
20 | pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, | |
3157f602 XL |
21 | sp: syntax_pos::Span, |
22 | tts: &[tokenstream::TokenTree]) | |
1a4d82fc JJ |
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(); | |
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) => { | |
85aaf69f | 36 | accumulator.push_str(&s); |
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 | } | |
c34b1796 | 60 | base::MacEager::expr(cx.expr_str( |
1a4d82fc | 61 | sp, |
85aaf69f | 62 | token::intern_and_get_ident(&accumulator[..]))) |
1a4d82fc | 63 | } |