@@ -113,19 +113,24 @@ pub fn maybe_new_parser_from_source_str(
113113 maybe_source_file_to_parser ( sess, sess. source_map ( ) . new_source_file ( name, source) )
114114}
115115
116- /// Creates a new parser, handling errors as appropriate if the file doesn't exist.
117- /// If a span is given, that is used on an error as the source of the problem.
116+ /// Creates a new parser, aborting if the file doesn't exist. If a span is given, that is used on
117+ /// an error as the source of the problem.
118118pub fn new_parser_from_file < ' a > ( sess : & ' a ParseSess , path : & Path , sp : Option < Span > ) -> Parser < ' a > {
119- source_file_to_parser ( sess, file_to_source_file ( sess, path, sp) )
120- }
119+ let source_file = sess. source_map ( ) . load_file ( path) . unwrap_or_else ( |e| {
120+ let msg = format ! ( "couldn't read {}: {}" , path. display( ) , e) ;
121+ let mut diag = Diagnostic :: new ( Level :: Fatal , msg) ;
122+ if let Some ( sp) = sp {
123+ diag. span ( sp) ;
124+ }
125+ sess. dcx . emit_diagnostic ( diag) ;
126+ FatalError . raise ( ) ;
127+ } ) ;
121128
122- /// Given a session and a `source_file`, returns a parser.
123- fn source_file_to_parser ( sess : & ParseSess , source_file : Lrc < SourceFile > ) -> Parser < ' _ > {
124129 panictry_buffer ! ( & sess. dcx, maybe_source_file_to_parser( sess, source_file) )
125130}
126131
127- /// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing the
128- /// initial token stream.
132+ /// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
133+ /// the initial token stream.
129134fn maybe_source_file_to_parser (
130135 sess : & ParseSess ,
131136 source_file : Lrc < SourceFile > ,
@@ -142,36 +147,6 @@ fn maybe_source_file_to_parser(
142147
143148// Base abstractions
144149
145- /// Given a session and a path and an optional span (for error reporting),
146- /// add the path to the session's source_map and return the new source_file or
147- /// error when a file can't be read.
148- fn try_file_to_source_file (
149- sess : & ParseSess ,
150- path : & Path ,
151- spanopt : Option < Span > ,
152- ) -> Result < Lrc < SourceFile > , Diagnostic > {
153- sess. source_map ( ) . load_file ( path) . map_err ( |e| {
154- let msg = format ! ( "couldn't read {}: {}" , path. display( ) , e) ;
155- let mut diag = Diagnostic :: new ( Level :: Fatal , msg) ;
156- if let Some ( sp) = spanopt {
157- diag. span ( sp) ;
158- }
159- diag
160- } )
161- }
162-
163- /// Given a session and a path and an optional span (for error reporting),
164- /// adds the path to the session's `source_map` and returns the new `source_file`.
165- fn file_to_source_file ( sess : & ParseSess , path : & Path , spanopt : Option < Span > ) -> Lrc < SourceFile > {
166- match try_file_to_source_file ( sess, path, spanopt) {
167- Ok ( source_file) => source_file,
168- Err ( d) => {
169- sess. dcx . emit_diagnostic ( d) ;
170- FatalError . raise ( ) ;
171- }
172- }
173- }
174-
175150/// Given a `source_file`, produces a sequence of token trees.
176151pub fn source_file_to_stream (
177152 sess : & ParseSess ,
@@ -183,7 +158,7 @@ pub fn source_file_to_stream(
183158
184159/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
185160/// parsing the token stream.
186- pub fn maybe_file_to_stream (
161+ fn maybe_file_to_stream (
187162 sess : & ParseSess ,
188163 source_file : Lrc < SourceFile > ,
189164 override_span : Option < Span > ,
0 commit comments