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
use crate::{
gen_parser::GenParser,
map_parser::MapParser,
order_parser::OrderParser,
repetition_parser::RepetitionParser,
sequence_parser::SequenceParser,
stringify_parser::StringifyParser,
Error,
};
use std::{
fmt::{
Debug,
Display,
},
usize::MAX,
};
pub trait Parser<'a, 'b>: Debug
{
type Error: Error<'a, 'b>;
type Output;
type Requirement: Debug + Display;
type RequirementContext;
fn parse(&self, src: &'b str, pos: &mut usize) -> Result<Self::Output, Self::Error>;
fn skip(&self, src: &'b str, pos: &mut usize) -> Option<Self::Error>
{
self.parse(src, pos).err()
}
fn requirement(&self, context: Option<&Self::RequirementContext>) -> Self::Requirement;
fn and_then<P>(self, next: P) -> SequenceParser<'a, 'b, Self, P>
where
Self: Sized,
P: Parser<'a, 'b>,
{
SequenceParser::new(self, next)
}
fn or<P>(self, next: P) -> OrderParser<'a, 'b, Self, P>
where
Self: Sized,
P: Parser<'a, 'b>,
{
OrderParser::new(self, next)
}
fn map<F, P>(self, mapper: F) -> MapParser<'a, 'b, Self, F, P>
where
Self: Sized,
F: 'static + Fn(Self::Output) -> P,
P: 'b,
{
MapParser::new(self, mapper)
}
fn and_gen<F, P>(self, generator: F) -> GenParser<'a, 'b, Self, F, P>
where
Self: Sized,
F: 'static + Fn(&Self::Output) -> P,
P: Parser<'a, 'b>,
{
GenParser::new(self, generator)
}
fn repeat(self, min: usize, max: usize) -> RepetitionParser<'a, 'b, Self>
where
Self: Sized,
{
RepetitionParser::new(self, min, max)
}
fn zero_or_more(self) -> RepetitionParser<'a, 'b, Self>
where
Self: Sized,
{
RepetitionParser::new(self, 0, MAX)
}
fn one_or_more(self) -> RepetitionParser<'a, 'b, Self>
where
Self: Sized,
{
RepetitionParser::new(self, 1, MAX)
}
fn stringify(self) -> StringifyParser<'a, 'b, Self>
where
Self: Sized,
{
StringifyParser::new(self)
}
}