ludus/src/errors.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

// use crate::process::{LErr, Trace};
2025-07-03 03:47:02 +00:00
use crate::lexer::Token;
2024-12-13 00:01:51 +00:00
use crate::validator::VErr;
2025-07-03 03:47:02 +00:00
use chumsky::prelude::*;
2024-12-13 00:01:51 +00:00
2025-07-03 03:47:02 +00:00
const SEPARATOR: &str = "\n\n***\n";
2024-12-13 00:01:51 +00:00
2025-07-03 03:47:02 +00:00
fn line_number(src: &'static str, span: SimpleSpan) -> usize {
src.chars().take(span.start).filter(|c| *c == '\n').count()
}
fn get_line(src: &'static str, line: usize) -> String {
src.split("\n").nth(line).unwrap().to_string()
}
pub fn lexing(errs: Vec<Rich<'static, char>>) -> String {
let mut msg = "Syntax errors".to_string();
for err in errs {
msg = format!("{msg}\n{:#?}", err);
}
msg
}
pub fn validation(errs: Vec<VErr>) -> String {
let mut msgs = vec![];
for err in errs {
let mut msg = vec![];
let line_number = line_number(err.src, *err.span);
let line = get_line(err.src, line_number);
msg.push(format!("Validation error: {}", err.msg));
msg.push(format!(" on line {} in {}", line_number + 1, err.input));
msg.push(format!(" >>> {line}"));
msgs.push(msg.join("\n"));
}
msgs.join(SEPARATOR)
}
pub fn parsing(errs: Vec<Rich<'static, Token>>) -> String {
let mut msg = "Syntax errors".to_string();
2024-12-13 00:01:51 +00:00
for err in errs {
2025-07-03 03:47:02 +00:00
msg = format!("{msg}\n{:#?}", err);
2024-12-13 00:01:51 +00:00
}
2025-07-03 03:47:02 +00:00
msg
2024-12-13 00:01:51 +00:00
}