Skip to content

Commit 3fb12d9

Browse files
authored
[C++] Concept Syntax Highlighting (#4140)
Fixes #2481 Adds concept/constraint support to C++
1 parent ca2c76d commit 3fb12d9

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

C++/C++.sublime-syntax

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ variables:
6161
control_keywords: 'break|case|catch|continue|default|do|else|for|goto|if|_Pragma|switch|throw|try|while|{{coroutine_keywords}}'
6262
memory_operators: 'new|delete'
6363
basic_types: 'asm|__asm__|auto|bool|_Bool|char|_Complex|double|float|_Imaginary|int|long|short|signed|unsigned|void'
64-
before_tag: 'struct|union|enum\s+class|enum\s+struct|enum|class'
64+
before_tag: 'struct|union|enum\s+class|enum\s+struct|enum|class|concept'
6565
declspec: '__declspec\(\s*\w+(?:\([^)]+\))?\s*\)'
6666
storage_classes: 'static|export|extern|friend|explicit|virtual|register|thread_local'
6767
type_qualifier: 'const|mutable|typename|volatile'
@@ -745,7 +745,10 @@ contexts:
745745
- meta_content_scope: meta.template.c++
746746
- match: '>'
747747
scope: meta.template.c++ punctuation.definition.generic.end.c++
748-
pop: true
748+
set:
749+
- include: constraint-requires
750+
- match: (?=\S)
751+
pop: true
749752
- match: \.{3}
750753
scope: keyword.operator.variadic.c++
751754
- match: \b(typename|{{before_tag}})\b
@@ -759,6 +762,26 @@ contexts:
759762
scope: keyword.declaration.c++
760763
- include: template-common
761764

765+
requires:
766+
- match: \brequires\b
767+
scope: keyword.operator.word.c++
768+
push: function-definition-params
769+
770+
constraint:
771+
- match: (?=;)
772+
pop: true
773+
- include: requires
774+
- include: expressions-minus-generic-type-function-call
775+
776+
constraint-requires:
777+
- match: '\brequires\b'
778+
scope: storage.modifier.c++
779+
push:
780+
- meta_scope: meta.constraint.c++
781+
- match: (?=\{|{{identifier}}\s*{{identifier}}\(|{{before_tag}}|:)
782+
pop: true
783+
- include: constraint
784+
762785
generic-type:
763786
- match: '(?=(?!template){{path_lookahead}}\s*{{generic_lookahead}}\s*(\(|\{))'
764787
push:
@@ -1248,6 +1271,7 @@ contexts:
12481271
scope: punctuation.separator.c++
12491272
set: function-definition-trailing-return
12501273
- include: function-specifiers
1274+
- include: constraint-requires
12511275
- match: '='
12521276
scope: keyword.operator.assignment.c++
12531277
- match: '&'
@@ -1268,6 +1292,7 @@ contexts:
12681292
- match: '(?=\{)'
12691293
set: function-definition-body
12701294
- include: function-specifiers
1295+
- include: constraint-requires
12711296
- include: function-trailing-return-type
12721297

12731298
function-definition-body:
@@ -1303,6 +1328,9 @@ contexts:
13031328
- match: '\bunion\b'
13041329
scope: keyword.declaration.union.type.c++
13051330
set: data-structures-union-definition
1331+
- match: '\bconcept\b'
1332+
scope: meta.concept.c++ keyword.declaration.concept.c++
1333+
set: data-structures-concept-definition
13061334
- match: '(?=\S)'
13071335
pop: true
13081336

@@ -1463,6 +1491,17 @@ contexts:
14631491
- match: '(?=;)'
14641492
pop: true
14651493

1494+
data-structures-concept-definition:
1495+
- meta_content_scope: meta.concept.c++
1496+
- include: data-structures-definition-common-begin
1497+
- match: '({{identifier}})\s+(=)'
1498+
captures:
1499+
1: entity.name.concept.c++
1500+
2: keyword.operator.assignment.c++
1501+
set:
1502+
- meta_content_scope: meta.concept.c++ meta.constraint.c++
1503+
- include: constraint
1504+
14661505
data-structures-definition-common-begin:
14671506
- include: comments
14681507
- match: '(?=\b(?:{{before_tag}}|{{control_keywords}})\b)'
@@ -1725,6 +1764,7 @@ contexts:
17251764
scope: punctuation.separator.c++
17261765
set: method-definition-trailing-return
17271766
- include: function-specifiers
1767+
- include: constraint-requires
17281768
- match: '='
17291769
scope: keyword.operator.assignment.c++
17301770
- match: '&'

C++/syntax_test_cpp.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,46 @@ void test4()
31393139
return;
31403140
}
31413141

3142+
/////////////////////////////////////////////
3143+
// Concepts
3144+
/////////////////////////////////////////////
3145+
3146+
template <typename T>
3147+
/* <- meta.template.c++ keyword.declaration.template.c++ */
3148+
concept has_foo = requires(T t) {
3149+
/* <- meta.concept.c++ keyword.declaration.concept.c++ */
3150+
/* ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.concept.c++ */
3151+
/* ^^^^^^^ meta.concept.c++ entity.name.concept.c++ */
3152+
/* ^^^^^^^^^^^^^^^ meta.concept.c++ meta.constraint.c++ */
3153+
/* ^^^^^ meta.function.parameters.c++ */
3154+
/* ^ meta.function.c++ meta.block.c++ */
3155+
t.foo();
3156+
/* ^^^^^^^^ meta.concept.c++ meta.constraint.c++ meta.function.c++ meta.block.c++ */
3157+
} && std::move_constructible<T>;
3158+
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.concept.c++ meta.constraint.c++ */
3159+
3160+
template <typename C>
3161+
/* <- meta.template.c++ keyword.declaration.template.c++ */
3162+
void foo() requires std::same_as<C, void>
3163+
/* <- storage.type.c */
3164+
/* ^^^ meta.function.c++ entity.name.function.c++ */
3165+
/* ^^^^^^^^ meta.function.c++ meta.constraint.c++ storage.modifier.c++ */
3166+
/* ^^^^^^^^^^^^^^^^^^^^^ meta.function.c++ meta.constraint.c++ */
3167+
{
3168+
return;
3169+
}
3170+
3171+
template <typename C> requires std::same_as<C, void>
3172+
/* <- meta.template.c++ keyword.declaration.template.c++ */
3173+
/* ^^^^^^^^ meta.constraint.c++ storage.modifier.c++ */
3174+
/* ^^^^^^^^^^^^^^^^^^^^^ meta.constraint.c++ */
3175+
void bar()
3176+
/* <- storage.type.c */
3177+
/* ^^^ meta.function.c++ entity.name.function.c++ */
3178+
{
3179+
return;
3180+
}
3181+
31423182
#define GTY0
31433183
/* ^^^^ meta.preprocessor.macro.c++ */
31443184
#define GTY1(A)

0 commit comments

Comments
 (0)