2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::{self, ExtCtxt};
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::kw;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2014-05-06 01:56:44 +00:00
|
|
|
|
2019-02-04 12:49:54 +00:00
|
|
|
pub fn expand_trace_macros(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
2013-08-31 16:13:04 +00:00
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tt: TokenStream,
|
2018-07-12 09:58:16 +00:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-08-31 17:08:06 +00:00
|
|
|
let mut cursor = tt.into_trees();
|
|
|
|
let mut err = false;
|
|
|
|
let value = match &cursor.next() {
|
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
|
|
|
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::True) => true,
|
|
|
|
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::False) => false,
|
2019-08-31 17:08:06 +00:00
|
|
|
_ => {
|
|
|
|
err = true;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
err |= cursor.next().is_some();
|
|
|
|
if err {
|
|
|
|
cx.span_err(sp, "trace_macros! accepts only `true` or `false`")
|
|
|
|
} else {
|
|
|
|
cx.set_trace_macros(value);
|
2012-08-15 17:45:10 +00:00
|
|
|
}
|
2012-11-27 03:12:31 +00:00
|
|
|
|
2018-12-20 00:57:48 +00:00
|
|
|
base::DummyResult::any_valid(sp)
|
2012-08-15 17:45:10 +00:00
|
|
|
}
|