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
// This is free and unencumbered software released into the public domain.

use crate::{
    prelude::{fmt, vec, Vec},
    Span,
};

#[cfg(feature = "std")]
extern crate std;

pub type SyntaxResult<'a, T, E = SyntaxError<'a>> = Result<T, Err<'a, E>>;

pub type Err<'a, E = SyntaxError<'a>> = nom::Err<E>;

#[derive(Clone, Debug, PartialEq)]
pub enum SyntaxErrorKind {
    Context(&'static str),
    Char(char),
    Combinator(nom::error::ErrorKind),
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct SyntaxError<'a> {
    pub errors: Vec<(SyntaxErrorKind, Span<'a>)>,
}

impl<'a> SyntaxError<'a> {
    pub fn new() -> Self {
        Self::default()
    }
}

impl<'a> fmt::Display for SyntaxError<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.write_str("parse error") // TODO
    }
}

// #[cfg(feature = "std")]
// impl From<std::io::Error> for ParseError<'_> {
//     fn from(error: std::io::Error) -> Self {
//         //nom::error::FromExternalError<Span<'a>>::from_ext error
//         todo!()
//     }
// }

impl<'a> Into<nom::Err<SyntaxError<'a>>> for SyntaxError<'a> {
    fn into(self) -> nom::Err<SyntaxError<'a>> {
        nom::Err::Failure(self)
    }
}

impl<'a> nom::error::ContextError<Span<'a>> for SyntaxError<'a> {
    fn add_context(input: Span<'a>, ctx: &'static str, mut other: Self) -> Self {
        other.errors.push((SyntaxErrorKind::Context(ctx), input));
        other
    }
}

impl<'a, E> nom::error::FromExternalError<Span<'a>, E> for SyntaxError<'a> {
    fn from_external_error(input: Span<'a>, kind: nom::error::ErrorKind, _e: E) -> Self {
        use nom::error::ParseError as _;
        Self::from_error_kind(input, kind)
    }
}

impl<'a> nom::error::ParseError<Span<'a>> for SyntaxError<'a> {
    fn from_error_kind(input: Span<'a>, kind: nom::error::ErrorKind) -> Self {
        Self {
            errors: vec![(SyntaxErrorKind::Combinator(kind), input)],
        }
    }

    fn append(input: Span<'a>, kind: nom::error::ErrorKind, mut other: Self) -> Self {
        other
            .errors
            .push((SyntaxErrorKind::Combinator(kind), input));
        other
    }

    fn from_char(input: Span<'a>, c: char) -> Self {
        Self {
            errors: vec![(SyntaxErrorKind::Char(c), input)],
        }
    }
}