Skip to content

Commit 014c8c4

Browse files
implement existing parser fns in terms of fallible fns
1 parent 0fe6aae commit 014c8c4

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/libsyntax/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ macro_rules! panictry {
7070
})
7171
}
7272

73+
// A variant of 'panictry!' that works on a Vec<Diagnostic> instead of a single DiagnosticBuilder.
74+
macro_rules! panictry_buffer {
75+
($handler:expr, $e:expr) => ({
76+
use std::result::Result::{Ok, Err};
77+
use errors::{FatalError, DiagnosticBuilder};
78+
match $e {
79+
Ok(e) => e,
80+
Err(errs) => {
81+
for e in errs {
82+
DiagnosticBuilder::new_diagnostic($handler, e).emit();
83+
}
84+
FatalError.raise()
85+
}
86+
}
87+
})
88+
}
89+
7390
#[macro_export]
7491
macro_rules! unwrap_or {
7592
($opt:expr, $default:expr) => {

src/libsyntax/parse/mod.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ pub fn parse_stream_from_source_str(name: FileName, source: String, sess: &Parse
177177
/// Create a new parser from a source string
178178
pub fn new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String)
179179
-> Parser {
180-
let mut parser = source_file_to_parser(sess, sess.source_map().new_source_file(name, source));
181-
parser.recurse_into_file_modules = false;
182-
parser
180+
panictry_buffer!(&sess.span_diagnostic, maybe_new_parser_from_source_str(sess, name, source))
183181
}
184182

185183
/// Create a new parser from a source string. Returns any buffered errors from lexing the initial
@@ -215,14 +213,8 @@ crate fn new_sub_parser_from_file<'a>(sess: &'a ParseSess,
215213

216214
/// Given a source_file and config, return a parser
217215
fn source_file_to_parser(sess: & ParseSess, source_file: Lrc<SourceFile>) -> Parser {
218-
let end_pos = source_file.end_pos;
219-
let mut parser = stream_to_parser(sess, source_file_to_stream(sess, source_file, None));
220-
221-
if parser.token == token::Eof && parser.span.is_dummy() {
222-
parser.span = Span::new(end_pos, end_pos, parser.span.ctxt());
223-
}
224-
225-
parser
216+
panictry_buffer!(&sess.span_diagnostic,
217+
maybe_source_file_to_parser(sess, source_file))
226218
}
227219

228220
/// Given a source_file and config, return a parser. Returns any buffered errors from lexing the
@@ -269,9 +261,7 @@ fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
269261
pub fn source_file_to_stream(sess: &ParseSess,
270262
source_file: Lrc<SourceFile>,
271263
override_span: Option<Span>) -> TokenStream {
272-
let mut srdr = lexer::StringReader::new(sess, source_file, override_span);
273-
srdr.real_token();
274-
panictry!(srdr.parse_all_token_trees())
264+
panictry_buffer!(&sess.span_diagnostic, maybe_file_to_stream(sess, source_file, override_span))
275265
}
276266

277267
/// Given a source file, produce a sequence of token-trees. Returns any buffered errors from

0 commit comments

Comments
 (0)