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
#[macro_export]
macro_rules! parser {
($id:ident,$x:expr) => {
parser!(($id->&'static str) $x);
};
($($doc:literal $(,)?)? ($id:ident -> $ot:ty) $(,)? $x:expr $(,)?) => {
parser!($($doc)? ($id->$ot) $x, stringify!($id));
};
($id:ident,$x:expr,$exp:expr) => {
parser!(($id->&'static str) $x, $exp);
};
($($doc:literal $(,)?)? ($id:ident -> $ot:ty) $(,)? $x:expr,$exp:expr $(,)?) => {
$(#[doc=$doc])?
#[derive(Copy, Clone)]
pub struct $id;
impl <'a> Parser<'a> for $id {
type Out = $ot;
fn parse(&self, it: &PIter<'a>) -> ParseRes<'a, Self::Out> {
let name_e = it.err_s($exp);
match (&$x).parse(it){
Ok(v)=> Ok(v),
Err(e)=> match (e.index,name_e.index) {
(Some(ei),Some(ii)) if (ii == ei) => Err(it.err_s($exp)),
_=>Err(e.join(name_e)),
}
}
}
}
};
}
#[macro_export]
macro_rules! parser_as {
(($ot:ty),(($id:ident->$res:expr) $(,)? $main:expr,$exp:expr $(,)?) ) => {
parser! {($id->$ot) ,$main.map(|_|$res),$exp}
};
(($ot:ty),(($id:ident->$res:expr) $(,)? $main:expr $(,)?) ) => {
parser! {($id->$ot) ,$main.map(|_|$res)}
};
(($ot:ty),($id:ident, $main:expr)) => {
parser! { ($id->$ot) $main}
};
}
#[macro_export]
macro_rules! as_id {
((($id:ident->$_x:expr) $($_t:tt)*) ) => {
$id
};
(($id:ident $($_t:tt)*) ) => {
$id
};
}
#[macro_export]
macro_rules! char_bool {
($id:ident,$x:expr) => {
char_bool!($id, $x, Expected::CharIn(stringify!($id)));
};
($id:ident,$x:expr,$s:literal) => {
char_bool!($id, $x, Expected::Char($s));
};
($id:ident,$x:expr,$exp:expr) => {
#[derive(Copy, Clone)]
pub struct $id;
impl CharBool for $id {
fn char_bool(&self, c: char) -> bool {
(&$x).char_bool(c)
}
fn expected(&self) -> Expected {
$exp
}
}
};
}
#[macro_export]
macro_rules! char_bools {
( $( ($id:ident,$x:expr) ),*) => {$(char_bool!($id,$x);)*};
}
#[macro_export]
macro_rules! or{
($s:expr,$($x:expr),* $(,)?) => { $s$(.or($x))*;};
}
#[macro_export]
macro_rules! or_ig{
($s:expr,$($x:expr),* $(,)?) => { $s.ig()$(.or($x.ig()))*;};
}
#[cfg(test)]
mod test {
fn size_of<T: Sized>(_t: &T) -> usize {
std::mem::size_of::<T>()
}
use crate::*;
parser!(DOG, "dog");
parser!(CAR, "car");
parser!(CAT, "cat");
parser!((GROW->Vec<&'static str>) star(or(CAT, DOG)));
#[test]
pub fn parser_makes_parser() {
assert_eq!(DOG.parse_s("dog "), Ok("dog"));
assert_eq!(CAT.parse_s("cat "), Ok("cat"));
assert_eq!(
GROW.parse_s("catdogcatcatno"),
Ok(vec!["cat", "dog", "cat", "cat"])
);
}
char_bool!(HOT, "hot");
char_bool!(MNUM, |c| c >= '0' && c <= '9');
#[test]
pub fn charbool_macro_makes_parser() {
use err::Expected::*;
let p = (HOT, MNUM);
assert_eq!(std::mem::size_of::<(HOT, MNUM)>(), 0);
assert_eq!(p.plus().parse_s("09h3f"), Ok("09h3"));
assert_eq!(p.expected(), OneOf(vec![CharIn("HOT"), CharIn("MNUM")]));
assert_eq!(size_of(&p), 0);
}
}