Skip to content

Commit

Permalink
Rename the internal simpler quote macro to minimal_quote
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Jan 1, 2025
1 parent 904d8f6 commit 2a2e87b
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions library/proc_macro/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use crate::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

macro_rules! quote_tt {
(($($t:tt)*)) => { Group::new(Delimiter::Parenthesis, quote!($($t)*)) };
([$($t:tt)*]) => { Group::new(Delimiter::Bracket, quote!($($t)*)) };
({$($t:tt)*}) => { Group::new(Delimiter::Brace, quote!($($t)*)) };
macro_rules! minimal_quote_tt {
(($($t:tt)*)) => { Group::new(Delimiter::Parenthesis, minimal_quote!($($t)*)) };
([$($t:tt)*]) => { Group::new(Delimiter::Bracket, minimal_quote!($($t)*)) };
({$($t:tt)*}) => { Group::new(Delimiter::Brace, minimal_quote!($($t)*)) };
(,) => { Punct::new(',', Spacing::Alone) };
(.) => { Punct::new('.', Spacing::Alone) };
(;) => { Punct::new(';', Spacing::Alone) };
Expand All @@ -21,7 +21,7 @@ macro_rules! quote_tt {
($i:ident) => { Ident::new(stringify!($i), Span::def_site()) };
}

macro_rules! quote_ts {
macro_rules! minimal_quote_ts {
((@ $($t:tt)*)) => { $($t)* };
(::) => {
[
Expand All @@ -35,7 +35,7 @@ macro_rules! quote_ts {
})
.collect::<TokenStream>()
};
($t:tt) => { TokenTree::from(quote_tt!($t)) };
($t:tt) => { TokenTree::from(minimal_quote_tt!($t)) };
}

/// Simpler version of the real `quote!` macro, implemented solely
Expand All @@ -46,11 +46,11 @@ macro_rules! quote_ts {
///
/// Note: supported tokens are a subset of the real `quote!`, but
/// unquoting is different: instead of `$x`, this uses `(@ expr)`.
macro_rules! quote {
macro_rules! minimal_quote {
() => { TokenStream::new() };
($($t:tt)*) => {
[
$(TokenStream::from(quote_ts!($t)),)*
$(TokenStream::from(minimal_quote_ts!($t)),)*
].iter().cloned().collect::<TokenStream>()
};
}
Expand All @@ -62,9 +62,9 @@ macro_rules! quote {
#[unstable(feature = "proc_macro_quote", issue = "54722")]
pub fn quote(stream: TokenStream) -> TokenStream {
if stream.is_empty() {
return quote!(crate::TokenStream::new());
return minimal_quote!(crate::TokenStream::new());
}
let proc_macro_crate = quote!(crate);
let proc_macro_crate = minimal_quote!(crate);
let mut after_dollar = false;
let tokens = stream
.into_iter()
Expand All @@ -73,7 +73,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
after_dollar = false;
match tree {
TokenTree::Ident(_) => {
return Some(quote!(Into::<crate::TokenStream>::into(
return Some(minimal_quote!(Into::<crate::TokenStream>::into(
Clone::clone(&(@ tree))),));
}
TokenTree::Punct(ref tt) if tt.as_char() == '$' => {}
Expand All @@ -86,28 +86,28 @@ pub fn quote(stream: TokenStream) -> TokenStream {
}
}

Some(quote!(crate::TokenStream::from((@ match tree {
TokenTree::Punct(tt) => quote!(crate::TokenTree::Punct(crate::Punct::new(
Some(minimal_quote!(crate::TokenStream::from((@ match tree {
TokenTree::Punct(tt) => minimal_quote!(crate::TokenTree::Punct(crate::Punct::new(
(@ TokenTree::from(Literal::character(tt.as_char()))),
(@ match tt.spacing() {
Spacing::Alone => quote!(crate::Spacing::Alone),
Spacing::Joint => quote!(crate::Spacing::Joint),
Spacing::Alone => minimal_quote!(crate::Spacing::Alone),
Spacing::Joint => minimal_quote!(crate::Spacing::Joint),
}),
))),
TokenTree::Group(tt) => quote!(crate::TokenTree::Group(crate::Group::new(
TokenTree::Group(tt) => minimal_quote!(crate::TokenTree::Group(crate::Group::new(
(@ match tt.delimiter() {
Delimiter::Parenthesis => quote!(crate::Delimiter::Parenthesis),
Delimiter::Brace => quote!(crate::Delimiter::Brace),
Delimiter::Bracket => quote!(crate::Delimiter::Bracket),
Delimiter::None => quote!(crate::Delimiter::None),
Delimiter::Parenthesis => minimal_quote!(crate::Delimiter::Parenthesis),
Delimiter::Brace => minimal_quote!(crate::Delimiter::Brace),
Delimiter::Bracket => minimal_quote!(crate::Delimiter::Bracket),
Delimiter::None => minimal_quote!(crate::Delimiter::None),
}),
(@ quote(tt.stream())),
))),
TokenTree::Ident(tt) => quote!(crate::TokenTree::Ident(crate::Ident::new(
TokenTree::Ident(tt) => minimal_quote!(crate::TokenTree::Ident(crate::Ident::new(
(@ TokenTree::from(Literal::string(&tt.to_string()))),
(@ quote_span(proc_macro_crate.clone(), tt.span())),
))),
TokenTree::Literal(tt) => quote!(crate::TokenTree::Literal({
TokenTree::Literal(tt) => minimal_quote!(crate::TokenTree::Literal({
let mut iter = (@ TokenTree::from(Literal::string(&tt.to_string())))
.parse::<crate::TokenStream>()
.unwrap()
Expand All @@ -129,13 +129,13 @@ pub fn quote(stream: TokenStream) -> TokenStream {
panic!("unexpected trailing `$` in `quote!`");
}

quote!([(@ tokens)].iter().cloned().collect::<crate::TokenStream>())
minimal_quote!([(@ tokens)].iter().cloned().collect::<crate::TokenStream>())
}

/// Quote a `Span` into a `TokenStream`.
/// This is needed to implement a custom quoter.
#[unstable(feature = "proc_macro_quote", issue = "54722")]
pub fn quote_span(proc_macro_crate: TokenStream, span: Span) -> TokenStream {
let id = span.save_span();
quote!((@ proc_macro_crate ) ::Span::recover_proc_macro_span((@ TokenTree::from(Literal::usize_unsuffixed(id)))))
minimal_quote!((@ proc_macro_crate ) ::Span::recover_proc_macro_span((@ TokenTree::from(Literal::usize_unsuffixed(id)))))
}

0 comments on commit 2a2e87b

Please sign in to comment.