Skip to content

Commit

Permalink
Fix and/or parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawarner32 committed Jan 21, 2025
1 parent ef4df61 commit 3c158f6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
35 changes: 18 additions & 17 deletions crates/compiler/parse/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3991,9 +3991,10 @@ enum OperatorOrDef {
}

fn bin_op<'a>(check_for_defs: bool) -> impl Parser<'a, BinOp, EExpr<'a>> {
(move |_, state: State<'a>, min_indent| {
(move |arena: &'a Bump, state: State<'a>, min_indent| {
let start = state.pos();
let (_, op, state) = operator_help(EExpr::Start, EExpr::BadOperator, state, min_indent)?;
let (_, op, state) =
operator_help(arena, EExpr::Start, EExpr::BadOperator, state, min_indent)?;
let err_progress = if check_for_defs {
MadeProgress
} else {
Expand All @@ -4014,12 +4015,15 @@ fn bin_op<'a>(check_for_defs: bool) -> impl Parser<'a, BinOp, EExpr<'a>> {
}

fn operator<'a>() -> impl Parser<'a, OperatorOrDef, EExpr<'a>> {
(move |_, state, min_indent| operator_help(EExpr::Start, EExpr::BadOperator, state, min_indent))
.trace("operator")
(move |arena: &'a Bump, state, min_indent| {
operator_help(arena, EExpr::Start, EExpr::BadOperator, state, min_indent)
})
.trace("operator")
}

#[inline(always)]
fn operator_help<'a, F, G, E>(
arena: &'a Bump,
to_expectation: F,
to_error: G,
mut state: State<'a>,
Expand All @@ -4030,20 +4034,17 @@ where
G: Fn(&'a str, Position) -> E,
E: 'a,
{
match *state.bytes() {
[b'o', b'r', ..] => {
return Ok((
MadeProgress,
OperatorOrDef::BinOp(BinOp::Or),
state.advance(2),
))
let and_or = either(
parser::keyword(keyword::AND, EExpr::End),
parser::keyword(keyword::OR, EExpr::End),
);

match and_or.parse(arena, state.clone(), min_indent) {
Ok((MadeProgress, Either::First(_), state)) => {
return Ok((MadeProgress, OperatorOrDef::BinOp(BinOp::And), state))
}
[b'a', b'n', b'd', ..] => {
return Ok((
MadeProgress,
OperatorOrDef::BinOp(BinOp::And),
state.advance(3),
))
Ok((MadeProgress, Either::Second(_), state)) => {
return Ok((MadeProgress, OperatorOrDef::BinOp(BinOp::Or), state))
}
_ => {}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a
ands
d
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@0-11 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
EitherIndex(2147483649),
],
regions: [
@0-1,
@2-9,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 1 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
Slice<roc_parse::ast::CommentOrNewline> { start: 1, length: 0 },
],
spaces: [
Newline,
],
type_defs: [],
value_defs: [
Stmt(
@0-1 Var {
module_name: "",
ident: "a",
},
),
Body(
@2-4 RecordDestructure(
[],
),
@5-9 Var {
module_name: "",
ident: "ands",
},
),
],
},
@10-11 SpaceBefore(
Var {
module_name: "",
ident: "d",
},
[
Newline,
],
),
),
[
Newline,
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a
{}=ands
d
1 change: 1 addition & 0 deletions crates/compiler/test_syntax/tests/test_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ mod test_snapshots {
pass/single_question_binop_closure.expr,
pass/single_question_binop_tag.expr,
pass/single_underscore_closure.expr,
pass/sneaky_and_expr.expr,
pass/sneaky_implements_in_opaque_fn_type.expr,
pass/space_after_opt_field_pat.expr,
pass/space_before_colon.full,
Expand Down

0 comments on commit 3c158f6

Please sign in to comment.