Rustfmt on error.rs

This commit is contained in:
Greg Shuflin 2021-10-14 04:16:20 -07:00
parent 0cb0145cc5
commit 90ede076cc
1 changed files with 79 additions and 65 deletions

View File

@ -1,7 +1,7 @@
use crate::parsing::ParseError;
use crate::schala::{SourceReference, Stage};
use crate::source_map::Location;
use crate::tokenizing::{Token, TokenKind};
use crate::parsing::ParseError;
use crate::typechecking::TypeError;
pub struct SchalaError {
@ -11,7 +11,6 @@ pub struct SchalaError {
}
impl SchalaError {
pub(crate) fn display(&self) -> String {
if let Some(ref err) = self.formatted_parse_error {
err.clone()
@ -23,22 +22,29 @@ impl SchalaError {
pub(crate) fn from_type_error(err: TypeError) -> Self {
Self {
formatted_parse_error: None,
errors: vec![
Error { location: None, text: Some(err.msg), stage: Stage::Typechecking }
]
errors: vec![Error {
location: None,
text: Some(err.msg),
stage: Stage::Typechecking,
}],
}
}
pub(crate) fn from_string(text: String, stage: Stage) -> Self {
Self {
formatted_parse_error: None,
errors: vec![
Error { location: None, text: Some(text), stage }
]
errors: vec![Error {
location: None,
text: Some(text),
stage,
}],
}
}
pub(crate) fn from_parse_error(parse_error: ParseError, source_reference: &SourceReference) -> Self {
pub(crate) fn from_parse_error(
parse_error: ParseError,
source_reference: &SourceReference,
) -> Self {
Self {
formatted_parse_error: Some(format_parse_error(parse_error, source_reference)),
errors: vec![],
@ -46,15 +52,17 @@ impl SchalaError {
}
pub(crate) fn from_tokens(tokens: &[Token]) -> Option<SchalaError> {
let token_errors: Vec<Error> = tokens.iter()
let token_errors: Vec<Error> = tokens
.iter()
.filter_map(|tok| match tok.kind {
TokenKind::Error(ref err) => Some(Error {
location: Some(tok.location),
text: Some(err.clone()),
stage: Stage::Tokenizing,
}),
_ => None
}).collect();
_ => None,
})
.collect();
if token_errors.is_empty() {
None
@ -74,7 +82,6 @@ struct Error {
stage: Stage,
}
fn format_parse_error(error: ParseError, source_reference: &SourceReference) -> String {
let line_num = error.token.location.line_num;
let ch = error.token.location.char_num;
@ -86,14 +93,21 @@ fn format_parse_error(error: ParseError, source_reference: &SourceReference) ->
let production = match error.production_name {
Some(n) => format!("\n(from production \"{}\")", n),
None => "".to_string()
None => "".to_string(),
};
format!(r#"
format!(
r#"
{error_msg}{production}
{space_padding} |
{line_num} | {}
{space_padding} | {}
"#, line_from_program, location_pointer, error_msg=error.msg, space_padding=space_padding, line_num=line_num, production=production
)
"#,
line_from_program,
location_pointer,
error_msg = error.msg,
space_padding = space_padding,
line_num = line_num,
production = production
)
}