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

Support multidimensional subscripts #221

Merged
merged 1 commit into from
Aug 9, 2023

Conversation

elbeno
Copy link
Contributor

@elbeno elbeno commented Aug 6, 2023

This change allows commas inside subscripts.

auto x = a[1, 2, 3];

From C++23, array subscripts work like arguments to operator[] as an n-ary function.

Before C++23, operator[] must be a unary function, but before C++20, it's possible to use the comma operator inside subscript expressions - and this is done in libraries that use expression templates to achieve multidimensional array syntax.

Use of the comma operator in subscript expressions was deprecated in C++20 to make way for the C++23 change to n-ary operator[].

grammar.js Outdated
Comment on lines 881 to 890
subscript_expression: $ => prec(PREC.SUBSCRIPT, seq(
field('argument', $._expression),
subscript_argument_list: $ => seq(
'[',
field('index', choice($._expression, $.initializer_list)),
commaSep(choice($._expression, $.initializer_list)),
']',
)),
),

subscript_expression: $ => prec(PREC.SUBSCRIPT, seq(
field('argument', $._expression),
field('indices', $.subscript_argument_list),
)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a particular need to segment the argument list out into their own node - let's just keep it contained in subscript_expression instead.

Otherwise this looks good.

So, just changing it to commaSep(field('index', choice($._expression, $.initializer_list))) would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I can do that easily enough. However the reason to present the argument list as its own node is to make it easier for higher-level code to treat it semantically like a list of function args. For example imagine an editor having a function to swap arguments around - not an uncommon operation when refactoring code.

I've written code to do this in my editor and it's useful to have a similar parent-node-child-node structure for:

  • function parameter lists
  • function argument lists
  • template parameter lists
  • template argument lists
  • subscript argument lists

Which was my rationale for doing it this way. Do you still want to change it back?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, you have a valid point, this is ok as is then

Can you just move the argument list below the expression then? Just a stylistic preference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

This change allows commas inside subscripts.

```cpp
auto x = a[1, 2, 3];
```

From C++23, array subscripts work like arguments to `operator[]` as an n-ary
function.

Before C++23, `operator[]` must be a unary function, but before C++20, it's
possible to use the comma operator inside subscript expressions - and this is
done in libraries that use expression templates to achieve multidimensional
array syntax.

Use of the comma operator in subscript expressions was deprecated in C++20 to
make way for the C++23 change to n-ary `operator[]`.
@elbeno elbeno force-pushed the multidimensional-subscripts branch from 460e66e to 472002f Compare August 6, 2023 18:19
@amaanq amaanq merged commit 5ea15e4 into tree-sitter:master Aug 9, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants