@@ -94,6 +94,19 @@ pub fn parse_crate_from_file(
94
94
// why is there no p.abort_if_errors here?
95
95
}
96
96
97
+ pub fn parse_crate_from_file_using_tts (
98
+ input : & Path ,
99
+ cfg : ast:: crate_cfg ,
100
+ sess : @mut ParseSess
101
+ ) -> @ast:: crate {
102
+ let p = new_parser_from_file ( sess, /*bad*/ copy cfg, input) ;
103
+ let tts = p. parse_all_token_trees ( ) ;
104
+ new_parser_from_tts ( sess, cfg, tts) . parse_crate_mod ( /*bad*/ copy cfg)
105
+ // why is there no p.abort_if_errors here?
106
+ }
107
+
108
+
109
+
97
110
pub fn parse_crate_from_source_str (
98
111
name : ~str ,
99
112
source : @~str ,
@@ -317,17 +330,46 @@ mod test {
317
330
use std;
318
331
use core:: io;
319
332
use core:: option:: None ;
333
+ use ast;
320
334
321
335
#[ test] fn to_json_str < E : Encodable < std:: json:: Encoder > > ( val : @E ) -> ~str {
322
336
do io:: with_str_writer |writer| {
323
337
val. encode ( ~std:: json:: Encoder ( writer) ) ;
324
338
}
325
339
}
326
340
341
+ fn string_to_crate ( source_str : @~str ) -> @ast:: crate {
342
+ parse_crate_from_source_str (
343
+ ~"bogofile",
344
+ source_str,
345
+ ~[ ] ,
346
+ new_parse_sess ( None ) )
347
+ }
348
+
349
+ fn string_to_tt_to_crate ( source_str : @~str ) -> @ast:: crate {
350
+ let tts = parse_tts_from_source_str (
351
+ ~"bogofile",
352
+ source_str,
353
+ ~[ ] ,
354
+ new_parse_sess ( None ) ) ;
355
+ new_parser_from_tts ( new_parse_sess ( None ) , ~[ ] , tts)
356
+ . parse_crate_mod ( ~[ ] )
357
+ }
358
+
359
+ // make sure that parsing from TTs produces the same result
360
+ // as parsing from strings
361
+ #[ test] fn tts_produce_the_same_result ( ) {
362
+ let source_str = @~"fn foo ( x : int) { x; } ";
363
+ assert_eq ! ( string_to_tt_to_crate( source_str) ,
364
+ string_to_crate( source_str) ) ;
365
+ }
366
+
367
+ // check the contents of the tt manually:
327
368
#[ test] fn alltts ( ) {
369
+ let source_str = @~"fn foo ( x : int) { x; } ";
328
370
let tts = parse_tts_from_source_str (
329
371
~"bogofile",
330
- @~" fn foo ( x : int ) { x ; } " ,
372
+ source_str ,
331
373
~[ ] ,
332
374
new_parse_sess ( None ) ) ;
333
375
assert_eq ! (
0 commit comments