improved error messages

- some small bugs are now fixed
- include comments in error messages (if this causes issues, use --hide-comments)
- colors should make more sense now
- error-related things moved to mers_lib/src/errors/
This commit is contained in:
Mark
2023-11-16 14:50:09 +01:00
parent c39e784939
commit 12925fed67
33 changed files with 462 additions and 320 deletions

View File

@@ -5,7 +5,8 @@ use std::{
use crate::{
data::{self, Data, MersType, Type},
program::run::{CheckError, CheckInfo, Info},
errors::CheckError,
program::run::{CheckInfo, Info},
};
use super::Config;

View File

@@ -9,10 +9,8 @@ use crate::{
function::{Function, FunctionT},
Data, MersData, MersType, Type,
},
program::{
self,
run::{CheckError, CheckInfo},
},
errors::CheckError,
program::{self, run::CheckInfo},
};
use super::Config;
@@ -140,13 +138,6 @@ impl Config {
}
}
fn iter_out(
a: &Type,
name: &str,
func: impl Fn(&FunctionT) -> ItersT + Sync + Send,
) -> Result<Type, CheckError> {
iter_out_arg(a, name, func)
}
fn iter_out_arg<T: MersType>(
a: &Type,
name: &str,

View File

@@ -135,7 +135,7 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| {
out: Arc::new(|a, _i| {
if let Some(v) = a.iterable() {
Ok(Type::new(ListT(v)))
} else {

View File

@@ -2,10 +2,8 @@ use std::sync::{Arc, Mutex};
use crate::{
data::{self, Data, MersType, Type},
program::{
self,
run::{CheckError, CheckInfo},
},
errors::CheckError,
program::{self, run::CheckInfo},
};
use super::Config;

View File

@@ -6,10 +6,8 @@ use std::{
use crate::{
data::{self, Data, MersData, MersType, Type},
program::{
self,
run::{CheckError, CheckInfo},
},
errors::CheckError,
program::{self, run::CheckInfo},
};
use super::Config;

View File

@@ -3,8 +3,11 @@ use std::{
sync::{Arc, Mutex},
};
use colored::Colorize;
use crate::{
data::{self, Data, Type},
errors::error_colors,
program::{self, run::CheckInfo},
};
@@ -23,7 +26,17 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::new(data::string::StringT))),
out: Arc::new(|a, _i| {
if a.is_zero_tuple() {
Ok(Type::new(data::string::StringT))
} else {
Err(format!(
"expected (), got {}",
a.to_string().color(error_colors::FunctionArgument)
)
.into())
}
}),
run: Arc::new(|_a, _i| {
let mut line = String::new();
_ = std::io::stdin().read_line(&mut line);
@@ -36,7 +49,7 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::empty_tuple())),
out: Arc::new(|_a, _i| Ok(Type::empty_tuple())),
run: Arc::new(|a, _i| {
eprintln!("{:#?}", a.get());
Data::empty_tuple()
@@ -48,10 +61,10 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::empty_tuple())),
out: Arc::new(|_a, _i| Ok(Type::empty_tuple())),
run: Arc::new(|a, _i| {
eprint!("{}", a.get());
std::io::stderr().lock().flush();
_ = std::io::stderr().lock().flush();
Data::empty_tuple()
}),
}),
@@ -61,7 +74,7 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::empty_tuple())),
out: Arc::new(|_a, _i| Ok(Type::empty_tuple())),
run: Arc::new(|a, _i| {
eprintln!("{}", a.get());
Data::empty_tuple()
@@ -73,10 +86,10 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::empty_tuple())),
out: Arc::new(|_a, _i| Ok(Type::empty_tuple())),
run: Arc::new(|a, _i| {
print!("{}", a.get());
std::io::stdout().lock().flush();
_ = std::io::stdout().lock().flush();
Data::empty_tuple()
}),
}),
@@ -86,7 +99,7 @@ impl Config {
Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, i| Ok(Type::empty_tuple())),
out: Arc::new(|_a, _i| Ok(Type::empty_tuple())),
run: Arc::new(|a, _i| {
println!("{}", a.get());
Data::empty_tuple()

View File

@@ -1,6 +1,6 @@
use crate::program::{
self,
run::{CheckError, SourceRange},
use crate::{
errors::{CheckError, SourceRange},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -1,9 +1,7 @@
use crate::{
errors::{CheckError, SourceRange},
info,
program::{
self,
run::{CheckError, SourceRange},
},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -1,5 +1,7 @@
use crate::program::run::{CheckError, SourceRange};
use crate::{info, program};
use crate::{
errors::{CheckError, SourceRange},
info, program,
};
use super::{CompInfo, MersStatement};

View File

@@ -1,8 +1,8 @@
use crate::program::run::{CheckError, SourceRange};
use std::sync::{Arc, Mutex};
use crate::{
data,
errors::{CheckError, SourceRange},
program::{self, run::CheckInfo},
};
@@ -29,8 +29,8 @@ impl MersStatement for Function {
let arg_target = Arc::new(self.arg.compile(info, comp)?);
comp.is_init = false;
let run = Arc::new(self.run.compile(info, comp)?);
let arg2 = Arc::clone(&arg_target);
let run2 = Arc::clone(&run);
let arg2: Arc<Box<dyn crate::program::run::MersStatement>> = Arc::clone(&arg_target);
let run2: Arc<Box<dyn crate::program::run::MersStatement>> = Arc::clone(&run);
Ok(Box::new(program::run::function::Function {
pos_in_src: self.pos_in_src,
func_no_info: data::function::Function {

View File

@@ -1,6 +1,6 @@
use crate::program::{
self,
run::{CheckError, SourceRange},
use crate::{
errors::{CheckError, SourceRange},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -4,11 +4,9 @@ use colored::Colorize;
use crate::{
data::{self, Data},
errors::{error_colors, CheckError, SourceRange},
info::{self, Local},
program::{
self,
run::{error_colors, CheckError, SourceRange},
},
program::{self},
};
use super::{CompInfo, MersStatement};
@@ -27,7 +25,7 @@ impl MersStatement for IncludeMers {
info: &mut info::Info<super::Local>,
comp: CompInfo,
) -> Result<Box<dyn program::run::MersStatement>, CheckError> {
let compiled = match self.include.compile(info, comp) {
let compiled: Arc<Box<dyn crate::program::run::MersStatement>> = match self.include.compile(info, comp) {
Ok(v) => Arc::new(v),
Err(e) => {
return Err(CheckError::new()

View File

@@ -1,5 +1,7 @@
use crate::program::run::SourceRange;
use crate::program::{self, run::CheckError};
use crate::{
errors::{CheckError, SourceRange},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -1,8 +1,9 @@
use std::{collections::HashMap, fmt::Debug};
use crate::info;
use super::run::{CheckError, SourceRange};
use crate::{
errors::{CheckError, SourceRange},
info,
};
#[cfg(feature = "parse")]
pub mod assign_to;

View File

@@ -1,9 +1,7 @@
use crate::{
errors::{CheckError, SourceRange},
info,
program::{
self,
run::{CheckError, SourceRange},
},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -1,5 +1,8 @@
use crate::program::run::{CheckError, SourceRange};
use crate::{data::Data, program};
use crate::{
data::Data,
errors::{CheckError, SourceRange},
program,
};
use super::{CompInfo, MersStatement};

View File

@@ -1,9 +1,7 @@
use crate::{
errors::{error_colors, CheckError, SourceRange},
info::Local,
program::{
self,
run::{error_colors, CheckError, SourceRange},
},
program::{self},
};
use super::{CompInfo, MersStatement};

View File

@@ -1,8 +1,11 @@
use colored::Colorize;
use crate::data::{self, Data, MersType, Type};
use crate::{
data::{self, Data, MersType, Type},
errors::{error_colors, CheckError, SourceRange},
};
use super::{error_colors, CheckError, CheckInfo, MersStatement, SourceRange};
use super::{CheckInfo, MersStatement};
#[derive(Debug)]
pub struct AssignTo {

View File

@@ -1,6 +1,9 @@
use crate::data::Type;
use crate::{
data::Type,
errors::{CheckError, SourceRange},
};
use super::{MersStatement, SourceRange};
use super::{CheckInfo, MersStatement};
#[derive(Debug)]
pub struct Block {
@@ -10,9 +13,9 @@ pub struct Block {
impl MersStatement for Block {
fn check_custom(
&self,
info: &mut super::CheckInfo,
info: &mut CheckInfo,
init_to: Option<&Type>,
) -> Result<crate::data::Type, super::CheckError> {
) -> Result<crate::data::Type, CheckError> {
if init_to.is_some() {
return Err("can't init to statement type Block".to_string().into());
}

View File

@@ -2,9 +2,12 @@ use std::sync::Arc;
use colored::Colorize;
use crate::data::{Data, Type};
use crate::{
data::{Data, Type},
errors::{error_colors, CheckError, SourceRange},
};
use super::{error_colors, CheckError, MersStatement, SourceRange};
use super::MersStatement;
#[derive(Debug)]
pub struct Chain {

View File

@@ -1,8 +1,11 @@
use std::sync::Arc;
use crate::data::{self, Data, MersData, Type};
use crate::{
data::{self, Data, MersData, Type},
errors::{CheckError, SourceRange},
};
use super::{MersStatement, SourceRange};
use super::MersStatement;
#[derive(Debug)]
pub struct Function {
@@ -15,7 +18,7 @@ impl MersStatement for Function {
&self,
info: &mut super::CheckInfo,
init_to: Option<&Type>,
) -> Result<data::Type, super::CheckError> {
) -> Result<data::Type, CheckError> {
if init_to.is_some() {
return Err("can't init to statement type Function".to_string().into());
}

View File

@@ -2,9 +2,12 @@ use std::sync::Arc;
use colored::Colorize;
use crate::data::{self, Data, MersType, Type};
use crate::{
data::{self, Data, MersType, Type},
errors::{error_colors, CheckError, SourceRange},
};
use super::{error_colors, CheckError, MersStatement, SourceRange};
use super::MersStatement;
#[derive(Debug)]
pub struct If {
@@ -19,7 +22,7 @@ impl MersStatement for If {
&self,
info: &mut super::CheckInfo,
init_to: Option<&Type>,
) -> Result<data::Type, super::CheckError> {
) -> Result<data::Type, CheckError> {
if init_to.is_some() {
return Err("can't init to statement type If".to_string().into());
}

View File

@@ -1,15 +1,12 @@
use std::{
fmt::{Debug, Display},
fmt::Debug,
sync::{Arc, RwLock},
};
use colored::Colorize;
use line_span::LineSpanExt;
use crate::{
data::{self, Data, Type},
errors::{CheckError, SourceRange},
info,
parsing::{Source, SourcePos},
};
#[cfg(feature = "run")]
@@ -61,208 +58,6 @@ pub trait MersStatement: Debug + Send + Sync {
fn source_range(&self) -> SourceRange;
}
#[derive(Clone, Copy, Debug)]
pub struct SourceRange {
start: SourcePos,
end: SourcePos,
}
impl From<(SourcePos, SourcePos)> for SourceRange {
fn from(value: (SourcePos, SourcePos)) -> Self {
SourceRange {
start: value.0,
end: value.1,
}
}
}
impl SourceRange {
pub fn start(&self) -> SourcePos {
self.start
}
pub fn end(&self) -> SourcePos {
self.end
}
}
#[derive(Clone, Debug)]
pub struct CheckError(Vec<CheckErrorComponent>);
#[allow(non_upper_case_globals)]
pub mod error_colors {
use colored::Color;
pub const UnknownVariable: Color = Color::Red;
pub const WhitespaceAfterHashtag: Color = Color::Red;
pub const HashUnknown: Color = Color::Red;
pub const HashIncludeCantLoadFile: Color = Color::Red;
pub const HashIncludeNotAString: Color = Color::Red;
pub const HashIncludeErrorInIncludedFile: Color = Color::Red;
pub const BackslashEscapeUnknown: Color = Color::Red;
pub const BackslashEscapeEOF: Color = Color::Red;
pub const StringEOF: Color = Color::Red;
pub const IfConditionNotBool: Color = Color::Red;
pub const ChainWithNonFunction: Color = Color::Yellow;
pub const Function: Color = Color::BrightMagenta;
pub const FunctionArgument: Color = Color::BrightBlue;
pub const InitFrom: Color = Color::BrightCyan;
pub const InitTo: Color = Color::Green;
pub const AssignFrom: Color = InitFrom;
pub const AssignTo: Color = InitTo;
pub const AssignTargetNonReference: Color = Color::BrightYellow;
}
#[derive(Clone, Debug)]
enum CheckErrorComponent {
Message(String),
Error(CheckError),
Source(Vec<(SourceRange, Option<colored::Color>)>),
}
#[derive(Clone)]
pub struct CheckErrorHRConfig {
indent_start: String,
indent_default: String,
indent_end: String,
}
pub struct CheckErrorDisplay<'a> {
e: &'a CheckError,
src: Option<&'a Source>,
}
impl Display for CheckErrorDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.e.human_readable(
f,
self.src,
&CheckErrorHRConfig {
indent_start: String::new(),
indent_default: String::new(),
indent_end: String::new(),
},
)
}
}
impl CheckError {
pub fn new() -> Self {
CheckError(vec![])
}
fn add(mut self, v: CheckErrorComponent) -> Self {
self.0.push(v);
self
}
pub(crate) fn msg(self, s: String) -> Self {
self.add(CheckErrorComponent::Message(s))
}
pub(crate) fn err(self, e: Self) -> Self {
self.add(CheckErrorComponent::Error(e))
}
pub(crate) fn src(self, s: Vec<(SourceRange, Option<colored::Color>)>) -> Self {
self.add(CheckErrorComponent::Source(s))
}
pub fn display<'a>(&'a self, src: &'a Source) -> CheckErrorDisplay<'a> {
CheckErrorDisplay {
e: self,
src: Some(src),
}
}
pub fn display_no_src<'a>(&'a self) -> CheckErrorDisplay<'a> {
CheckErrorDisplay { e: self, src: None }
}
// will, unless empty, end in a newline
fn human_readable(
&self,
f: &mut std::fmt::Formatter<'_>,
src: Option<&Source>,
cfg: &CheckErrorHRConfig,
) -> std::fmt::Result {
let len = self.0.len();
for (i, component) in self.0.iter().enumerate() {
macro_rules! indent {
() => {
if i + 1 == len {
&cfg.indent_end
} else if i == 0 {
&cfg.indent_start
} else {
&cfg.indent_default
}
};
}
match component {
CheckErrorComponent::Message(msg) => writeln!(f, "{}{msg}", indent!())?,
CheckErrorComponent::Error(err) => {
let mut cfg = cfg.clone();
cfg.indent_start.push_str("");
cfg.indent_default.push_str("");
cfg.indent_end.push_str("");
err.human_readable(f, src, &cfg)?;
}
CheckErrorComponent::Source(highlights) => {
if let Some(src) = src {
let start = highlights.iter().map(|v| v.0.start.pos()).min();
let end = highlights.iter().map(|v| v.0.start.pos()).max();
if let (Some(start), Some(end)) = (start, end) {
writeln!(f, "{}Line(s) [?] ({start}..{end})", indent!())?;
let start = src.get_line_start(start);
let end = src.get_line_end(end);
let lines = src.src()[start..end].line_spans().collect::<Vec<_>>();
for line in lines {
let line_start = line.start();
let line_end = line.end();
let line = line.as_str();
writeln!(f, "{} {line}", indent!())?;
let mut right = 0;
for (pos, color) in highlights {
if let Some(color) = color {
let highlight_start = pos.start.pos() - start;
let highlight_end = pos.end.pos() - start;
if highlight_start < line_end && highlight_end > line_start
{
// where the highlight starts in this line
let hl_start =
highlight_start.saturating_sub(line_start);
// highlight would be further left than cursor, so we need a new line
if hl_start < right {
right = 0;
writeln!(f)?;
}
// length of the highlight
let hl_len = highlight_end
.saturating_sub(line_start)
.saturating_sub(hl_start);
let hl_space = hl_start - right;
let print_indent = right == 0;
let hl_len = hl_len.min(line.len() - right);
right += hl_space + hl_len;
if print_indent && right != 0 {
write!(f, "{} ", indent!())?;
}
write!(
f,
"{}{}",
" ".repeat(hl_space),
"~".repeat(hl_len).color(*color)
)?;
}
}
}
if right != 0 {
writeln!(f)?;
}
}
}
}
}
}
}
Ok(())
}
}
impl From<String> for CheckError {
fn from(value: String) -> Self {
Self::new().msg(value)
}
}
pub type Info = info::Info<Local>;
pub type CheckInfo = info::Info<CheckLocal>;

View File

@@ -2,9 +2,12 @@ use std::{collections::VecDeque, sync::Arc};
use colored::Colorize;
use crate::data::{self, tuple::TupleT, Data, Type};
use crate::{
data::{self, tuple::TupleT, Data, Type},
errors::{error_colors, SourceRange},
};
use super::{error_colors, MersStatement, SourceRange};
use super::MersStatement;
#[derive(Debug)]
pub struct Tuple {

View File

@@ -1,6 +1,9 @@
use crate::data::{Data, Type};
use crate::{
data::{Data, Type},
errors::SourceRange,
};
use super::{MersStatement, SourceRange};
use super::{CheckInfo, MersStatement};
#[derive(Debug)]
pub struct Value {
@@ -14,7 +17,7 @@ impl MersStatement for Value {
}
fn check_custom(
&self,
info: &mut super::CheckInfo,
_info: &mut CheckInfo,
init_to: Option<&Type>,
) -> Result<crate::data::Type, super::CheckError> {
if init_to.is_some() {

View File

@@ -1,8 +1,11 @@
use std::sync::{Arc, RwLock};
use crate::data::{self, Data, Type};
use crate::{
data::{self, Data, Type},
errors::SourceRange,
};
use super::{MersStatement, SourceRange};
use super::MersStatement;
#[derive(Debug)]
pub struct Variable {