mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-17 02:20:16 +00:00
added and_then docs
This commit is contained in:
parent
3f2d2dd908
commit
3cb0edf958
1 changed files with 42 additions and 0 deletions
|
@ -834,6 +834,48 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a parser from two other parser.
|
||||
/// However, the second parser is created based on the results of the first parser
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## Success Case
|
||||
/// ```
|
||||
/// # use roc_parse::state::{State};
|
||||
/// # use crate::roc_parse::parser::{Parser, Progress, and_then, word};
|
||||
/// # use roc_region::all::Position;
|
||||
/// # use bumpalo::Bump;
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # enum Problem {
|
||||
/// # NotFound(Position),
|
||||
/// # }
|
||||
/// # let arena = Bump::new();
|
||||
/// let parser1 = word("hello", Problem::NotFound);
|
||||
/// let parser = and_then(parser1, move |p,b| word(", ", 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(), Position::new(7));
|
||||
/// ```
|
||||
/// ## Failure Case
|
||||
/// ```
|
||||
/// # use roc_parse::state::{State};
|
||||
/// # use crate::roc_parse::parser::{Parser, Progress, and_then, word};
|
||||
/// # use roc_region::all::Position;
|
||||
/// # use bumpalo::Bump;
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # enum Problem {
|
||||
/// # NotFound(Position),
|
||||
/// # }
|
||||
/// # let arena = Bump::new();
|
||||
/// let parser1 = word("hello", Problem::NotFound);
|
||||
/// let parser = and_then(parser1, move |p,b| word("!! ", Problem::NotFound));
|
||||
/// let (progress, problem) = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).unwrap_err();
|
||||
///
|
||||
/// assert_eq!(progress, Progress::NoProgress);
|
||||
/// assert_eq!(problem, Problem::NotFound(Position::new(5)));
|
||||
/// ```
|
||||
pub fn and_then<'a, P1, P2, F, Before, After, Error>(
|
||||
parser: P1,
|
||||
transform: F,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue