|
| 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 std::char; |
| 12 | + |
| 13 | +use ast; |
| 14 | +use codemap; |
| 15 | +use ext::base; |
| 16 | +use ext::build::AstBuilder; |
| 17 | + |
| 18 | +pub fn expand_syntax_ext(cx: @base::ExtCtxt, |
| 19 | + sp: codemap::Span, |
| 20 | + tts: &[ast::token_tree]) -> base::MacResult { |
| 21 | + let es = base::get_exprs_from_tts(cx, sp, tts); |
| 22 | + let mut accumulator = ~""; |
| 23 | + for e in es.move_iter() { |
| 24 | + let e = cx.expand_expr(e); |
| 25 | + match e.node { |
| 26 | + ast::ExprLit(lit) => { |
| 27 | + match lit.node { |
| 28 | + ast::lit_str(s, _) | |
| 29 | + ast::lit_float(s, _) | |
| 30 | + ast::lit_float_unsuffixed(s) => { |
| 31 | + accumulator.push_str(s); |
| 32 | + } |
| 33 | + ast::lit_char(c) => { |
| 34 | + accumulator.push_char(char::from_u32(c).unwrap()); |
| 35 | + } |
| 36 | + ast::lit_int(i, _) | |
| 37 | + ast::lit_int_unsuffixed(i) => { |
| 38 | + accumulator.push_str(format!("{}", i)); |
| 39 | + } |
| 40 | + ast::lit_uint(u, _) => { |
| 41 | + accumulator.push_str(format!("{}", u)); |
| 42 | + } |
| 43 | + ast::lit_nil => {} |
| 44 | + ast::lit_bool(b) => { |
| 45 | + accumulator.push_str(format!("{}", b)); |
| 46 | + } |
| 47 | + ast::lit_binary(*) => { |
| 48 | + cx.span_err(e.span, "cannot concatenate a binary literal"); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + _ => { |
| 53 | + cx.span_err(e.span, "expected a literal"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return base::MRExpr(cx.expr_str(sp, accumulator.to_managed())); |
| 58 | +} |
0 commit comments