Skip to content

Commit a80dde1

Browse files
committed
feat!: add support for reflection (P2996)
This adds support for all the language changes introduced in P2996 which was recently voted into the upcoming c++26 standard. This includes: - reflection expressions: the reflect operator (^^) followed by expressions, type descriptors, or the global namespace (::) - splice specifiers/expressions: [: expression :], which can appear as a type, expression, alias descriptor, and more. - consteval blocks: similar to static_asserts but have been introduced specifically to allow constant evaluation side effects to occur at specific places in code
1 parent 2a682d3 commit a80dde1

2 files changed

Lines changed: 893 additions & 7 deletions

File tree

‎grammar.js‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = grammar(C, {
7575
[$.template_function, $.template_type],
7676
[$.template_function, $.template_type, $.expression],
7777
[$.template_function, $.template_type, $.qualified_identifier],
78+
[$.template_function, $.template_type, $.qualified_identifier, $.qualified_type_identifier],
7879
[$.template_type, $.qualified_type_identifier],
7980
[$.qualified_type_identifier, $.qualified_identifier],
8081
[$.comma_expression, $.initializer_list],
@@ -98,6 +99,8 @@ module.exports = grammar(C, {
9899
[$.field_expression, $.template_method, $.template_type],
99100
[$.field_expression, $.template_method],
100101
[$.qualified_field_identifier, $.template_method, $.template_type],
102+
[$.type_specifier, $.template_type, $.template_function, $.expression],
103+
[$.splice_type_specifier, $.splice_expression],
101104
],
102105

103106
inline: ($, original) => original.concat([
@@ -118,6 +121,7 @@ module.exports = grammar(C, {
118121
$.using_declaration,
119122
$.alias_declaration,
120123
$.static_assert_declaration,
124+
$.consteval_block_declaration,
121125
$.template_declaration,
122126
$.template_instantiation,
123127
$.module_declaration,
@@ -137,6 +141,7 @@ module.exports = grammar(C, {
137141
$.using_declaration,
138142
$.alias_declaration,
139143
$.static_assert_declaration,
144+
$.consteval_block_declaration,
140145
$.template_declaration,
141146
$.template_instantiation,
142147
alias($.constructor_or_destructor_definition, $.function_definition),
@@ -178,6 +183,7 @@ module.exports = grammar(C, {
178183
$.primitive_type,
179184
$.template_type,
180185
$.dependent_type,
186+
$.splice_type_specifier,
181187
$.placeholder_type_specifier,
182188
$.decltype,
183189
prec.right(choice(
@@ -240,6 +246,7 @@ module.exports = grammar(C, {
240246
_class_name: $ => prec.right(choice(
241247
$._type_identifier,
242248
$.template_type,
249+
$.splice_type_specifier,
243250
alias($.qualified_type_identifier, $.qualified_identifier),
244251
)),
245252

@@ -541,6 +548,7 @@ module.exports = grammar(C, {
541548
$.using_declaration,
542549
$.type_definition,
543550
$.static_assert_declaration,
551+
$.consteval_block_declaration,
544552
';',
545553
),
546554

@@ -800,6 +808,7 @@ module.exports = grammar(C, {
800808
choice(
801809
$._namespace_identifier,
802810
$.nested_namespace_specifier,
811+
$.splice_specifier,
803812
),
804813
';',
805814
),
@@ -825,6 +834,7 @@ module.exports = grammar(C, {
825834
choice(
826835
$.identifier,
827836
$.qualified_identifier,
837+
$.splice_type_specifier,
828838
),
829839
';',
830840
),
@@ -850,6 +860,11 @@ module.exports = grammar(C, {
850860
';',
851861
),
852862

863+
consteval_block_declaration: $ => seq(
864+
'consteval',
865+
field('body', $.compound_statement),
866+
),
867+
853868
concept_definition: $ => seq(
854869
'concept',
855870
field('name', $.identifier),
@@ -1003,6 +1018,8 @@ module.exports = grammar(C, {
10031018
$.this,
10041019
$.user_defined_literal,
10051020
$.fold_expression,
1021+
$.reflect_expression,
1022+
$.splice_expression,
10061023
),
10071024

10081025
_string: $ => choice(
@@ -1037,10 +1054,13 @@ module.exports = grammar(C, {
10371054
']',
10381055
),
10391056

1040-
call_expression: ($, original) => choice(original, seq(
1041-
field('function', $.primitive_type),
1057+
call_expression: ($, original) => prec.dynamic(1, choice(original, seq(
1058+
field('function', choice($.primitive_type, seq(
1059+
optional('typename'),
1060+
$.splice_type_specifier,
1061+
))),
10421062
field('arguments', $.argument_list),
1043-
)),
1063+
))),
10441064

10451065
co_await_expression: $ => prec.left(PREC.UNARY, seq(
10461066
field('operator', 'co_await'),
@@ -1085,6 +1105,7 @@ module.exports = grammar(C, {
10851105
$.template_method,
10861106
alias($.dependent_field_identifier, $.dependent_name),
10871107
$.operator_name,
1108+
$.splice_expression,
10881109
)),
10891110
),
10901111

@@ -1340,7 +1361,10 @@ module.exports = grammar(C, {
13401361
compound_literal_expression: ($, original) => choice(
13411362
original,
13421363
seq(
1343-
field('type', choice($._class_name, $.primitive_type)),
1364+
field('type', choice(
1365+
$._class_name,
1366+
$.primitive_type,
1367+
seq(optional('typename'), $.splice_type_specifier))),
13441368
field('value', $.initializer_list),
13451369
),
13461370
),
@@ -1354,6 +1378,8 @@ module.exports = grammar(C, {
13541378
$._namespace_identifier,
13551379
$.template_type,
13561380
$.decltype,
1381+
$.splice_expression,
1382+
$.splice_type_specifier,
13571383
alias($.dependent_type_identifier, $.dependent_name),
13581384
))),
13591385
'::',
@@ -1365,7 +1391,7 @@ module.exports = grammar(C, {
13651391
alias($.dependent_field_identifier, $.dependent_name),
13661392
alias($.qualified_field_identifier, $.qualified_identifier),
13671393
$.template_method,
1368-
prec.dynamic(1, $._field_identifier),
1394+
prec.dynamic(2, $._field_identifier),
13691395
)),
13701396
),
13711397

@@ -1375,7 +1401,7 @@ module.exports = grammar(C, {
13751401
alias($.dependent_identifier, $.dependent_name),
13761402
$.qualified_identifier,
13771403
$.template_function,
1378-
seq(optional('template'), $.identifier),
1404+
prec.dynamic(1, seq(optional('template'), $.identifier)),
13791405
$.operator_name,
13801406
$.destructor_name,
13811407
$.pointer_type_declarator,
@@ -1388,7 +1414,7 @@ module.exports = grammar(C, {
13881414
alias($.dependent_type_identifier, $.dependent_name),
13891415
alias($.qualified_type_identifier, $.qualified_identifier),
13901416
$.template_type,
1391-
$._type_identifier,
1417+
prec.dynamic(1, $._type_identifier),
13921418
)),
13931419
),
13941420

@@ -1425,6 +1451,28 @@ module.exports = grammar(C, {
14251451
seq('(', alias($._assignment_expression_lhs, $.assignment_expression), ')'),
14261452
),
14271453

1454+
reflect_expression: $ => prec.right(seq(
1455+
'^^',
1456+
choice(
1457+
'::',
1458+
$.expression,
1459+
$.type_descriptor,
1460+
),
1461+
)),
1462+
1463+
splice_specifier: $ => seq( '[:', $.expression, ':]'),
1464+
_splice_specialization_specifier: $ => seq($.splice_specifier, $.template_argument_list),
1465+
1466+
splice_type_specifier: $ => prec.right(choice(
1467+
$.splice_specifier,
1468+
$._splice_specialization_specifier,
1469+
)),
1470+
1471+
splice_expression: $ => prec.right(choice(
1472+
$.splice_specifier,
1473+
seq('template', $._splice_specialization_specifier),
1474+
)),
1475+
14281476
operator_name: $ => prec(1, seq(
14291477
'operator',
14301478
choice(

0 commit comments

Comments
 (0)