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

[C++] Concept Syntax Highlighting #4140

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions C++/C++.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ variables:
control_keywords: 'break|case|catch|continue|default|do|else|for|goto|if|_Pragma|switch|throw|try|while|{{coroutine_keywords}}'
memory_operators: 'new|delete'
basic_types: 'asm|__asm__|auto|bool|_Bool|char|_Complex|double|float|_Imaginary|int|long|short|signed|unsigned|void'
before_tag: 'struct|union|enum\s+class|enum\s+struct|enum|class'
before_tag: 'struct|union|enum\s+class|enum\s+struct|enum|class|concept'
declspec: '__declspec\(\s*\w+(?:\([^)]+\))?\s*\)'
storage_classes: 'static|export|extern|friend|explicit|virtual|register|thread_local'
type_qualifier: 'const|mutable|typename|volatile'
Expand Down Expand Up @@ -745,7 +745,10 @@ contexts:
- meta_content_scope: meta.template.c++
- match: '>'
scope: meta.template.c++ punctuation.definition.generic.end.c++
pop: true
set:
- include: constraint-requires
- match: (?=\S)
pop: true
- match: \.{3}
scope: keyword.operator.variadic.c++
- match: \b(typename|{{before_tag}})\b
Expand All @@ -759,6 +762,26 @@ contexts:
scope: keyword.declaration.c++
- include: template-common

requires:
- match: \brequires\b
scope: keyword.operator.word.c++
camila314 marked this conversation as resolved.
Show resolved Hide resolved
push: function-definition-params

constraint:
- match: (?=;)
pop: true
- include: requires
- include: expressions-minus-generic-type-function-call

constraint-requires:
- match: '\brequires\b'
scope: storage.modifier.c++
push:
- meta_scope: meta.constraint.c++
- match: (?=\{|{{identifier}}\s*{{identifier}}\(|{{before_tag}}|:)
pop: true
- include: constraint

generic-type:
- match: '(?=(?!template){{path_lookahead}}\s*{{generic_lookahead}}\s*(\(|\{))'
push:
Expand Down Expand Up @@ -1248,6 +1271,7 @@ contexts:
scope: punctuation.separator.c++
set: function-definition-trailing-return
- include: function-specifiers
- include: constraint-requires
- match: '='
scope: keyword.operator.assignment.c++
- match: '&'
Expand All @@ -1268,6 +1292,7 @@ contexts:
- match: '(?=\{)'
set: function-definition-body
- include: function-specifiers
- include: constraint-requires
camila314 marked this conversation as resolved.
Show resolved Hide resolved
- include: function-trailing-return-type

function-definition-body:
Expand Down Expand Up @@ -1303,6 +1328,9 @@ contexts:
- match: '\bunion\b'
scope: keyword.declaration.union.type.c++
set: data-structures-union-definition
- match: '\bconcept\b'
scope: meta.concept.c++ keyword.declaration.concept.c++
set: data-structures-concept-definition
- match: '(?=\S)'
pop: true

Expand Down Expand Up @@ -1463,6 +1491,17 @@ contexts:
- match: '(?=;)'
pop: true

data-structures-concept-definition:
- meta_content_scope: meta.concept.c++
- include: data-structures-definition-common-begin
- match: '({{identifier}})\s+(=)'
captures:
1: entity.name.concept.c++
2: keyword.operator.assignment.c++
set:
- meta_content_scope: meta.concept.c++ meta.constraint.c++
- include: constraint

data-structures-definition-common-begin:
- include: comments
- match: '(?=\b(?:{{before_tag}}|{{control_keywords}})\b)'
Expand Down Expand Up @@ -1725,6 +1764,7 @@ contexts:
scope: punctuation.separator.c++
set: method-definition-trailing-return
- include: function-specifiers
- include: constraint-requires
- match: '='
scope: keyword.operator.assignment.c++
- match: '&'
Expand Down
40 changes: 40 additions & 0 deletions C++/syntax_test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3139,6 +3139,46 @@ void test4()
return;
}

/////////////////////////////////////////////
// Concepts
/////////////////////////////////////////////

template <typename T>
/* <- meta.template.c++ keyword.declaration.template.c++ */
concept has_foo = requires(T t) {
/* <- meta.concept.c++ keyword.declaration.concept.c++ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.concept.c++ */
/* ^^^^^^^ meta.concept.c++ entity.name.concept.c++ */
/* ^^^^^^^^^^^^^^^ meta.concept.c++ meta.constraint.c++ */
/* ^^^^^ meta.function.parameters.c++ */
/* ^ meta.function.c++ meta.block.c++ */
t.foo();
/* ^^^^^^^^ meta.concept.c++ meta.constraint.c++ meta.function.c++ meta.block.c++ */
} && std::move_constructible<T>;
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.concept.c++ meta.constraint.c++ */

template <typename C>
/* <- meta.template.c++ keyword.declaration.template.c++ */
void foo() requires std::same_as<C, void>
/* <- storage.type.c */
/* ^^^ meta.function.c++ entity.name.function.c++ */
/* ^^^^^^^^ meta.function.c++ meta.constraint.c++ storage.modifier.c++ */
/* ^^^^^^^^^^^^^^^^^^^^^ meta.function.c++ meta.constraint.c++ */
{
return;
}

template <typename C> requires std::same_as<C, void>
/* <- meta.template.c++ keyword.declaration.template.c++ */
/* ^^^^^^^^ meta.constraint.c++ storage.modifier.c++ */
/* ^^^^^^^^^^^^^^^^^^^^^ meta.constraint.c++ */
void bar()
/* <- storage.type.c */
/* ^^^ meta.function.c++ entity.name.function.c++ */
{
return;
}

#define GTY0
/* ^^^^ meta.preprocessor.macro.c++ */
#define GTY1(A)
Expand Down