only file name now shown in message
This commit is contained in:
parent
f02bfe7b34
commit
a7899c15b1
|
@ -8,7 +8,7 @@ pub struct LangSpecs {
|
|||
builtin_features: Vec<crate::builtin::modules::Module>,
|
||||
lang_version: u32,
|
||||
authors: Vec<String>,
|
||||
embedded_files: Vec<(usize, String)>,
|
||||
embedded_files: Vec<(usize, String, String)>,
|
||||
}
|
||||
|
||||
impl LangSpecs {
|
||||
|
@ -17,7 +17,7 @@ impl LangSpecs {
|
|||
&self.builtin_features
|
||||
}
|
||||
|
||||
pub fn embedded_files(&self) -> &[(usize, String)] {
|
||||
pub fn embedded_files(&self) -> &[(usize, String, String)] {
|
||||
&self.embedded_files
|
||||
}
|
||||
}
|
||||
|
@ -91,7 +91,7 @@ fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs) {
|
|||
4 => {
|
||||
for path in from_list(mat.as_str()).iter() {
|
||||
if let Ok(str) = std::fs::read_to_string(path) {
|
||||
specs.embedded_files.push((token_idx, str));
|
||||
specs.embedded_files.push((token_idx, str, path.to_owned()));
|
||||
} else {
|
||||
crate::message(MessageType::Warning, format!("Unable to read embedded file: {path}"));
|
||||
}
|
||||
|
|
|
@ -32,14 +32,14 @@ fn compile(settings: &Settings) -> Option<(Vec<Func>, Vec<Declr>, Vec<BuiltinFun
|
|||
let code = src.code();
|
||||
|
||||
let mut diagnostics = Diagnostics::new(&settings);
|
||||
diagnostics.add_source_origin(code);
|
||||
diagnostics.add_source_origin(code, src.path());
|
||||
|
||||
if let Ok(mut tokens) = tokenize(code, &mut diagnostics) {
|
||||
let specs = crate::direct::resolve_directives(&mut tokens);
|
||||
|
||||
// read source of every embedded file and tokenize
|
||||
for (idx, src) in specs.embedded_files() {
|
||||
diagnostics.add_source_origin(src);
|
||||
for (idx, src, path) in specs.embedded_files() {
|
||||
diagnostics.add_source_origin(src, path);
|
||||
if let Ok(em_tokens) = tokenize(src, &mut diagnostics) {
|
||||
for (idy, em_token) in em_tokens.into_iter().enumerate() {
|
||||
tokens.insert(idx + idy, em_token.to_owned());
|
||||
|
|
|
@ -7,6 +7,7 @@ use core::panic;
|
|||
use std::collections::{VecDeque, HashMap};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum LogLvl {
|
||||
|
@ -31,7 +32,7 @@ pub struct Diagnostics {
|
|||
/// all non critical
|
||||
hints: Vec<DebugNotice>,
|
||||
/// source hash and source string
|
||||
source: HashMap<u64, String>,
|
||||
source: HashMap<u64, (String, String)>,
|
||||
/// flags
|
||||
loglvl: LogLvl,
|
||||
}
|
||||
|
@ -46,12 +47,15 @@ impl Diagnostics {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_source_origin<A>(&mut self, source: A) where A: Into<String> + Hash {
|
||||
pub fn add_source_origin<A>(&mut self, source: A, url: 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());
|
||||
let string = url.into();
|
||||
let file_name = Path::new(&string).file_name().expect("not a falid file path");
|
||||
|
||||
self.source.insert(origin, (file_name.to_str().unwrap().to_string(), source.into()));
|
||||
}
|
||||
|
||||
pub fn set_err<T, S>(&mut self, source: &S, message: &'static crate::token::DebugMsg, ext: T)
|
||||
|
@ -66,7 +70,8 @@ impl Diagnostics {
|
|||
|
||||
let info: DebugInfo = source.clone().into();
|
||||
|
||||
let context = self.source.get(&info.origin).unwrap()
|
||||
let origin = self.source.get(&info.origin).unwrap();
|
||||
let context = origin.1
|
||||
.lines()
|
||||
.nth(info.line)
|
||||
.unwrap()
|
||||
|
@ -77,6 +82,7 @@ impl Diagnostics {
|
|||
msg: message,
|
||||
ext: ext.into(),
|
||||
source: context.to_string(),
|
||||
origin: origin.0.to_owned()
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -87,11 +93,19 @@ impl Diagnostics {
|
|||
{
|
||||
let info: DebugInfo = source.clone().into();
|
||||
|
||||
let origin = self.source.get(&info.origin).unwrap();
|
||||
let context = origin.1
|
||||
.lines()
|
||||
.nth(info.line)
|
||||
.unwrap()
|
||||
.trim();
|
||||
|
||||
self.hints.push(DebugNotice {
|
||||
info,
|
||||
msg: message,
|
||||
ext: ext.into(),
|
||||
source: self.source.get(&info.origin).unwrap().clone(),
|
||||
source: context.to_string(),
|
||||
origin: origin.0.to_owned()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::{fs};
|
||||
|
||||
pub struct CodeSrc {
|
||||
_path: String,
|
||||
path: String,
|
||||
src: String,
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ impl CodeSrc {
|
|||
|
||||
pub fn new(path: &String) -> Result<CodeSrc, String> {
|
||||
Ok(Self {
|
||||
_path: path.to_owned(),
|
||||
path: path.to_owned(),
|
||||
src: Self::read_code(path)?,
|
||||
})
|
||||
}
|
||||
|
@ -28,4 +28,8 @@ impl CodeSrc {
|
|||
pub fn code(&self) -> &String {
|
||||
&self.src
|
||||
}
|
||||
|
||||
pub fn path(&self) -> &String {
|
||||
&self.path
|
||||
}
|
||||
}
|
|
@ -428,6 +428,7 @@ pub struct DebugNotice {
|
|||
/// extra message which is case specific
|
||||
pub ext: String,
|
||||
pub source: String,
|
||||
pub origin: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DebugNotice {
|
||||
|
@ -435,10 +436,11 @@ impl std::fmt::Display for DebugNotice {
|
|||
// write header as:
|
||||
// `Error (56) some syntax error message in line 5:`
|
||||
f.write_fmt(format_args!(
|
||||
"{} ({}) {} in line {}: {}\n",
|
||||
"{} ({}) {} at: {} in line {}: {}\n",
|
||||
self.msg.typ.to_colored(),
|
||||
self.msg.code,
|
||||
self.msg.msg.bold().bright_white(),
|
||||
self.origin,
|
||||
self.info.line + 1,
|
||||
self.ext
|
||||
))?;
|
||||
|
|
Loading…
Reference in New Issue