332dffb1f9
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is not quite right. `Spacing` makes sense for `TokenTree::Token`, but does not make sense for `TokenTree::Delimited`, because a `TokenTree::Delimited` cannot be joined with another `TokenTree`. This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`, changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the `TreeAndSpacing` typedef. The commit removes these two impls: - `impl From<TokenTree> for TokenStream` - `impl From<TokenTree> for TreeAndSpacing` These were useful, but also resulted in code with many `.into()` calls that was hard to read, particularly for anyone not highly familiar with the relevant types. This commit makes some other changes to compensate: - `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`. - `TokenStream::token_{alone,joint}()` are added. - `TokenStream::delimited` is added. This results in things like this: ```rust TokenTree::token(token::Semi, stmt.span).into() ``` changing to this: ```rust TokenStream::token_alone(token::Semi, stmt.span) ``` This makes the type of the result, and its spacing, clearer. These changes also simplifies `Cursor` and `CursorRef`, because they no longer need to distinguish between `next` and `next_with_spacing`.
71 lines
2.1 KiB
Rust
71 lines
2.1 KiB
Rust
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, *};
|
|
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() {
|
|
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, .. }, _) => {}
|
|
_ => {
|
|
cx.span_err(sp, "concat_idents! expecting comma");
|
|
return DummyResult::any(sp);
|
|
}
|
|
}
|
|
} else {
|
|
if let TokenTree::Token(token, _) = e {
|
|
if let Some((ident, _)) = token.ident() {
|
|
res_str.push_str(ident.name.as_str());
|
|
continue;
|
|
}
|
|
}
|
|
|
|
cx.span_err(sp, "concat_idents! requires ident args");
|
|
return DummyResult::any(sp);
|
|
}
|
|
}
|
|
|
|
let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
|
|
|
|
struct ConcatIdentsResult {
|
|
ident: Ident,
|
|
}
|
|
|
|
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)),
|
|
span: self.ident.span,
|
|
attrs: ast::AttrVec::new(),
|
|
tokens: None,
|
|
}))
|
|
}
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
Some(P(ast::Ty {
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
|
|
span: self.ident.span,
|
|
tokens: None,
|
|
}))
|
|
}
|
|
}
|
|
|
|
Box::new(ConcatIdentsResult { ident })
|
|
}
|