futilehdl/src/parser/error.rs

29 lines
1.0 KiB
Rust
Raw Normal View History

2022-02-01 18:46:06 +00:00
use super::{IErr, Span};
use ariadne::{Label, Report, ReportKind};
use nom_greedyerror::{GreedyErrorKind, Position};
2022-02-01 01:00:27 +00:00
fn span_to_range(input: Span) -> std::ops::Range<usize> {
input.position()..(input.position() + input.len())
}
2022-02-02 00:03:03 +00:00
pub fn convert_error(_input: Span, e: IErr<Span>) -> Report {
2022-02-01 01:00:27 +00:00
let mut labels = Vec::new();
for err in e.errors {
let label = match err.1 {
GreedyErrorKind::Context(ctx) => {
2022-02-01 18:46:06 +00:00
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:?}")),
2022-02-01 22:11:59 +00:00
GreedyErrorKind::Nom(kind) => Label::new(err.0.position()..err.0.position())
.with_message(format!("nom error {kind:?}")),
2022-02-01 01:00:27 +00:00
};
labels.push(label);
}
2022-02-01 18:46:06 +00:00
let mut rep = Report::build(ReportKind::Error, (), 0).with_message("Parse Error");
2022-02-01 01:00:27 +00:00
for lbl in labels {
rep = rep.with_label(lbl)
}
rep.finish()
}