hypen_parser/
error.rs

1use ariadne::{Color, Label, Report, ReportKind, Source};
2use chumsky::error::Rich;
3
4/// Pretty-print parse errors using Ariadne
5pub fn print_parse_errors(filename: &str, source: &str, errors: &[Rich<char>]) {
6    for error in errors {
7        let span = error.span();
8        let msg = format!("{}", error);
9
10        Report::build(ReportKind::Error, filename, span.start)
11            .with_message("Parse error")
12            .with_label(
13                Label::new((filename, span.into_range()))
14                    .with_message(msg)
15                    .with_color(Color::Red),
16            )
17            .finish()
18            .print((filename, Source::from(source)))
19            .unwrap();
20    }
21}
22
23/// Format parse errors as a string without printing
24pub fn format_parse_errors(filename: &str, source: &str, errors: &[Rich<char>]) -> String {
25    let mut output = Vec::new();
26
27    for error in errors {
28        let span = error.span();
29        let msg = format!("{}", error);
30
31        Report::build(ReportKind::Error, filename, span.start)
32            .with_message("Parse error")
33            .with_label(
34                Label::new((filename, span.into_range()))
35                    .with_message(msg)
36                    .with_color(Color::Red),
37            )
38            .finish()
39            .write((filename, Source::from(source)), &mut output)
40            .unwrap();
41    }
42
43    String::from_utf8(output).unwrap()
44}