@@ -42,13 +42,15 @@ extern crate phf_generator;
42
42
43
43
use std:: collections:: HashMap ;
44
44
use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
45
- use syntax:: ast:: { self , TokenTree , Expr , ExprLit , ExprVec } ;
45
+ use syntax:: ast:: { self , Expr , ExprLit , ExprVec , MutImmutable , TokenTree , TyVec } ;
46
46
use syntax:: codemap:: { Span , Spanned } ;
47
47
use syntax:: ext:: base:: { DummyResult , ExtCtxt , MacResult } ;
48
+ use syntax:: ext:: build:: AstBuilder ;
48
49
use syntax:: fold:: Folder ;
49
50
use syntax:: parse;
50
51
use syntax:: parse:: token:: { InternedString , Comma , Eof , FatArrow } ;
51
52
use syntax:: print:: pprust;
53
+ use syntax:: ptr:: P ;
52
54
use rustc_plugin:: Registry ;
53
55
use phf_generator:: HashState ;
54
56
use std:: env;
@@ -162,9 +164,9 @@ fn parse_map(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {
162
164
let mut bad = false ;
163
165
while parser. token != Eof {
164
166
let key = cx. expander ( ) . fold_expr ( parser. parse_expr ( ) . unwrap ( ) ) ;
165
- let key_contents = parse_key ( cx, & * key) . unwrap_or_else ( || {
167
+ let ( key_contents, key ) = parse_key ( cx, key. clone ( ) ) . unwrap_or_else ( || {
166
168
bad = true ;
167
- Key :: Str ( InternedString :: new ( "" ) )
169
+ ( Key :: Str ( InternedString :: new ( "" ) ) , key )
168
170
} ) ;
169
171
170
172
if !parser. eat ( & FatArrow ) {
@@ -201,9 +203,9 @@ fn parse_set(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {
201
203
let mut bad = false ;
202
204
while parser. token != Eof {
203
205
let key = cx. expander ( ) . fold_expr ( parser. parse_expr ( ) . unwrap ( ) ) ;
204
- let key_contents = parse_key ( cx, & * key) . unwrap_or_else ( || {
206
+ let ( key_contents, key ) = parse_key ( cx, key. clone ( ) ) . unwrap_or_else ( || {
205
207
bad = true ;
206
- Key :: Str ( InternedString :: new ( "" ) )
208
+ ( Key :: Str ( InternedString :: new ( "" ) ) , key )
207
209
} ) ;
208
210
209
211
entries. push ( Entry {
@@ -225,34 +227,40 @@ fn parse_set(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {
225
227
Some ( entries)
226
228
}
227
229
228
- fn parse_key ( cx : & mut ExtCtxt , e : & Expr ) -> Option < Key > {
230
+ fn parse_key ( cx : & mut ExtCtxt , e : P < Expr > ) -> Option < ( Key , P < Expr > ) > {
229
231
match e. node {
230
232
ExprLit ( ref lit) => {
231
233
match lit. node {
232
- ast:: LitStr ( ref s, _) => Some ( Key :: Str ( s. clone ( ) ) ) ,
233
- ast:: LitByteStr ( ref b) => Some ( Key :: Binary ( b. clone ( ) ) ) ,
234
- ast:: LitByte ( b) => Some ( Key :: U8 ( b) ) ,
235
- ast:: LitChar ( c) => Some ( Key :: Char ( c) ) ,
236
- ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI8 , ast:: Plus ) ) => Some ( Key :: I8 ( i as i8 ) ) ,
234
+ ast:: LitStr ( ref s, _) => Some ( ( Key :: Str ( s. clone ( ) ) , e. clone ( ) ) ) ,
235
+ ast:: LitByteStr ( ref b) => {
236
+ let u8_type = cx. ty_path ( cx. path_ident ( e. span , cx. ident_of ( "u8" ) ) ) ;
237
+ let array_type = cx. ty ( e. span , TyVec ( u8_type) ) ;
238
+ let slice_type = cx. ty_rptr ( e. span , array_type, None , MutImmutable ) ;
239
+ let key = cx. expr_cast ( e. span , e. clone ( ) , slice_type) ;
240
+ Some ( ( Key :: Binary ( b. clone ( ) ) , key) )
241
+ } ,
242
+ ast:: LitByte ( b) => Some ( ( Key :: U8 ( b) , e. clone ( ) ) ) ,
243
+ ast:: LitChar ( c) => Some ( ( Key :: Char ( c) , e. clone ( ) ) ) ,
244
+ ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI8 , ast:: Plus ) ) => Some ( ( Key :: I8 ( i as i8 ) , e. clone ( ) ) ) ,
237
245
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI8 , ast:: Minus ) ) =>
238
- Some ( Key :: I8 ( -( i as i8 ) ) ) ,
246
+ Some ( ( Key :: I8 ( -( i as i8 ) ) , e . clone ( ) ) ) ,
239
247
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI16 , ast:: Plus ) ) =>
240
- Some ( Key :: I16 ( i as i16 ) ) ,
248
+ Some ( ( Key :: I16 ( i as i16 ) , e . clone ( ) ) ) ,
241
249
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI16 , ast:: Minus ) ) =>
242
- Some ( Key :: I16 ( -( i as i16 ) ) ) ,
250
+ Some ( ( Key :: I16 ( -( i as i16 ) ) , e . clone ( ) ) ) ,
243
251
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI32 , ast:: Plus ) ) =>
244
- Some ( Key :: I32 ( i as i32 ) ) ,
252
+ Some ( ( Key :: I32 ( i as i32 ) , e . clone ( ) ) ) ,
245
253
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI32 , ast:: Minus ) ) =>
246
- Some ( Key :: I32 ( -( i as i32 ) ) ) ,
254
+ Some ( ( Key :: I32 ( -( i as i32 ) ) , e . clone ( ) ) ) ,
247
255
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI64 , ast:: Plus ) ) =>
248
- Some ( Key :: I64 ( i as i64 ) ) ,
256
+ Some ( ( Key :: I64 ( i as i64 ) , e . clone ( ) ) ) ,
249
257
ast:: LitInt ( i, ast:: SignedIntLit ( ast:: TyI64 , ast:: Minus ) ) =>
250
- Some ( Key :: I64 ( -( i as i64 ) ) ) ,
251
- ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU8 ) ) => Some ( Key :: U8 ( i as u8 ) ) ,
252
- ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU16 ) ) => Some ( Key :: U16 ( i as u16 ) ) ,
253
- ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU32 ) ) => Some ( Key :: U32 ( i as u32 ) ) ,
254
- ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU64 ) ) => Some ( Key :: U64 ( i as u64 ) ) ,
255
- ast:: LitBool ( b) => Some ( Key :: Bool ( b) ) ,
258
+ Some ( ( Key :: I64 ( -( i as i64 ) ) , e . clone ( ) ) ) ,
259
+ ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU8 ) ) => Some ( ( Key :: U8 ( i as u8 ) , e . clone ( ) ) ) ,
260
+ ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU16 ) ) => Some ( ( Key :: U16 ( i as u16 ) , e . clone ( ) ) ) ,
261
+ ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU32 ) ) => Some ( ( Key :: U32 ( i as u32 ) , e . clone ( ) ) ) ,
262
+ ast:: LitInt ( i, ast:: UnsignedIntLit ( ast:: TyU64 ) ) => Some ( ( Key :: U64 ( i as u64 ) , e . clone ( ) ) ) ,
263
+ ast:: LitBool ( b) => Some ( ( Key :: Bool ( b) , e . clone ( ) ) ) ,
256
264
_ => {
257
265
cx. span_err ( e. span , "unsupported literal type" ) ;
258
266
None
@@ -271,7 +279,7 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option<Key> {
271
279
None
272
280
} ) . collect ( ) ;
273
281
if bytes. iter ( ) . all ( |x| x. is_some ( ) ) {
274
- Some ( Key :: Binary ( std:: rc:: Rc :: new ( bytes. iter ( ) . map ( |x| x. unwrap ( ) ) . collect ( ) ) ) )
282
+ Some ( ( Key :: Binary ( std:: rc:: Rc :: new ( bytes. iter ( ) . map ( |x| x. unwrap ( ) ) . collect ( ) ) ) , e . clone ( ) ) )
275
283
} else {
276
284
cx. span_err ( e. span ,
277
285
"not all elements of an expected u8 array literal were u8 literals" ) ;
0 commit comments