2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token::{self, Token};
|
|
|
|
use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
2024-02-25 21:25:26 +00:00
|
|
|
use rustc_ast::{AttrVec, Expr, ExprKind, Path, Ty, TyKind, DUMMY_NODE_ID};
|
2024-03-12 02:55:17 +00:00
|
|
|
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacResult, MacroExpanderResult};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2014-04-02 23:54:22 +00:00
|
|
|
|
2023-04-08 19:37:41 +00:00
|
|
|
use crate::errors;
|
|
|
|
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn expand_concat_idents<'cx>(
|
2019-09-03 14:43:25 +00:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 02:55:17 +00:00
|
|
|
) -> MacroExpanderResult<'cx> {
|
2018-05-03 00:51:39 +00:00
|
|
|
if tts.is_empty() {
|
2024-02-25 21:22:11 +00:00
|
|
|
let guar = cx.dcx().emit_err(errors::ConcatIdentsMissingArgs { span: sp });
|
2024-03-12 02:55:17 +00:00
|
|
|
return ExpandResult::Ready(DummyResult::any(sp, guar));
|
2018-05-03 00:51:39 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
let mut res_str = String::new();
|
2023-05-12 02:51:41 +00:00
|
|
|
for (i, e) in tts.trees().enumerate() {
|
2012-12-13 01:08:09 +00:00
|
|
|
if i & 1 == 1 {
|
2019-08-31 17:08:06 +00:00
|
|
|
match e {
|
Remove `TreeAndSpacing`.
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`.
2022-07-28 00:31:04 +00:00
|
|
|
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
|
2014-01-17 14:53:10 +00:00
|
|
|
_ => {
|
2024-02-25 21:22:11 +00:00
|
|
|
let guar = cx.dcx().emit_err(errors::ConcatIdentsMissingComma { span: sp });
|
2024-03-12 02:55:17 +00:00
|
|
|
return ExpandResult::Ready(DummyResult::any(sp, guar));
|
2016-06-06 14:52:48 +00:00
|
|
|
}
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
Remove `TreeAndSpacing`.
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`.
2022-07-28 00:31:04 +00:00
|
|
|
if let TokenTree::Token(token, _) = e {
|
2020-09-27 16:47:52 +00:00
|
|
|
if let Some((ident, _)) = token.ident() {
|
2021-12-15 03:39:23 +00:00
|
|
|
res_str.push_str(ident.name.as_str());
|
2020-09-27 16:47:52 +00:00
|
|
|
continue;
|
2016-06-06 14:52:48 +00:00
|
|
|
}
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2020-09-27 16:47:52 +00:00
|
|
|
|
2024-02-25 21:22:11 +00:00
|
|
|
let guar = cx.dcx().emit_err(errors::ConcatIdentsIdentArgs { span: sp });
|
2024-03-12 02:55:17 +00:00
|
|
|
return ExpandResult::Ready(DummyResult::any(sp, guar));
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2011-08-03 18:46:32 +00:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
|
2018-03-18 13:47:09 +00:00
|
|
|
|
|
|
|
struct ConcatIdentsResult {
|
2020-04-19 11:00:18 +00:00
|
|
|
ident: Ident,
|
2018-03-18 13:47:09 +00:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2024-02-25 21:25:26 +00:00
|
|
|
impl MacResult for ConcatIdentsResult {
|
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<Expr>> {
|
|
|
|
Some(P(Expr {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
kind: ExprKind::Path(None, Path::from_ident(self.ident)),
|
2018-03-18 13:47:09 +00:00
|
|
|
span: self.ident.span,
|
2024-02-25 21:25:26 +00:00
|
|
|
attrs: AttrVec::new(),
|
2020-05-19 20:56:20 +00:00
|
|
|
tokens: None,
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:25:26 +00:00
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<Ty>> {
|
|
|
|
Some(P(Ty {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
kind: TyKind::Path(None, Path::from_ident(self.ident)),
|
2018-03-18 13:47:09 +00:00
|
|
|
span: self.ident.span,
|
2020-08-21 22:18:04 +00:00
|
|
|
tokens: None,
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 02:55:17 +00:00
|
|
|
ExpandResult::Ready(Box::new(ConcatIdentsResult { ident }))
|
2011-08-04 23:20:09 +00:00
|
|
|
}
|