1 | use crate::combinator::trace; |
2 | use crate::combinator::trace_result; |
3 | #[cfg (feature = "unstable-recover" )] |
4 | use crate::error::FromRecoverableError; |
5 | use crate::error::{AddContext, ErrMode, ErrorKind, FromExternalError, ParserError}; |
6 | use crate::lib::std::borrow::Borrow; |
7 | use crate::lib::std::ops::Range; |
8 | #[cfg (feature = "unstable-recover" )] |
9 | use crate::stream::Recover; |
10 | use crate::stream::StreamIsPartial; |
11 | use crate::stream::{Location, Stream}; |
12 | use crate::*; |
13 | |
14 | /// Implementation of [`Parser::by_ref`] |
15 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
16 | pub struct ByRef<'p, P> { |
17 | p: &'p mut P, |
18 | } |
19 | |
20 | impl<'p, P> ByRef<'p, P> { |
21 | #[inline (always)] |
22 | pub(crate) fn new(p: &'p mut P) -> Self { |
23 | Self { p } |
24 | } |
25 | } |
26 | |
27 | impl<'p, I, O, E, P> Parser<I, O, E> for ByRef<'p, P> |
28 | where |
29 | P: Parser<I, O, E>, |
30 | { |
31 | #[inline (always)] |
32 | fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { |
33 | self.p.parse_next(input:i) |
34 | } |
35 | } |
36 | |
37 | /// Implementation of [`Parser::map`] |
38 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
39 | pub struct Map<F, G, I, O, O2, E> |
40 | where |
41 | F: Parser<I, O, E>, |
42 | G: FnMut(O) -> O2, |
43 | { |
44 | parser: F, |
45 | map: G, |
46 | i: core::marker::PhantomData<I>, |
47 | o: core::marker::PhantomData<O>, |
48 | o2: core::marker::PhantomData<O2>, |
49 | e: core::marker::PhantomData<E>, |
50 | } |
51 | |
52 | impl<F, G, I, O, O2, E> Map<F, G, I, O, O2, E> |
53 | where |
54 | F: Parser<I, O, E>, |
55 | G: FnMut(O) -> O2, |
56 | { |
57 | #[inline (always)] |
58 | pub(crate) fn new(parser: F, map: G) -> Self { |
59 | Self { |
60 | parser, |
61 | map, |
62 | i: Default::default(), |
63 | o: Default::default(), |
64 | o2: Default::default(), |
65 | e: Default::default(), |
66 | } |
67 | } |
68 | } |
69 | |
70 | impl<F, G, I, O, O2, E> Parser<I, O2, E> for Map<F, G, I, O, O2, E> |
71 | where |
72 | F: Parser<I, O, E>, |
73 | G: FnMut(O) -> O2, |
74 | { |
75 | #[inline ] |
76 | fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { |
77 | match self.parser.parse_next(input:i) { |
78 | Err(e: ErrMode) => Err(e), |
79 | Ok(o: O) => Ok((self.map)(o)), |
80 | } |
81 | } |
82 | } |
83 | |
84 | /// Implementation of [`Parser::try_map`] |
85 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
86 | pub struct TryMap<F, G, I, O, O2, E, E2> |
87 | where |
88 | F: Parser<I, O, E>, |
89 | G: FnMut(O) -> Result<O2, E2>, |
90 | I: Stream, |
91 | E: FromExternalError<I, E2>, |
92 | { |
93 | parser: F, |
94 | map: G, |
95 | i: core::marker::PhantomData<I>, |
96 | o: core::marker::PhantomData<O>, |
97 | o2: core::marker::PhantomData<O2>, |
98 | e: core::marker::PhantomData<E>, |
99 | e2: core::marker::PhantomData<E2>, |
100 | } |
101 | |
102 | impl<F, G, I, O, O2, E, E2> TryMap<F, G, I, O, O2, E, E2> |
103 | where |
104 | F: Parser<I, O, E>, |
105 | G: FnMut(O) -> Result<O2, E2>, |
106 | I: Stream, |
107 | E: FromExternalError<I, E2>, |
108 | { |
109 | #[inline (always)] |
110 | pub(crate) fn new(parser: F, map: G) -> Self { |
111 | Self { |
112 | parser, |
113 | map, |
114 | i: Default::default(), |
115 | o: Default::default(), |
116 | o2: Default::default(), |
117 | e: Default::default(), |
118 | e2: Default::default(), |
119 | } |
120 | } |
121 | } |
122 | |
123 | impl<F, G, I, O, O2, E, E2> Parser<I, O2, E> for TryMap<F, G, I, O, O2, E, E2> |
124 | where |
125 | F: Parser<I, O, E>, |
126 | G: FnMut(O) -> Result<O2, E2>, |
127 | I: Stream, |
128 | E: FromExternalError<I, E2>, |
129 | { |
130 | #[inline ] |
131 | fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { |
132 | let start: ::Checkpoint = input.checkpoint(); |
133 | let o: O = self.parser.parse_next(input)?; |
134 | let res: Result> = (self.map)(o).map_err(|err: E2| { |
135 | input.reset(checkpoint:start); |
136 | ErrMode::from_external_error(input, kind:ErrorKind::Verify, e:err) |
137 | }); |
138 | trace_result(name:"verify" , &res); |
139 | res |
140 | } |
141 | } |
142 | |
143 | /// Implementation of [`Parser::verify_map`] |
144 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
145 | pub struct VerifyMap<F, G, I, O, O2, E> |
146 | where |
147 | F: Parser<I, O, E>, |
148 | G: FnMut(O) -> Option<O2>, |
149 | I: Stream, |
150 | E: ParserError<I>, |
151 | { |
152 | parser: F, |
153 | map: G, |
154 | i: core::marker::PhantomData<I>, |
155 | o: core::marker::PhantomData<O>, |
156 | o2: core::marker::PhantomData<O2>, |
157 | e: core::marker::PhantomData<E>, |
158 | } |
159 | |
160 | impl<F, G, I, O, O2, E> VerifyMap<F, G, I, O, O2, E> |
161 | where |
162 | F: Parser<I, O, E>, |
163 | G: FnMut(O) -> Option<O2>, |
164 | I: Stream, |
165 | E: ParserError<I>, |
166 | { |
167 | #[inline (always)] |
168 | pub(crate) fn new(parser: F, map: G) -> Self { |
169 | Self { |
170 | parser, |
171 | map, |
172 | i: Default::default(), |
173 | o: Default::default(), |
174 | o2: Default::default(), |
175 | e: Default::default(), |
176 | } |
177 | } |
178 | } |
179 | |
180 | impl<F, G, I, O, O2, E> Parser<I, O2, E> for VerifyMap<F, G, I, O, O2, E> |
181 | where |
182 | F: Parser<I, O, E>, |
183 | G: FnMut(O) -> Option<O2>, |
184 | I: Stream, |
185 | E: ParserError<I>, |
186 | { |
187 | #[inline ] |
188 | fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { |
189 | let start: ::Checkpoint = input.checkpoint(); |
190 | let o: O = self.parser.parse_next(input)?; |
191 | let res: Result> = (self.map)(o).ok_or_else(|| { |
192 | input.reset(checkpoint:start); |
193 | ErrMode::from_error_kind(input, kind:ErrorKind::Verify) |
194 | }); |
195 | trace_result(name:"verify" , &res); |
196 | res |
197 | } |
198 | } |
199 | |
200 | /// Implementation of [`Parser::and_then`] |
201 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
202 | pub struct AndThen<F, G, I, O, O2, E> |
203 | where |
204 | F: Parser<I, O, E>, |
205 | G: Parser<O, O2, E>, |
206 | O: StreamIsPartial, |
207 | I: Stream, |
208 | { |
209 | outer: F, |
210 | inner: G, |
211 | i: core::marker::PhantomData<I>, |
212 | o: core::marker::PhantomData<O>, |
213 | o2: core::marker::PhantomData<O2>, |
214 | e: core::marker::PhantomData<E>, |
215 | } |
216 | |
217 | impl<F, G, I, O, O2, E> AndThen<F, G, I, O, O2, E> |
218 | where |
219 | F: Parser<I, O, E>, |
220 | G: Parser<O, O2, E>, |
221 | O: StreamIsPartial, |
222 | I: Stream, |
223 | { |
224 | #[inline (always)] |
225 | pub(crate) fn new(outer: F, inner: G) -> Self { |
226 | Self { |
227 | outer, |
228 | inner, |
229 | i: Default::default(), |
230 | o: Default::default(), |
231 | o2: Default::default(), |
232 | e: Default::default(), |
233 | } |
234 | } |
235 | } |
236 | |
237 | impl<F, G, I, O, O2, E> Parser<I, O2, E> for AndThen<F, G, I, O, O2, E> |
238 | where |
239 | F: Parser<I, O, E>, |
240 | G: Parser<O, O2, E>, |
241 | O: StreamIsPartial, |
242 | I: Stream, |
243 | { |
244 | #[inline (always)] |
245 | fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { |
246 | let start: ::Checkpoint = i.checkpoint(); |
247 | let mut o: O = self.outer.parse_next(input:i)?; |
248 | let _ = o.complete(); |
249 | let o2: O2 = self.inner.parse_next(&mut o).map_err(|err: ErrMode| { |
250 | i.reset(checkpoint:start); |
251 | err |
252 | })?; |
253 | Ok(o2) |
254 | } |
255 | } |
256 | |
257 | /// Implementation of [`Parser::parse_to`] |
258 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
259 | pub struct ParseTo<P, I, O, O2, E> |
260 | where |
261 | P: Parser<I, O, E>, |
262 | I: Stream, |
263 | O: crate::stream::ParseSlice<O2>, |
264 | E: ParserError<I>, |
265 | { |
266 | p: P, |
267 | i: core::marker::PhantomData<I>, |
268 | o: core::marker::PhantomData<O>, |
269 | o2: core::marker::PhantomData<O2>, |
270 | e: core::marker::PhantomData<E>, |
271 | } |
272 | |
273 | impl<P, I, O, O2, E> ParseTo<P, I, O, O2, E> |
274 | where |
275 | P: Parser<I, O, E>, |
276 | I: Stream, |
277 | O: crate::stream::ParseSlice<O2>, |
278 | E: ParserError<I>, |
279 | { |
280 | #[inline (always)] |
281 | pub(crate) fn new(p: P) -> Self { |
282 | Self { |
283 | p, |
284 | i: Default::default(), |
285 | o: Default::default(), |
286 | o2: Default::default(), |
287 | e: Default::default(), |
288 | } |
289 | } |
290 | } |
291 | |
292 | impl<P, I, O, O2, E> Parser<I, O2, E> for ParseTo<P, I, O, O2, E> |
293 | where |
294 | P: Parser<I, O, E>, |
295 | I: Stream, |
296 | O: crate::stream::ParseSlice<O2>, |
297 | E: ParserError<I>, |
298 | { |
299 | #[inline ] |
300 | fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { |
301 | let start: ::Checkpoint = i.checkpoint(); |
302 | let o: O = self.p.parse_next(input:i)?; |
303 | let res: Result> = o.parse_slice().ok_or_else(|| { |
304 | i.reset(checkpoint:start); |
305 | ErrMode::from_error_kind(input:i, kind:ErrorKind::Verify) |
306 | }); |
307 | trace_result(name:"verify" , &res); |
308 | res |
309 | } |
310 | } |
311 | |
312 | /// Implementation of [`Parser::flat_map`] |
313 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
314 | pub struct FlatMap<F, G, H, I, O, O2, E> |
315 | where |
316 | F: Parser<I, O, E>, |
317 | G: FnMut(O) -> H, |
318 | H: Parser<I, O2, E>, |
319 | { |
320 | f: F, |
321 | g: G, |
322 | h: core::marker::PhantomData<H>, |
323 | i: core::marker::PhantomData<I>, |
324 | o: core::marker::PhantomData<O>, |
325 | o2: core::marker::PhantomData<O2>, |
326 | e: core::marker::PhantomData<E>, |
327 | } |
328 | |
329 | impl<F, G, H, I, O, O2, E> FlatMap<F, G, H, I, O, O2, E> |
330 | where |
331 | F: Parser<I, O, E>, |
332 | G: FnMut(O) -> H, |
333 | H: Parser<I, O2, E>, |
334 | { |
335 | #[inline (always)] |
336 | pub(crate) fn new(f: F, g: G) -> Self { |
337 | Self { |
338 | f, |
339 | g, |
340 | h: Default::default(), |
341 | i: Default::default(), |
342 | o: Default::default(), |
343 | o2: Default::default(), |
344 | e: Default::default(), |
345 | } |
346 | } |
347 | } |
348 | |
349 | impl<F, G, H, I, O, O2, E> Parser<I, O2, E> for FlatMap<F, G, H, I, O, O2, E> |
350 | where |
351 | F: Parser<I, O, E>, |
352 | G: FnMut(O) -> H, |
353 | H: Parser<I, O2, E>, |
354 | { |
355 | #[inline (always)] |
356 | fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { |
357 | let o: O = self.f.parse_next(input:i)?; |
358 | (self.g)(o).parse_next(input:i) |
359 | } |
360 | } |
361 | |
362 | /// Implementation of [`Parser::complete_err`] |
363 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
364 | pub struct CompleteErr<F> { |
365 | f: F, |
366 | } |
367 | |
368 | impl<F> CompleteErr<F> { |
369 | #[inline (always)] |
370 | pub(crate) fn new(f: F) -> Self { |
371 | Self { f } |
372 | } |
373 | } |
374 | |
375 | impl<F, I, O, E> Parser<I, O, E> for CompleteErr<F> |
376 | where |
377 | I: Stream, |
378 | F: Parser<I, O, E>, |
379 | E: ParserError<I>, |
380 | { |
381 | #[inline ] |
382 | fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { |
383 | traceimpl Parser(name:"complete_err" , |input: &mut I| { |
384 | match (self.f).parse_next(input) { |
385 | Err(ErrMode::Incomplete(_)) => { |
386 | Err(ErrMode::from_error_kind(input, kind:ErrorKind::Complete)) |
387 | } |
388 | rest: Result> => rest, |
389 | } |
390 | }) |
391 | .parse_next(input) |
392 | } |
393 | } |
394 | |
395 | /// Implementation of [`Parser::verify`] |
396 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
397 | pub struct Verify<F, G, I, O, O2, E> |
398 | where |
399 | F: Parser<I, O, E>, |
400 | G: FnMut(&O2) -> bool, |
401 | I: Stream, |
402 | O: Borrow<O2>, |
403 | O2: ?Sized, |
404 | E: ParserError<I>, |
405 | { |
406 | parser: F, |
407 | filter: G, |
408 | i: core::marker::PhantomData<I>, |
409 | o: core::marker::PhantomData<O>, |
410 | o2: core::marker::PhantomData<O2>, |
411 | e: core::marker::PhantomData<E>, |
412 | } |
413 | |
414 | impl<F, G, I, O, O2, E> Verify<F, G, I, O, O2, E> |
415 | where |
416 | F: Parser<I, O, E>, |
417 | G: FnMut(&O2) -> bool, |
418 | I: Stream, |
419 | O: Borrow<O2>, |
420 | O2: ?Sized, |
421 | E: ParserError<I>, |
422 | { |
423 | #[inline (always)] |
424 | pub(crate) fn new(parser: F, filter: G) -> Self { |
425 | Self { |
426 | parser, |
427 | filter, |
428 | i: Default::default(), |
429 | o: Default::default(), |
430 | o2: Default::default(), |
431 | e: Default::default(), |
432 | } |
433 | } |
434 | } |
435 | |
436 | impl<F, G, I, O, O2, E> Parser<I, O, E> for Verify<F, G, I, O, O2, E> |
437 | where |
438 | F: Parser<I, O, E>, |
439 | G: FnMut(&O2) -> bool, |
440 | I: Stream, |
441 | O: Borrow<O2>, |
442 | O2: ?Sized, |
443 | E: ParserError<I>, |
444 | { |
445 | #[inline ] |
446 | fn parse_next(&mut self, input: &mut I) -> PResult<O, E> { |
447 | let start: ::Checkpoint = input.checkpoint(); |
448 | let o: O = self.parser.parse_next(input)?; |
449 | let res: Result> = (self.filter)(o.borrow()).then_some(o).ok_or_else(|| { |
450 | input.reset(checkpoint:start); |
451 | ErrMode::from_error_kind(input, kind:ErrorKind::Verify) |
452 | }); |
453 | trace_result(name:"verify" , &res); |
454 | res |
455 | } |
456 | } |
457 | |
458 | /// Implementation of [`Parser::value`] |
459 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
460 | pub struct Value<F, I, O, O2, E> |
461 | where |
462 | F: Parser<I, O, E>, |
463 | O2: Clone, |
464 | { |
465 | parser: F, |
466 | val: O2, |
467 | i: core::marker::PhantomData<I>, |
468 | o: core::marker::PhantomData<O>, |
469 | e: core::marker::PhantomData<E>, |
470 | } |
471 | |
472 | impl<F, I, O, O2, E> Value<F, I, O, O2, E> |
473 | where |
474 | F: Parser<I, O, E>, |
475 | O2: Clone, |
476 | { |
477 | #[inline (always)] |
478 | pub(crate) fn new(parser: F, val: O2) -> Self { |
479 | Self { |
480 | parser, |
481 | val, |
482 | i: Default::default(), |
483 | o: Default::default(), |
484 | e: Default::default(), |
485 | } |
486 | } |
487 | } |
488 | |
489 | impl<F, I, O, O2, E> Parser<I, O2, E> for Value<F, I, O, O2, E> |
490 | where |
491 | F: Parser<I, O, E>, |
492 | O2: Clone, |
493 | { |
494 | #[inline ] |
495 | fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { |
496 | (self.parser).parse_next(input).map(|_| self.val.clone()) |
497 | } |
498 | } |
499 | |
500 | /// Implementation of [`Parser::default_value`] |
501 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
502 | pub struct DefaultValue<F, I, O, O2, E> |
503 | where |
504 | F: Parser<I, O, E>, |
505 | O2: core::default::Default, |
506 | { |
507 | parser: F, |
508 | o2: core::marker::PhantomData<O2>, |
509 | i: core::marker::PhantomData<I>, |
510 | o: core::marker::PhantomData<O>, |
511 | e: core::marker::PhantomData<E>, |
512 | } |
513 | |
514 | impl<F, I, O, O2, E> DefaultValue<F, I, O, O2, E> |
515 | where |
516 | F: Parser<I, O, E>, |
517 | O2: core::default::Default, |
518 | { |
519 | #[inline (always)] |
520 | pub(crate) fn new(parser: F) -> Self { |
521 | Self { |
522 | parser, |
523 | o2: Default::default(), |
524 | i: Default::default(), |
525 | o: Default::default(), |
526 | e: Default::default(), |
527 | } |
528 | } |
529 | } |
530 | |
531 | impl<F, I, O, O2, E> Parser<I, O2, E> for DefaultValue<F, I, O, O2, E> |
532 | where |
533 | F: Parser<I, O, E>, |
534 | O2: core::default::Default, |
535 | { |
536 | #[inline ] |
537 | fn parse_next(&mut self, input: &mut I) -> PResult<O2, E> { |
538 | (self.parser).parse_next(input).map(|_| O2::default()) |
539 | } |
540 | } |
541 | |
542 | /// Implementation of [`Parser::void`] |
543 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
544 | pub struct Void<F, I, O, E> |
545 | where |
546 | F: Parser<I, O, E>, |
547 | { |
548 | parser: F, |
549 | i: core::marker::PhantomData<I>, |
550 | o: core::marker::PhantomData<O>, |
551 | e: core::marker::PhantomData<E>, |
552 | } |
553 | |
554 | impl<F, I, O, E> Void<F, I, O, E> |
555 | where |
556 | F: Parser<I, O, E>, |
557 | { |
558 | #[inline (always)] |
559 | pub(crate) fn new(parser: F) -> Self { |
560 | Self { |
561 | parser, |
562 | i: Default::default(), |
563 | o: Default::default(), |
564 | e: Default::default(), |
565 | } |
566 | } |
567 | } |
568 | |
569 | impl<F, I, O, E> Parser<I, (), E> for Void<F, I, O, E> |
570 | where |
571 | F: Parser<I, O, E>, |
572 | { |
573 | #[inline (always)] |
574 | fn parse_next(&mut self, input: &mut I) -> PResult<(), E> { |
575 | (self.parser).parse_next(input).map(|_| ()) |
576 | } |
577 | } |
578 | |
579 | /// Implementation of [`Parser::recognize`] |
580 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
581 | pub struct Recognize<F, I, O, E> |
582 | where |
583 | F: Parser<I, O, E>, |
584 | I: Stream, |
585 | { |
586 | parser: F, |
587 | i: core::marker::PhantomData<I>, |
588 | o: core::marker::PhantomData<O>, |
589 | e: core::marker::PhantomData<E>, |
590 | } |
591 | |
592 | impl<F, I, O, E> Recognize<F, I, O, E> |
593 | where |
594 | F: Parser<I, O, E>, |
595 | I: Stream, |
596 | { |
597 | #[inline (always)] |
598 | pub(crate) fn new(parser: F) -> Self { |
599 | Self { |
600 | parser, |
601 | i: Default::default(), |
602 | o: Default::default(), |
603 | e: Default::default(), |
604 | } |
605 | } |
606 | } |
607 | |
608 | impl<I, O, E, F> Parser<I, <I as Stream>::Slice, E> for Recognize<F, I, O, E> |
609 | where |
610 | F: Parser<I, O, E>, |
611 | I: Stream, |
612 | { |
613 | #[inline ] |
614 | fn parse_next(&mut self, input: &mut I) -> PResult<<I as Stream>::Slice, E> { |
615 | let checkpoint: ::Checkpoint = input.checkpoint(); |
616 | match (self.parser).parse_next(input) { |
617 | Ok(_) => { |
618 | let offset: usize = input.offset_from(&checkpoint); |
619 | input.reset(checkpoint); |
620 | let recognized: ::Slice = input.next_slice(offset); |
621 | Ok(recognized) |
622 | } |
623 | Err(e: ErrMode) => Err(e), |
624 | } |
625 | } |
626 | } |
627 | |
628 | /// Implementation of [`Parser::with_recognized`] |
629 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
630 | pub struct WithRecognized<F, I, O, E> |
631 | where |
632 | F: Parser<I, O, E>, |
633 | I: Stream, |
634 | { |
635 | parser: F, |
636 | i: core::marker::PhantomData<I>, |
637 | o: core::marker::PhantomData<O>, |
638 | e: core::marker::PhantomData<E>, |
639 | } |
640 | |
641 | impl<F, I, O, E> WithRecognized<F, I, O, E> |
642 | where |
643 | F: Parser<I, O, E>, |
644 | I: Stream, |
645 | { |
646 | #[inline (always)] |
647 | pub(crate) fn new(parser: F) -> Self { |
648 | Self { |
649 | parser, |
650 | i: Default::default(), |
651 | o: Default::default(), |
652 | e: Default::default(), |
653 | } |
654 | } |
655 | } |
656 | |
657 | impl<F, I, O, E> Parser<I, (O, <I as Stream>::Slice), E> for WithRecognized<F, I, O, E> |
658 | where |
659 | F: Parser<I, O, E>, |
660 | I: Stream, |
661 | { |
662 | #[inline ] |
663 | fn parse_next(&mut self, input: &mut I) -> PResult<(O, <I as Stream>::Slice), E> { |
664 | let checkpoint: ::Checkpoint = input.checkpoint(); |
665 | match (self.parser).parse_next(input) { |
666 | Ok(result: O) => { |
667 | let offset: usize = input.offset_from(&checkpoint); |
668 | input.reset(checkpoint); |
669 | let recognized: ::Slice = input.next_slice(offset); |
670 | Ok((result, recognized)) |
671 | } |
672 | Err(e: ErrMode) => Err(e), |
673 | } |
674 | } |
675 | } |
676 | |
677 | /// Implementation of [`Parser::span`] |
678 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
679 | pub struct Span<F, I, O, E> |
680 | where |
681 | F: Parser<I, O, E>, |
682 | I: Stream + Location, |
683 | { |
684 | parser: F, |
685 | i: core::marker::PhantomData<I>, |
686 | o: core::marker::PhantomData<O>, |
687 | e: core::marker::PhantomData<E>, |
688 | } |
689 | |
690 | impl<F, I, O, E> Span<F, I, O, E> |
691 | where |
692 | F: Parser<I, O, E>, |
693 | I: Stream + Location, |
694 | { |
695 | #[inline (always)] |
696 | pub(crate) fn new(parser: F) -> Self { |
697 | Self { |
698 | parser, |
699 | i: Default::default(), |
700 | o: Default::default(), |
701 | e: Default::default(), |
702 | } |
703 | } |
704 | } |
705 | |
706 | impl<I, O, E, F> Parser<I, Range<usize>, E> for Span<F, I, O, E> |
707 | where |
708 | F: Parser<I, O, E>, |
709 | I: Stream + Location, |
710 | { |
711 | #[inline ] |
712 | fn parse_next(&mut self, input: &mut I) -> PResult<Range<usize>, E> { |
713 | let start: usize = input.location(); |
714 | self.parser.parse_next(input).map(op:move |_| { |
715 | let end: usize = input.location(); |
716 | start..end |
717 | }) |
718 | } |
719 | } |
720 | |
721 | /// Implementation of [`Parser::with_span`] |
722 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
723 | pub struct WithSpan<F, I, O, E> |
724 | where |
725 | F: Parser<I, O, E>, |
726 | I: Stream + Location, |
727 | { |
728 | parser: F, |
729 | i: core::marker::PhantomData<I>, |
730 | o: core::marker::PhantomData<O>, |
731 | e: core::marker::PhantomData<E>, |
732 | } |
733 | |
734 | impl<F, I, O, E> WithSpan<F, I, O, E> |
735 | where |
736 | F: Parser<I, O, E>, |
737 | I: Stream + Location, |
738 | { |
739 | #[inline (always)] |
740 | pub(crate) fn new(parser: F) -> Self { |
741 | Self { |
742 | parser, |
743 | i: Default::default(), |
744 | o: Default::default(), |
745 | e: Default::default(), |
746 | } |
747 | } |
748 | } |
749 | |
750 | impl<F, I, O, E> Parser<I, (O, Range<usize>), E> for WithSpan<F, I, O, E> |
751 | where |
752 | F: Parser<I, O, E>, |
753 | I: Stream + Location, |
754 | { |
755 | #[inline ] |
756 | fn parse_next(&mut self, input: &mut I) -> PResult<(O, Range<usize>), E> { |
757 | let start: usize = input.location(); |
758 | self.parser.parse_next(input).map(op:move |output: O| { |
759 | let end: usize = input.location(); |
760 | (output, (start..end)) |
761 | }) |
762 | } |
763 | } |
764 | |
765 | /// Implementation of [`Parser::output_into`] |
766 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
767 | pub struct OutputInto<F, I, O, O2, E> |
768 | where |
769 | F: Parser<I, O, E>, |
770 | O: Into<O2>, |
771 | { |
772 | parser: F, |
773 | i: core::marker::PhantomData<I>, |
774 | o: core::marker::PhantomData<O>, |
775 | o2: core::marker::PhantomData<O2>, |
776 | e: core::marker::PhantomData<E>, |
777 | } |
778 | |
779 | impl<F, I, O, O2, E> OutputInto<F, I, O, O2, E> |
780 | where |
781 | F: Parser<I, O, E>, |
782 | O: Into<O2>, |
783 | { |
784 | #[inline (always)] |
785 | pub(crate) fn new(parser: F) -> Self { |
786 | Self { |
787 | parser, |
788 | i: Default::default(), |
789 | o: Default::default(), |
790 | o2: Default::default(), |
791 | e: Default::default(), |
792 | } |
793 | } |
794 | } |
795 | |
796 | impl<F, I, O, O2, E> Parser<I, O2, E> for OutputInto<F, I, O, O2, E> |
797 | where |
798 | F: Parser<I, O, E>, |
799 | O: Into<O2>, |
800 | { |
801 | #[inline ] |
802 | fn parse_next(&mut self, i: &mut I) -> PResult<O2, E> { |
803 | self.parser.parse_next(i).map(|o: O| o.into()) |
804 | } |
805 | } |
806 | |
807 | /// Implementation of [`Parser::err_into`] |
808 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
809 | pub struct ErrInto<F, I, O, E, E2> |
810 | where |
811 | F: Parser<I, O, E>, |
812 | E: Into<E2>, |
813 | { |
814 | parser: F, |
815 | i: core::marker::PhantomData<I>, |
816 | o: core::marker::PhantomData<O>, |
817 | e: core::marker::PhantomData<E>, |
818 | e2: core::marker::PhantomData<E2>, |
819 | } |
820 | |
821 | impl<F, I, O, E, E2> ErrInto<F, I, O, E, E2> |
822 | where |
823 | F: Parser<I, O, E>, |
824 | E: Into<E2>, |
825 | { |
826 | #[inline (always)] |
827 | pub(crate) fn new(parser: F) -> Self { |
828 | Self { |
829 | parser, |
830 | i: Default::default(), |
831 | o: Default::default(), |
832 | e: Default::default(), |
833 | e2: Default::default(), |
834 | } |
835 | } |
836 | } |
837 | |
838 | impl<F, I, O, E, E2> Parser<I, O, E2> for ErrInto<F, I, O, E, E2> |
839 | where |
840 | F: Parser<I, O, E>, |
841 | E: Into<E2>, |
842 | { |
843 | #[inline ] |
844 | fn parse_next(&mut self, i: &mut I) -> PResult<O, E2> { |
845 | match self.parser.parse_next(input:i) { |
846 | Ok(ok: O) => Ok(ok), |
847 | Err(ErrMode::Backtrack(e: E)) => Err(ErrMode::Backtrack(e.into())), |
848 | Err(ErrMode::Cut(e: E)) => Err(ErrMode::Cut(e.into())), |
849 | Err(ErrMode::Incomplete(e: Needed)) => Err(ErrMode::Incomplete(e)), |
850 | } |
851 | } |
852 | } |
853 | |
854 | /// Implementation of [`Parser::context`] |
855 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
856 | pub struct Context<F, I, O, E, C> |
857 | where |
858 | F: Parser<I, O, E>, |
859 | I: Stream, |
860 | E: AddContext<I, C>, |
861 | C: Clone + crate::lib::std::fmt::Debug, |
862 | { |
863 | parser: F, |
864 | context: C, |
865 | i: core::marker::PhantomData<I>, |
866 | o: core::marker::PhantomData<O>, |
867 | e: core::marker::PhantomData<E>, |
868 | } |
869 | |
870 | impl<F, I, O, E, C> Context<F, I, O, E, C> |
871 | where |
872 | F: Parser<I, O, E>, |
873 | I: Stream, |
874 | E: AddContext<I, C>, |
875 | C: Clone + crate::lib::std::fmt::Debug, |
876 | { |
877 | #[inline (always)] |
878 | pub(crate) fn new(parser: F, context: C) -> Self { |
879 | Self { |
880 | parser, |
881 | context, |
882 | i: Default::default(), |
883 | o: Default::default(), |
884 | e: Default::default(), |
885 | } |
886 | } |
887 | } |
888 | |
889 | impl<F, I, O, E, C> Parser<I, O, E> for Context<F, I, O, E, C> |
890 | where |
891 | F: Parser<I, O, E>, |
892 | I: Stream, |
893 | E: AddContext<I, C>, |
894 | C: Clone + crate::lib::std::fmt::Debug, |
895 | { |
896 | #[inline ] |
897 | fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { |
898 | #[cfg (feature = "debug" )] |
899 | let name = format!("context= {:?}" , self.context); |
900 | #[cfg (not(feature = "debug" ))] |
901 | let name: &str = "context" ; |
902 | trace(name, move |i: &mut I| { |
903 | (self.parser) |
904 | .parse_next(i) |
905 | .map_err(|err| err.add_context(i, self.context.clone())) |
906 | }) |
907 | .parse_next(input:i) |
908 | } |
909 | } |
910 | |
911 | /// Implementation of [`Parser::retry_after`] |
912 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
913 | #[cfg (feature = "unstable-recover" )] |
914 | pub struct RetryAfter<P, R, I, O, E> |
915 | where |
916 | P: Parser<I, O, E>, |
917 | R: Parser<I, (), E>, |
918 | I: Stream, |
919 | I: Recover<E>, |
920 | E: FromRecoverableError<I, E>, |
921 | { |
922 | parser: P, |
923 | recover: R, |
924 | i: core::marker::PhantomData<I>, |
925 | o: core::marker::PhantomData<O>, |
926 | e: core::marker::PhantomData<E>, |
927 | } |
928 | |
929 | #[cfg (feature = "unstable-recover" )] |
930 | impl<P, R, I, O, E> RetryAfter<P, R, I, O, E> |
931 | where |
932 | P: Parser<I, O, E>, |
933 | R: Parser<I, (), E>, |
934 | I: Stream, |
935 | I: Recover<E>, |
936 | E: FromRecoverableError<I, E>, |
937 | { |
938 | #[inline (always)] |
939 | pub(crate) fn new(parser: P, recover: R) -> Self { |
940 | Self { |
941 | parser, |
942 | recover, |
943 | i: Default::default(), |
944 | o: Default::default(), |
945 | e: Default::default(), |
946 | } |
947 | } |
948 | } |
949 | |
950 | #[cfg (feature = "unstable-recover" )] |
951 | impl<P, R, I, O, E> Parser<I, O, E> for RetryAfter<P, R, I, O, E> |
952 | where |
953 | P: Parser<I, O, E>, |
954 | R: Parser<I, (), E>, |
955 | I: Stream, |
956 | I: Recover<E>, |
957 | E: FromRecoverableError<I, E>, |
958 | { |
959 | #[inline (always)] |
960 | fn parse_next(&mut self, i: &mut I) -> PResult<O, E> { |
961 | if I::is_recovery_supported() { |
962 | retry_after_inner(&mut self.parser, &mut self.recover, i) |
963 | } else { |
964 | self.parser.parse_next(i) |
965 | } |
966 | } |
967 | } |
968 | |
969 | #[cfg (feature = "unstable-recover" )] |
970 | fn retry_after_inner<P, R, I, O, E>(parser: &mut P, recover: &mut R, i: &mut I) -> PResult<O, E> |
971 | where |
972 | P: Parser<I, O, E>, |
973 | R: Parser<I, (), E>, |
974 | I: Stream, |
975 | I: Recover<E>, |
976 | E: FromRecoverableError<I, E>, |
977 | { |
978 | loop { |
979 | let token_start = i.checkpoint(); |
980 | let mut err = match parser.parse_next(i) { |
981 | Ok(o) => { |
982 | return Ok(o); |
983 | } |
984 | Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)), |
985 | Err(err) => err, |
986 | }; |
987 | let err_start = i.checkpoint(); |
988 | let err_start_eof_offset = i.eof_offset(); |
989 | if recover.parse_next(i).is_ok() { |
990 | let i_eof_offset = i.eof_offset(); |
991 | if err_start_eof_offset == i_eof_offset { |
992 | // Didn't advance so bubble the error up |
993 | } else if let Err(err_) = i.record_err(&token_start, &err_start, err) { |
994 | err = err_; |
995 | } else { |
996 | continue; |
997 | } |
998 | } |
999 | |
1000 | i.reset(err_start.clone()); |
1001 | err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err)); |
1002 | return Err(err); |
1003 | } |
1004 | } |
1005 | |
1006 | /// Implementation of [`Parser::resume_after`] |
1007 | #[cfg (feature = "unstable-recover" )] |
1008 | #[cfg_attr (nightly, warn(rustdoc::missing_doc_code_examples))] |
1009 | pub struct ResumeAfter<P, R, I, O, E> |
1010 | where |
1011 | P: Parser<I, O, E>, |
1012 | R: Parser<I, (), E>, |
1013 | I: Stream, |
1014 | I: Recover<E>, |
1015 | E: FromRecoverableError<I, E>, |
1016 | { |
1017 | parser: P, |
1018 | recover: R, |
1019 | i: core::marker::PhantomData<I>, |
1020 | o: core::marker::PhantomData<O>, |
1021 | e: core::marker::PhantomData<E>, |
1022 | } |
1023 | |
1024 | #[cfg (feature = "unstable-recover" )] |
1025 | impl<P, R, I, O, E> ResumeAfter<P, R, I, O, E> |
1026 | where |
1027 | P: Parser<I, O, E>, |
1028 | R: Parser<I, (), E>, |
1029 | I: Stream, |
1030 | I: Recover<E>, |
1031 | E: FromRecoverableError<I, E>, |
1032 | { |
1033 | #[inline (always)] |
1034 | pub(crate) fn new(parser: P, recover: R) -> Self { |
1035 | Self { |
1036 | parser, |
1037 | recover, |
1038 | i: Default::default(), |
1039 | o: Default::default(), |
1040 | e: Default::default(), |
1041 | } |
1042 | } |
1043 | } |
1044 | |
1045 | #[cfg (feature = "unstable-recover" )] |
1046 | impl<P, R, I, O, E> Parser<I, Option<O>, E> for ResumeAfter<P, R, I, O, E> |
1047 | where |
1048 | P: Parser<I, O, E>, |
1049 | R: Parser<I, (), E>, |
1050 | I: Stream, |
1051 | I: Recover<E>, |
1052 | E: FromRecoverableError<I, E>, |
1053 | { |
1054 | #[inline (always)] |
1055 | fn parse_next(&mut self, i: &mut I) -> PResult<Option<O>, E> { |
1056 | if I::is_recovery_supported() { |
1057 | resume_after_inner(&mut self.parser, &mut self.recover, i) |
1058 | } else { |
1059 | self.parser.parse_next(i).map(Some) |
1060 | } |
1061 | } |
1062 | } |
1063 | |
1064 | #[cfg (feature = "unstable-recover" )] |
1065 | fn resume_after_inner<P, R, I, O, E>( |
1066 | parser: &mut P, |
1067 | recover: &mut R, |
1068 | i: &mut I, |
1069 | ) -> PResult<Option<O>, E> |
1070 | where |
1071 | P: Parser<I, O, E>, |
1072 | R: Parser<I, (), E>, |
1073 | I: Stream, |
1074 | I: Recover<E>, |
1075 | E: FromRecoverableError<I, E>, |
1076 | { |
1077 | let token_start = i.checkpoint(); |
1078 | let mut err = match parser.parse_next(i) { |
1079 | Ok(o) => { |
1080 | return Ok(Some(o)); |
1081 | } |
1082 | Err(ErrMode::Incomplete(e)) => return Err(ErrMode::Incomplete(e)), |
1083 | Err(err) => err, |
1084 | }; |
1085 | let err_start = i.checkpoint(); |
1086 | if recover.parse_next(i).is_ok() { |
1087 | if let Err(err_) = i.record_err(&token_start, &err_start, err) { |
1088 | err = err_; |
1089 | } else { |
1090 | return Ok(None); |
1091 | } |
1092 | } |
1093 | |
1094 | i.reset(err_start.clone()); |
1095 | err = err.map(|err| E::from_recoverable_error(&token_start, &err_start, i, err)); |
1096 | Err(err) |
1097 | } |
1098 | |