-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
grammar.js
Outdated
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), | ||
)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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[]`.
460e66e
to
472002f
Compare
This change allows commas inside subscripts.
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[]
.