Skip to content

Commit

Permalink
fix(compiler): array access operator precedence (#6277)
Browse files Browse the repository at this point in the history
Fixes #6276 by adding a precedence level for array access expressions. I inserted the precedence right below member access expressions, similar to JavaScript's operator precedence [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence#table).

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
Chriscbr authored Apr 19, 2024
1 parent 24178a4 commit 8d9d77e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/tests/valid/indexing.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ let arr = Array<num>[1, 2, 3];

assert(arr[0] == 1);
assert(arr[2 - 5] == 1);
assert(arr[0] != arr[1]);


try {
arr[-5];
Expand Down
9 changes: 5 additions & 4 deletions libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const PREC = {
UNARY: 120,
OPTIONAL_TEST: 130,
POWER: 140,
MEMBER: 150,
CALL: 160,
OPTIONAL_UNWRAP: 170,
STRUCTURED_ACCESS: 150, // x[y]
MEMBER: 160,
CALL: 170,
OPTIONAL_UNWRAP: 180,
};

module.exports = grammar({
Expand Down Expand Up @@ -674,7 +675,7 @@ module.exports = grammar({
map_literal_member: ($) => seq($.expression, "=>", $.expression),
struct_literal_member: ($) => seq($.identifier, ":", $.expression),
structured_access_expression: ($) =>
prec.right(seq($.expression, "[", $.expression, "]")),
prec.right(PREC.STRUCTURED_ACCESS, seq($.expression, "[", $.expression, "]")),

json_literal: ($) =>
choice(
Expand Down
8 changes: 4 additions & 4 deletions libs/tree-sitter-wing/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions libs/tree-sitter-wing/test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,55 @@ let mo = 10mo;
value: (duration
(months
value: (number)))))

================================================================================
Array access
================================================================================

log(x[y]);
x[y] += 3;
assert(x[y] == w[z]);

--------------------------------------------------------------------------------

(source
(expression_statement
(call
(reference
(reference_identifier))
(argument_list
(positional_argument
(reference
(structured_access_expression
(reference
(reference_identifier))
(reference
(reference_identifier))))))))
(variable_assignment_statement
(lvalue
(structured_access_expression
(reference
(reference_identifier))
(reference
(reference_identifier))))
(assignment_operator)
(number))
(expression_statement
(call
(reference
(reference_identifier))
(argument_list
(positional_argument
(binary_expression
(reference
(structured_access_expression
(reference
(reference_identifier))
(reference
(reference_identifier))))
(reference
(structured_access_expression
(reference
(reference_identifier))
(reference
(reference_identifier))))))))))
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class $Root extends $stdlib.std.Resource {
const arr = [1, 2, 3];
$helpers.assert($helpers.eq($helpers.lookup(arr, 0), 1), "arr[0] == 1");
$helpers.assert($helpers.eq($helpers.lookup(arr, (2 - 5)), 1), "arr[2 - 5] == 1");
$helpers.assert($helpers.neq($helpers.lookup(arr, 0), $helpers.lookup(arr, 1)), "arr[0] != arr[1]");
try {
$helpers.lookup(arr, (-5));
}
Expand Down

0 comments on commit 8d9d77e

Please sign in to comment.