From 9230e7b857d989afa679444c867d36ea8b322eea Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 14 Sep 2023 14:46:20 +0200 Subject: [PATCH] error message localized to origin files --- src/main.rs | 4 +++- src/parser/data.rs | 32 ++++++++++++++++++++------------ src/token/mod.rs | 17 ++++++++++++----- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index d2f46cf..5266346 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,15 @@ fn compile(settings: &Settings) -> Option<(Vec, Vec, Vec { +pub struct Diagnostics { /// terminating factor on error - err: Option>, + err: Option, /// additional warning and informations /// all non critical - hints: Vec>, - /// source string - source: &'a str, + hints: Vec, + /// source hash and source string + source: HashMap, /// flags loglvl: LogLvl, } -impl<'a> Diagnostics<'a> { - pub fn new(settings: &Settings, source: &'a str) -> Diagnostics<'a> { +impl Diagnostics { + pub fn new(settings: &Settings) -> Diagnostics { Self { err: None, hints: vec![], - source, + source: HashMap::new(), loglvl: settings.loglvl() } } + pub fn add_source_origin(&mut self, source: A) where A: Into + Hash { + let mut hasher = DefaultHasher::new(); + source.hash(&mut hasher); + let origin = hasher.finish(); + + self.source.insert(origin, source.into()); + } + pub fn set_err(&mut self, source: &S, message: &'static crate::token::DebugMsg, ext: T) where T: Into, @@ -62,7 +70,7 @@ impl<'a> Diagnostics<'a> { info, msg: message, ext: ext.into(), - source: self.source, + source: self.source.get(&info.origin).unwrap().clone(), }); } @@ -77,12 +85,12 @@ impl<'a> Diagnostics<'a> { info, msg: message, ext: ext.into(), - source: self.source, + source: self.source.get(&info.origin).unwrap().clone(), }); } } -impl<'a> std::fmt::Display for Diagnostics<'a> { +impl std::fmt::Display for Diagnostics { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for hint in self.hints.iter() { match hint.msg.typ { diff --git a/src/token/mod.rs b/src/token/mod.rs index 94c87ab..301a97b 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -1,5 +1,6 @@ use colored::{ColoredString, Colorize}; -use std::{collections::VecDeque}; +use std::{collections::{VecDeque, hash_map::DefaultHasher}, hash::Hasher}; +use std::hash::Hash; use crate::parser::data::Diagnostics; @@ -420,16 +421,16 @@ pub struct DebugMsg { pub msg: &'static str, } -pub struct DebugNotice<'a> { +pub struct DebugNotice { pub info: DebugInfo, /// generic error description pub msg: &'static DebugMsg, /// extra message which is case specific pub ext: String, - pub source: &'a str, + pub source: String, } -impl<'a> std::fmt::Display for DebugNotice<'a> { +impl std::fmt::Display for DebugNotice { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // write header as: // `Error (56) some syntax error message in line 5:` @@ -472,7 +473,9 @@ pub struct DebugInfo { /// index in source string where the token ends in the current line pub end: usize, /// line number where the line in which the token is begins - pub line: usize + pub line: usize, + /// string url of the source origin + pub origin: u64 } #[derive(Debug)] @@ -623,6 +626,9 @@ pub fn tokenize<'a>(source: &'a str, diagnostics: &mut Diagnostics) -> Result(source: &'a str, diagnostics: &mut Diagnostics) -> Result