2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-06-25 22:35:30 +00:00
|
|
|
use syntax::ast;
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ext::base::*;
|
|
|
|
use syntax::ext::base;
|
|
|
|
use syntax::feature_gate;
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::ptr::P;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2017-03-28 05:32:43 +00:00
|
|
|
use syntax_pos::symbol::Symbol;
|
|
|
|
use syntax_pos::hygiene::SyntaxContext;
|
2016-06-20 08:49:33 -07:00
|
|
|
use syntax::tokenstream::TokenTree;
|
2014-04-02 16:54:22 -07:00
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[TokenTree])
|
|
|
|
-> Box<base::MacResult + 'cx> {
|
2015-02-15 23:49:55 +01:00
|
|
|
if !cx.ecfg.enable_concat_idents() {
|
2016-09-24 18:42:54 +02:00
|
|
|
feature_gate::emit_feature_err(&cx.parse_sess,
|
2015-02-15 23:49:55 +01:00
|
|
|
"concat_idents",
|
|
|
|
sp,
|
2015-09-04 16:37:22 -07:00
|
|
|
feature_gate::GateIssue::Language,
|
2015-02-15 23:49:55 +01:00
|
|
|
feature_gate::EXPLAIN_CONCAT_IDENTS);
|
|
|
|
return base::DummyResult::expr(sp);
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut res_str = String::new();
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, e) in tts.iter().enumerate() {
|
2012-12-12 17:08:09 -08:00
|
|
|
if i & 1 == 1 {
|
|
|
|
match *e {
|
2016-06-06 20:22:48 +05:30
|
|
|
TokenTree::Token(_, token::Comma) => {}
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => {
|
|
|
|
cx.span_err(sp, "concat_idents! expecting comma.");
|
2014-04-15 22:00:14 +10:00
|
|
|
return DummyResult::expr(sp);
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match *e {
|
2016-06-06 20:22:48 +05:30
|
|
|
TokenTree::Token(_, token::Ident(ident)) => res_str.push_str(&ident.name.as_str()),
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => {
|
|
|
|
cx.span_err(sp, "concat_idents! requires ident args.");
|
2014-04-15 22:00:14 +10:00
|
|
|
return DummyResult::expr(sp);
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 11:46:32 -07:00
|
|
|
}
|
2017-03-28 05:32:43 +00:00
|
|
|
let res = ast::Ident {
|
|
|
|
name: Symbol::intern(&res_str),
|
|
|
|
ctxt: SyntaxContext::empty().apply_mark(cx.current_expansion.mark),
|
|
|
|
};
|
2011-08-03 11:46:32 -07:00
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
struct Result {
|
|
|
|
ident: ast::Ident,
|
|
|
|
span: Span,
|
|
|
|
};
|
2016-05-19 05:22:42 +00:00
|
|
|
|
|
|
|
impl Result {
|
|
|
|
fn path(&self) -> ast::Path {
|
2016-06-06 20:22:48 +05:30
|
|
|
ast::Path {
|
|
|
|
span: self.span,
|
2017-03-08 20:30:06 +03:00
|
|
|
segments: vec![ast::PathSegment::from_ident(self.ident, self.span)],
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl base::MacResult for Result {
|
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
|
|
|
Some(P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::ExprKind::Path(None, self.path()),
|
|
|
|
span: self.span,
|
2016-06-18 04:01:57 +00:00
|
|
|
attrs: ast::ThinVec::new(),
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
Some(P(ast::Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::TyKind::Path(None, self.path()),
|
|
|
|
span: self.span,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
Box::new(Result {
|
|
|
|
ident: res,
|
|
|
|
span: sp,
|
|
|
|
})
|
2011-08-04 16:20:09 -07:00
|
|
|
}
|