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
//! General error types for the parser

use std::borrow::Cow;

use crate::coords::Coords;

/// Global result type used throughout the parser stages
pub type ParserResult<T> = Result<T, ParserError>;

/// Enumeration of the various different parser stages that can produce an error
#[derive(Debug, Copy, Clone)]
pub enum Stage {
    /// The lexer stage of the parser
    Lexer,
    /// The parsing/AST construction stage of the parser
    Parser,
}

/// A global enumeration of error codes
#[derive(Debug, Clone)]
pub enum Details {
    EndOfInput,
    StreamFailure,
    NonUtf8InputDetected,
    UnexpectedToken,
    PairExpected,
    InvalidObject,
    InvalidArray,
    InvalidCharacter(char),
    MatchFailed,
    InvalidNumericRepresentation(String),
    InvalidEscapeSequence(String),
    InvalidUnicodeEscapeSequence(String),
}

/// The general error structure
#[derive(Debug, Clone)]
pub struct ParserError {
    /// The originating stage for the error
    pub stage: Stage,
    /// The global error code for the error
    pub details: Details,
    /// Optional parser coordinates
    pub coords: Option<Coords>,
}

#[macro_export]
macro_rules! lexer_error {
    ($details: expr, $coords : expr) => {
        Err(ParserError {
            stage: Stage::Lexer,
            details: $details,
            coords: Some($coords),
        })
    };
}

#[macro_export]
macro_rules! parser_error {
    ($details: expr, $coords: expr) => {
        Err(ParserError {
            stage: Stage::Parser,
            details: $details,
            coords: Some($coords),
        })
    };
}