mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-15 09:30:16 +00:00
docs and example for and
This commit is contained in:
parent
bf40247aec
commit
09dbc93b7d
1 changed files with 33 additions and 0 deletions
|
@ -1621,6 +1621,39 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a new parser from two other parsers.
|
||||
/// The two parsers can have different outputs, but must have the same error type.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use roc_parse::state::{State};
|
||||
/// # use crate::roc_parse::parser::{Parser, Progress, word, word1};
|
||||
/// # use roc_region::all::{Loc, Position};
|
||||
/// # use roc_parse::ident::lowercase_ident;
|
||||
/// # use roc_parse::and;
|
||||
/// # use bumpalo::Bump;
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # enum Problem {
|
||||
/// # NotFound(Position),
|
||||
/// # }
|
||||
/// # let arena = Bump::new();
|
||||
/// # fn foo<'a>(arena: &'a Bump) {
|
||||
/// let parser = and!(
|
||||
/// word("hello", Problem::NotFound),
|
||||
/// word1(b',', Problem::NotFound)
|
||||
/// );
|
||||
///
|
||||
/// let (progress, output, state) = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).unwrap();
|
||||
/// assert_eq!(progress, Progress::MadeProgress);
|
||||
/// assert_eq!(output, ((), ()));
|
||||
/// assert_eq!(state.pos().offset, 6);
|
||||
///
|
||||
/// let (progress, err) = parser.parse(&arena, State::new("hello! world".as_bytes()), 0).unwrap_err();
|
||||
/// assert_eq!(progress, Progress::MadeProgress);
|
||||
/// assert_eq!(err, Problem::NotFound(Position::new(5)));
|
||||
/// # }
|
||||
/// # foo(&arena);
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! and {
|
||||
($p1:expr, $p2:expr) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue