Skip to content
Draft
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
96 changes: 47 additions & 49 deletions grammar.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check

const REDIR_OPERATOR_RE = /(e(rr)?|o(ut)?)(\+(e(rr)?|o(ut)?))?>>?/;

module.exports = grammar({
name: 'nu',

word: ($) => $.identifier,

extras: ($) => [/[ \t]/, $.comment],
extras: ($) => [/[ \t\r]/, $.comment],

supertypes: $ => [
$.expression
],

inline: ($) => [
$._flag_value,
Expand Down Expand Up @@ -61,12 +68,30 @@

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

shebang: ($) => seq(optional($._repeat_newline), '#!', /.*\r?\n?/),
shebang: ($) => seq(optional($._repeat_newline), '#!', /.*\n?/),

...block_body_rules(),

...parenthesized_body_rules(),

/// pipeline
pipeline: ($) =>
seq(
repeat(seq($.pipe_element, $._pipe_separator, optional($._newline))),
$.pipe_element,
),
pipeline_parenthesized: ($) =>
seq(
repeat(
seq(
alias($.pipe_element_parenthesized, $.pipe_element),
$._pipe_separator,
optional($._repeat_newline),
),
),
alias($.pipe_element_parenthesized, $.pipe_element),
),

_block_body: ($) =>
general_body_rules(
'',
Expand Down Expand Up @@ -134,13 +159,12 @@

// remove newline characters from extras to reduce ambiguity
// manually controlled by adding the following to parenthesized rules
_newline: (_$) => /\r?\n/,
_newline: (_$) => /\n/,
_repeat_newline: ($) => repeat1($._newline),
_space: (_$) => /[ \t]+/,
_separator: ($) => choice($._space, $._newline),
_terminator: ($) => choice(';', $._newline),
_pipe_separator: ($) =>
repeat1(seq(optional($._repeat_newline), choice('|', ...redir_pipe()))),
_pipe_separator: ($) => seq(optional(token.immediate(REDIR_OPERATOR_RE)), '|'),

/// Attributes
attribute_list: ($) => repeat1(seq($.attribute, choice(';', $._newline))),
Expand All @@ -150,7 +174,7 @@
seq(
'@',
field('type', $.attribute_identifier),
repeat(seq($._space, optional($._cmd_arg))),
repeat($._cmd_arg),
),

/// Top Level Items
Expand Down Expand Up @@ -386,7 +410,7 @@
keyword().for,
field('looping_var', $._variable_name),
keyword().in,
field('iterable', $._expression),
field('iterable', $.expression),
field('body', $.block),
),

Expand All @@ -395,7 +419,7 @@
ctrl_while: ($) =>
seq(
keyword().while,
field('condition', $._expression),
field('condition', $.expression),
field('body', $.block),
),

Expand Down Expand Up @@ -452,7 +476,7 @@
_match_pattern: ($) =>
choice($._match_pattern_expression, alias($.unquoted, $.val_string)),

match_guard: ($) => seq(keyword().if, $._expression),
match_guard: ($) => seq(keyword().if, $.expression),

_match_pattern_expression: ($) =>
choice($._match_pattern_value, $.val_range, $.expr_parenthesized),
Expand Down Expand Up @@ -527,16 +551,18 @@
/// Pipelines

pipe_element: ($) =>
prec(10,
choice(
seq(
_env_variable_rule(false, $),
$._expression,
$.expression,
optional($.redirection),
),
seq(_env_variable_rule(false, $), $.command),
$._ctrl_expression,
$.where_command,
),
),

pipe_element_parenthesized: ($) =>
choice(
Expand Down Expand Up @@ -644,13 +670,14 @@

/// Expressions

_expression: ($) =>
expression: ($) =>
choice(
$._value,
$.expr_binary,
$.expr_unary,
$.val_range,
$.expr_parenthesized,
$.command,
),

_expression_parenthesized: ($) =>
Expand Down Expand Up @@ -1188,7 +1215,11 @@

/// Commands

command: _command_rule(false),
command: ($) => prec.left(10, seq(
field('head', seq(optional('^'), choice($.cmd_identifier, $._stringish))),
repeat($._cmd_arg)),
),

_command_parenthesized: _command_rule(true),

_cmd_arg: ($) =>
Expand All @@ -1206,10 +1237,11 @@

flag_value: ($) => choice($._value, $.val_string),

_redir_operator: _ => REDIR_OPERATOR_RE,

redirection: ($) =>
seq(
choice(...redir_append()),
$._space,
$._redir_operator,
field(
'file_path',
choice(alias($._unquoted_naive, $.val_string), $._stringish),
Expand Down Expand Up @@ -1353,17 +1385,6 @@

/// pipeline

pipeline_parenthesized: ($) =>
seq(
repeat(
seq(
alias($.pipe_element_parenthesized, $.pipe_element),
$._pipe_separator,
optional($._repeat_newline),
),
),
alias($.pipe_element_parenthesized, $.pipe_element),
),
};
}

Expand All @@ -1374,13 +1395,6 @@
return {
..._block_body_rules(''),

/// pipeline

pipeline: ($) =>
seq(
repeat(seq($.pipe_element, $._pipe_separator, optional($._newline))),
$.pipe_element,
),
};
}

Expand Down Expand Up @@ -1595,7 +1609,7 @@
*/
function _ctrl_if_rule(parenthesized) {
return (/** @type {any} */ $) => {
const _expr = parenthesized ? $._expression_parenthesized : $._expression;
const _expr = parenthesized ? $._expression_parenthesized : $.expression;
const seq_else_array = [
keyword().else,
choice(
Expand Down Expand Up @@ -1965,26 +1979,10 @@
/**
*
*/
function redir() {

Check failure on line 1982 in grammar.js

View workflow job for this annotation

GitHub Actions / lint

'redir' is defined but never used
return ['err>', 'out>', 'e>', 'o>', 'err+out>', 'out+err>', 'o+e>', 'e+o>'];
}

/**
*
*/
function redir_append() {
const rewrite = redir();
const append = rewrite.map((x) => x + '>');
return rewrite.concat(append);
}

/**
*
*/
function redir_pipe() {
return redir().map((x) => x + '|');
}

// delimiters
/**
*
Expand Down
Loading
Loading