Refactor several parse methods to use more combinators

This commit is contained in:
Joshua Warner 2022-10-31 19:57:20 -07:00
parent dc2500aac2
commit 5d5b71a083
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
12 changed files with 351 additions and 359 deletions

View file

@ -29,6 +29,26 @@ where
)
}
pub fn space0_around_e_no_after_indent_check<'a, P, S, E>(
parser: P,
indent_before_problem: fn(Position) -> E,
) -> impl Parser<'a, Loc<S>, E>
where
S: Spaceable<'a>,
S: 'a,
P: Parser<'a, Loc<S>, E>,
P: 'a,
E: 'a + SpaceProblem,
{
parser::map_with_arena(
and(
space0_e(indent_before_problem),
and(parser, space0_no_after_indent_check()),
),
spaces_around_help,
)
}
pub fn space0_before_optional_after<'a, P, S, E>(
parser: P,
indent_before_problem: fn(Position) -> E,
@ -200,6 +220,38 @@ where
}
}
#[inline(always)]
fn space0_no_after_indent_check<'a, E>() -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
where
E: 'a + SpaceProblem,
{
move |arena, state: State<'a>, _min_indent: u32| match fast_eat_spaces(&state) {
FastSpaceState::HasTab(position) => Err((
MadeProgress,
E::space_problem(BadInputError::HasTab, position),
state,
)),
FastSpaceState::Good {
newlines,
consumed,
column: _,
} => {
if consumed == 0 {
Ok((NoProgress, &[] as &[_], state))
} else {
let comments_and_newlines = Vec::with_capacity_in(newlines, arena);
let spaces = eat_spaces(state, comments_and_newlines);
Ok((
MadeProgress,
spaces.comments_and_newlines.into_bump_slice(),
spaces.state,
))
}
}
}
}
enum FastSpaceState {
Good {
newlines: usize,