]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/ext/concat_idents.rs
9410a51e7a5f660a368534d275fa651d7d5e13e1
[rustc.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 ast;
12 use codemap::Span;
13 use ext::base::*;
14 use ext::base;
15 use feature_gate;
16 use parse::token;
17 use parse::token::{str_to_ident};
18 use ptr::P;
19
20 pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::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::EXPLAIN_CONCAT_IDENTS);
27 return base::DummyResult::expr(sp);
28 }
29
30 let mut res_str = String::new();
31 for (i, e) in tts.iter().enumerate() {
32 if i & 1 == 1 {
33 match *e {
34 ast::TtToken(_, token::Comma) => {},
35 _ => {
36 cx.span_err(sp, "concat_idents! expecting comma.");
37 return DummyResult::expr(sp);
38 },
39 }
40 } else {
41 match *e {
42 ast::TtToken(_, token::Ident(ident, _)) => {
43 res_str.push_str(&token::get_ident(ident))
44 },
45 _ => {
46 cx.span_err(sp, "concat_idents! requires ident args.");
47 return DummyResult::expr(sp);
48 },
49 }
50 }
51 }
52 let res = str_to_ident(&res_str[..]);
53
54 let e = P(ast::Expr {
55 id: ast::DUMMY_NODE_ID,
56 node: ast::ExprPath(
57 ast::Path {
58 span: sp,
59 global: false,
60 segments: vec!(
61 ast::PathSegment {
62 identifier: res,
63 parameters: ast::PathParameters::none(),
64 }
65 )
66 }
67 ),
68 span: sp,
69 });
70 MacExpr::new(e)
71 }