]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/concat_idents.rs
Auto merge of #65202 - pietroalbini:scriptify-ci-config, r=alexcrichton
[rust.git] / src / libsyntax_ext / concat_idents.rs
1 use rustc_data_structures::thin_vec::ThinVec;
2
3 use syntax::ast;
4 use syntax_expand::base::{self, *};
5 use syntax::parse::token::{self, Token};
6 use syntax::ptr::P;
7 use syntax_pos::Span;
8 use syntax_pos::symbol::Symbol;
9 use syntax::tokenstream::{TokenTree, TokenStream};
10
11 pub fn expand_concat_idents<'cx>(cx: &'cx mut ExtCtxt<'_>,
12                                  sp: Span,
13                                  tts: TokenStream)
14                                  -> Box<dyn base::MacResult + 'cx> {
15     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.into_trees().enumerate() {
22         if i & 1 == 1 {
23             match e {
24                 TokenTree::Token(Token { kind: token::Comma, .. }) => {}
25                 _ => {
26                     cx.span_err(sp, "concat_idents! expecting comma.");
27                     return DummyResult::any(sp);
28                 }
29             }
30         } else {
31             match e {
32                 TokenTree::Token(Token { kind: token::Ident(name, _), .. }) =>
33                     res_str.push_str(&name.as_str()),
34                 _ => {
35                     cx.span_err(sp, "concat_idents! requires ident args.");
36                     return DummyResult::any(sp);
37                 }
38             }
39         }
40     }
41
42     let ident = ast::Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
43
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                 kind: 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                 kind: 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 }