1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fmt::{self, Display, Formatter};

#[derive(Debug, PartialEq, Clone)]
pub struct ParserError {
    pub pos: (usize, usize),
    pub slice: Option<(usize, usize)>,
    pub kind: ErrorKind,
}

impl std::error::Error for ParserError {}

impl Display for ParserError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.kind, f)
    }
}

macro_rules! error {
    ($kind:expr, $start:expr) => {{
        Err(ParserError {
            pos: ($start, $start + 1),
            slice: None,
            kind: $kind,
        })
    }};
    ($kind:expr, $start:expr, $end:expr) => {{
        Err(ParserError {
            pos: ($start, $end),
            slice: None,
            kind: $kind,
        })
    }};
}

#[derive(Debug, PartialEq, Clone)]
pub enum ErrorKind {
    Generic,
    ExpectedEntry,
    ExpectedToken(char),
    ExpectedCharRange { range: String },
    ExpectedMessageField { entry_id: String },
    ExpectedTermField { entry_id: String },
    ForbiddenWhitespace,
    ForbiddenCallee,
    ForbiddenKey,
    MissingDefaultVariant,
    MissingVariants,
    MissingValue,
    MissingVariantKey,
    MissingLiteral,
    MultipleDefaultVariants,
    MessageReferenceAsSelector,
    TermReferenceAsSelector,
    MessageAttributeAsSelector,
    TermAttributeAsPlaceable,
    UnterminatedStringExpression,
    PositionalArgumentFollowsNamed,
    DuplicatedNamedArgument(String),
    ForbiddenVariantAccessor,
    UnknownEscapeSequence(String),
    InvalidUnicodeEscapeSequence(String),
    UnbalancedClosingBrace,
    ExpectedInlineExpression,
    ExpectedSimpleExpressionAsSelector,
}

impl Display for ErrorKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Generic => write!(f, "An error occurred"),
            Self::ExpectedEntry => write!(f, "Expected an entry"),
            Self::ExpectedToken(letter) => {
                write!(f, "Expected a token starting with \"{}\"", letter)
            }
            Self::ExpectedCharRange { range } => write!(f, "Expected one of \"{}\"", range),
            Self::ExpectedMessageField { entry_id } => {
                write!(f, "Expected a message field for \"{}\"", entry_id)
            }
            Self::ExpectedTermField { entry_id } => {
                write!(f, "Expected a term field for \"{}\"", entry_id)
            }
            Self::ForbiddenWhitespace => write!(f, "Whitespace is not allowed here"),
            Self::ForbiddenCallee => write!(f, "Callee is not allowed here"),
            Self::ForbiddenKey => write!(f, "Key is not allowed here"),
            Self::MissingDefaultVariant => {
                write!(f, "The select expression must have a default variant")
            }
            Self::MissingVariants => {
                write!(f, "The select expression must have one or more variants")
            }
            Self::MissingValue => write!(f, "Expected a value"),
            Self::MissingVariantKey => write!(f, "Expected a variant key"),
            Self::MissingLiteral => write!(f, "Expected a literal"),
            Self::MultipleDefaultVariants => {
                write!(f, "A select expression can only have one default variant",)
            }
            Self::MessageReferenceAsSelector => {
                write!(f, "Message references can't be used as a selector")
            }
            Self::TermReferenceAsSelector => {
                write!(f, "Term references can't be used as a selector")
            }
            Self::MessageAttributeAsSelector => {
                write!(f, "Message attributes can't be used as a selector")
            }
            Self::TermAttributeAsPlaceable => {
                write!(f, "Term attributes can't be used as a placeable")
            }
            Self::UnterminatedStringExpression => write!(f, "Unterminated string expression"),
            Self::PositionalArgumentFollowsNamed => {
                write!(f, "Positional arguments must come before named arguments",)
            }
            Self::DuplicatedNamedArgument(name) => {
                write!(f, "The \"{}\" argument appears twice", name)
            }
            Self::ForbiddenVariantAccessor => write!(f, "Forbidden variant accessor"),
            Self::UnknownEscapeSequence(seq) => write!(f, "Unknown escape sequence, \"{}\"", seq),
            Self::InvalidUnicodeEscapeSequence(seq) => {
                write!(f, "Invalid unicode escape sequence, \"{}\"", seq)
            }
            Self::UnbalancedClosingBrace => write!(f, "Unbalanced closing brace"),
            Self::ExpectedInlineExpression => write!(f, "Expected an inline expression"),
            Self::ExpectedSimpleExpressionAsSelector => {
                write!(f, "Expected a simple expression as selector")
            }
        }
    }
}