futilehdl/src/parser/error.rs

39 lines
1.1 KiB
Rust

use std::fmt::Debug;
use std::ops::Deref;
use super::{Span, IErr};
use nom::error::ErrorKind;
use nom_greedyerror::{Position, GreedyErrorKind};
use ariadne::{Report, ReportKind, Label};
fn span_to_range(input: Span) -> std::ops::Range<usize> {
input.position()..(input.position() + input.len())
}
pub fn convert_error(
input: Span,
e: IErr<Span>,
) -> Report {
let mut labels = Vec::new();
for err in e.errors {
let label = match err.1 {
GreedyErrorKind::Context(ctx) => {
Label::new(span_to_range(err.0))
.with_message(format!("in {ctx}"))
},
GreedyErrorKind::Char(c) => {
Label::new(err.0.position()..err.0.position())
.with_message(format!("expected {c:?}"))
},
GreedyErrorKind::Nom(_) => todo!(),
};
labels.push(label);
}
let mut rep = Report::build(ReportKind::Error, (), 0)
.with_message("Parse Error");
for lbl in labels {
rep = rep.with_label(lbl)
}
rep.finish()
}