Skip to content

Commit

Permalink
fix: correctly consume else branch in if-statement
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
Nsidorenco committed Apr 4, 2024
1 parent fde8017 commit 33b88d9
Show file tree
Hide file tree
Showing 5 changed files with 222,135 additions and 257,032 deletions.
2 changes: 1 addition & 1 deletion examples/expressions.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
do
if b = b then
if b then
1
else
2
70 changes: 43 additions & 27 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ module.exports = grammar({

conflicts: $ => [
[$.long_identifier, $._identifier_or_op],
[$.type_argument, $.static_type_argument]
[$.type_argument, $.static_type_argument],
[$.sequential_expression, $._if_then_expression, $._if_then_else_expression]

Check failure on line 71 in grammar.js

View workflow job for this annotation

GitHub Actions / Test parser (ubuntu-latest)

Missing trailing comma
],

word: $ => $.identifier,
Expand Down Expand Up @@ -404,29 +405,6 @@ module.exports = grammar({
// (static-typars : (member-sig) expr)
),

application_expression: $ =>
prec.left(PREC.APP_EXPR,
seq(
$._expression,
$._expression
)
),

sequential_expression: $ =>
prec.right(PREC.SEQ_EXPR,
seq(
$._expression,
repeat1(
prec.right(PREC.SEQ_EXPR,
seq(
choice($._newline, ';'),
$._expression
)
)
)
)
),

call_expression: $ =>
prec.left(PREC.PAREN_APP + 100,
seq(
Expand Down Expand Up @@ -575,6 +553,7 @@ module.exports = grammar({
_else_expression: $ =>
prec(PREC.ELSE_EXPR,
seq(
optional($._newline),
"else",
$._indent,
field("else_branch", $._expression),
Expand All @@ -584,23 +563,36 @@ module.exports = grammar({
elif_expression: $ =>
prec(PREC.ELSE_EXPR,
seq(
optional($._newline),
"elif",
field("guard", $._expression),
optional($._newline),
"then",
field("then", $._expression),
)),

if_expression: $ =>
prec.left(PREC.IF_EXPR,
_if_then_else_expression: $ =>
prec.right(PREC.IF_EXPR + 1,
seq(
"if",
field("guard", $._expression),
"then",
field("then", $._expression),
repeat($.elif_expression),
optional($._else_expression),
$._else_expression,
)),

_if_then_expression: $ =>
prec.right(PREC.IF_EXPR,
seq(
"if",
field("guard", $._expression),
"then",
field("then", $._expression),
)),

if_expression: $ => choice($._if_then_expression, $._if_then_else_expression),

fun_expression: $ =>
prec.right(PREC.FUN_EXPR,
seq(
Expand Down Expand Up @@ -789,6 +781,30 @@ module.exports = grammar({
repeat(seq("|", $.rule)),
)),

application_expression: $ =>
prec.left(PREC.APP_EXPR,
seq(
$._expression,
$._expression
)
),

sequential_expression: $ =>
prec(PREC.SEQ_EXPR,
seq(
$._expression,
repeat1(
prec.right(PREC.SEQ_EXPR,
seq(
choice($._newline, ';'),
$._expression
)
)
)
)
),


//
// Expressions (END)
//
Expand Down
Loading

0 comments on commit 33b88d9

Please sign in to comment.