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