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++20 modules support #266

Merged
merged 6 commits into from
Feb 6, 2025
Merged
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
50 changes: 49 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Max Brunsfeld <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @author John Drouhard <[email protected]>
* @author Pablo Hugen <[email protected]>
* @license MIT
*/

Expand Down Expand Up @@ -116,11 +117,15 @@ module.exports = grammar(C, {
$.static_assert_declaration,
$.template_declaration,
$.template_instantiation,
$.module_declaration,
$.export_declaration,
$.import_declaration,
$.global_module_fragment_declaration,
$.private_module_fragment_declaration,
alias($.constructor_or_destructor_definition, $.function_definition),
alias($.operator_cast_definition, $.function_definition),
alias($.operator_cast_declaration, $.declaration),
),

_block_item: ($, original) => choice(
...original.members.filter((member) => member.content?.name != '_old_style_function_definition'),
$.namespace_definition,
Expand Down Expand Up @@ -319,6 +324,49 @@ module.exports = grammar(C, {

// Declarations

module_name: $ => seq(
$.identifier,
repeat(seq('.', $.identifier),
),
),

module_partition: $ => seq(
':',
$.module_name,
),

module_declaration: $ => seq(
optional('export'),
'module',
field('name', $.module_name),
field('partition', optional($.module_partition)),
optional($.attribute_declaration),
';',
),

export_declaration: $ => seq(
'export',
choice($._block_item, seq('{', repeat($._block_item), '}')),
),

import_declaration: $ => seq(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it reduce productions to remove the optional('export') and instead add this to the list of exportable items? The standard enforces that a module import declaration shall not be contained in a declaration-seq of an export-declaration: https://eel.is/c%2B%2Bdraft/module#interface-1. This could simplify the productions here since it's more of a semantic error.

optional('export'),
'import',
choice(
field('name', $.module_name),
field('partition', $.module_partition),
field('header', choice(
$.string_literal,
$.system_lib_string,
)),
),
optional($.attribute_declaration),
';',
),

global_module_fragment_declaration: _ => seq('module', ';'),
private_module_fragment_declaration: _ => seq('module', ':', 'private', ';'),

template_declaration: $ => seq(
'template',
field('parameters', $.template_parameter_list),
Expand Down
7 changes: 7 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
(this) @variable.builtin
(null "nullptr" @constant)

; Modules
(module_name
(identifier) @module)

; Keywords

[
Expand Down Expand Up @@ -63,6 +67,9 @@
"concept"
"requires"
"virtual"
"import"
"export"
"module"
] @keyword

; Strings
Expand Down
266 changes: 266 additions & 0 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading