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