From 90ede076cce2c9ce490c1b567f13a499cb3b8ea8 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 14 Oct 2021 04:16:20 -0700 Subject: [PATCH] Rustfmt on error.rs --- schala-lang/language/src/error.rs | 144 ++++++++++++++++-------------- 1 file changed, 79 insertions(+), 65 deletions(-) diff --git a/schala-lang/language/src/error.rs b/schala-lang/language/src/error.rs index 7323ca5..3c6a409 100644 --- a/schala-lang/language/src/error.rs +++ b/schala-lang/language/src/error.rs @@ -1,99 +1,113 @@ +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 { - errors: Vec, - //TODO unify these sometime - formatted_parse_error: Option, + errors: Vec, + //TODO unify these sometime + formatted_parse_error: Option, } impl SchalaError { - - pub(crate) fn display(&self) -> String { - if let Some(ref err) = self.formatted_parse_error { - err.clone() - } else { - self.errors[0].text.as_ref().cloned().unwrap_or_default() + pub(crate) fn display(&self) -> String { + if let Some(ref err) = self.formatted_parse_error { + err.clone() + } else { + self.errors[0].text.as_ref().cloned().unwrap_or_default() + } } - } - 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 } - ] + 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, + }], + } } - } - pub(crate) fn from_string(text: String, stage: Stage) -> Self { - Self { - formatted_parse_error: None, - errors: vec![ - Error { location: None, text: Some(text), stage } - ] + pub(crate) fn from_string(text: String, stage: Stage) -> Self { + Self { + formatted_parse_error: None, + errors: vec![Error { + location: None, + text: Some(text), + stage, + }], + } } - } - 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![], + 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![], + } } - } - pub(crate) fn from_tokens(tokens: &[Token]) -> Option { - let token_errors: Vec = 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(); + pub(crate) fn from_tokens(tokens: &[Token]) -> Option { + let token_errors: Vec = 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(); - if token_errors.is_empty() { - None - } else { - Some(SchalaError { - errors: token_errors, - formatted_parse_error: None, - }) + if token_errors.is_empty() { + None + } else { + Some(SchalaError { + errors: token_errors, + formatted_parse_error: None, + }) + } } - } } #[allow(dead_code)] struct Error { - location: Option, - text: Option, - stage: Stage, + location: Option, + text: Option, + 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; - let line_from_program = source_reference.get_line(line_num); - let location_pointer = format!("{}^", " ".repeat(ch)); + let line_num = error.token.location.line_num; + let ch = error.token.location.char_num; + let line_from_program = source_reference.get_line(line_num); + let location_pointer = format!("{}^", " ".repeat(ch)); - let line_num_digits = format!("{}", line_num).chars().count(); - let space_padding = " ".repeat(line_num_digits); + let line_num_digits = format!("{}", line_num).chars().count(); + let space_padding = " ".repeat(line_num_digits); - let production = match error.production_name { - Some(n) => format!("\n(from production \"{}\")", n), - None => "".to_string() - }; + let production = match error.production_name { + Some(n) => format!("\n(from production \"{}\")", n), + 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 + ) }