Skip to content

Commit 3fd1c51

Browse files
rktdmjdrouhard
authored andcommitted
feat: add support for annotations (P3394)
Related to reflection, annotations use the same syntax as attributes, but begin with an `=` and support any constant expression. Annotations and Attributes can't be mixed within [[ ... ]].
1 parent c95a0b1 commit 3fd1c51

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎grammar.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ module.exports = grammar(C, {
206206
...original.members,
207207
),
208208

209+
annotation: $ => seq('=', $.expression),
210+
211+
attribute_declaration: ($, original) => choice(
212+
original,
213+
seq('[[', commaSep1($.annotation), ']]'),
214+
),
215+
209216
// When used in a trailing return type, these specifiers can now occur immediately before
210217
// a compound statement. This introduces a shift/reduce conflict that needs to be resolved
211218
// with an associativity.

‎test/corpus/reflection.txt‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,3 +836,100 @@ consteval { TCls<void>::sfn(); }
836836
(primitive_type))))
837837
(identifier))
838838
(argument_list))))))
839+
840+
================================================================================
841+
Annotations
842+
================================================================================
843+
844+
[[=1]] void f();
845+
[[=2, =3]] void g();
846+
void g [[=4]] [[=2]] ();
847+
848+
template <class T>
849+
[[=T::type()]] void f(T t);
850+
851+
struct Option { bool value; };
852+
struct C {
853+
[[=Option{true}]] int a;
854+
[[=Option{false}]] int b;
855+
};
856+
857+
--------------------------------------------------------------------------------
858+
859+
(translation_unit
860+
(declaration
861+
(attribute_declaration
862+
(annotation
863+
(number_literal)))
864+
(primitive_type)
865+
(function_declarator
866+
(identifier)
867+
(parameter_list)))
868+
(declaration
869+
(attribute_declaration
870+
(annotation
871+
(number_literal))
872+
(annotation
873+
(number_literal)))
874+
(primitive_type)
875+
(function_declarator
876+
(identifier)
877+
(parameter_list)))
878+
(declaration
879+
(primitive_type)
880+
(function_declarator
881+
(attributed_declarator
882+
(identifier)
883+
(attribute_declaration
884+
(annotation
885+
(number_literal)))
886+
(attribute_declaration
887+
(annotation
888+
(number_literal))))
889+
(parameter_list)))
890+
(template_declaration
891+
(template_parameter_list
892+
(type_parameter_declaration
893+
(type_identifier)))
894+
(declaration
895+
(attribute_declaration
896+
(annotation
897+
(call_expression
898+
(qualified_identifier
899+
(namespace_identifier)
900+
(identifier))
901+
(argument_list))))
902+
(primitive_type)
903+
(function_declarator
904+
(identifier)
905+
(parameter_list
906+
(parameter_declaration
907+
(type_identifier)
908+
(identifier))))))
909+
(struct_specifier
910+
(type_identifier)
911+
(field_declaration_list
912+
(field_declaration
913+
(primitive_type)
914+
(field_identifier))))
915+
(struct_specifier
916+
(type_identifier)
917+
(field_declaration_list
918+
(field_declaration
919+
(attribute_declaration
920+
(annotation
921+
(compound_literal_expression
922+
(type_identifier)
923+
(initializer_list
924+
(true)))))
925+
(primitive_type)
926+
(field_identifier))
927+
(field_declaration
928+
(attribute_declaration
929+
(annotation
930+
(compound_literal_expression
931+
(type_identifier)
932+
(initializer_list
933+
(false)))))
934+
(primitive_type)
935+
(field_identifier)))))

0 commit comments

Comments
 (0)