@@ -94,6 +94,19 @@ pub fn parse_crate_from_file(
9494 // why is there no p.abort_if_errors here?
9595}
9696
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+
97110pub fn parse_crate_from_source_str (
98111 name : ~str ,
99112 source : @~str ,
@@ -317,17 +330,46 @@ mod test {
317330 use std;
318331 use core:: io;
319332 use core:: option:: None ;
333+ use ast;
320334
321335 #[ test] fn to_json_str < E : Encodable < std:: json:: Encoder > > ( val : @E ) -> ~str {
322336 do io:: with_str_writer |writer| {
323337 val. encode ( ~std:: json:: Encoder ( writer) ) ;
324338 }
325339 }
326340
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:
327368 #[ test] fn alltts ( ) {
369+ let source_str = @~"fn foo ( x : int) { x; } ";
328370 let tts = parse_tts_from_source_str (
329371 ~"bogofile",
330- @~" fn foo ( x : int ) { x ; } " ,
372+ source_str ,
331373 ~[ ] ,
332374 new_parse_sess ( None ) ) ;
333375 assert_eq ! (
0 commit comments