@@ -35,7 +35,7 @@ mod errors;
3535rustc_fluent_macro:: fluent_messages! { "../messages.ftl" }
3636
3737// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.
38- fn unwrap_or_emit_fatal < T > ( expr : Result < T , Vec < Diag < ' _ > > > ) -> T {
38+ pub fn unwrap_or_emit_fatal < T > ( expr : Result < T , Vec < Diag < ' _ > > > ) -> T {
3939 match expr {
4040 Ok ( expr) => expr,
4141 Err ( errs) => {
@@ -47,25 +47,28 @@ fn unwrap_or_emit_fatal<T>(expr: Result<T, Vec<Diag<'_>>>) -> T {
4747 }
4848}
4949
50- /// Creates a new parser from a source string.
51- pub fn new_parser_from_source_str ( psess : & ParseSess , name : FileName , source : String ) -> Parser < ' _ > {
52- unwrap_or_emit_fatal ( maybe_new_parser_from_source_str ( psess, name, source) )
53- }
54-
55- /// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
56- /// token stream; these must be consumed via `emit`, `cancel`, etc., otherwise a panic will occur
57- /// when they are dropped.
58- pub fn maybe_new_parser_from_source_str (
50+ /// Creates a new parser from a source string. On failure, the errors must be consumed via
51+ /// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
52+ /// dropped.
53+ pub fn new_parser_from_source_str (
5954 psess : & ParseSess ,
6055 name : FileName ,
6156 source : String ,
6257) -> Result < Parser < ' _ > , Vec < Diag < ' _ > > > {
63- maybe_new_parser_from_source_file ( psess, psess. source_map ( ) . new_source_file ( name, source) )
58+ let source_file = psess. source_map ( ) . new_source_file ( name, source) ;
59+ new_parser_from_source_file ( psess, source_file)
6460}
6561
66- /// Creates a new parser, aborting if the file doesn't exist. If a span is given, that is used on
67- /// an error as the source of the problem.
68- pub fn new_parser_from_file < ' a > ( psess : & ' a ParseSess , path : & Path , sp : Option < Span > ) -> Parser < ' a > {
62+ /// Creates a new parser from a filename. On failure, the errors must be consumed via
63+ /// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
64+ /// dropped.
65+ ///
66+ /// If a span is given, that is used on an error as the source of the problem.
67+ pub fn new_parser_from_file < ' a > (
68+ psess : & ' a ParseSess ,
69+ path : & Path ,
70+ sp : Option < Span > ,
71+ ) -> Result < Parser < ' a > , Vec < Diag < ' a > > > {
6972 let source_file = psess. source_map ( ) . load_file ( path) . unwrap_or_else ( |e| {
7073 let msg = format ! ( "couldn't read {}: {}" , path. display( ) , e) ;
7174 let mut err = psess. dcx . struct_fatal ( msg) ;
@@ -74,23 +77,21 @@ pub fn new_parser_from_file<'a>(psess: &'a ParseSess, path: &Path, sp: Option<Sp
7477 }
7578 err. emit ( ) ;
7679 } ) ;
77-
78- unwrap_or_emit_fatal ( maybe_new_parser_from_source_file ( psess, source_file) )
80+ new_parser_from_source_file ( psess, source_file)
7981}
8082
8183/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
8284/// the initial token stream.
83- fn maybe_new_parser_from_source_file (
85+ fn new_parser_from_source_file (
8486 psess : & ParseSess ,
8587 source_file : Lrc < SourceFile > ,
8688) -> Result < Parser < ' _ > , Vec < Diag < ' _ > > > {
8789 let end_pos = source_file. end_position ( ) ;
88- let stream = maybe_source_file_to_stream ( psess, source_file, None ) ?;
90+ let stream = source_file_to_stream ( psess, source_file, None ) ?;
8991 let mut parser = Parser :: new ( psess, stream, None ) ;
9092 if parser. token == token:: Eof {
9193 parser. token . span = Span :: new ( end_pos, end_pos, parser. token . span . ctxt ( ) , None ) ;
9294 }
93-
9495 Ok ( parser)
9596}
9697
@@ -99,14 +100,14 @@ pub fn source_str_to_stream(
99100 name : FileName ,
100101 source : String ,
101102 override_span : Option < Span > ,
102- ) -> TokenStream {
103+ ) -> Result < TokenStream , Vec < Diag < ' _ > > > {
103104 let source_file = psess. source_map ( ) . new_source_file ( name, source) ;
104- unwrap_or_emit_fatal ( maybe_source_file_to_stream ( psess, source_file, override_span) )
105+ source_file_to_stream ( psess, source_file, override_span)
105106}
106107
107108/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
108109/// parsing the token stream.
109- fn maybe_source_file_to_stream < ' psess > (
110+ fn source_file_to_stream < ' psess > (
110111 psess : & ' psess ParseSess ,
111112 source_file : Lrc < SourceFile > ,
112113 override_span : Option < Span > ,
@@ -139,13 +140,18 @@ pub fn parse_in<'a, T>(
139140pub fn fake_token_stream_for_item ( psess : & ParseSess , item : & ast:: Item ) -> TokenStream {
140141 let source = pprust:: item_to_string ( item) ;
141142 let filename = FileName :: macro_expansion_source_code ( & source) ;
142- source_str_to_stream ( psess, filename, source, Some ( item. span ) )
143+ unwrap_or_emit_fatal ( source_str_to_stream ( psess, filename, source, Some ( item. span ) ) )
143144}
144145
145146pub fn fake_token_stream_for_crate ( psess : & ParseSess , krate : & ast:: Crate ) -> TokenStream {
146147 let source = pprust:: crate_to_string_for_macros ( krate) ;
147148 let filename = FileName :: macro_expansion_source_code ( & source) ;
148- source_str_to_stream ( psess, filename, source, Some ( krate. spans . inner_span ) )
149+ unwrap_or_emit_fatal ( source_str_to_stream (
150+ psess,
151+ filename,
152+ source,
153+ Some ( krate. spans . inner_span ) ,
154+ ) )
149155}
150156
151157pub fn parse_cfg_attr (
0 commit comments