diff --git a/examples/tests/valid/indexing.test.w b/examples/tests/valid/indexing.test.w index b4ad2d837fb..7a34d28ee77 100644 --- a/examples/tests/valid/indexing.test.w +++ b/examples/tests/valid/indexing.test.w @@ -5,6 +5,8 @@ let arr = Array[1, 2, 3]; assert(arr[0] == 1); assert(arr[2 - 5] == 1); +assert(arr[0] != arr[1]); + try { arr[-5]; diff --git a/libs/tree-sitter-wing/grammar.js b/libs/tree-sitter-wing/grammar.js index fbadd02cd80..12a8f692b06 100644 --- a/libs/tree-sitter-wing/grammar.js +++ b/libs/tree-sitter-wing/grammar.js @@ -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({ @@ -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( diff --git a/libs/tree-sitter-wing/src/grammar.json b/libs/tree-sitter-wing/src/grammar.json index 18ac32e2e77..837202c4aa1 100644 --- a/libs/tree-sitter-wing/src/grammar.json +++ b/libs/tree-sitter-wing/src/grammar.json @@ -192,7 +192,7 @@ }, "nested_identifier": { "type": "PREC", - "value": 150, + "value": 160, "content": { "type": "SEQ", "members": [ @@ -2206,7 +2206,7 @@ }, "call": { "type": "PREC_LEFT", - "value": 160, + "value": 170, "content": { "type": "SEQ", "members": [ @@ -3170,7 +3170,7 @@ }, "optional_unwrap": { "type": "PREC_RIGHT", - "value": 170, + "value": 180, "content": { "type": "SEQ", "members": [ @@ -4183,7 +4183,7 @@ }, "structured_access_expression": { "type": "PREC_RIGHT", - "value": 0, + "value": 150, "content": { "type": "SEQ", "members": [ diff --git a/libs/tree-sitter-wing/test/corpus/expressions.txt b/libs/tree-sitter-wing/test/corpus/expressions.txt index 925bdb0b9ab..df66f7d281f 100644 --- a/libs/tree-sitter-wing/test/corpus/expressions.txt +++ b/libs/tree-sitter-wing/test/corpus/expressions.txt @@ -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)))))))))) diff --git a/tools/hangar/__snapshots__/test_corpus/valid/indexing.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/indexing.test.w_compile_tf-aws.md index 60b4a059e4e..11d7a61fd58 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/indexing.test.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/indexing.test.w_compile_tf-aws.md @@ -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)); }