Compare commits
No commits in common. "a567fc75b96a053923fcdf7567c015562fe3a062" and "1e029bd604edfda363a17353a0baa5ad33cb791a" have entirely different histories.
a567fc75b9
...
1e029bd604
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::VecDeque, path::Path};
|
use std::{collections::VecDeque};
|
||||||
|
|
||||||
use crate::{token::{Token, MessageType}, builtin::modules::Module};
|
use crate::{token::{Token, MessageType}, builtin::modules::Module};
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ pub struct LangSpecs {
|
||||||
builtin_features: Vec<crate::builtin::modules::Module>,
|
builtin_features: Vec<crate::builtin::modules::Module>,
|
||||||
lang_version: u32,
|
lang_version: u32,
|
||||||
authors: Vec<String>,
|
authors: Vec<String>,
|
||||||
embedded_files: Vec<(usize, String, String)>,
|
embedded_files: Vec<(usize, String)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LangSpecs {
|
impl LangSpecs {
|
||||||
|
@ -17,17 +17,17 @@ impl LangSpecs {
|
||||||
&self.builtin_features
|
&self.builtin_features
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn embedded_files(&self) -> &[(usize, String, String)] {
|
pub fn embedded_files(&self) -> &[(usize, String)] {
|
||||||
&self.embedded_files
|
&self.embedded_files
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_directives(tokens: &mut VecDeque<Token>, origin: &String) -> LangSpecs {
|
pub fn resolve_directives(tokens: &mut VecDeque<Token>) -> LangSpecs {
|
||||||
let mut specs = LangSpecs::default();
|
let mut specs = LangSpecs::default();
|
||||||
|
|
||||||
for (idx, token) in tokens.iter().enumerate() {
|
for (idx, token) in tokens.iter().enumerate() {
|
||||||
match token {
|
match token {
|
||||||
Token::CompilerDirective(text, _) => parse_directive(text, idx, &mut specs, origin),
|
Token::CompilerDirective(text, _) => parse_directive(text, idx, &mut specs),
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,9 +56,7 @@ pub fn from_list(text: &str) -> Vec<String> {
|
||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin: &String) {
|
fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs) {
|
||||||
|
|
||||||
std::env::set_current_dir(Path::new(origin).parent().unwrap()).expect("unable to change cwd");
|
|
||||||
|
|
||||||
for cap in DIRECTIVE_REGEX.captures_iter(text) {
|
for cap in DIRECTIVE_REGEX.captures_iter(text) {
|
||||||
let mut enumerator = cap.iter().enumerate();
|
let mut enumerator = cap.iter().enumerate();
|
||||||
|
@ -93,7 +91,7 @@ fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin:
|
||||||
4 => {
|
4 => {
|
||||||
for path in from_list(mat.as_str()).iter() {
|
for path in from_list(mat.as_str()).iter() {
|
||||||
if let Ok(str) = std::fs::read_to_string(path) {
|
if let Ok(str) = std::fs::read_to_string(path) {
|
||||||
specs.embedded_files.push((token_idx, str, path.to_owned()));
|
specs.embedded_files.push((token_idx, str));
|
||||||
} else {
|
} else {
|
||||||
crate::message(MessageType::Warning, format!("Unable to read embedded file: {path}"));
|
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 code = src.code();
|
||||||
|
|
||||||
let mut diagnostics = Diagnostics::new(&settings);
|
let mut diagnostics = Diagnostics::new(&settings);
|
||||||
diagnostics.add_source_origin(code, src.path());
|
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, src.path());
|
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, path) in specs.embedded_files() {
|
for (idx, src) in specs.embedded_files() {
|
||||||
diagnostics.add_source_origin(src, path);
|
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());
|
||||||
|
|
|
@ -7,7 +7,6 @@ use core::panic;
|
||||||
use std::collections::{VecDeque, HashMap};
|
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};
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
pub enum LogLvl {
|
pub enum LogLvl {
|
||||||
|
@ -32,7 +31,7 @@ pub struct Diagnostics {
|
||||||
/// all non critical
|
/// all non critical
|
||||||
hints: Vec<DebugNotice>,
|
hints: Vec<DebugNotice>,
|
||||||
/// source hash and source string
|
/// source hash and source string
|
||||||
source: HashMap<u64, (String, String)>,
|
source: HashMap<u64, String>,
|
||||||
/// flags
|
/// flags
|
||||||
loglvl: LogLvl,
|
loglvl: LogLvl,
|
||||||
}
|
}
|
||||||
|
@ -47,15 +46,12 @@ impl Diagnostics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_source_origin<A>(&mut self, source: A, url: A) where A: Into<String> + Hash {
|
pub fn add_source_origin<A>(&mut self, source: A) where A: Into<String> + Hash {
|
||||||
let mut hasher = DefaultHasher::new();
|
let mut hasher = DefaultHasher::new();
|
||||||
source.hash(&mut hasher);
|
source.hash(&mut hasher);
|
||||||
let origin = hasher.finish();
|
let origin = hasher.finish();
|
||||||
|
|
||||||
let string = url.into();
|
self.source.insert(origin, source.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)
|
pub fn set_err<T, S>(&mut self, source: &S, message: &'static crate::token::DebugMsg, ext: T)
|
||||||
|
@ -70,19 +66,11 @@ impl Diagnostics {
|
||||||
|
|
||||||
let info: DebugInfo = source.clone().into();
|
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.err = Some(DebugNotice {
|
self.err = Some(DebugNotice {
|
||||||
info,
|
info,
|
||||||
msg: message,
|
msg: message,
|
||||||
ext: ext.into(),
|
ext: ext.into(),
|
||||||
source: context.to_string(),
|
source: self.source.get(&info.origin).unwrap().clone(),
|
||||||
origin: origin.0.to_owned()
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,19 +81,11 @@ impl Diagnostics {
|
||||||
{
|
{
|
||||||
let info: DebugInfo = source.clone().into();
|
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 {
|
self.hints.push(DebugNotice {
|
||||||
info,
|
info,
|
||||||
msg: message,
|
msg: message,
|
||||||
ext: ext.into(),
|
ext: ext.into(),
|
||||||
source: context.to_string(),
|
source: self.source.get(&info.origin).unwrap().clone(),
|
||||||
origin: origin.0.to_owned()
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{fs};
|
use std::{fs};
|
||||||
|
|
||||||
pub struct CodeSrc {
|
pub struct CodeSrc {
|
||||||
path: String,
|
_path: String,
|
||||||
src: String,
|
src: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ impl CodeSrc {
|
||||||
|
|
||||||
pub fn new(path: &String) -> Result<CodeSrc, String> {
|
pub fn new(path: &String) -> Result<CodeSrc, String> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
path: path.to_owned(),
|
_path: path.to_owned(),
|
||||||
src: Self::read_code(path)?,
|
src: Self::read_code(path)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,4 @@ impl CodeSrc {
|
||||||
pub fn code(&self) -> &String {
|
pub fn code(&self) -> &String {
|
||||||
&self.src
|
&self.src
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path(&self) -> &String {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -428,7 +428,6 @@ pub struct DebugNotice {
|
||||||
/// extra message which is case specific
|
/// extra message which is case specific
|
||||||
pub ext: String,
|
pub ext: String,
|
||||||
pub source: String,
|
pub source: String,
|
||||||
pub origin: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for DebugNotice {
|
impl std::fmt::Display for DebugNotice {
|
||||||
|
@ -436,11 +435,10 @@ impl std::fmt::Display for DebugNotice {
|
||||||
// write header as:
|
// write header as:
|
||||||
// `Error (56) some syntax error message in line 5:`
|
// `Error (56) some syntax error message in line 5:`
|
||||||
f.write_fmt(format_args!(
|
f.write_fmt(format_args!(
|
||||||
"{} ({}) {} at: {} in line {}: {}\n",
|
"{} ({}) {} in line {}: {}\n",
|
||||||
self.msg.typ.to_colored(),
|
self.msg.typ.to_colored(),
|
||||||
self.msg.code,
|
self.msg.code,
|
||||||
self.msg.msg.bold().bright_white(),
|
self.msg.msg.bold().bright_white(),
|
||||||
self.origin,
|
|
||||||
self.info.line + 1,
|
self.info.line + 1,
|
||||||
self.ext
|
self.ext
|
||||||
))?;
|
))?;
|
||||||
|
@ -448,6 +446,10 @@ impl std::fmt::Display for DebugNotice {
|
||||||
f.write_fmt(format_args!(
|
f.write_fmt(format_args!(
|
||||||
" somewhere in here:\n --> `{}`\n",
|
" somewhere in here:\n --> `{}`\n",
|
||||||
self.source
|
self.source
|
||||||
|
.lines()
|
||||||
|
.nth(self.info.line)
|
||||||
|
.unwrap()
|
||||||
|
.trim()
|
||||||
.bold()
|
.bold()
|
||||||
.bright_white()
|
.bright_white()
|
||||||
))
|
))
|
||||||
|
|
Loading…
Reference in New Issue