cedar_policy_core/parser/
err.rs1use lalrpop_util as lalr;
18use std::fmt::Display;
19use thiserror::Error;
20
21#[derive(Debug, Error, PartialEq, Clone)]
23pub enum ParseError {
24 #[error("{0}")]
26 ToCST(String),
27 #[error("poorly formed: {0}")]
29 ToAST(String),
30 #[error("error while {context}: {}", MultipleParseErrors(errs))]
33 WithContext {
34 context: String,
36 errs: Vec<ParseError>,
38 },
39 #[error(transparent)]
41 RestrictedExpressionError(#[from] crate::ast::RestrictedExpressionError),
42}
43
44impl<L: Display, T: Display, E: Display> From<lalr::ParseError<L, T, E>> for ParseError {
45 fn from(e: lalr::ParseError<L, T, E>) -> Self {
46 ParseError::ToCST(format!("{}", e))
47 }
48}
49
50impl<L: Display, T: Display, E: Display> From<lalr::ErrorRecovery<L, T, E>> for ParseError {
51 fn from(e: lalr::ErrorRecovery<L, T, E>) -> Self {
52 e.error.into()
53 }
54}
55
56#[derive(Debug, Error)]
59pub struct ParseErrors(pub Vec<ParseError>);
60
61impl ParseErrors {
62 pub fn errors_as_strings(&self) -> Vec<String> {
64 self.0
65 .iter()
66 .map(|parser_error| format!("{}", parser_error))
67 .collect()
68 }
69}
70
71impl std::fmt::Display for ParseErrors {
72 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 write!(f, "{}", MultipleParseErrors(&self.0))
74 }
75}
76
77impl From<ParseError> for ParseErrors {
78 fn from(e: ParseError) -> ParseErrors {
79 ParseErrors(vec![e])
80 }
81}
82
83#[derive(Debug, Error)]
85pub struct MultipleParseErrors<'a>(pub &'a [ParseError]);
86
87impl<'a> std::fmt::Display for MultipleParseErrors<'a> {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 if self.0.is_empty() {
90 write!(f, "no errors found")
91 } else {
92 for err in self.0 {
93 write!(f, "\n {}", err)?;
94 }
95 Ok(())
96 }
97 }
98}