quil_rs/program/error/
syntax.rs1use std::{error::Error, fmt};
16
17use crate::parser::{LexError, ParseError};
18
19use super::LeftoverError;
20
21#[derive(Clone, Debug, PartialEq)]
31pub enum SyntaxError<T> {
32 LexError(LexError),
33 ParseError(ParseError),
34 Leftover(LeftoverError<T>),
35}
36
37impl<T> SyntaxError<T> {
38 pub fn map_parsed<T2>(self, map: impl Fn(T) -> T2) -> SyntaxError<T2> {
39 match self {
40 Self::LexError(err) => SyntaxError::LexError(err),
41 Self::ParseError(err) => SyntaxError::ParseError(err),
42 Self::Leftover(err) => SyntaxError::Leftover(err.map_parsed(map)),
43 }
44 }
45
46 pub fn recover(self) -> Result<T, Self> {
47 match self {
48 Self::Leftover(err) => Ok(err.recover()),
49 err => Err(err),
50 }
51 }
52}
53
54impl<T: fmt::Debug + 'static> fmt::Display for SyntaxError<T> {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 if f.alternate() {
57 write!(f, "error while parsing: {:#}", self.source().unwrap())
58 } else {
59 write!(f, "error while parsing: {}", self.source().unwrap())
60 }
61 }
62}
63
64impl<T> From<LexError> for SyntaxError<T> {
65 fn from(err: LexError) -> Self {
66 Self::LexError(err)
67 }
68}
69
70impl<T> From<ParseError> for SyntaxError<T> {
71 fn from(err: ParseError) -> Self {
72 Self::ParseError(err)
73 }
74}
75
76impl<T> From<LeftoverError<T>> for SyntaxError<T> {
77 fn from(err: LeftoverError<T>) -> Self {
78 Self::Leftover(err)
79 }
80}
81
82impl<T: fmt::Debug + 'static> Error for SyntaxError<T> {
83 fn source(&self) -> Option<&(dyn Error + 'static)> {
84 match self {
85 Self::LexError(err) => Some(err),
86 Self::ParseError(err) => Some(err),
87 Self::Leftover(err) => Some(err),
88 }
89 }
90}