futilehdl/src/parser/error.rs

29 lines
1.0 KiB
Rust

use super::{IErr, Span};
use ariadne::{Label, Report, ReportKind};
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(kind) => Label::new(err.0.position()..err.0.position())
.with_message(format!("nom error {kind:?}")),
};
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()
}