4
4
//! This quasiquoter uses macros 2.0 hygiene to reliably access
5
5
//! items from `proc_macro`, to build a `proc_macro::TokenStream`.
6
6
7
- use crate :: { Delimiter , Group , Ident , Literal , Punct , Spacing , Span , TokenStream , TokenTree } ;
7
+ use crate :: {
8
+ Delimiter , Group , Ident , Literal , Punct , Spacing , Span , ToTokens , TokenStream , TokenTree ,
9
+ } ;
8
10
9
11
macro_rules! minimal_quote_tt {
10
12
( ( $( $t: tt) * ) ) => { Group :: new( Delimiter :: Parenthesis , minimal_quote!( $( $t) * ) ) } ;
@@ -50,7 +52,7 @@ macro_rules! minimal_quote {
50
52
( ) => { TokenStream :: new( ) } ;
51
53
( $( $t: tt) * ) => {
52
54
[
53
- $( TokenStream :: from ( minimal_quote_ts!( $t) ) , ) *
55
+ $( ToTokens :: into_token_stream ( minimal_quote_ts!( $t) ) , ) *
54
56
] . iter( ) . cloned( ) . collect:: <TokenStream >( )
55
57
} ;
56
58
}
@@ -66,48 +68,56 @@ pub fn quote(stream: TokenStream) -> TokenStream {
66
68
}
67
69
let proc_macro_crate = minimal_quote ! ( crate ) ;
68
70
let mut after_dollar = false ;
69
- let tokens = stream
70
- . into_iter ( )
71
- . filter_map ( |tree| {
72
- if after_dollar {
73
- after_dollar = false ;
74
- match tree {
75
- TokenTree :: Ident ( _) => {
76
- return Some ( minimal_quote ! ( Into :: <crate :: TokenStream >:: into(
77
- Clone :: clone( & ( @ tree) ) ) , ) ) ;
78
- }
79
- TokenTree :: Punct ( ref tt) if tt. as_char ( ) == '$' => { }
80
- _ => panic ! ( "`$` must be followed by an ident or `$` in `quote!`" ) ,
81
- }
82
- } else if let TokenTree :: Punct ( ref tt) = tree {
83
- if tt. as_char ( ) == '$' {
84
- after_dollar = true ;
85
- return None ;
71
+
72
+ let mut tokens = crate :: TokenStream :: new ( ) ;
73
+ for tree in stream {
74
+ if after_dollar {
75
+ after_dollar = false ;
76
+ match tree {
77
+ TokenTree :: Ident ( _) => {
78
+ minimal_quote ! ( crate :: ToTokens :: to_tokens( & ( @ tree) , & mut ts) ; )
79
+ . to_tokens ( & mut tokens) ;
80
+ continue ;
86
81
}
82
+ TokenTree :: Punct ( ref tt) if tt. as_char ( ) == '$' => { }
83
+ _ => panic ! ( "`$` must be followed by an ident or `$` in `quote!`" ) ,
87
84
}
85
+ } else if let TokenTree :: Punct ( ref tt) = tree {
86
+ if tt. as_char ( ) == '$' {
87
+ after_dollar = true ;
88
+ continue ;
89
+ }
90
+ }
88
91
89
- Some ( minimal_quote ! ( crate :: TokenStream :: from( ( @ match tree {
90
- TokenTree :: Punct ( tt) => minimal_quote!( crate :: TokenTree :: Punct ( crate :: Punct :: new(
92
+ match tree {
93
+ TokenTree :: Punct ( tt) => {
94
+ minimal_quote ! ( crate :: ToTokens :: to_tokens( & crate :: TokenTree :: Punct ( crate :: Punct :: new(
91
95
( @ TokenTree :: from( Literal :: character( tt. as_char( ) ) ) ) ,
92
96
( @ match tt. spacing( ) {
93
97
Spacing :: Alone => minimal_quote!( crate :: Spacing :: Alone ) ,
94
98
Spacing :: Joint => minimal_quote!( crate :: Spacing :: Joint ) ,
95
99
} ) ,
96
- ) ) ) ,
97
- TokenTree :: Group ( tt) => minimal_quote!( crate :: TokenTree :: Group ( crate :: Group :: new(
100
+ ) ) , & mut ts) ; )
101
+ }
102
+ TokenTree :: Group ( tt) => {
103
+ minimal_quote ! ( crate :: ToTokens :: to_tokens( & crate :: TokenTree :: Group ( crate :: Group :: new(
98
104
( @ match tt. delimiter( ) {
99
105
Delimiter :: Parenthesis => minimal_quote!( crate :: Delimiter :: Parenthesis ) ,
100
106
Delimiter :: Brace => minimal_quote!( crate :: Delimiter :: Brace ) ,
101
107
Delimiter :: Bracket => minimal_quote!( crate :: Delimiter :: Bracket ) ,
102
108
Delimiter :: None => minimal_quote!( crate :: Delimiter :: None ) ,
103
109
} ) ,
104
110
( @ quote( tt. stream( ) ) ) ,
105
- ) ) ) ,
106
- TokenTree :: Ident ( tt) => minimal_quote!( crate :: TokenTree :: Ident ( crate :: Ident :: new(
111
+ ) ) , & mut ts) ; )
112
+ }
113
+ TokenTree :: Ident ( tt) => {
114
+ minimal_quote ! ( crate :: ToTokens :: to_tokens( & crate :: TokenTree :: Ident ( crate :: Ident :: new(
107
115
( @ TokenTree :: from( Literal :: string( & tt. to_string( ) ) ) ) ,
108
116
( @ quote_span( proc_macro_crate. clone( ) , tt. span( ) ) ) ,
109
- ) ) ) ,
110
- TokenTree :: Literal ( tt) => minimal_quote!( crate :: TokenTree :: Literal ( {
117
+ ) ) , & mut ts) ; )
118
+ }
119
+ TokenTree :: Literal ( tt) => {
120
+ minimal_quote ! ( crate :: ToTokens :: to_tokens( & crate :: TokenTree :: Literal ( {
111
121
let mut iter = ( @ TokenTree :: from( Literal :: string( & tt. to_string( ) ) ) )
112
122
. parse:: <crate :: TokenStream >( )
113
123
. unwrap( )
@@ -120,16 +130,22 @@ pub fn quote(stream: TokenStream) -> TokenStream {
120
130
} else {
121
131
unreachable!( )
122
132
}
123
- } ) )
124
- } ) ) , ) )
125
- } )
126
- . collect :: < TokenStream > ( ) ;
127
-
133
+ } ) , & mut ts ) ; )
134
+ }
135
+ }
136
+ . to_tokens ( & mut tokens ) ;
137
+ }
128
138
if after_dollar {
129
139
panic ! ( "unexpected trailing `$` in `quote!`" ) ;
130
140
}
131
141
132
- minimal_quote ! ( [ ( @ tokens) ] . iter( ) . cloned( ) . collect:: <crate :: TokenStream >( ) )
142
+ minimal_quote ! {
143
+ {
144
+ let mut ts = crate :: TokenStream :: new( ) ;
145
+ ( @ tokens)
146
+ ts
147
+ }
148
+ }
133
149
}
134
150
135
151
/// Quote a `Span` into a `TokenStream`.
0 commit comments