Refactor Parser trait to pass min_indent

This removes the need to explicitly pass thru min_indent when using the parser combinators.

My ultimate goal here is to evolve the current parser closer toward a purely combinator-based parser,
at which point we can more easily transition smoothly to a formal(ish) grammar, or expand the meanings of combinators
to include things like:
* Incremental (re)parsing
* Unified parsing and formatting code
* Better error recovery
* Using the main parser directly for syntax highlighting
This commit is contained in:
Joshua Warner 2022-10-25 21:03:28 -07:00
parent ae1a9e4dd6
commit 07be8ec000
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
18 changed files with 704 additions and 844 deletions

View file

@ -10,7 +10,6 @@ use roc_region::all::Position;
pub fn space0_around_ee<'a, P, S, E>(
parser: P,
min_indent: u32,
indent_before_problem: fn(Position) -> E,
indent_after_problem: fn(Position) -> E,
) -> impl Parser<'a, Loc<S>, E>
@ -23,8 +22,8 @@ where
{
parser::map_with_arena(
and(
space0_e(min_indent, indent_before_problem),
and(parser, space0_e(min_indent, indent_after_problem)),
space0_e(indent_before_problem),
and(parser, space0_e(indent_after_problem)),
),
spaces_around_help,
)
@ -32,7 +31,6 @@ where
pub fn space0_before_optional_after<'a, P, S, E>(
parser: P,
min_indent: u32,
indent_before_problem: fn(Position) -> E,
indent_after_problem: fn(Position) -> E,
) -> impl Parser<'a, Loc<S>, E>
@ -45,11 +43,11 @@ where
{
parser::map_with_arena(
and(
space0_e(min_indent, indent_before_problem),
space0_e(indent_before_problem),
and(
parser,
one_of![
backtrackable(space0_e(min_indent, indent_after_problem)),
backtrackable(space0_e(indent_after_problem)),
succeed!(&[] as &[_]),
],
),
@ -96,7 +94,6 @@ where
pub fn space0_before_e<'a, P, S, E>(
parser: P,
min_indent: u32,
indent_problem: fn(Position) -> E,
) -> impl Parser<'a, Loc<S>, E>
where
@ -107,7 +104,7 @@ where
E: 'a + SpaceProblem,
{
parser::map_with_arena(
and!(space0_e(min_indent, indent_problem), parser),
and!(space0_e(indent_problem), parser),
|arena: &'a Bump, (space_list, loc_expr): (&'a [CommentOrNewline<'a>], Loc<S>)| {
if space_list.is_empty() {
loc_expr
@ -122,7 +119,6 @@ where
pub fn space0_after_e<'a, P, S, E>(
parser: P,
min_indent: u32,
indent_problem: fn(Position) -> E,
) -> impl Parser<'a, Loc<S>, E>
where
@ -133,7 +129,7 @@ where
E: 'a + SpaceProblem,
{
parser::map_with_arena(
and!(parser, space0_e(min_indent, indent_problem)),
and!(parser, space0_e(indent_problem)),
|arena: &'a Bump, (loc_expr, space_list): (Loc<S>, &'a [CommentOrNewline<'a>])| {
if space_list.is_empty() {
loc_expr
@ -146,14 +142,11 @@ where
)
}
pub fn check_indent<'a, E>(
min_indent: u32,
indent_problem: fn(Position) -> E,
) -> impl Parser<'a, (), E>
pub fn check_indent<'a, E>(indent_problem: fn(Position) -> E) -> impl Parser<'a, (), E>
where
E: 'a,
{
move |_, state: State<'a>| {
move |_, state: State<'a>, min_indent: u32| {
if state.column() >= min_indent {
Ok((NoProgress, (), state))
} else {
@ -163,24 +156,22 @@ where
}
pub fn space0_e<'a, E>(
min_indent: u32,
indent_problem: fn(Position) -> E,
) -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
where
E: 'a + SpaceProblem,
{
spaces_help_help(min_indent, indent_problem)
spaces_help_help(indent_problem)
}
#[inline(always)]
fn spaces_help_help<'a, E>(
min_indent: u32,
indent_problem: fn(Position) -> E,
) -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
where
E: 'a + SpaceProblem,
{
move |arena, state: State<'a>| match fast_eat_spaces(&state) {
move |arena, state: State<'a>, min_indent: u32| match fast_eat_spaces(&state) {
FastSpaceState::HasTab(position) => Err((
MadeProgress,
E::space_problem(BadInputError::HasTab, position),