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
use std::fmt::{Display, Write};
use super::LSpan;
use crate::error::LocatedError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorKind {
UnescapedChar,
UnescapableChar,
ElementNotClose,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error<'a> {
nom_kind: nom::error::ErrorKind,
pub(crate) span: LSpan<'a>,
kind: Option<ErrorKind>,
}
impl<'a> Display for Error<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self.kind {
Some(kind) => match kind {
ErrorKind::UnescapedChar => "unescaped character",
ErrorKind::UnescapableChar => "unescapable character",
ErrorKind::ElementNotClose => "expect '>' to close element for element starter",
},
None => "unknown error",
})?;
f.write_char(' ')?;
if let Some(c) = self.span.chars().next() {
f.write_char('\'')?;
f.write_char(c)?;
f.write_char('\'')?;
f.write_char(' ')?;
}
f.write_fmt(format_args!("near {}:{}", self.span.extra, self.span.get_column()))
}
}
impl<'a> std::error::Error for Error<'a> {}
impl<'a> Error<'a> {
pub(crate) fn attach(mut self, kind: ErrorKind) -> Self {
if self.kind.is_none() {
self.kind = Some(kind);
}
self
}
pub fn kind(&self) -> Option<ErrorKind> {
self.kind
}
}
impl<'a> LocatedError for Error<'a> {
fn location(&self) -> (usize, usize) {
(self.span.extra, self.span.get_column())
}
}
impl<'a> nom::error::ParseError<LSpan<'a>> for Error<'a> {
fn from_error_kind(input: LSpan<'a>, kind: nom::error::ErrorKind) -> Self {
Self {
nom_kind: kind,
span: input,
kind: None,
}
}
fn append(_input: LSpan<'a>, _kind: nom::error::ErrorKind, other: Self) -> Self {
other
}
}