Un-macro one_or_more

This commit is contained in:
Jackson Wambolt 2024-04-15 20:43:12 -05:00
parent f88e46fc32
commit 2f776f366f
No known key found for this signature in database
GPG key ID: 76F29A42FEE8811C

View file

@ -2311,39 +2311,39 @@ pub fn zero_or_more<'a, Output, E: 'a>(
/// # } /// # }
/// # foo(&arena); /// # foo(&arena);
/// ``` /// ```
#[macro_export] pub fn one_or_more<'a, Output, E: 'a>(
macro_rules! one_or_more { parser: impl Parser<'a, Output, E>,
($parser:expr, $to_error:expr) => { to_error: impl Fn(Position) -> E,
move |arena, state: State<'a>, min_indent: u32| { ) -> impl Parser<'a, bumpalo::collections::Vec<'a, Output>, E> {
use bumpalo::collections::Vec; move |arena, state: State<'a>, min_indent: u32| match parser.parse(
arena,
state.clone(),
min_indent,
) {
Ok((_, first_output, next_state)) => {
let mut state = next_state;
let mut buf = Vec::with_capacity_in(1, arena);
match $parser.parse(arena, state.clone(), min_indent) { buf.push(first_output);
Ok((_, first_output, next_state)) => {
let mut state = next_state;
let mut buf = Vec::with_capacity_in(1, arena);
buf.push(first_output); loop {
let old_state = state.clone();
loop { match parser.parse(arena, state, min_indent) {
let old_state = state.clone(); Ok((_, next_output, next_state)) => {
match $parser.parse(arena, state, min_indent) { state = next_state;
Ok((_, next_output, next_state)) => { buf.push(next_output);
state = next_state; }
buf.push(next_output); Err((NoProgress, _)) => {
} return Ok((MadeProgress, buf, old_state));
Err((NoProgress, _)) => { }
return Ok((MadeProgress, buf, old_state)); Err((MadeProgress, fail)) => {
} return Err((MadeProgress, fail));
Err((MadeProgress, fail)) => {
return Err((MadeProgress, fail));
}
}
} }
} }
Err((progress, _)) => Err((progress, $to_error(state.pos()))),
} }
} }
}; Err((progress, _)) => Err((progress, to_error(state.pos()))),
}
} }
/// Creates a parser that debug prints the result of parsing. /// Creates a parser that debug prints the result of parsing.