Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of if/else and try/catch in parens #61

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions corpus/expr/subexpr.nu
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,110 @@ subexpr-009-closure
(identifier))
(val_variable
(identifier)))))))))))))

=====
subexpr-010-newline-before-else
=====

(
if $cond { echo 'foo' }
else { echo 'bar' }
)

-----

(nu_script
(pipeline
(pipe_element
(expr_parenthesized
(pipeline
(pipe_element
(ctrl_if
(val_variable
(identifier))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string)))))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string))))))))))))

=====
subexpr-011-newline-before-else-if
=====

(
if $cond { echo 'foo' }
else if $cond2 { echo 'bar' }
else { echo 'foo bar' }
)

-----

(nu_script
(pipeline
(pipe_element
(expr_parenthesized
(pipeline
(pipe_element
(ctrl_if
(val_variable
(identifier))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string)))))
(ctrl_if
(val_variable
(identifier))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string)))))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string)))))))))))))

=====
subexpr-012-newline-before-catch
=====

(
try { echo 'foo' }
catch { echo 'bar' }
)

-----

(nu_script
(pipeline
(pipe_element
(expr_parenthesized
(pipeline
(pipe_element
(ctrl_try
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string)))))
(block
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_string))))))))))))
47 changes: 45 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = grammar({
[$.pipe_element_parenthesized, $.pipe_element_parenthesized_last],
[$.command],
[$.block, $.val_record],
[$.ctrl_if_parenthesized],
[$.ctrl_try_parenthesized],
],

rules: {
Expand Down Expand Up @@ -253,6 +255,17 @@ module.exports = grammar({
$.ctrl_return,
),

_ctrl_expression_parenthesized: ($) =>
choice(
field("ctrl_break", KEYWORD().break),
field("ctrl_continue", KEYWORD().continue),
$.ctrl_do,
alias($.ctrl_if_parenthesized, $.ctrl_if),
alias($.ctrl_try_parenthesized, $.ctrl_try),
$.ctrl_match,
$.ctrl_return,
),

// Standalone Controls

ctrl_for: ($) =>
Expand Down Expand Up @@ -327,6 +340,23 @@ module.exports = grammar({
),
),

ctrl_if_parenthesized: ($) =>
seq(
KEYWORD().if,
field("condition", choice($._expression, $.identifier)),
field("then_branch", $.block),
optional(
seq(
optional("\n"),
KEYWORD().else,
choice(
field("else_block", $.block),
field("else_branch", alias($.ctrl_if_parenthesized, $.ctrl_if)),
),
),
),
),

ctrl_match: ($) =>
seq(
KEYWORD().match,
Expand Down Expand Up @@ -388,6 +418,19 @@ module.exports = grammar({
optional(seq(KEYWORD().catch, field("catch_branch", $._blosure))),
),

ctrl_try_parenthesized: ($) =>
seq(
KEYWORD().try,
field("try_branch", $.block),
optional(
seq(
optional("\n"),
KEYWORD().catch,
field("catch_branch", $._blosure),
),
),
),

ctrl_return: ($) =>
choice(
prec(
Expand Down Expand Up @@ -425,7 +468,7 @@ module.exports = grammar({
seq(
choice(
prec.right(69, $._expression),
$._ctrl_expression,
$._ctrl_expression_parenthesized,
$.where_command,
alias($._command_parenthesized_body, $.command),
),
Expand All @@ -440,7 +483,7 @@ module.exports = grammar({
pipe_element_parenthesized_last: ($) =>
choice(
$._expression,
$._ctrl_expression,
$._ctrl_expression_parenthesized,
$.where_command,
alias($._command_parenthesized_body, $.command),
),
Expand Down
Loading