Skip to content

Commit

Permalink
Fixed some minor errors (#137)
Browse files Browse the repository at this point in the history
This PR fixed following issues in current grammar:
1. hide-env with flags

<img width="555" alt="image"
src="https://github.com/user-attachments/assets/f781c461-e753-4f32-9465-fe0c81a1817a">

2. do + closure + more than one arguments / parenthesized multiline do
statement

<img width="599" alt="image"
src="https://github.com/user-attachments/assets/4b9be6c1-8bf2-42b7-b512-89dff78099d1">

3. wrong result for multiline `expr_interpolated` (here `bar` should be
argument instead of the command head of another pipeline)

<img width="599" alt="image"
src="https://github.com/user-attachments/assets/f16ddabe-ff87-4410-b728-c5081daf00bf">

4. hide those `"\n"` terminal tokens, which makes it easier for query,
also more windows friendly.
  • Loading branch information
blindFS authored Nov 9, 2024
1 parent caf8baf commit 7e0f16f
Show file tree
Hide file tree
Showing 7 changed files with 317,765 additions and 409,311 deletions.
72 changes: 43 additions & 29 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = grammar({

nu_script: ($) => seq(optional($.shebang), optional($._block_body)),

shebang: (_$) => seq("#!", /.*\n/),
shebang: (_$) => seq("#!", /.*\r?\n/),

...block_body_rules("", (/** @type {any} */ $) => $._terminator),
...block_body_rules("_last", (/** @type {any} */ $) =>
Expand Down Expand Up @@ -88,7 +88,7 @@ module.exports = grammar({
field("dollar_name", $.val_variable),
),

_terminator: (_$) => choice(PUNC().semicolon, "\n"),
_terminator: (_$) => choice(PUNC().semicolon, /\r?\n/),

/// Top Level Items

Expand Down Expand Up @@ -293,7 +293,7 @@ module.exports = grammar({
choice(
field("ctrl_break", KEYWORD().break),
field("ctrl_continue", KEYWORD().continue),
$.ctrl_do,
alias($.ctrl_do_parenthesized, $.ctrl_do),
alias($.ctrl_if_parenthesized, $.ctrl_if),
alias($.ctrl_try_parenthesized, $.ctrl_try),
$.ctrl_match,
Expand Down Expand Up @@ -334,27 +334,39 @@ module.exports = grammar({
),

// Nestable Controls
_do_expression: ($) =>
choice(
$._list_item_expression,
$._flag,
alias($.unquoted, $.val_string),
alias($._unquoted_with_expr, $.val_string),
),

ctrl_do: ($) =>
prec.left(
-1,
seq(
// `do` has 4 flags, is there a better way to do this?
KEYWORD().do,
optional($._flag),
optional($._flag),
optional($._flag),
optional($._flag),
repeat($._flag),
choice($._blosure, $.val_variable),
optional($._flag),
optional($._flag),
optional($._flag),
optional($._flag),
optional($._expression),
optional($._flag),
optional($._flag),
optional($._flag),
optional($._flag),
// optional to allow comment at the end
repeat(seq(token.immediate(/[ \t]+/), optional($._do_expression))),
),
),

ctrl_do_parenthesized: ($) =>
prec.left(
-1,
seq(
KEYWORD().do,
repeat($._flag),
choice($._blosure, $.val_variable),
repeat(
seq(
choice(/\r?\n/, token.immediate(/[ \t]+/)),
optional($._do_expression),
),
),
),
),

Expand Down Expand Up @@ -382,7 +394,7 @@ module.exports = grammar({
field("then_branch", $.block),
optional(
seq(
optional("\n"),
optional(/\r?\n/),
KEYWORD().else,
choice(
field("else_block", choice($.block, $._expression, $.command)),
Expand Down Expand Up @@ -518,7 +530,7 @@ module.exports = grammar({
field("try_branch", $.block),
optional(
seq(
optional("\n"),
optional(/\r?\n/),
KEYWORD().catch,
field("catch_branch", $._blosure),
),
Expand Down Expand Up @@ -555,8 +567,8 @@ module.exports = grammar({
$.command,
),
// Allow for empty pipeline elements like `ls | | print`
repeat1(seq(optional("\n"), PUNC().pipe)),
optional("\n"),
repeat1(seq(optional(/\r?\n/), PUNC().pipe)),
optional(/\r?\n/),
),

pipe_element_parenthesized: ($) =>
Expand All @@ -568,8 +580,8 @@ module.exports = grammar({
alias($._command_parenthesized_body, $.command),
),
// Allow for empty pipeline elements like `ls | | print`
repeat1(seq(optional("\n"), PUNC().pipe)),
optional("\n"),
repeat1(seq(optional(/\r?\n/), PUNC().pipe)),
optional(/\r?\n/),
),

pipe_element_last: ($) =>
Expand Down Expand Up @@ -626,7 +638,11 @@ module.exports = grammar({
),

hide_env: ($) =>
seq(KEYWORD().hide_env, field("variable", $._variable_name)),
seq(
KEYWORD().hide_env,
repeat($._flag),
field("variable", $._variable_name),
),

_stmt_overlay: ($) =>
choice($.overlay_hide, $.overlay_list, $.overlay_new, $.overlay_use),
Expand Down Expand Up @@ -1015,7 +1031,7 @@ module.exports = grammar({
),

expr_interpolated: ($) =>
seq(BRACK().open_paren, $._block_body, BRACK().close_paren),
seq(BRACK().open_paren, $._parenthesized_body, BRACK().close_paren),

/// Collections

Expand Down Expand Up @@ -1059,8 +1075,7 @@ module.exports = grammar({
"_entry_separator",
),

_entry_separator: (_$) =>
token(prec(20, choice(PUNC().comma, /\s/, /[\r\n]/))),
_entry_separator: (_$) => token(prec(20, choice(PUNC().comma, /\s/))),

record_entry: ($) =>
seq(
Expand Down Expand Up @@ -1171,12 +1186,11 @@ module.exports = grammar({
10,
repeat(
seq(
choice(token.immediate("\n"), token.immediate(/[ \t]+/)),
choice(/\r?\n/, token.immediate(/[ \t]+/)),
optional($._cmd_arg),
),
),
),
optional("\n"),
),
),

Expand Down
Loading

0 comments on commit 7e0f16f

Please sign in to comment.