futilehdl/src/parser/error.rs

32 lines
1010 B
Rust

use std::fmt::Debug;
use std::ops::Deref;
use super::{IErr, Span};
use ariadne::{Label, Report, ReportKind};
use nom::error::ErrorKind;
use nom_greedyerror::{GreedyErrorKind, Position};
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()
}