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

Fixes and fixes #191

Merged
merged 9 commits into from
Aug 22, 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
101 changes: 91 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ module.exports = grammar({
c_style_for_statement: $ => seq(
'for',
'((',
choice($._for_body, ';;'),
choice($._for_body),
'))',
optional(';'),
field('body', choice(
Expand All @@ -164,9 +164,9 @@ module.exports = grammar({
),
_for_body: $ => seq(
field('initializer', commaSep($._c_expression)),
$._terminator,
$._c_terminator,
field('condition', commaSep($._c_expression)),
$._terminator,
$._c_terminator,
field('update', commaSep($._c_expression)),
),

Expand All @@ -184,6 +184,7 @@ module.exports = grammar({
alias($._c_binary_expression, $.binary_expression),
alias($._c_postfix_expression, $.postfix_expression),
alias($._c_parenthesized_expression, $.parenthesized_expression),
$.command_substitution,
),

_c_variable_assignment: $ => seq(
Expand All @@ -196,7 +197,7 @@ module.exports = grammar({
$._c_expression_not_assignment,
)),
_c_binary_expression: $ => prec.right(seq(
choice($._c_word, $.simple_expansion),
$._c_expression_not_assignment,
choice(
'+=', '-=', '*=', '/=', '%=', '**=',
'<<=', '>>=', '&=', '^=', '|=',
Expand All @@ -213,7 +214,7 @@ module.exports = grammar({
),
_c_parenthesized_expression: $ => seq(
'(',
$._c_expression,
commaSep1($._c_expression),
')',
),
_c_word: $ => alias(/[a-zA-Z_][a-zA-Z0-9_]*/, $.word),
Expand Down Expand Up @@ -314,7 +315,7 @@ module.exports = grammar({

compound_statement: $ => seq(
'{',
optional(choice($._statements2, seq($._statement, optional($._terminator)))),
optional(choice($._statements2, seq($._statement, $._terminator))),
alias(token(prec(-1, '}')), '}'),
),

Expand Down Expand Up @@ -352,7 +353,7 @@ module.exports = grammar({
choice(
seq('[', choice($._expression, $.redirected_statement), ']'),
seq('[[', $._expression, ']]'),
seq('(', '(', $._expression, '))'),
seq('(', '(', optional($._expression), '))'),
),
),

Expand Down Expand Up @@ -541,7 +542,76 @@ module.exports = grammar({
$.arithmetic_expansion,
),

arithmetic_expansion: $ => seq('$(', '(', $._expression, '))'),
arithmetic_expansion: $ => seq('$((', $._arithmetic_expression, '))'),

_arithmetic_expression: $ => choice(
$._arithmetic_literal,
alias($._arithmetic_unary_expression, $.unary_expression),
alias($._arithmetic_ternary_expression, $.ternary_expression),
alias($._arithmetic_binary_expression, $.binary_expression),
alias($._arithmetic_postfix_expression, $.postfix_expression),
alias($._arithmetic_parenthesized_expression, $.parenthesized_expression),
),

_arithmetic_literal: $ => prec(1, choice(
$.number,
$.subscript,
$.simple_expansion,
$.expansion,
$._simple_variable_name,
)),

_arithmetic_binary_expression: $ => prec.left(2, choice(
seq(
field('left', $._arithmetic_expression),
field('operator', choice(
'=', '==', '=~', '!=',
'+', '-', '+=', '-=',
'*', '/', '*=', '/=',
'%', '%=', '**',
'<', '>', '<=', '>=',
'||', '&&',
'<<', '>>', '<<=', '>>=',
'&', '|', '^',
'&=', '|=', '^=',
$.test_operator,
)),
field('right', $._arithmetic_expression),
),
)),

_arithmetic_ternary_expression: $ => prec.left(
seq(
field('condition', $._arithmetic_expression),
'?',
field('consequence', $._arithmetic_expression),
':',
field('alternative', $._arithmetic_expression),
),
),

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

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

_arithmetic_parenthesized_expression: $ => seq(
'(',
$._arithmetic_expression,
')',
),


concatenation: $ => prec(-1, seq(
choice(
Expand Down Expand Up @@ -591,7 +661,11 @@ module.exports = grammar({

ansi_c_string: _ => /\$'([^']|\\')*'/,

number: _ => /(0x)?[0-9]+(#[0-9A-Za-z@_]+)?/,
number: $ => choice(
/-?(0x)?[0-9]+(#[0-9A-Za-z@_]+)?/,
// the base can be an expansion
seq(/-?(0x)?[0-9]+#/, $.expansion),
),

simple_expansion: $ => seq(
'$',
Expand Down Expand Up @@ -626,7 +700,13 @@ module.exports = grammar({
$.command_substitution,
),
optional(seq(
token(prec(1, '/')),
choice(
alias(token(prec(1, '/')), '/'),
alias(token(prec(1, ',')), ','),
alias(token(prec(1, ',,')), ',,'),
alias(token(prec(1, '^')), '^'),
alias(token(prec(1, '^^')), '^^'),
),
optional($.regex),
)),
repeat(prec.right(choice(
Expand Down Expand Up @@ -684,6 +764,7 @@ module.exports = grammar({

test_operator: _ => token(prec(1, seq('-', /[a-zA-Z]+/))),

_c_terminator: _ => choice(';', '\n', '&'),
_terminator: _ => choice(';', ';;', '\n', '&'),
},
});
Expand Down
1 change: 0 additions & 1 deletion script/known-failures.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
examples/bash/tests/arith-for.tests
examples/bash/tests/arith.tests
examples/bash/tests/array.tests
examples/bash/tests/assoc.tests
Expand Down
Loading