-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: chenyan-dfinity <[email protected]>
- Loading branch information
1 parent
979765c
commit 1c46d16
Showing
5 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::token::ParserError; | ||
use codespan_reporting::diagnostic::{Diagnostic, Label}; | ||
use codespan_reporting::files::SimpleFile; | ||
use codespan_reporting::term::{self, termcolor::StandardStream}; | ||
|
||
fn report(e: &ParserError) -> Diagnostic<()> { | ||
use lalrpop_util::ParseError::*; | ||
let mut diag = Diagnostic::error().with_message("parser error"); | ||
let label = match e { | ||
User { error } => Label::primary((), error.span.clone()).with_message(&error.err), | ||
InvalidToken { location } => { | ||
Label::primary((), *location..location + 1).with_message("Invalid token") | ||
} | ||
UnrecognizedEOF { location, expected } => { | ||
diag = diag.with_notes(report_expected(&expected)); | ||
Label::primary((), *location..location + 1).with_message("Unexpected EOF") | ||
} | ||
UnrecognizedToken { token, expected } => { | ||
diag = diag.with_notes(report_expected(&expected)); | ||
Label::primary((), token.0..token.2).with_message("Unexpected token") | ||
} | ||
ExtraToken { token } => Label::primary((), token.0..token.2).with_message("Extra token"), | ||
}; | ||
diag.with_labels(vec![label]) | ||
} | ||
|
||
fn report_expected(expected: &[String]) -> Vec<String> { | ||
if expected.is_empty() { | ||
return Vec::new(); | ||
} | ||
use pretty::RcDoc; | ||
let doc: RcDoc<()> = RcDoc::intersperse( | ||
expected.iter().map(RcDoc::text), | ||
RcDoc::text(",").append(RcDoc::softline()), | ||
); | ||
let header = if expected.len() == 1 { | ||
"Expects" | ||
} else { | ||
"Expects one of" | ||
}; | ||
let doc = RcDoc::text(header).append(RcDoc::softline().append(doc)); | ||
vec![doc.pretty(70).to_string()] | ||
} | ||
|
||
pub fn pretty_parse<T>(name: &str, str: &str) -> Result<T, ParserError> | ||
where | ||
T: std::str::FromStr<Err = ParserError>, | ||
{ | ||
str.parse::<T>().map_err(|e| { | ||
let writer = StandardStream::stderr(term::termcolor::ColorChoice::Auto); | ||
let config = term::Config::default(); | ||
let file = SimpleFile::new(name, str); | ||
term::emit(&mut writer.lock(), &config, &file, &report(&e)).unwrap(); | ||
e | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters