Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.1 KiB
Rust
Raw Normal View History

2020-04-27 23:26:11 +05:30
use rustc_ast as ast;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_expand::base::{self, *};
2020-04-19 13:00:18 +02:00
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::Span;
pub fn expand_concat_idents<'cx>(
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
if tts.is_empty() {
2021-10-03 15:53:02 +09:00
cx.span_err(sp, "concat_idents! takes 1 or more arguments");
return DummyResult::any(sp);
}
let mut res_str = String::new();
for (i, e) in tts.into_trees().enumerate() {
if i & 1 == 1 {
match e {
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
_ => {
2021-10-03 15:53:02 +09:00
cx.span_err(sp, "concat_idents! expecting comma");
return DummyResult::any(sp);
2016-06-06 20:22:48 +05:30
}
}
} else {
if let TokenTree::Token(token, _) = e {
if let Some((ident, _)) = token.ident() {
res_str.push_str(ident.name.as_str());
continue;
2016-06-06 20:22:48 +05:30
}
}
2021-10-03 15:53:02 +09:00
cx.span_err(sp, "concat_idents! requires ident args");
return DummyResult::any(sp);
}
}
2020-04-19 13:00:18 +02:00
let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
2018-03-18 16:47:09 +03:00
struct ConcatIdentsResult {
2020-04-19 13:00:18 +02:00
ident: Ident,
2018-03-18 16:47:09 +03:00
}
2018-03-18 16:47:09 +03:00
impl base::MacResult for ConcatIdentsResult {
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
Some(P(ast::Expr {
id: ast::DUMMY_NODE_ID,
kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
2018-03-18 16:47:09 +03:00
span: self.ident.span,
2019-12-03 16:38:34 +01:00
attrs: ast::AttrVec::new(),
2020-05-19 16:56:20 -04:00
tokens: None,
}))
}
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
Some(P(ast::Ty {
id: ast::DUMMY_NODE_ID,
2019-09-26 17:25:31 +01:00
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
2018-03-18 16:47:09 +03:00
span: self.ident.span,
tokens: None,
}))
}
}
2018-03-18 16:47:09 +03:00
Box::new(ConcatIdentsResult { ident })
}