Parenthesize closures in unary ops and pre-emptively disallow multi-backpassing in such closures, to avoid formatter trouble later

This commit is contained in:
Joshua Warner 2025-01-01 12:22:09 -05:00
parent 14b18f4213
commit 37e7caa1aa
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
12 changed files with 77 additions and 53 deletions

View file

@ -80,6 +80,15 @@ pub struct ExprParseOptions {
pub check_for_arrow: bool,
}
impl ExprParseOptions {
pub fn disallow_multi_backpassing(&self) -> Self {
Self {
accept_multi_backpassing: false,
check_for_arrow: self.check_for_arrow,
}
}
}
pub fn expr_help<'a>() -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
move |arena, state: State<'a>, min_indent: u32| {
loc_expr(true, false)
@ -308,7 +317,11 @@ fn loc_possibly_negative_or_negated_term<'a>(
let (_, (loc_op, loc_expr), state) = and(
loc(unary_negate()),
loc_possibly_negative_or_negated_term(options, true, false),
loc_possibly_negative_or_negated_term(
options.disallow_multi_backpassing(),
true,
false,
),
)
.parse(arena, state, min_indent)?;
@ -325,7 +338,11 @@ fn loc_possibly_negative_or_negated_term<'a>(
and(
loc(unary_not()).trace("not"),
space0_before_e(
loc_possibly_negative_or_negated_term(options, true, false),
loc_possibly_negative_or_negated_term(
options.disallow_multi_backpassing(),
true,
false
),
EExpr::IndentStart
)
.trace("not_expr")
@ -645,7 +662,11 @@ fn parse_stmt_operator_chain<'a>(
let allow_negate = state.pos() > end;
let parser = skip_first(
crate::blankspace::check_indent(EExpr::IndentEnd),
loc_possibly_negative_or_negated_term(options, allow_negate, false),
loc_possibly_negative_or_negated_term(
options.disallow_multi_backpassing(),
allow_negate,
false,
),
);
end = state.pos();
match parser.parse(arena, state.clone(), call_min_indent) {
@ -1495,7 +1516,7 @@ fn parse_stmt_operator<'a>(
min_indent,
call_min_indent,
expr_state,
options,
options.disallow_multi_backpassing(),
initial_state,
loc_op.with_value(BinOp::Minus),
)