]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/concat_idents.rs
Auto merge of #31077 - nagisa:mir-temp-promotion, r=dotdash
[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::{self, TokenTree};
12 use syntax::codemap::Span;
13 use syntax::ext::base::*;
14 use syntax::ext::base;
15 use syntax::feature_gate;
16 use syntax::parse::token;
17 use syntax::parse::token::str_to_ident;
18 use syntax::ptr::P;
19
20 pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
21                               -> Box<base::MacResult+'cx> {
22     if !cx.ecfg.enable_concat_idents() {
23         feature_gate::emit_feature_err(&cx.parse_sess.span_diagnostic,
24                                        "concat_idents",
25                                        sp,
26                                        feature_gate::GateIssue::Language,
27                                        feature_gate::EXPLAIN_CONCAT_IDENTS);
28         return base::DummyResult::expr(sp);
29     }
30
31     let mut res_str = String::new();
32     for (i, e) in tts.iter().enumerate() {
33         if i & 1 == 1 {
34             match *e {
35                 TokenTree::Token(_, token::Comma) => {},
36                 _ => {
37                     cx.span_err(sp, "concat_idents! expecting comma.");
38                     return DummyResult::expr(sp);
39                 },
40             }
41         } else {
42             match *e {
43                 TokenTree::Token(_, token::Ident(ident, _)) => {
44                     res_str.push_str(&ident.name.as_str())
45                 },
46                 _ => {
47                     cx.span_err(sp, "concat_idents! requires ident args.");
48                     return DummyResult::expr(sp);
49                 },
50             }
51         }
52     }
53     let res = str_to_ident(&res_str);
54
55     let e = P(ast::Expr {
56         id: ast::DUMMY_NODE_ID,
57         node: ast::ExprPath(None,
58             ast::Path {
59                  span: sp,
60                  global: false,
61                  segments: vec!(
62                     ast::PathSegment {
63                         identifier: res,
64                         parameters: ast::PathParameters::none(),
65                     }
66                 )
67             }
68         ),
69         span: sp,
70         attrs: None,
71     });
72     MacEager::expr(e)
73 }