mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
make def helper to optionally parse final expr
This commit is contained in:
parent
1434ab6ed7
commit
53a0f4f9a5
1 changed files with 66 additions and 70 deletions
|
@ -839,7 +839,7 @@ fn parse_defs_end<'a>(
|
||||||
mut def_state: DefState<'a>,
|
mut def_state: DefState<'a>,
|
||||||
arena: &'a Bump,
|
arena: &'a Bump,
|
||||||
state: State<'a>,
|
state: State<'a>,
|
||||||
) -> ParseResult<'a, Expr<'a>, EExpr<'a>> {
|
) -> ParseResult<'a, DefState<'a>, EExpr<'a>> {
|
||||||
let min_indent = start.col;
|
let min_indent = start.col;
|
||||||
let initial = state;
|
let initial = state;
|
||||||
|
|
||||||
|
@ -868,34 +868,9 @@ fn parse_defs_end<'a>(
|
||||||
{
|
{
|
||||||
Err((_, _, _)) => {
|
Err((_, _, _)) => {
|
||||||
// a hacky way to get expression-based error messages. TODO fix this
|
// a hacky way to get expression-based error messages. TODO fix this
|
||||||
let state = initial;
|
Ok((NoProgress, def_state, initial))
|
||||||
|
|
||||||
let parse_final_expr = space0_before_e(
|
|
||||||
move |a, s| parse_expr_start(min_indent, start, a, s),
|
|
||||||
min_indent,
|
|
||||||
EExpr::Space,
|
|
||||||
EExpr::IndentStart,
|
|
||||||
);
|
|
||||||
|
|
||||||
match parse_final_expr.parse(arena, state) {
|
|
||||||
Err((_, fail, state)) => {
|
|
||||||
return Err((
|
|
||||||
MadeProgress,
|
|
||||||
EExpr::DefMissingFinalExpr2(arena.alloc(fail), state.line, state.column),
|
|
||||||
state,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
Ok((_, loc_ret, state)) => {
|
Ok((_, loc_pattern, state)) => match operator().parse(arena, state) {
|
||||||
return Ok((
|
|
||||||
MadeProgress,
|
|
||||||
Expr::Defs(def_state.defs.into_bump_slice(), arena.alloc(loc_ret)),
|
|
||||||
state,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok((_, loc_pattern, state)) => {
|
|
||||||
match operator().parse(arena, state) {
|
|
||||||
Ok((_, BinOp::Assignment, state)) => {
|
Ok((_, BinOp::Assignment, state)) => {
|
||||||
let parse_def_expr = space0_before_e(
|
let parse_def_expr = space0_before_e(
|
||||||
move |a, s| parse_expr_help(min_indent + 1, a, s),
|
move |a, s| parse_expr_help(min_indent + 1, a, s),
|
||||||
|
@ -938,9 +913,23 @@ fn parse_defs_end<'a>(
|
||||||
|
|
||||||
parse_defs_end(start, def_state, arena, state)
|
parse_defs_end(start, def_state, arena, state)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => Ok((MadeProgress, def_state, initial)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_defs_expr<'a>(
|
||||||
|
start: Position,
|
||||||
|
def_state: DefState<'a>,
|
||||||
|
arena: &'a Bump,
|
||||||
|
state: State<'a>,
|
||||||
|
) -> ParseResult<'a, Expr<'a>, EExpr<'a>> {
|
||||||
|
let min_indent = start.col;
|
||||||
|
|
||||||
|
match parse_defs_end(start, def_state, arena, state) {
|
||||||
|
Err(bad) => Err(bad),
|
||||||
|
Ok((_, def_state, state)) => {
|
||||||
// this is no def, because there is no `=` or `:`; parse as an expr
|
// this is no def, because there is no `=` or `:`; parse as an expr
|
||||||
let state = initial;
|
|
||||||
let parse_final_expr = space0_before_e(
|
let parse_final_expr = space0_before_e(
|
||||||
move |a, s| parse_expr_help(min_indent, a, s),
|
move |a, s| parse_expr_help(min_indent, a, s),
|
||||||
min_indent,
|
min_indent,
|
||||||
|
@ -948,8 +937,15 @@ fn parse_defs_end<'a>(
|
||||||
EExpr::IndentEnd,
|
EExpr::IndentEnd,
|
||||||
);
|
);
|
||||||
|
|
||||||
let (_, loc_ret, state) = parse_final_expr.parse(arena, state)?;
|
match parse_final_expr.parse(arena, state) {
|
||||||
|
Err((_, fail, state)) => {
|
||||||
|
return Err((
|
||||||
|
MadeProgress,
|
||||||
|
EExpr::DefMissingFinalExpr2(arena.alloc(fail), state.line, state.column),
|
||||||
|
state,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok((_, loc_ret, state)) => {
|
||||||
return Ok((
|
return Ok((
|
||||||
MadeProgress,
|
MadeProgress,
|
||||||
Expr::Defs(def_state.defs.into_bump_slice(), arena.alloc(loc_ret)),
|
Expr::Defs(def_state.defs.into_bump_slice(), arena.alloc(loc_ret)),
|
||||||
|
@ -1055,7 +1051,7 @@ fn parse_expr_operator<'a>(
|
||||||
spaces_after: &[],
|
spaces_after: &[],
|
||||||
};
|
};
|
||||||
|
|
||||||
parse_defs_end(start, def_state, arena, state)
|
parse_defs_expr(start, def_state, arena, state)
|
||||||
}
|
}
|
||||||
BinOp::Backpassing => {
|
BinOp::Backpassing => {
|
||||||
let expr_region = expr_state.expr.region;
|
let expr_region = expr_state.expr.region;
|
||||||
|
@ -1203,7 +1199,7 @@ fn parse_expr_operator<'a>(
|
||||||
spaces_after: &[],
|
spaces_after: &[],
|
||||||
};
|
};
|
||||||
|
|
||||||
parse_defs_end(start, def_state, arena, state)
|
parse_defs_expr(start, def_state, arena, state)
|
||||||
}
|
}
|
||||||
_ => match loc_possibly_negative_or_negated_term(min_indent).parse(arena, state) {
|
_ => match loc_possibly_negative_or_negated_term(min_indent).parse(arena, state) {
|
||||||
Err((MadeProgress, f, s)) => Err((MadeProgress, f, s)),
|
Err((MadeProgress, f, s)) => Err((MadeProgress, f, s)),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue