]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/concat_idents.rs
Auto merge of #43648 - RalfJung:jemalloc-debug, r=alexcrichton
[rust.git] / src / libsyntax_ext / concat_idents.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use syntax::ast;
12 use syntax::ext::base::*;
13 use syntax::ext::base;
14 use syntax::feature_gate;
15 use syntax::parse::token;
16 use syntax::ptr::P;
17 use syntax_pos::Span;
18 use syntax_pos::symbol::Symbol;
19 use syntax_pos::hygiene::SyntaxContext;
20 use syntax::tokenstream::TokenTree;
21
22 pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt,
23                               sp: Span,
24                               tts: &[TokenTree])
25                               -> Box<base::MacResult + 'cx> {
26     if !cx.ecfg.enable_concat_idents() {
27         feature_gate::emit_feature_err(&cx.parse_sess,
28                                        "concat_idents",
29                                        sp,
30                                        feature_gate::GateIssue::Language,
31                                        feature_gate::EXPLAIN_CONCAT_IDENTS);
32         return base::DummyResult::expr(sp);
33     }
34
35     let mut res_str = String::new();
36     for (i, e) in tts.iter().enumerate() {
37         if i & 1 == 1 {
38             match *e {
39                 TokenTree::Token(_, token::Comma) => {}
40                 _ => {
41                     cx.span_err(sp, "concat_idents! expecting comma.");
42                     return DummyResult::expr(sp);
43                 }
44             }
45         } else {
46             match *e {
47                 TokenTree::Token(_, token::Ident(ident)) => res_str.push_str(&ident.name.as_str()),
48                 _ => {
49                     cx.span_err(sp, "concat_idents! requires ident args.");
50                     return DummyResult::expr(sp);
51                 }
52             }
53         }
54     }
55     let res = ast::Ident {
56         name: Symbol::intern(&res_str),
57         ctxt: SyntaxContext::empty().apply_mark(cx.current_expansion.mark),
58     };
59
60     struct Result {
61         ident: ast::Ident,
62         span: Span,
63     };
64
65     impl Result {
66         fn path(&self) -> ast::Path {
67             ast::Path {
68                 span: self.span,
69                 segments: vec![ast::PathSegment::from_ident(self.ident, self.span)],
70             }
71         }
72     }
73
74     impl base::MacResult for Result {
75         fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
76             Some(P(ast::Expr {
77                 id: ast::DUMMY_NODE_ID,
78                 node: ast::ExprKind::Path(None, self.path()),
79                 span: self.span,
80                 attrs: ast::ThinVec::new(),
81             }))
82         }
83
84         fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
85             Some(P(ast::Ty {
86                 id: ast::DUMMY_NODE_ID,
87                 node: ast::TyKind::Path(None, self.path()),
88                 span: self.span,
89             }))
90         }
91     }
92
93     Box::new(Result {
94         ident: res,
95         span: sp,
96     })
97 }