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

Expansions #196

Merged
merged 5 commits into from
Aug 24, 2023
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
230 changes: 168 additions & 62 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = grammar({
[$.compound_statement],
[$.redirected_statement, $.command],
[$.redirected_statement, $.command_substitution],
[$._expansion_body],
],

inline: $ => [
Expand All @@ -45,6 +44,7 @@ module.exports = grammar({
$._special_variable_name,
$._c_word,
$._statement_not_subshell,
$._expansion_syms,
],

externals: $ => [
Expand All @@ -63,6 +63,10 @@ module.exports = grammar({
$.extglob_pattern,
$._bare_dollar,
$._brace_start,
$._immediate_double_hash,
$._external_expansion_sym_hash,
$._external_expansion_sym_bang,
$._external_expansion_sym_equal,
'}',
']',
'<<',
Expand Down Expand Up @@ -199,24 +203,24 @@ module.exports = grammar({
$._c_expression,
),
_c_unary_expression: $ => prec.left(seq(
choice('++', '--'),
field('operator', choice('++', '--')),
$._c_expression_not_assignment,
)),
_c_binary_expression: $ => prec.right(seq(
$._c_expression_not_assignment,
choice(
field('operator', choice(
'+=', '-=', '*=', '/=', '%=', '**=',
'<<=', '>>=', '&=', '^=', '|=',
'==', '!=', '<=', '>=', '&&', '||',
'<<', '>>',
'+', '-', '*', '/', '%', '**',
'<', '>',
),
)),
$._c_expression_not_assignment,
)),
_c_postfix_expression: $ => seq(
$._c_expression_not_assignment,
choice('++', '--'),
field('operator', choice('++', '--')),
),
_c_parenthesized_expression: $ => seq(
'(',
Expand Down Expand Up @@ -521,18 +525,18 @@ module.exports = grammar({

unary_expression: $ => choice(
prec(1, seq(
token(prec(1, choice('-', '+', '~', '++', '--'))),
field('operator', tokenLiterals(1, '-', '+', '~', '++', '--')),
$._expression,
)),
prec.right(1, seq(
choice('!', $.test_operator),
field('operator', choice('!', $.test_operator)),
$._expression,
)),
),

postfix_expression: $ => seq(
$._expression,
choice('++', '--'),
field('operator', choice('++', '--')),
),

parenthesized_expression: $ => seq(
Expand Down Expand Up @@ -586,7 +590,6 @@ module.exports = grammar({

_arithmetic_literal: $ => prec(1, choice(
$.number,
$.test_operator,
$.subscript,
$.simple_expansion,
$.expansion,
Expand All @@ -607,7 +610,6 @@ module.exports = grammar({
'<<', '>>', '<<=', '>>=',
'&', '|', '^',
'&=', '|=', '^=',
$.test_operator,
)),
field('right', $._arithmetic_expression),
),
Expand All @@ -625,18 +627,18 @@ module.exports = grammar({

_arithmetic_unary_expression: $ => choice(
prec(3, seq(
token(prec(1, choice('-', '+', '~', '++', '--'))),
field('operator', tokenLiterals(1, '-', '+', '~', '++', '--')),
$._arithmetic_expression,
)),
prec.right(3, seq(
'!',
field('operator', '!'),
$._arithmetic_expression,
)),
),

_arithmetic_postfix_expression: $ => seq(
$._arithmetic_expression,
choice('++', '--'),
field('operator', choice('++', '--')),
),

_arithmetic_parenthesized_expression: $ => seq(
Expand Down Expand Up @@ -715,75 +717,153 @@ module.exports = grammar({

expansion: $ => seq(
'${',
repeat(choice('#', '!', '=')),
optional($._expansion_body),
'}',
),
_expansion_body: $ => choice(
// ${!##} ${!#}
repeat1(field(
'operator',
choice(
alias($._external_expansion_sym_hash, '#'),
alias($._external_expansion_sym_bang, '!'),
alias($._external_expansion_sym_equal, '='),
),
)),
seq(
choice($.variable_name, $._special_variable_name),
optional(field('operator', token.immediate('!'))),
choice($.variable_name, $._simple_variable_name, $._special_variable_name, $.subscript),
choice(
seq(
field('operator', choice('=', ':=', '-', ':-', '+', ':+', '?', ':?')),
repeat(choice($._literal, $.array)),
),
seq(
field('operator', choice('#', '##', '%', '%%')),
choice($.regex, alias(')', $.regex), $.string, $.raw_string),
),
seq(
choice('/', '//', '/#', '/%'),
alias($._regex_no_slash, $.regex),
// This can be elided
optional(seq(
'/',
optional(seq(
$._literal,
optional('/'),
)),
)),
),
seq(
choice(',', ',,', '^', '^^'),
$.regex,
),
seq(
':',
choice($._simple_variable_name, $.number, $.arithmetic_expansion, $.expansion, '\n'),
optional(seq(
':',
optional(choice($._simple_variable_name, $.number, $.arithmetic_expansion, '\n')),
)),
),
seq(
'@',
field('operator', choice('U', 'u', 'L', 'Q', 'E', 'P', 'A', 'K', 'a', 'k')),
),
$._expansion_expression,
$._expansion_regex,
$._expansion_regex_replacement,
$._expansion_regex_removal,
$._expansion_max_length,
$._expansion_operator,
),
),
seq(
field('operator', token.immediate('!')),
choice($._simple_variable_name, $.variable_name),
optional(field('operator', choice(
token.immediate('@'),
token.immediate('*'),
))),
),
seq(
optional(field('operator', immediateLiterals('#', '!', '='))),
choice(
$.subscript,
$._simple_variable_name,
$._special_variable_name,
$.command_substitution,
),
optional(seq(
repeat(field(
'operator',
choice(
alias(token(prec(1, ',')), ','),
alias(token(prec(1, ',,')), ',,'),
alias(token(prec(1, '^')), '^'),
alias(token(prec(1, '^^')), '^^'),
alias($._external_expansion_sym_hash, '#'),
alias($._external_expansion_sym_bang, '!'),
alias($._external_expansion_sym_equal, '='),
),
optional($.regex),
)),
repeat(prec.right(choice(
$._literal, $.array,
':', ':?', '=', ':-', '%', '-', '#', ';', '|', '(', ')', '<', '>',
))),
),
),

_expansion_syms: _ => repeat1(field('operator',
immediateLiterals('#', '!', '='),
)),

_expansion_expression: $ => prec(1, seq(
field('operator', immediateLiterals('=', ':=', '-', ':-', '+', ':+', '?', ':?')),
optional(seq(
choice(
alias($._concatenation_in_expansion, $.concatenation),
// $._simple_variable_name,
$.command_substitution,
$.word,
$.expansion,
$.simple_expansion,
$.array,
$.string,
$.raw_string,
$.ansi_c_string,
alias(/[\s]+[\w]*/, $.word),
),
)),
)),

_expansion_regex: $ => seq(
field('operator', choice('#', alias($._immediate_double_hash, '##'), '%', '%%')),
choice($.regex, alias(')', $.regex), $.string, $.raw_string, alias(/\s+/, $.regex)),
),

_expansion_regex_replacement: $ => seq(
field('operator', choice('/', '//', '/#', '/%')),
alias($._regex_no_slash, $.regex),
// This can be elided
optional(seq(
field('operator', '/'),
optional(seq(
$._literal,
field('operator', optional('/')),
)),
)),
),

_expansion_regex_removal: $ => seq(
field('operator', choice(',', ',,', '^', '^^')),
optional($.regex),
),

_expansion_max_length: $ => seq(
field('operator', ':'),
choice(
$._simple_variable_name,
$.number,
$.arithmetic_expansion,
$.expansion,
$.parenthesized_expression,
'\n',
),
optional(seq(
field('operator', ':'),
optional(choice(
$._simple_variable_name,
$.number,
$.arithmetic_expansion,
'\n',
)),
)),
),

_expansion_operator: $ => seq(
field('operator', token.immediate('@')),
field('operator', immediateLiterals('U', 'u', 'L', 'Q', 'E', 'P', 'A', 'K', 'a', 'k')),
),

_concatenation_in_expansion: $ => prec(-2, seq(
choice(
$.word,
$.variable_name,
$.simple_expansion,
$.expansion,
$.string,
$.raw_string,
),
repeat1(seq(
choice($._concat, alias(/`\s*`/, '``')),
choice(
$.word,
$.variable_name,
$.simple_expansion,
$.expansion,
$.string,
$.raw_string,
alias($._comment_word, $.word),
),
)),
)),

command_substitution: $ => choice(
seq('$(', $._statements, ')'),
seq('$(', $.file_redirect, ')'),
Expand Down Expand Up @@ -816,7 +896,7 @@ module.exports = grammar({
$.variable_name,
),

_special_variable_name: $ => alias(choice('*', '@', '?', '-', '$', '0', '_'), $.special_variable_name),
_special_variable_name: $ => alias(choice('*', '@', '?', '!', '#', '-', '$', '0', '_'), $.special_variable_name),

word: _ => token(seq(
choice(
Expand Down Expand Up @@ -874,3 +954,29 @@ function commaSep(rule) {
function commaSep1(rule) {
return seq(rule, repeat(seq(',', rule)));
}

/**
*
* Turns a list of rules into a choice of immediate rule
*
* @param {(RegExp|String)[]} literals
*
* @return {ChoiceRule}
*/
function immediateLiterals(...literals) {
return choice(...literals.map(l => token.immediate(l)));
}

/**
*
* Turns a list of rules into a choice of aliased token rules
*
* @param {number} precedence
*
* @param {(RegExp|String)[]} literals
*
* @return {ChoiceRule}
*/
function tokenLiterals(precedence, ...literals) {
return choice(...literals.map(l => token(prec(precedence, l))));
}
Loading