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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{
Error,
Parseable,
Parser,
};
use std::{
fmt::{
Debug,
Display,
Formatter,
Result as FmtResult,
},
marker::PhantomData,
};
pub struct ParseableParser<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
_a: PhantomData<&'a ()>,
_b: PhantomData<&'b ()>,
_p: PhantomData<P>,
}
impl<'a, 'b, P> ParseableParser<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
pub fn new() -> Self
{
Self {
_a: PhantomData,
_b: PhantomData,
_p: PhantomData,
}
}
}
impl<'a, 'b, P> Debug for ParseableParser<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
fn fmt(&self, f: &mut Formatter) -> FmtResult
{
f.debug_struct("ParseableParser")
.field("type", &P::name())
.finish()
}
}
impl<'a, 'b, P> Parser<'a, 'b> for ParseableParser<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
type Error = ParseableParserError<'a, 'b, P>;
type Output = P::Output;
type Requirement = &'a str;
type RequirementContext = ();
fn parse(&self, src: &'b str, pos: &mut usize) -> Result<Self::Output, Self::Error>
{
let from = *pos;
P::parse(src, pos).map_err(|err| ParseableParserError::new(from, P::name(), err))
}
fn skip(&self, src: &'b str, pos: &mut usize) -> Option<Self::Error>
{
let from = *pos;
P::skip(src, pos).map(|err| ParseableParserError::new(from, P::name(), err))
}
fn requirement(&self, _: Option<&Self::RequirementContext>) -> Self::Requirement
{
P::name()
}
}
#[derive(Debug)]
pub struct ParseableParserError<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
from: usize,
requirement: &'a str,
cause: P::Error,
}
impl<'a, 'b, P> ParseableParserError<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
pub fn new(from: usize, requirement: &'a str, cause: P::Error) -> Self
{
Self {
from,
requirement,
cause,
}
}
}
impl<'a, 'b, P> Error<'a, 'b> for ParseableParserError<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
fn from(&self, f: &mut Formatter) -> FmtResult
{
write!(f, "{}", self.from)
}
fn requirement(&self, f: &mut Formatter) -> FmtResult
{
write!(f, "{}", self.requirement)
}
fn result(&self, f: &mut Formatter) -> FmtResult
{
write!(f, "failed to parse")
}
fn causes(&self, f: &mut Formatter, depth: usize) -> FmtResult
{
self.cause.print(f, depth)
}
fn print(&self, f: &mut Formatter, depth: usize) -> FmtResult
{
for _ in 0..depth
{
write!(f, "\t")?;
}
write!(f, "at position ")?;
self.from(f)?;
write!(f, " required ")?;
self.requirement(f)?;
write!(f, " but ")?;
self.result(f)?;
write!(f, ".\n")?;
self.causes(f, depth + 1)
}
fn print_full(&self, f: &mut Formatter, depth: usize) -> FmtResult
{
self.print(f, depth)
}
}
impl<'a, 'b, P> Display for ParseableParserError<'a, 'b, P>
where
P: Parseable<'a, 'b>,
{
fn fmt(&self, f: &mut Formatter) -> FmtResult
{
self.print(f, 0)
}
}