2013-10-05 21:15:46 -07: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.
|
|
|
|
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ext::base;
|
|
|
|
use syntax::ext::build::AstBuilder;
|
2016-11-16 10:52:37 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos;
|
2016-06-20 08:49:33 -07:00
|
|
|
use syntax::tokenstream;
|
2013-10-05 21:15:46 -07:00
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
use std::string::String;
|
2014-04-02 16:54:22 -07:00
|
|
|
|
2013-12-28 22:06:22 -07:00
|
|
|
pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
|
2016-06-21 18:08:13 -04:00
|
|
|
sp: syntax_pos::Span,
|
2016-06-20 08:49:33 -07:00
|
|
|
tts: &[tokenstream::TokenTree])
|
2016-06-06 20:22:48 +05:30
|
|
|
-> Box<base::MacResult + 'static> {
|
2014-01-18 01:53:10 +11:00
|
|
|
let es = match base::get_exprs_from_tts(cx, sp, tts) {
|
|
|
|
Some(e) => e,
|
2016-06-06 20:22:48 +05:30
|
|
|
None => return base::DummyResult::expr(sp),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut accumulator = String::new();
|
2015-01-31 20:03:04 -05:00
|
|
|
for e in es {
|
2013-10-05 21:15:46 -07:00
|
|
|
match e.node {
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Lit(ref lit) => {
|
2013-10-05 21:15:46 -07:00
|
|
|
match lit.node {
|
2016-02-08 17:06:20 +01:00
|
|
|
ast::LitKind::Str(ref s, _) |
|
|
|
|
ast::LitKind::Float(ref s, _) |
|
|
|
|
ast::LitKind::FloatUnsuffixed(ref s) => {
|
2016-11-16 10:52:37 +00:00
|
|
|
accumulator.push_str(&s.as_str());
|
2014-01-10 14:02:36 -08:00
|
|
|
}
|
2016-02-08 17:06:20 +01:00
|
|
|
ast::LitKind::Char(c) => {
|
2014-10-14 23:05:01 -07:00
|
|
|
accumulator.push(c);
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
2016-02-08 17:16:23 +01:00
|
|
|
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_)) |
|
|
|
|
ast::LitKind::Int(i, ast::LitIntType::Signed(_)) |
|
|
|
|
ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
|
2015-02-20 14:08:14 -05:00
|
|
|
accumulator.push_str(&format!("{}", i));
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
2016-02-08 17:06:20 +01:00
|
|
|
ast::LitKind::Bool(b) => {
|
2015-02-20 14:08:14 -05:00
|
|
|
accumulator.push_str(&format!("{}", b));
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
2016-02-08 17:06:20 +01:00
|
|
|
ast::LitKind::Byte(..) |
|
|
|
|
ast::LitKind::ByteStr(..) => {
|
2015-09-03 10:54:53 +03:00
|
|
|
cx.span_err(e.span, "cannot concatenate a byte string literal");
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.span_err(e.span, "expected a literal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 23:51:53 +03:00
|
|
|
let sp = sp.apply_mark(cx.current_expansion.mark);
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
|
2013-10-05 21:15:46 -07:00
|
|
|
}
|