Start fixing error outputs

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-16 11:22:29 +01:00
parent d6ac7af36b
commit 7f7bf5c98d
16 changed files with 319 additions and 21 deletions

View file

@ -5,11 +5,13 @@ use annotate_snippets::Level;
use annotate_snippets::Renderer;
use annotate_snippets::Snippet;
use thiserror::Error;
use winnow::stream::Offset;
use crate::input::NomoInput;
use crate::lexer::ParseError;
use crate::parser::AstError;
/// An error occurred while producing an Ast
#[derive(Debug, Error)]
pub struct AstFailure {
errors: Vec<AstError>,
@ -26,6 +28,7 @@ impl AstFailure {
AstFailure { errors }
}
/// Create a CLI printable report
pub fn to_report(&self, source: &str) -> String {
let reports = self
.errors
@ -38,12 +41,14 @@ impl AstFailure {
.as_deref()
.unwrap_or("An error occurred while producing an Ast"),
)
.element(
annotate_snippets::Snippet::source(source).annotation(
annotate_snippets::AnnotationKind::Primary
.span(error.span.clone().map(|s| s.range).unwrap_or_else(|| 0..0)),
.element(annotate_snippets::Snippet::source(source).annotation(
annotate_snippets::AnnotationKind::Primary.span(
constrain_without_whitespace(
source,
error.span.clone().map(|s| s.range).unwrap_or_else(|| 0..0),
),
),
)
))
.elements(
error
.help
@ -59,6 +64,18 @@ impl AstFailure {
}
}
fn constrain_without_whitespace(
input: &str,
range: std::ops::Range<usize>,
) -> std::ops::Range<usize> {
let trimmed = input[range].trim();
let start = trimmed.offset_from(&input);
let end = start + trimmed.len();
start..end
}
/// An error occurred during lexing
#[derive(Debug, Error)]
pub struct ParseFailure {
input: Arc<str>,
@ -79,6 +96,7 @@ impl ParseFailure {
}
}
/// Produce a CLi printable report
pub fn to_report(&self) -> String {
let reports = self
.errors