@@ -34,7 +34,7 @@ mod errors;
3434rustc_fluent_macro:: fluent_messages! { "../messages.ftl" }
3535
3636// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.
37- fn unwrap_or_emit_fatal < T > ( expr : Result < T , Vec < Diag < ' _ > > > ) -> T {
37+ pub fn unwrap_or_emit_fatal < T > ( expr : Result < T , Vec < Diag < ' _ > > > ) -> T {
3838 match expr {
3939 Ok ( expr) => expr,
4040 Err ( errs) => {
@@ -46,25 +46,28 @@ fn unwrap_or_emit_fatal<T>(expr: Result<T, Vec<Diag<'_>>>) -> T {
4646 }
4747}
4848
49- /// Creates a new parser from a source string.
50- pub fn new_parser_from_source_str ( psess : & ParseSess , name : FileName , source : String ) -> Parser < ' _ > {
51- unwrap_or_emit_fatal ( maybe_new_parser_from_source_str ( psess, name, source) )
52- }
53-
54- /// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
55- /// token stream; these must be consumed via `emit`, `cancel`, etc., otherwise a panic will occur
56- /// when they are dropped.
57- pub fn maybe_new_parser_from_source_str (
49+ /// Creates a new parser from a source string. On failure, the errors must be consumed via
50+ /// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
51+ /// dropped.
52+ pub fn new_parser_from_source_str (
5853 psess : & ParseSess ,
5954 name : FileName ,
6055 source : String ,
6156) -> Result < Parser < ' _ > , Vec < Diag < ' _ > > > {
62- maybe_new_parser_from_source_file ( psess, psess. source_map ( ) . new_source_file ( name, source) )
57+ let source_file = psess. source_map ( ) . new_source_file ( name, source) ;
58+ new_parser_from_source_file ( psess, source_file)
6359}
6460
65- /// Creates a new parser, aborting if the file doesn't exist. If a span is given, that is used on
66- /// an error as the source of the problem.
67- pub fn new_parser_from_file < ' a > ( psess : & ' a ParseSess , path : & Path , sp : Option < Span > ) -> Parser < ' a > {
61+ /// Creates a new parser from a filename. On failure, the errors must be consumed via
62+ /// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
63+ /// dropped.
64+ ///
65+ /// If a span is given, that is used on an error as the source of the problem.
66+ pub fn new_parser_from_file < ' a > (
67+ psess : & ' a ParseSess ,
68+ path : & Path ,
69+ sp : Option < Span > ,
70+ ) -> Result < Parser < ' a > , Vec < Diag < ' a > > > {
6871 let source_file = psess. source_map ( ) . load_file ( path) . unwrap_or_else ( |e| {
6972 let msg = format ! ( "couldn't read {}: {}" , path. display( ) , e) ;
7073 let mut err = psess. dcx . struct_fatal ( msg) ;
@@ -73,23 +76,21 @@ pub fn new_parser_from_file<'a>(psess: &'a ParseSess, path: &Path, sp: Option<Sp
7376 }
7477 err. emit ( ) ;
7578 } ) ;
76-
77- unwrap_or_emit_fatal ( maybe_new_parser_from_source_file ( psess, source_file) )
79+ new_parser_from_source_file ( psess, source_file)
7880}
7981
8082/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
8183/// the initial token stream.
82- fn maybe_new_parser_from_source_file (
84+ fn new_parser_from_source_file (
8385 psess : & ParseSess ,
8486 source_file : Lrc < SourceFile > ,
8587) -> Result < Parser < ' _ > , Vec < Diag < ' _ > > > {
8688 let end_pos = source_file. end_position ( ) ;
87- let stream = maybe_source_file_to_stream ( psess, source_file, None ) ?;
89+ let stream = source_file_to_stream ( psess, source_file, None ) ?;
8890 let mut parser = Parser :: new ( psess, stream, None ) ;
8991 if parser. token == token:: Eof {
9092 parser. token . span = Span :: new ( end_pos, end_pos, parser. token . span . ctxt ( ) , None ) ;
9193 }
92-
9394 Ok ( parser)
9495}
9596
@@ -98,14 +99,14 @@ pub fn source_str_to_stream(
9899 name : FileName ,
99100 source : String ,
100101 override_span : Option < Span > ,
101- ) -> TokenStream {
102+ ) -> Result < TokenStream , Vec < Diag < ' _ > > > {
102103 let source_file = psess. source_map ( ) . new_source_file ( name, source) ;
103- unwrap_or_emit_fatal ( maybe_source_file_to_stream ( psess, source_file, override_span) )
104+ source_file_to_stream ( psess, source_file, override_span)
104105}
105106
106107/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
107108/// parsing the token stream.
108- fn maybe_source_file_to_stream < ' psess > (
109+ fn source_file_to_stream < ' psess > (
109110 psess : & ' psess ParseSess ,
110111 source_file : Lrc < SourceFile > ,
111112 override_span : Option < Span > ,
@@ -138,13 +139,18 @@ pub fn parse_in<'a, T>(
138139pub fn fake_token_stream_for_item ( psess : & ParseSess , item : & ast:: Item ) -> TokenStream {
139140 let source = pprust:: item_to_string ( item) ;
140141 let filename = FileName :: macro_expansion_source_code ( & source) ;
141- source_str_to_stream ( psess, filename, source, Some ( item. span ) )
142+ unwrap_or_emit_fatal ( source_str_to_stream ( psess, filename, source, Some ( item. span ) ) )
142143}
143144
144145pub fn fake_token_stream_for_crate ( psess : & ParseSess , krate : & ast:: Crate ) -> TokenStream {
145146 let source = pprust:: crate_to_string_for_macros ( krate) ;
146147 let filename = FileName :: macro_expansion_source_code ( & source) ;
147- source_str_to_stream ( psess, filename, source, Some ( krate. spans . inner_span ) )
148+ unwrap_or_emit_fatal ( source_str_to_stream (
149+ psess,
150+ filename,
151+ source,
152+ Some ( krate. spans . inner_span ) ,
153+ ) )
148154}
149155
150156pub fn parse_cfg_attr (
0 commit comments