17
17
use { Delimiter , Literal , Spacing , Span , Ident , Punct , Group , TokenStream , TokenTree } ;
18
18
19
19
use syntax:: ext:: base:: { ExtCtxt , ProcMacro } ;
20
- use syntax:: parse:: token;
21
- use syntax:: symbol:: Symbol ;
22
20
use syntax:: tokenstream;
23
21
24
22
/// This is the actual quote!() proc macro
@@ -42,6 +40,7 @@ macro_rules! quote_tok {
42
40
( , ) => { tt2ts!( Punct :: new( ',' , Spacing :: Alone ) ) } ;
43
41
( . ) => { tt2ts!( Punct :: new( '.' , Spacing :: Alone ) ) } ;
44
42
( : ) => { tt2ts!( Punct :: new( ':' , Spacing :: Alone ) ) } ;
43
+ ( ; ) => { tt2ts!( Punct :: new( ';' , Spacing :: Alone ) ) } ;
45
44
( |) => { tt2ts!( Punct :: new( '|' , Spacing :: Alone ) ) } ;
46
45
( :: ) => {
47
46
[
@@ -61,6 +60,7 @@ macro_rules! quote_tok {
61
60
( _) => { tt2ts!( Punct :: new( '_' , Spacing :: Alone ) ) } ;
62
61
( 0 ) => { tt2ts!( Literal :: i8_unsuffixed( 0 ) ) } ;
63
62
( & ) => { tt2ts!( Punct :: new( '&' , Spacing :: Alone ) ) } ;
63
+ ( =) => { tt2ts!( Punct :: new( '=' , Spacing :: Alone ) ) } ;
64
64
( $i: ident) => { tt2ts!( Ident :: new( stringify!( $i) , Span :: def_site( ) ) ) } ;
65
65
}
66
66
@@ -92,15 +92,6 @@ impl ProcMacro for Quoter {
92
92
}
93
93
}
94
94
95
- impl < T : Quote > Quote for Option < T > {
96
- fn quote ( self ) -> TokenStream {
97
- match self {
98
- Some ( t) => quote ! ( Some ( ( quote t) ) ) ,
99
- None => quote ! ( None ) ,
100
- }
101
- }
102
- }
103
-
104
95
impl Quote for TokenStream {
105
96
fn quote ( self ) -> TokenStream {
106
97
if self . is_empty ( ) {
@@ -194,93 +185,23 @@ impl Quote for Span {
194
185
}
195
186
}
196
187
197
- macro_rules! literals {
198
- ( $( $i: ident) ,* ; $( $raw: ident) ,* ) => {
199
- pub struct SpannedSymbol {
200
- sym: Symbol ,
201
- span: Span ,
202
- }
203
-
204
- impl SpannedSymbol {
205
- pub fn new( string: & str , span: Span ) -> SpannedSymbol {
206
- SpannedSymbol { sym: Symbol :: intern( string) , span }
207
- }
208
- }
209
-
210
- impl Quote for SpannedSymbol {
211
- fn quote( self ) -> TokenStream {
212
- quote!( :: __internal:: SpannedSymbol :: new( ( quote self . sym. as_str( ) ) ,
213
- ( quote self . span) ) )
214
- }
215
- }
216
-
217
- pub enum LiteralKind {
218
- $( $i, ) *
219
- $( $raw( u16 ) , ) *
220
- }
221
-
222
- impl LiteralKind {
223
- pub fn with_contents_and_suffix( self , contents: SpannedSymbol ,
224
- suffix: Option <SpannedSymbol >) -> Literal {
225
- let sym = contents. sym;
226
- let suffix = suffix. map( |t| t. sym) ;
227
- match self {
228
- $( LiteralKind :: $i => {
229
- Literal {
230
- lit: token:: Lit :: $i( sym) ,
231
- suffix,
232
- span: contents. span,
233
- }
234
- } ) *
235
- $( LiteralKind :: $raw( n) => {
236
- Literal {
237
- lit: token:: Lit :: $raw( sym, n) ,
238
- suffix,
239
- span: contents. span,
240
- }
241
- } ) *
242
- }
243
- }
244
- }
245
-
246
- impl Literal {
247
- fn kind_contents_and_suffix( self ) -> ( LiteralKind , SpannedSymbol , Option <SpannedSymbol >)
248
- {
249
- let ( kind, contents) = match self . lit {
250
- $( token:: Lit :: $i( contents) => ( LiteralKind :: $i, contents) , ) *
251
- $( token:: Lit :: $raw( contents, n) => ( LiteralKind :: $raw( n) , contents) , ) *
252
- } ;
253
- let suffix = self . suffix. map( |sym| SpannedSymbol :: new( & sym. as_str( ) , self . span( ) ) ) ;
254
- ( kind, SpannedSymbol :: new( & contents. as_str( ) , self . span( ) ) , suffix)
255
- }
256
- }
257
-
258
- impl Quote for LiteralKind {
259
- fn quote( self ) -> TokenStream {
260
- match self {
261
- $( LiteralKind :: $i => quote! {
262
- :: __internal:: LiteralKind :: $i
263
- } , ) *
264
- $( LiteralKind :: $raw( n) => quote! {
265
- :: __internal:: LiteralKind :: $raw( ( quote n) )
266
- } , ) *
267
- }
268
- }
269
- }
270
-
271
- impl Quote for Literal {
272
- fn quote( self ) -> TokenStream {
273
- let ( kind, contents, suffix) = self . kind_contents_and_suffix( ) ;
274
- quote! {
275
- ( quote kind) . with_contents_and_suffix( ( quote contents) , ( quote suffix) )
276
- }
188
+ impl Quote for Literal {
189
+ fn quote ( self ) -> TokenStream {
190
+ quote ! { {
191
+ let mut iter = ( quote self . to_string( ) )
192
+ . parse:: <:: TokenStream >( )
193
+ . unwrap( )
194
+ . into_iter( ) ;
195
+ if let ( Some ( :: TokenTree :: Literal ( mut lit) ) , None ) = ( iter. next( ) , iter. next( ) ) {
196
+ lit. set_span( ( quote self . span) ) ;
197
+ lit
198
+ } else {
199
+ unreachable!( )
277
200
}
278
- }
201
+ } }
279
202
}
280
203
}
281
204
282
- literals ! ( Byte , Char , Float , Str_ , Integer , ByteStr ; StrRaw , ByteStrRaw ) ;
283
-
284
205
impl Quote for Delimiter {
285
206
fn quote ( self ) -> TokenStream {
286
207
macro_rules! gen_match {
0 commit comments