blob: 8184fc442676dc517464e4588288d97c2a4b4ced [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001use rustc_data_structures::thin_vec::ThinVec;
2
3use syntax::ast;
4use syntax::ext::base::{self, *};
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -08005use syntax::parse::token::{self, Token};
Inna Palantff3f07a2019-07-11 16:15:26 -07006use syntax::ptr::P;
7use syntax_pos::Span;
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08008use syntax_pos::symbol::Symbol;
Inna Palantff3f07a2019-07-11 16:15:26 -07009use syntax::tokenstream::TokenTree;
10
11pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>,
12 sp: Span,
13 tts: &[TokenTree])
14 -> Box<dyn base::MacResult + 'cx> {
Inna Palantff3f07a2019-07-11 16:15:26 -070015 if tts.is_empty() {
16 cx.span_err(sp, "concat_idents! takes 1 or more arguments.");
17 return DummyResult::any(sp);
18 }
19
20 let mut res_str = String::new();
21 for (i, e) in tts.iter().enumerate() {
22 if i & 1 == 1 {
23 match *e {
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -080024 TokenTree::Token(Token { kind: token::Comma, .. }) => {}
Inna Palantff3f07a2019-07-11 16:15:26 -070025 _ => {
26 cx.span_err(sp, "concat_idents! expecting comma.");
27 return DummyResult::any(sp);
28 }
29 }
30 } else {
31 match *e {
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -080032 TokenTree::Token(Token { kind: token::Ident(name, _), .. }) =>
33 res_str.push_str(&name.as_str()),
Inna Palantff3f07a2019-07-11 16:15:26 -070034 _ => {
35 cx.span_err(sp, "concat_idents! requires ident args.");
36 return DummyResult::any(sp);
37 }
38 }
39 }
40 }
41
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080042 let ident = ast::Ident::new(Symbol::intern(&res_str), sp.apply_mark(cx.current_expansion.id));
Inna Palantff3f07a2019-07-11 16:15:26 -070043
44 struct ConcatIdentsResult { ident: ast::Ident }
45
46 impl base::MacResult for ConcatIdentsResult {
47 fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
48 Some(P(ast::Expr {
49 id: ast::DUMMY_NODE_ID,
50 node: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
51 span: self.ident.span,
52 attrs: ThinVec::new(),
53 }))
54 }
55
56 fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
57 Some(P(ast::Ty {
58 id: ast::DUMMY_NODE_ID,
59 node: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
60 span: self.ident.span,
61 }))
62 }
63 }
64
65 Box::new(ConcatIdentsResult { ident })
66}