1use std::ops::Range;
2
3use nom::error::ParseError;
4use nom::Needed;
5use thiserror::Error;
6
7use crate::domain::requirement::Requirement;
8use crate::lexer::Token;
9
10#[derive(Error, Debug, PartialEq, Clone, Default)]
12pub enum ParserError {
13 #[error("Unsupported PDDL Requirement: {0:?}")]
15 UnsupportedRequirement(Requirement),
16
17 #[error("Parse error: {0:?}")]
19 ParseError(nom::error::ErrorKind, String),
20
21 #[error("Incomplete input: {0:?}")]
23 IncompleteInput(Needed),
24
25 #[error("Expected identifier")]
27 ExpectedIdentifier,
28
29 #[error("Expected token: {0:?}")]
31 ExpectedToken(Token, Range<usize>, Option<Vec<(Result<Token, ParserError>, String)>>),
32
33 #[error("Expected float")]
35 ExpectedFloat,
36
37 #[error("Expected integer")]
39 ExpectedInteger,
40
41 #[error("Lexer error")]
43 LexerError,
44
45 #[error("Expected end of input")]
47 ExpectedEndOfInput,
48
49 #[default]
51 #[error("Unknown error")]
52 UnknownError,
53}
54
55impl<I: ToString> ParseError<I> for ParserError {
56 fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self {
57 ParserError::ParseError(kind, input.to_string())
58 }
59
60 fn append(_: I, _: nom::error::ErrorKind, other: Self) -> Self {
61 other
62 }
63}
64
65impl From<std::num::ParseIntError> for ParserError {
66 fn from(_: std::num::ParseIntError) -> Self {
67 ParserError::ExpectedInteger
68 }
69}
70
71impl From<std::num::ParseFloatError> for ParserError {
72 fn from(_: std::num::ParseFloatError) -> Self {
73 ParserError::ExpectedFloat
74 }
75}
76
77impl From<nom::Err<ParserError>> for ParserError {
78 fn from(err: nom::Err<ParserError>) -> Self {
79 match err {
80 nom::Err::Incomplete(e) => ParserError::IncompleteInput(e),
81 nom::Err::Error(e) | nom::Err::Failure(e) => match e {
82 ParserError::ParseError(kind, string) => ParserError::ParseError(kind, string),
83 ParserError::IncompleteInput(e) => ParserError::IncompleteInput(e),
84 ParserError::UnsupportedRequirement(e) => ParserError::UnsupportedRequirement(e),
85 ParserError::ExpectedIdentifier => ParserError::ExpectedIdentifier,
86 ParserError::ExpectedToken(token, span, next_tokens) => {
87 ParserError::ExpectedToken(token, span, next_tokens)
88 },
89 ParserError::ExpectedFloat => ParserError::ExpectedFloat,
90 ParserError::ExpectedInteger => ParserError::ExpectedInteger,
91 ParserError::LexerError => ParserError::LexerError,
92 ParserError::UnknownError => ParserError::UnknownError,
93 ParserError::ExpectedEndOfInput => ParserError::ExpectedEndOfInput,
94 },
95 }
96 }
97}