Update grammar.js

This commit is contained in:
Maieul BOYER 2024-06-07 19:12:32 +02:00
parent f9a7d5b38b
commit 21c10090b3
No known key found for this signature in database

View file

@ -248,25 +248,20 @@ module.exports = grammar({
)), )),
compound_statement: $ => seq( compound_statement: $ => seq(
'{', '{', $._terminated_statement, token(prec(1, '}'))
$._terminated_statement,
token(prec(1, '}')),
), ),
subshell: $ => seq('(', $._statements, ')'), subshell: $ => seq('(', $._statements, ')'),
pipeline: $ => prec.right(seq( pipeline: $ => prec.right(seq(
$._statement_not_pipeline, $._statement_not_pipeline,
repeat1(seq( repeat1(seq('|', $._statement_not_pipeline)),
choice('|', '|&'),
$._statement_not_pipeline,
)),
)), )),
list: $ => prec.left(-1, seq( list: $ => prec.left(-1, seq(
$._statement, field('cmd', $._statement),
choice('&&', '||'), field('op', choice('&&', '||')),
$._statement, field('cmd', $._statement),
)), )),
// Commands // Commands
@ -387,18 +382,11 @@ module.exports = grammar({
)), )),
), ),
_simple_heredoc_body: $ => seq( _simple_heredoc_body: $ => seq(alias($.simple_heredoc_body, $.heredoc_body), $.heredoc_end),
alias($.simple_heredoc_body, $.heredoc_body),
$.heredoc_end,
),
// Literals // Literals
_literal: $ => choice( _literal: $ => choice($.concatenation, $._primary_expression),
$.concatenation,
$._primary_expression,
// alias(prec(-2, repeat1($._special_character)), $.word),
),
_primary_expression: $ => choice( _primary_expression: $ => choice(
$.word, $.word,
@ -414,16 +402,16 @@ module.exports = grammar({
arithmetic_expansion: $ => seq('$((', optional($._arithmetic_expression), '))'), arithmetic_expansion: $ => seq('$((', optional($._arithmetic_expression), '))'),
_arithmetic_expression: $ => prec(1, choice( _arithmetic_expression: $ => prec(1, choice(
$._arithmetic_literal, $.arithmetic_literal,
alias($._arithmetic_unary_expression, $.unary_expression), $.arithmetic_unary_expression,
alias($._arithmetic_ternary_expression, $.ternary_expression), $.arithmetic_ternary_expression,
alias($._arithmetic_binary_expression, $.binary_expression), $.arithmetic_binary_expression,
alias($._arithmetic_postfix_expression, $.postfix_expression), $.arithmetic_postfix_expression,
alias($._arithmetic_parenthesized_expression, $.parenthesized_expression), $.arithmetic_parenthesized_expression,
$.command_substitution, $.command_substitution,
)), )),
_arithmetic_literal: $ => prec(1, choice( arithmetic_literal: $ => prec(1, choice(
$.number, $.number,
$.simple_expansion, $.simple_expansion,
$.expansion, $.expansion,
@ -432,7 +420,9 @@ module.exports = grammar({
$.string, $.string,
)), )),
_arithmetic_binary_expression: $ => { arithmetic_binary_expression: $ => {
/** @type {[RuleOrLiteral, number][]} */
const table = [ const table = [
[choice('+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '&=', '^=', '|='), PREC.UPDATE], [choice('+=', '-=', '*=', '/=', '%=', '<<=', '>>=', '&=', '^=', '|='), PREC.UPDATE],
['=', PREC.ASSIGN], ['=', PREC.ASSIGN],
@ -448,18 +438,16 @@ module.exports = grammar({
[choice('*', '/', '%'), PREC.MULTIPLY], [choice('*', '/', '%'), PREC.MULTIPLY],
]; ];
return choice(...table.map(([operator, precedence]) => { return choice(...table.map(([operator, precedence]) =>
// @ts-ignore prec.left(precedence, seq(
return prec.left(precedence, seq(
field('left', $._arithmetic_expression), field('left', $._arithmetic_expression),
// @ts-ignore
field('operator', operator), field('operator', operator),
field('right', $._arithmetic_expression), field('right', $._arithmetic_expression),
)); ))
})); ));
}, },
_arithmetic_ternary_expression: $ => prec.left(PREC.TERNARY, seq( arithmetic_ternary_expression: $ => prec.left(PREC.TERNARY, seq(
field('condition', $._arithmetic_expression), field('condition', $._arithmetic_expression),
'?', '?',
field('consequence', $._arithmetic_expression), field('consequence', $._arithmetic_expression),
@ -467,7 +455,7 @@ module.exports = grammar({
field('alternative', $._arithmetic_expression), field('alternative', $._arithmetic_expression),
)), )),
_arithmetic_unary_expression: $ => choice( arithmetic_unary_expression: $ => choice(
prec(PREC.PREFIX, seq( prec(PREC.PREFIX, seq(
field('operator', tokenLiterals(1, '++', '--')), field('operator', tokenLiterals(1, '++', '--')),
$._arithmetic_expression, $._arithmetic_expression,
@ -482,12 +470,12 @@ module.exports = grammar({
)), )),
), ),
_arithmetic_postfix_expression: $ => prec(PREC.POSTFIX, seq( arithmetic_postfix_expression: $ => prec(PREC.POSTFIX, seq(
$._arithmetic_expression, $._arithmetic_expression,
field('operator', choice('++', '--')), field('operator', choice('++', '--')),
)), )),
_arithmetic_parenthesized_expression: $ => seq('(', $._arithmetic_expression, ')'), arithmetic_parenthesized_expression: $ => seq('(', $._arithmetic_expression, ')'),
concatenation: $ => prec(-1, seq( concatenation: $ => prec(-1, seq(
$._primary_expression, $._primary_expression,