2018-08-05 05:04:56 -05:00
|
|
|
use rustc_data_structures::thin_vec::ThinVec;
|
|
|
|
|
2016-06-25 17:35:30 -05:00
|
|
|
use syntax::ast;
|
2019-02-04 06:49:54 -06:00
|
|
|
use syntax::ext::base::{self, *};
|
2019-06-04 12:42:43 -05:00
|
|
|
use syntax::parse::token::{self, Token};
|
2015-12-10 08:23:14 -06:00
|
|
|
use syntax::ptr::P;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2019-06-22 08:18:05 -05:00
|
|
|
use syntax_pos::symbol::Symbol;
|
2016-06-20 10:49:33 -05:00
|
|
|
use syntax::tokenstream::TokenTree;
|
2014-04-02 18:54:22 -05:00
|
|
|
|
2019-02-04 06:49:54 -06:00
|
|
|
pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 09:52:48 -05:00
|
|
|
sp: Span,
|
|
|
|
tts: &[TokenTree])
|
2018-07-12 04:58:16 -05:00
|
|
|
-> Box<dyn base::MacResult + 'cx> {
|
2018-05-02 19:51:39 -05:00
|
|
|
if tts.is_empty() {
|
|
|
|
cx.span_err(sp, "concat_idents! takes 1 or more arguments.");
|
2018-12-29 15:56:55 -06:00
|
|
|
return DummyResult::any(sp);
|
2018-05-02 19:51:39 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
let mut res_str = String::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, e) in tts.iter().enumerate() {
|
2012-12-12 19:08:09 -06:00
|
|
|
if i & 1 == 1 {
|
|
|
|
match *e {
|
2019-06-04 12:42:43 -05:00
|
|
|
TokenTree::Token(Token { kind: token::Comma, .. }) => {}
|
2014-01-17 08:53:10 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_err(sp, "concat_idents! expecting comma.");
|
2018-12-29 15:56:55 -06:00
|
|
|
return DummyResult::any(sp);
|
2016-06-06 09:52:48 -05:00
|
|
|
}
|
2012-12-12 19:08:09 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match *e {
|
2019-06-05 03:56:06 -05:00
|
|
|
TokenTree::Token(Token { kind: token::Ident(name, _), .. }) =>
|
|
|
|
res_str.push_str(&name.as_str()),
|
2014-01-17 08:53:10 -06:00
|
|
|
_ => {
|
|
|
|
cx.span_err(sp, "concat_idents! requires ident args.");
|
2018-12-29 15:56:55 -06:00
|
|
|
return DummyResult::any(sp);
|
2016-06-06 09:52:48 -05:00
|
|
|
}
|
2012-12-12 19:08:09 -06:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 13:46:32 -05:00
|
|
|
}
|
2016-05-19 00:22:42 -05:00
|
|
|
|
2019-07-15 17:42:58 -05:00
|
|
|
let ident = ast::Ident::new(Symbol::intern(&res_str), sp.apply_mark(cx.current_expansion.id));
|
2018-03-18 08:47:09 -05:00
|
|
|
|
|
|
|
struct ConcatIdentsResult { ident: ast::Ident }
|
2016-05-19 00:22:42 -05:00
|
|
|
|
2018-03-18 08:47:09 -05:00
|
|
|
impl base::MacResult for ConcatIdentsResult {
|
2016-05-19 00:22:42 -05:00
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
|
|
|
Some(P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-03-18 19:54:56 -05:00
|
|
|
node: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 08:47:09 -05:00
|
|
|
span: self.ident.span,
|
2018-08-05 05:04:56 -05:00
|
|
|
attrs: ThinVec::new(),
|
2016-05-19 00:22:42 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
Some(P(ast::Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-03-18 19:54:56 -05:00
|
|
|
node: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 08:47:09 -05:00
|
|
|
span: self.ident.span,
|
2016-05-19 00:22:42 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 08:47:09 -05:00
|
|
|
Box::new(ConcatIdentsResult { ident })
|
2011-08-04 18:20:09 -05:00
|
|
|
}
|