prism_parser/error/
aggregate_error.rs

1use crate::error::error_printer::ErrorLabel;
2use crate::error::ParseError;
3use ariadne::Source;
4use std::io;
5
6pub struct AggregatedParseError<'p, E: ParseError<L = ErrorLabel<'p>> + 'p> {
7    pub input: &'p str,
8    pub errors: Vec<E>,
9}
10
11impl<'p, E: ParseError<L = ErrorLabel<'p>> + 'p> AggregatedParseError<'p, E> {
12    pub fn eprint(&self) -> io::Result<()> {
13        for e in &self.errors {
14            e.report(false).eprint(Source::from(self.input))?
15        }
16        Ok(())
17    }
18}
19
20pub trait ParseResultExt<T> {
21    fn unwrap_or_eprint(self) -> T;
22}
23
24impl<'p, E: ParseError<L = ErrorLabel<'p>> + 'p, T> ParseResultExt<T>
25    for Result<T, AggregatedParseError<'p, E>>
26{
27    fn unwrap_or_eprint(self) -> T {
28        self.unwrap_or_else(|es| {
29            es.eprint().unwrap();
30            panic!("Failed to parse grammar")
31        })
32    }
33}