Make error msg better

This commit is contained in:
greg 2019-01-07 16:52:46 -08:00
parent 30676722a3
commit b88def8a2e

View File

@ -137,16 +137,20 @@ fn parsing(input: Vec<tokenizing::Token>, handle: &mut Schala, comp: Option<&mut
fn format_parse_error(error: parsing::ParseError, handle: &mut Schala) -> String { fn format_parse_error(error: parsing::ParseError, handle: &mut Schala) -> String {
//TODO make token no longer be Optoinal //TODO make token no longer be Optoinal
if let Some(tok) = error.token { if let Some(tok) = error.token {
let line = tok.offset.0; let line_num = tok.offset.0;
let ch = tok.offset.1; 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 location_pointer = format!("{}^", " ".repeat(ch));
let line_num_digits = format!("{}", line_num).chars().count();
let space_padding = " ".repeat(line_num_digits);
format!(r#" format!(r#"
{} {error_msg}
| {space_padding} |
| {} {line_num} | {}
| {} {space_padding} | {}
"#, error.msg, line_from_program, location_pointer) "#, line_from_program, location_pointer, error_msg=error.msg, space_padding=space_padding, line_num=line_num)
} else { } else {
format!("{}", error.msg) format!("{}", error.msg)
} }
@ -205,8 +209,7 @@ impl SourceReference {
fn load_new_source(&mut self, source: &str) { 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? //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 { 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")) self.lines.as_ref().and_then(|x| x.get(line).map(|s| s.to_string())).unwrap_or(format!("NO LINE FOUND"))