Skip to content

Commit

Permalink
Use non-panicking maybe_parser_from_source_str
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Dec 8, 2018
1 parent 69c9765 commit 6d441ce
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::{Duration, Instant};

use syntax::ast;
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
use syntax::errors::Handler;
use syntax::errors::{DiagnosticBuilder, Handler};
use syntax::parse::{self, ParseSess};
use syntax::source_map::{FilePathMapping, SourceMap, Span};

Expand Down Expand Up @@ -604,30 +604,41 @@ fn parse_crate(
) -> Result<ast::Crate, ErrorKind> {
let input_is_stdin = input.is_text();

let mut parser = match input {
Input::File(file) => parse::new_parser_from_file(parse_session, &file),
Input::Text(text) => parse::new_parser_from_source_str(
let parser = match input {
Input::File(file) => Ok(parse::new_parser_from_file(parse_session, &file)),
Input::Text(text) => parse::maybe_new_parser_from_source_str(
parse_session,
syntax::source_map::FileName::Custom("stdin".to_owned()),
text,
),
)
.map_err(|diags| {
diags
.into_iter()
.map(|d| DiagnosticBuilder::new_diagnostic(&parse_session.span_diagnostic, d))
.collect::<Vec<_>>()
}),
};

parser.cfg_mods = false;
if config.skip_children() {
parser.recurse_into_file_modules = false;
}
let result = match parser {
Ok(mut parser) => {
parser.cfg_mods = false;
if config.skip_children() {
parser.recurse_into_file_modules = false;
}

let mut parser = AssertUnwindSafe(parser);
let result = catch_unwind(move || parser.0.parse_crate_mod());
let mut parser = AssertUnwindSafe(parser);
catch_unwind(move || parser.0.parse_crate_mod().map_err(|d| vec![d]))
}
Err(db) => Ok(Err(db)),
};

match result {
Ok(Ok(c)) => {
if !parse_session.span_diagnostic.has_errors() {
return Ok(c);
}
}
Ok(Err(mut e)) => e.emit(),
Ok(Err(mut diagnostics)) => diagnostics.iter_mut().for_each(|d| d.emit()),
Err(_) => {
// Note that if you see this message and want more information,
// then run the `parse_crate_mod` function above without
Expand Down

0 comments on commit 6d441ce

Please sign in to comment.