(index<- )        ./libsyntax/ext/concat_idents.rs

    git branch:    * master           5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
    modified:    Fri May  9 13:02:28 2014
  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 owned_slice::OwnedSlice;
 16  use parse::token;
 17  use parse::token::{str_to_ident};
 18  
 19  use std::strbuf::StrBuf;
 20  
 21  pub fn expand_syntax_ext(cx: &mut ExtCtxt, spSpan, tts: &[ast::TokenTree])
 22                           -> Box<base::MacResult> {
 23      let mut res_str = StrBuf::new();
 24      for (i, e) in tts.iter().enumerate() {
 25          if i & 1 == 1 {
 26              match *e {
 27                  ast::TTTok(_, token::COMMA) => (),
 28                  _ => {
 29                      cx.span_err(sp, "concat_idents! expecting comma.");
 30                      return DummyResult::expr(sp);
 31                  }
 32              }
 33          } else {
 34              match *e {
 35                  ast::TTTok(_, token::IDENT(ident,_)) => {
 36                      res_str.push_str(token::get_ident(ident).get())
 37                  }
 38                  _ => {
 39                      cx.span_err(sp, "concat_idents! requires ident args.");
 40                      return DummyResult::expr(sp);
 41                  }
 42              }
 43          }
 44      }
 45      let res = str_to_ident(res_str.into_owned());
 46  
 47      let e = @ast::Expr {
 48          id: ast::DUMMY_NODE_ID,
 49          node: ast::ExprPath(
 50              ast::Path {
 51                   span: sp,
 52                   global: false,
 53                   segments: vec!(
 54                      ast::PathSegment {
 55                          identifier: res,
 56                          lifetimes: Vec::new(),
 57                          types: OwnedSlice::empty(),
 58                      }
 59                  )
 60              }
 61          ),
 62          span: sp,
 63      };
 64      MacExpr::new(e)
 65  }