Skip to main content

openpql_pql_parser/
error.rs

1use super::{Expected, Loc, LocInfo, ParseError, Token};
2
3/// Parse failure produced while parsing a PQL source.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum Error {
6    /// Token is not recognized by the lexer.
7    InvalidToken(LocInfo),
8    /// Input ended before the parser expected.
9    UnrecognizedEof(LocInfo, Expected),
10    /// Token is not valid in this position.
11    UnrecognizedToken(LocInfo, Expected),
12    /// Trailing token after a complete parse.
13    ExtraToken(LocInfo),
14
15    /// Selector name is not one of the supported aggregates.
16    UnrecognizedSelector(LocInfo),
17    /// `from` clause contains the same key more than once.
18    DuplicatedKeyInFrom(LocInfo),
19    /// Two selectors share the same alias.
20    DuplicatedSelectorName(LocInfo),
21    /// Numeric literal is not a valid number.
22    InvalidNumericValue(LocInfo),
23}
24
25/// LALRPOP parse error specialized for this grammar.
26pub type LalrError<'input> = ParseError<Loc, Token<'input>, Error>;
27
28/// Result alias for parser entry points.
29pub type ResultE<'input, T> = Result<T, LalrError<'input>>;
30
31pub const fn user_err<'input>(error: Error) -> LalrError<'input> {
32    ParseError::User { error }
33}
34
35impl<'input> From<LalrError<'input>> for Error {
36    fn from(err: LalrError<'input>) -> Self {
37        match err {
38            ParseError::InvalidToken { location: l } => Self::InvalidToken((l, l + 1)),
39
40            ParseError::UnrecognizedEof {
41                location: l,
42                expected: v,
43            } => Self::UnrecognizedEof((l, l + 1), v),
44
45            ParseError::UnrecognizedToken {
46                token: t,
47                expected: v,
48            } => Self::UnrecognizedToken((t.0, t.2), v),
49
50            ParseError::ExtraToken { token: t } => Self::ExtraToken((t.0, t.2)),
51
52            ParseError::User { error } => error,
53        }
54    }
55}
56
57impl From<&Error> for LocInfo {
58    fn from(e: &Error) -> Self {
59        match e {
60            Error::InvalidToken(loc)
61            | Error::UnrecognizedEof(loc, _)
62            | Error::UnrecognizedToken(loc, _)
63            | Error::ExtraToken(loc)
64            | Error::UnrecognizedSelector(loc)
65            | Error::DuplicatedKeyInFrom(loc)
66            | Error::DuplicatedSelectorName(loc)
67            | Error::InvalidNumericValue(loc) => *loc,
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_into_loc() {
78        let loc = (1, 2);
79
80        assert_eq!(loc, (&Error::InvalidToken(loc)).into());
81        assert_eq!(loc, (&Error::UnrecognizedEof(loc, vec![])).into());
82        assert_eq!(loc, (&Error::UnrecognizedToken(loc, vec![])).into());
83        assert_eq!(loc, (&Error::ExtraToken(loc)).into());
84        assert_eq!(loc, (&Error::UnrecognizedSelector(loc)).into());
85        assert_eq!(loc, (&Error::DuplicatedKeyInFrom(loc)).into());
86        assert_eq!(loc, (&Error::DuplicatedSelectorName(loc)).into());
87        assert_eq!(loc, (&Error::InvalidNumericValue(loc)).into());
88    }
89
90    #[test]
91    fn test_error() {
92        let err = LalrError::ExtraToken {
93            token: (0, Token(0, "a"), 1),
94        };
95
96        assert_eq!(Error::ExtraToken((0, 1)), err.into());
97    }
98}