error message localized to origin files

This commit is contained in:
Sven Vogel 2023-09-14 14:46:20 +02:00
parent 2cec9f1df4
commit 9230e7b857
3 changed files with 35 additions and 18 deletions

View File

@ -31,13 +31,15 @@ fn compile(settings: &Settings) -> Option<(Vec<Func>, Vec<Declr>, Vec<BuiltinFun
for src in settings.get_source().iter() { for src in settings.get_source().iter() {
let code = src.code(); let code = src.code();
let mut diagnostics = Diagnostics::new(&settings, code); let mut diagnostics = Diagnostics::new(&settings);
diagnostics.add_source_origin(code);
if let Ok(mut tokens) = tokenize(code, &mut diagnostics) { if let Ok(mut tokens) = tokenize(code, &mut diagnostics) {
let specs = crate::direct::resolve_directives(&mut tokens); let specs = crate::direct::resolve_directives(&mut tokens);
// read source of every embedded file and tokenize // read source of every embedded file and tokenize
for (idx, src) in specs.embedded_files() { for (idx, src) in specs.embedded_files() {
diagnostics.add_source_origin(src);
if let Ok(em_tokens) = tokenize(src, &mut diagnostics) { if let Ok(em_tokens) = tokenize(src, &mut diagnostics) {
for (idy, em_token) in em_tokens.into_iter().enumerate() { for (idy, em_token) in em_tokens.into_iter().enumerate() {
tokens.insert(idx + idy, em_token.to_owned()); tokens.insert(idx + idy, em_token.to_owned());

View File

@ -4,7 +4,7 @@ use crate::conf::Settings;
use crate::token::{DebugInfo, DebugNotice, Token, MessageType}; use crate::token::{DebugInfo, DebugNotice, Token, MessageType};
use crate::Prim; use crate::Prim;
use core::panic; use core::panic;
use std::collections::VecDeque; use std::collections::{VecDeque, HashMap};
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -24,28 +24,36 @@ impl Default for LogLvl {
} }
} }
pub struct Diagnostics<'a> { pub struct Diagnostics {
/// terminating factor on error /// terminating factor on error
err: Option<DebugNotice<'a>>, err: Option<DebugNotice>,
/// additional warning and informations /// additional warning and informations
/// all non critical /// all non critical
hints: Vec<DebugNotice<'a>>, hints: Vec<DebugNotice>,
/// source string /// source hash and source string
source: &'a str, source: HashMap<u64, String>,
/// flags /// flags
loglvl: LogLvl, loglvl: LogLvl,
} }
impl<'a> Diagnostics<'a> { impl Diagnostics {
pub fn new(settings: &Settings, source: &'a str) -> Diagnostics<'a> { pub fn new(settings: &Settings) -> Diagnostics {
Self { Self {
err: None, err: None,
hints: vec![], hints: vec![],
source, source: HashMap::new(),
loglvl: settings.loglvl() loglvl: settings.loglvl()
} }
} }
pub fn add_source_origin<A>(&mut self, source: A) where A: Into<String> + Hash {
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
let origin = hasher.finish();
self.source.insert(origin, source.into());
}
pub fn set_err<T, S>(&mut self, source: &S, message: &'static crate::token::DebugMsg, ext: T) pub fn set_err<T, S>(&mut self, source: &S, message: &'static crate::token::DebugMsg, ext: T)
where where
T: Into<String>, T: Into<String>,
@ -62,7 +70,7 @@ impl<'a> Diagnostics<'a> {
info, info,
msg: message, msg: message,
ext: ext.into(), ext: ext.into(),
source: self.source, source: self.source.get(&info.origin).unwrap().clone(),
}); });
} }
@ -77,12 +85,12 @@ impl<'a> Diagnostics<'a> {
info, info,
msg: message, msg: message,
ext: ext.into(), 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for hint in self.hints.iter() { for hint in self.hints.iter() {
match hint.msg.typ { match hint.msg.typ {

View File

@ -1,5 +1,6 @@
use colored::{ColoredString, Colorize}; 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; use crate::parser::data::Diagnostics;
@ -420,16 +421,16 @@ pub struct DebugMsg {
pub msg: &'static str, pub msg: &'static str,
} }
pub struct DebugNotice<'a> { pub struct DebugNotice {
pub info: DebugInfo, pub info: DebugInfo,
/// generic error description /// generic error description
pub msg: &'static DebugMsg, pub msg: &'static DebugMsg,
/// extra message which is case specific /// extra message which is case specific
pub ext: String, 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// write header as: // write header as:
// `Error (56) some syntax error message in line 5:` // `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 /// index in source string where the token ends in the current line
pub end: usize, pub end: usize,
/// line number where the line in which the token is begins /// 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)] #[derive(Debug)]
@ -623,6 +626,9 @@ pub fn tokenize<'a>(source: &'a str, diagnostics: &mut Diagnostics) -> Result<Ve
for cap in TOKEN_REGEX.captures_iter(source.as_ref()) { for cap in TOKEN_REGEX.captures_iter(source.as_ref()) {
let mut enumerator = cap.iter().enumerate(); let mut enumerator = cap.iter().enumerate();
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
let origin = hasher.finish();
loop { loop {
let next = enumerator.next(); let next = enumerator.next();
if next.is_none() { if next.is_none() {
@ -648,6 +654,7 @@ pub fn tokenize<'a>(source: &'a str, diagnostics: &mut Diagnostics) -> Result<Ve
start: mat.start() - line_start, start: mat.start() - line_start,
end: mat.end() - line_start, end: mat.end() - line_start,
line: line_count, line: line_count,
origin
}; };
tokens.push_back(match i { tokens.push_back(match i {