2013-10-06 04:15:46 +00:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use ast;
|
|
|
|
use codemap;
|
|
|
|
use ext::base;
|
|
|
|
use ext::build::AstBuilder;
|
2014-01-10 22:02:36 +00:00
|
|
|
use parse::token;
|
2013-10-06 04:15:46 +00:00
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
use std::string::String;
|
2015-02-02 23:23:08 +00:00
|
|
|
use std::ops::Deref;
|
2014-04-02 23:54:22 +00:00
|
|
|
|
2013-12-29 05:06:22 +00:00
|
|
|
pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
|
2013-10-06 04:15:46 +00:00
|
|
|
sp: codemap::Span,
|
2014-05-06 01:56:44 +00:00
|
|
|
tts: &[ast::TokenTree])
|
2014-08-28 01:46:52 +00:00
|
|
|
-> Box<base::MacResult+'static> {
|
2014-01-17 14:53:10 +00:00
|
|
|
let es = match base::get_exprs_from_tts(cx, sp, tts) {
|
|
|
|
Some(e) => e,
|
2014-04-15 12:00:14 +00:00
|
|
|
None => return base::DummyResult::expr(sp)
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2014-05-22 23:57:53 +00:00
|
|
|
let mut accumulator = String::new();
|
2015-02-01 01:03:04 +00:00
|
|
|
for e in es {
|
2013-10-06 04:15:46 +00:00
|
|
|
match e.node {
|
2014-09-13 16:06:01 +00:00
|
|
|
ast::ExprLit(ref lit) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
match lit.node {
|
2014-01-16 01:15:39 +00:00
|
|
|
ast::LitStr(ref s, _) |
|
|
|
|
ast::LitFloat(ref s, _) |
|
|
|
|
ast::LitFloatUnsuffixed(ref s) => {
|
2015-02-02 23:23:08 +00:00
|
|
|
accumulator.push_str(s.deref());
|
2014-01-10 22:02:36 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitChar(c) => {
|
2014-10-15 06:05:01 +00:00
|
|
|
accumulator.push(c);
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2014-08-05 07:59:03 +00:00
|
|
|
ast::LitInt(i, ast::UnsignedIntLit(_)) |
|
|
|
|
ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) |
|
|
|
|
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
accumulator.push_str(&format!("{}", i)[]);
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2014-08-05 07:59:03 +00:00
|
|
|
ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) |
|
|
|
|
ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
accumulator.push_str(&format!("-{}", i)[]);
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitBool(b) => {
|
2015-01-07 16:58:31 +00:00
|
|
|
accumulator.push_str(&format!("{}", b)[]);
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|
2014-06-06 15:04:04 +00:00
|
|
|
ast::LitByte(..) |
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::LitBinary(..) => {
|
2013-10-06 04:15:46 +00:00
|
|
|
cx.span_err(e.span, "cannot concatenate a binary literal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.span_err(e.span, "expected a literal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 12:00:14 +00:00
|
|
|
base::MacExpr::new(cx.expr_str(
|
2014-04-02 23:54:22 +00:00
|
|
|
sp,
|
2015-01-07 16:58:31 +00:00
|
|
|
token::intern_and_get_ident(&accumulator[])))
|
2013-10-06 04:15:46 +00:00
|
|
|
}
|