mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
pass and fail examples
This commit is contained in:
parent
26e21addee
commit
ff19d1b834
1 changed files with 40 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::state::State;
|
||||
use crate::{ast::Pattern, state::State};
|
||||
use bumpalo::collections::vec::Vec;
|
||||
use bumpalo::Bump;
|
||||
use roc_region::all::{Loc, Position, Region};
|
||||
|
@ -1519,6 +1519,45 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use roc_parse::state::{State};
|
||||
/// # use crate::roc_parse::parser::{Parser, Progress, word};
|
||||
/// # use roc_region::all::Position;
|
||||
/// # use bumpalo::Bump;
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # enum Problem {
|
||||
/// # NotFound(Position),
|
||||
/// # }
|
||||
/// # fn foo() {
|
||||
/// # let arena = Bump::new();
|
||||
/// let parser = word("hello", Problem::NotFound);
|
||||
/// let (progress, m, state) = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).unwrap();
|
||||
/// assert_eq!(progress, Progress::MadeProgress);
|
||||
/// assert_eq!(m, ());
|
||||
/// assert_eq!(state.pos(), Position::new(5));
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// ```rust
|
||||
/// # use roc_parse::state::{State};
|
||||
/// # use crate::roc_parse::parser::{Parser, Progress, word};
|
||||
/// # use roc_region::all::Position;
|
||||
/// # use bumpalo::Bump;
|
||||
/// # #[derive(Debug, PartialEq)]
|
||||
/// # enum Problem {
|
||||
/// # NotFound(Position),
|
||||
/// # }
|
||||
/// # fn foo() {
|
||||
/// # let arena = Bump::new();
|
||||
/// let parser = word("bye", Problem::NotFound);
|
||||
/// let actual = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).map(|(a,b,_)| (a,b));
|
||||
/// assert_eq!(
|
||||
/// actual,
|
||||
/// Err((Progress::NoProgress, Problem::NotFound(Position::zero()))),
|
||||
/// );
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn word<'a, ToError, E>(word: &'static str, to_error: ToError) -> impl Parser<'a, (), E>
|
||||
where
|
||||
ToError: Fn(Position) -> E,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue