1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::error::error_printer::ErrorLabel;
use crate::error::ParseError;
use ariadne::Source;
use std::io;

pub struct AggregatedParseError<'p, E: ParseError<L = ErrorLabel<'p>> + 'p> {
    pub input: &'p str,
    pub errors: Vec<E>,
}

impl<'p, E: ParseError<L = ErrorLabel<'p>> + 'p> AggregatedParseError<'p, E> {
    pub fn eprint(&self) -> io::Result<()> {
        for e in &self.errors {
            e.report(false).eprint(Source::from(self.input))?
        }
        Ok(())
    }
}

pub trait ParseResultExt<T> {
    fn unwrap_or_eprint(self) -> T;
}

impl<'p, E: ParseError<L = ErrorLabel<'p>> + 'p, T> ParseResultExt<T>
    for Result<T, AggregatedParseError<'p, E>>
{
    fn unwrap_or_eprint(self) -> T {
        self.unwrap_or_else(|es| {
            es.eprint().unwrap();
            panic!("Failed to parse grammar")
        })
    }
}