1use crate::position::Pos;
2use crate::state::State;
3use crate::{CombineFail, CombineManyFail};
4
5use std::rc::Rc;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum ParseResult<T, E, F = ()> {
9 Ok(T, Pos),
11 Fail(F, Pos),
14 Err(E, Pos),
16}
17
18pub struct Parser<'a, T, E, F = ()> {
24 pub(crate) name: Rc<String>,
26 parse: Rc<dyn Fn(State<'a>) -> ParseResult<T, E, F> + 'a>,
28}
29
30impl<T, E, F> std::fmt::Debug for Parser<'_, T, E, F> {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 let name = self.name.as_ref();
33 write!(f, "Parser(\"{name}\")")
34 }
35}
36
37impl<'a, T, E, F> Clone for Parser<'a, T, E, F> {
40 fn clone(&self) -> Self {
41 Self {
42 name: self.name.clone(),
43 parse: self.parse.clone(),
44 }
45 }
46}
47
48impl<'a, T, E, F> Parser<'a, T, E, F> {
49 pub fn ret(value: T) -> Self
52 where
53 T: Clone + 'a,
54 {
55 Parser {
56 name: Rc::new("ret".to_string()),
57 parse: Rc::new(move |state| ParseResult::Ok(value.clone(), state.pos)),
58 }
59 }
60
61 pub fn ret_with(value: impl Fn() -> T + 'a) -> Self {
62 Parser {
63 name: Rc::new("ret_with".to_string()),
64 parse: Rc::new(move |state| ParseResult::Ok(value(), state.pos)),
65 }
66 }
67
68 pub fn fail(value: F) -> Self
69 where
70 F: Clone + 'a,
71 {
72 Parser {
73 name: Rc::new("fail".to_string()),
74 parse: Rc::new(move |state| ParseResult::Fail(value.clone(), state.pos)),
75 }
76 }
77
78 pub fn fail_with(value: impl Fn() -> F + 'a) -> Self {
79 Parser {
80 name: Rc::new("fail_with".to_string()),
81 parse: Rc::new(move |state| ParseResult::Fail(value(), state.pos)),
82 }
83 }
84
85 pub fn err(value: E) -> Self
86 where
87 E: Clone + 'a,
88 {
89 Parser {
90 name: Rc::new("err".to_string()),
91 parse: Rc::new(move |state| ParseResult::Err(value.clone(), state.pos)),
92 }
93 }
94
95 pub fn err_with(value: impl Fn() -> E + 'a) -> Self {
96 Parser {
97 name: Rc::new("err_with".to_string()),
98 parse: Rc::new(move |state| ParseResult::Err(value(), state.pos)),
99 }
100 }
101
102 pub fn from_fn<Func>(func: Func) -> Self
103 where
104 Func: Fn(State<'a>) -> ParseResult<T, E, F> + 'a,
105 {
106 Parser {
107 name: Rc::new("from_fn".to_string()),
108 parse: Rc::new(func),
109 }
110 }
111
112 pub fn parse(&self, state: State<'a>) -> ParseResult<T, E, F> {
113 (self.parse)(state)
114 }
115
116 pub fn with_name(mut self, name: impl Into<String>) -> Self {
120 self.name = Rc::new(name.into());
121 self
122 }
123
124 pub fn map<U>(self, f: impl Fn(T) -> U + 'a) -> Parser<'a, U, E, F>
125 where
126 F: 'a,
127 E: 'a,
128 T: 'a,
129 {
130 let name = format!("map({})", self.name);
131 Parser::from_fn(move |state| match self.parse(state) {
132 ParseResult::Ok(value, pos) => ParseResult::Ok(f(value), pos),
133 ParseResult::Fail(fail_value, pos) => ParseResult::Fail(fail_value, pos),
134 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
135 })
136 .with_name(name)
137 }
138
139 pub fn map_fail<G>(self, f: impl Fn(F) -> G + 'a) -> Parser<'a, T, E, G>
140 where
141 F: 'a,
142 E: 'a,
143 T: 'a,
144 {
145 let name = format!("map_fail({})", self.name);
146 Parser::from_fn(move |state| match self.parse(state) {
147 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
148 ParseResult::Fail(fail_value, pos) => ParseResult::Fail(f(fail_value), pos),
149 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
150 })
151 .with_name(name)
152 }
153
154 pub fn map_err<E2>(self, f: impl Fn(E) -> E2 + 'a) -> Parser<'a, T, E2, F>
155 where
156 F: 'a,
157 E: 'a,
158 T: 'a,
159 {
160 let name = format!("map_err({})", self.name);
161 Parser::from_fn(move |state| match self.parse(state) {
162 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
163 ParseResult::Fail(fail_value, pos) => ParseResult::Fail(fail_value, pos),
164 ParseResult::Err(err_value, pos) => ParseResult::Err(f(err_value), pos),
165 })
166 .with_name(name)
167 }
168
169 pub fn and_then<U, Func>(self, func: Func) -> Parser<'a, U, E, F>
170 where
171 Func: Fn(T) -> Parser<'a, U, E, F> + 'a,
172 F: 'a,
173 E: 'a,
174 T: 'a,
175 {
176 let name = format!("and_then({})", self.name);
177 Parser::from_fn(move |state| match self.parse(state) {
178 ParseResult::Ok(value, pos) => func(value).parse(state.with_pos(pos)),
179 ParseResult::Fail(fail_value, pos) => ParseResult::Fail(fail_value, pos),
180 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
181 })
182 .with_name(name)
183 }
184
185 pub fn and_then_fail<G, Func>(self, func: Func) -> Parser<'a, T, E, G>
190 where
191 Func: Fn(F) -> Parser<'a, T, E, G> + 'a,
192 F: 'a,
193 E: 'a,
194 T: 'a,
195 {
196 let name = format!("and_then_fail({})", self.name);
197 Parser::from_fn(move |state| match self.parse(state) {
198 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
199 ParseResult::Fail(fail_value, _) => func(fail_value).parse(state),
200 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
201 })
202 .with_name(name)
203 }
204
205 pub fn and_then_err<E2, Func>(self, func: Func) -> Parser<'a, T, E2, F>
210 where
211 Func: Fn(E) -> Parser<'a, T, E2, F> + 'a,
212 F: 'a,
213 E: 'a,
214 T: 'a,
215 {
216 let name = format!("and_then_err({})", self.name);
217 Parser::from_fn(move |state| match self.parse(state) {
218 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
219 ParseResult::Fail(fail_value, pos) => ParseResult::Fail(fail_value, pos),
220 ParseResult::Err(err_value, _) => func(err_value).parse(state),
221 })
222 .with_name(name)
223 }
224
225 pub fn or<G, H>(self, other: Parser<'a, T, E, G>) -> Parser<'a, T, E, H>
226 where
227 T: 'a,
228 E: 'a,
229 F: 'a,
230 G: 'a,
231 F: CombineFail<'a, G, H>,
232 {
233 let name = format!("or({} | {})", self.name, other.name);
234 Parser::from_fn(move |state| match self.parse(state) {
235 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
236 ParseResult::Fail(f1, f1_pos) => {
237 match other.parse(state) {
238 ParseResult::Ok(value, pos) => ParseResult::Ok(value, pos),
239 ParseResult::Fail(f2, f2_pos) => {
240 let f =
241 F::combine_fail(f1, state.with_pos(f1_pos), f2, state.with_pos(f2_pos));
242 ParseResult::Fail(f, state.pos)
244 }
245 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
246 }
247 }
248 ParseResult::Err(err_value, pos) => ParseResult::Err(err_value, pos),
249 })
250 .with_name(name)
251 }
252
253 pub fn or_ret(self, x: T) -> Parser<'a, T, E, F>
254 where
255 T: Clone + 'a,
256 E: 'a,
257 F: 'a,
258 {
259 self.map(move |_| x.clone())
260 }
261
262 pub fn or_fail<G>(self, f: G) -> Parser<'a, T, E, G>
263 where
264 T: 'a,
265 E: 'a,
266 F: 'a,
267 G: Clone + 'a,
268 {
269 self.map_fail(move |_| f.clone())
270 }
271
272 pub fn or_err<G>(self, e: E) -> Parser<'a, T, E, G>
273 where
274 T: 'a,
275 E: Clone + 'a,
276 F: 'a,
277 G: 'a,
278 {
279 self.or(Parser::err(e)).map_fail(|(_f, g)| g)
280 }
281
282 pub fn one_of<G>(
283 parsers: impl IntoIterator<Item = Parser<'a, T, E, F>> + 'a,
284 ) -> Parser<'a, T, E, G>
285 where
286 T: 'a,
287 E: 'a,
288 F: 'a + CombineManyFail<'a, G>,
289 G: 'a,
290 {
291 let mut ret = Parser::fail_with(Vec::new);
292 let mut names = vec![];
293 for parser in parsers {
294 names.push(parser.name.clone());
295 ret = ret.or(parser).map_fail(|(f1, _f1_state, f2, f2_state)| {
296 let mut f = f1;
297 f.push((f2, f2_state));
298 f
299 });
300 }
301 let name = format!(
302 "one_of({})",
303 names
304 .iter()
305 .map(|s| s.as_str())
306 .collect::<Vec<_>>()
307 .join(", ")
308 );
309 ret.map_fail(F::combine_many_fail).with_name(name)
310 }
311
312 pub fn filter(self, pred: impl Fn(&T) -> bool + 'a) -> Self
313 where
314 T: Clone + 'a,
315 E: 'a,
316 F: Clone + Default + 'a,
317 {
318 let name = format!("filter({})", self.name);
319 Parser::and_then(self, move |value| {
320 if pred(&value) {
321 Parser::ret(value)
322 } else {
323 Parser::fail(F::default())
324 }
325 })
326 .with_name(name)
327 }
328}
329
330impl<'a, E, F> Parser<'a, State<'a>, E, F> {
331 pub fn state() -> Self {
333 Parser {
334 name: Rc::new("state".to_string()),
335 parse: Rc::new(|state| ParseResult::Ok(state, state.pos)),
336 }
337 }
338}
339
340#[macro_export]
341macro_rules! one_of {
342 ( $($parser:expr),* $(,)? ) => {{
343 $crate::Parser::one_of(
344 vec![
345 $($parser),*
346 ]
347 )
348 }};
349}