@@ -167,7 +167,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
167
167
)
168
168
}
169
169
170
- fn create_rules ( macro_definition : & str ) -> MacroRules {
170
+ pub ( crate ) fn create_rules ( macro_definition : & str ) -> MacroRules {
171
171
let source_file = ast:: SourceFile :: parse ( macro_definition) ;
172
172
let macro_definition =
173
173
source_file. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast) . unwrap ( ) ;
@@ -176,7 +176,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
176
176
crate :: MacroRules :: parse ( & definition_tt) . unwrap ( )
177
177
}
178
178
179
- fn expand ( rules : & MacroRules , invocation : & str ) -> tt:: Subtree {
179
+ pub ( crate ) fn expand ( rules : & MacroRules , invocation : & str ) -> tt:: Subtree {
180
180
let source_file = ast:: SourceFile :: parse ( invocation) ;
181
181
let macro_invocation =
182
182
source_file. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast) . unwrap ( ) ;
@@ -186,7 +186,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
186
186
rules. expand ( & invocation_tt) . unwrap ( )
187
187
}
188
188
189
- fn assert_expansion ( rules : & MacroRules , invocation : & str , expansion : & str ) {
189
+ pub ( crate ) fn assert_expansion ( rules : & MacroRules , invocation : & str , expansion : & str ) {
190
190
let expanded = expand ( rules, invocation) ;
191
191
assert_eq ! ( expanded. to_string( ) , expansion) ;
192
192
}
@@ -337,4 +337,46 @@ SOURCE_FILE@[0; 40)
337
337
) ;
338
338
}
339
339
340
+ #[ test]
341
+ fn expand_literals_to_token_tree ( ) {
342
+ fn to_subtree ( tt : & tt:: TokenTree ) -> & tt:: Subtree {
343
+ if let tt:: TokenTree :: Subtree ( subtree) = tt {
344
+ return & subtree;
345
+ }
346
+ unreachable ! ( "It is not a subtree" ) ;
347
+ }
348
+
349
+ fn to_literal ( tt : & tt:: TokenTree ) -> & tt:: Literal {
350
+ if let tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( lit) ) = tt {
351
+ return lit;
352
+ }
353
+ unreachable ! ( "It is not a literal" ) ;
354
+ }
355
+
356
+ let rules = create_rules (
357
+ r#"
358
+ macro_rules! literals {
359
+ ($i:ident) => {
360
+ {
361
+ let a = 'c';
362
+ let c = 1000;
363
+ let f = 12E+99_f64;
364
+ let s = "rust1";
365
+ }
366
+ }
367
+ }
368
+ "# ,
369
+ ) ;
370
+ let expansion = expand ( & rules, "literals!(foo)" ) ;
371
+ let stm_tokens = & to_subtree ( & expansion. token_trees [ 0 ] ) . token_trees ;
372
+
373
+ // [let] [a] [=] ['c'] [;]
374
+ assert_eq ! ( to_literal( & stm_tokens[ 3 ] ) . text, "'c'" ) ;
375
+ // [let] [c] [=] [1000] [;]
376
+ assert_eq ! ( to_literal( & stm_tokens[ 5 + 3 ] ) . text, "1000" ) ;
377
+ // [let] [f] [=] [12E+99_f64] [;]
378
+ assert_eq ! ( to_literal( & stm_tokens[ 10 + 3 ] ) . text, "12E+99_f64" ) ;
379
+ // [let] [s] [=] ["rust1"] [;]
380
+ assert_eq ! ( to_literal( & stm_tokens[ 15 + 3 ] ) . text, "\" rust1\" " ) ;
381
+ }
340
382
}
0 commit comments