From b88def8a2e6137a8d348d545f7692e651b13b862 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 7 Jan 2019 16:52:46 -0800 Subject: [PATCH] Make error msg better --- schala-lang/language/src/lib.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/schala-lang/language/src/lib.rs b/schala-lang/language/src/lib.rs index ffbd440..b604630 100644 --- a/schala-lang/language/src/lib.rs +++ b/schala-lang/language/src/lib.rs @@ -137,16 +137,20 @@ fn parsing(input: Vec, handle: &mut Schala, comp: Option<&mut fn format_parse_error(error: parsing::ParseError, handle: &mut Schala) -> String { //TODO make token no longer be Optoinal if let Some(tok) = error.token { - let line = tok.offset.0; + let line_num = tok.offset.0; let ch = tok.offset.1; - let line_from_program = handle.source_reference.get_line(line); + let line_from_program = handle.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); + format!(r#" -{} -| -| {} -| {} -"#, error.msg, line_from_program, location_pointer) +{error_msg} +{space_padding} | +{line_num} | {} +{space_padding} | {} +"#, line_from_program, location_pointer, error_msg=error.msg, space_padding=space_padding, line_num=line_num) } else { format!("{}", error.msg) } @@ -205,8 +209,7 @@ impl SourceReference { fn load_new_source(&mut self, source: &str) { //TODO this is a lot of heap allocations - maybe there's a way to make it more efficient? - self.lines = Some(source.lines().map(|s| s.to_string()).collect()); - } + self.lines = Some(source.lines().map(|s| s.to_string()).collect()); } fn get_line(&self, line: usize) -> String { self.lines.as_ref().and_then(|x| x.get(line).map(|s| s.to_string())).unwrap_or(format!("NO LINE FOUND"))