example for optional

This commit is contained in:
Trevor Settles 2024-03-08 22:04:27 -07:00
parent f3666b346b
commit 9c67ae15be
No known key found for this signature in database
GPG key ID: F46B83058222DBAA

View file

@ -1290,6 +1290,31 @@ where
}
}
/// Creates a parser that only fails if its inner parser partially matches
///
/// # Examples
/// ```
/// # use roc_parse::state::{State};
/// # use crate::roc_parse::parser::{Parser, Progress, optional, word};
/// # use roc_region::all::Position;
/// # use bumpalo::Bump;
/// # #[derive(Debug, PartialEq)]
/// # enum Problem {
/// # NotFound(Position),
/// # }
/// # let arena = Bump::new();
/// let parser = optional(word("hello", Problem::NotFound));
///
/// let (progress, output, state) = parser.parse(&arena, State::new("bye, world".as_bytes()), 0).unwrap();
/// assert_eq!(progress, Progress::NoProgress);
/// assert_eq!(output, None);
/// assert_eq!(state.pos().offset, 0);
///
/// let (progress, output, state) = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).unwrap();
/// assert_eq!(progress, Progress::MadeProgress);
/// assert_eq!(output, Some(()));
/// assert_eq!(state.pos().offset, 5);
/// ```
pub fn optional<'a, P, T, E>(parser: P) -> impl Parser<'a, Option<T>, E>
where
P: Parser<'a, T, E>,