Skip to main content

openpql_range_parser/
error.rs

1use super::{Expected, Loc, LocInfo, ParseError, Token};
2
3/// Parse failure produced while parsing a range expression.
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    /// Range lists more cards than allowed.
16    TooManyCardsInRange(LocInfo),
17    /// Span endpoints disagree on rank count.
18    NumberOfRanksMismatchInSpan(LocInfo),
19    /// Span endpoints disagree on rank distance.
20    RankDistanceMismatchInSpan(LocInfo),
21    /// Span endpoints disagree on suit.
22    SuitMismatchInSpan(LocInfo),
23    /// Span is otherwise invalid.
24    InvalidSpan(LocInfo),
25    /// List is otherwise invalid.
26    InvalidList(LocInfo),
27    /// Rank is out of range for the deck.
28    InvalidRank(LocInfo),
29    /// Suit is invalid.
30    InvalidSuit(LocInfo),
31}
32
33/// LALRPOP parse error specialized for this grammar.
34pub type LalrError<'input> = ParseError<Loc, Token<'input>, Error>;
35
36/// Result alias for parser entry points.
37pub type ResultE<'input, T> = Result<T, LalrError<'input>>;
38
39impl<'input> From<LalrError<'input>> for Error {
40    fn from(err: LalrError<'input>) -> Self {
41        match err {
42            ParseError::InvalidToken { location: l } => {
43                Self::InvalidToken((l, l + 1))
44            }
45
46            ParseError::UnrecognizedEof {
47                location: l,
48                expected: v,
49            } => Self::UnrecognizedEof((l, l + 1), v),
50
51            ParseError::UnrecognizedToken {
52                token: t,
53                expected: v,
54            } => Self::UnrecognizedToken((t.0, t.2), v),
55
56            ParseError::ExtraToken { token: t } => Self::ExtraToken((t.0, t.2)),
57
58            ParseError::User { error } => error,
59        }
60    }
61}
62
63impl From<&Error> for LocInfo {
64    fn from(e: &Error) -> Self {
65        match e {
66            Error::InvalidToken(loc)
67            | Error::UnrecognizedEof(loc, _)
68            | Error::UnrecognizedToken(loc, _)
69            | Error::ExtraToken(loc)
70            | Error::TooManyCardsInRange(loc)
71            | Error::NumberOfRanksMismatchInSpan(loc)
72            | Error::RankDistanceMismatchInSpan(loc)
73            | Error::SuitMismatchInSpan(loc)
74            | Error::InvalidSpan(loc)
75            | Error::InvalidList(loc)
76            | Error::InvalidRank(loc)
77            | Error::InvalidSuit(loc) => *loc,
78        }
79    }
80}