pddl_parser/
error.rs

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/// A PDDL parser error
11#[derive(Error, Debug, PartialEq, Clone, Default)]
12pub enum ParserError {
13    /// The PDDL file contains an unsupported requirement (e.g. `:fluents`). See the `Requirement` section of the [README.md](https://github.com/MrRobb/pddl-parser#pddl-requirements-supported) for a list of supported requirements.
14    #[error("Unsupported PDDL Requirement: {0:?}")]
15    UnsupportedRequirement(Requirement),
16
17    /// A generic parse error.
18    #[error("Parse error: {0:?}")]
19    ParseError(nom::error::ErrorKind, String),
20
21    /// An incomplete input error. This is returned by the parser when it needs more input to continue parsing.
22    #[error("Incomplete input: {0:?}")]
23    IncompleteInput(Needed),
24
25    /// The parser expected an identifier, but found something else.
26    #[error("Expected identifier")]
27    ExpectedIdentifier,
28
29    /// The parser expected a token, but found something else.
30    #[error("Expected token: {0:?}")]
31    ExpectedToken(Token, Range<usize>, Option<Vec<(Result<Token, ParserError>, String)>>),
32
33    /// The parser expected a float, but found something else.
34    #[error("Expected float")]
35    ExpectedFloat,
36
37    /// The parser expected an integer, but found something else.
38    #[error("Expected integer")]
39    ExpectedInteger,
40
41    /// The lexer encountered an error. This is returned by the lexer when it encounters an invalid token.
42    #[error("Lexer error")]
43    LexerError,
44
45    /// The parser expected the end of input, but there was more input. This is returned by the parser when it encounters more input after the end of the PDDL file.
46    #[error("Expected end of input")]
47    ExpectedEndOfInput,
48
49    /// An unknown error. Default error variant. This should never be returned.
50    #[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}