1717use { Delimiter , Literal , Spacing , Span , Ident , Punct , Group , TokenStream , TokenTree } ;
1818
1919use syntax:: ext:: base:: { ExtCtxt , ProcMacro } ;
20- use syntax:: parse:: token;
21- use syntax:: symbol:: Symbol ;
2220use syntax:: tokenstream;
2321
2422/// This is the actual quote!() proc macro
@@ -42,6 +40,7 @@ macro_rules! quote_tok {
4240 ( , ) => { tt2ts!( Punct :: new( ',' , Spacing :: Alone ) ) } ;
4341 ( . ) => { tt2ts!( Punct :: new( '.' , Spacing :: Alone ) ) } ;
4442 ( : ) => { tt2ts!( Punct :: new( ':' , Spacing :: Alone ) ) } ;
43+ ( ; ) => { tt2ts!( Punct :: new( ';' , Spacing :: Alone ) ) } ;
4544 ( |) => { tt2ts!( Punct :: new( '|' , Spacing :: Alone ) ) } ;
4645 ( :: ) => {
4746 [
@@ -61,6 +60,7 @@ macro_rules! quote_tok {
6160 ( _) => { tt2ts!( Punct :: new( '_' , Spacing :: Alone ) ) } ;
6261 ( 0 ) => { tt2ts!( Literal :: i8_unsuffixed( 0 ) ) } ;
6362 ( & ) => { tt2ts!( Punct :: new( '&' , Spacing :: Alone ) ) } ;
63+ ( =) => { tt2ts!( Punct :: new( '=' , Spacing :: Alone ) ) } ;
6464 ( $i: ident) => { tt2ts!( Ident :: new( stringify!( $i) , Span :: def_site( ) ) ) } ;
6565}
6666
@@ -92,15 +92,6 @@ impl ProcMacro for Quoter {
9292 }
9393}
9494
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-
10495impl Quote for TokenStream {
10596 fn quote ( self ) -> TokenStream {
10697 if self . is_empty ( ) {
@@ -194,93 +185,23 @@ impl Quote for Span {
194185 }
195186}
196187
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!( )
277200 }
278- }
201+ } }
279202 }
280203}
281204
282- literals ! ( Byte , Char , Float , Str_ , Integer , ByteStr ; StrRaw , ByteStrRaw ) ;
283-
284205impl Quote for Delimiter {
285206 fn quote ( self ) -> TokenStream {
286207 macro_rules! gen_match {
0 commit comments