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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
68 changes: 67 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Max Brunsfeld <maxbrunsfeld@gmail.com>
* @author Amaan Qureshi <amaanq12@gmail.com>
* @author John Drouhard <john@drouhard.dev>
* @author Pablo Hugen <pabloashugen@protonmail.com>
* @license MIT
*/

Expand Down Expand Up @@ -106,11 +107,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 All @@ -121,6 +126,11 @@ 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),
Expand Down Expand Up @@ -319,6 +329,62 @@ module.exports = grammar(C, {

// Declarations

module_name: $ => seq(
$.identifier,
repeat(seq('.', $.identifier)
),
),
module_partition: $ => seq(
':',
$.module_name,
),
_exportable_item: $ => choice(
$.declaration,
$.function_definition,
$.template_declaration,
$.template_instantiation,
$.namespace_definition,
$.linkage_specification,
$.attributed_statement,
';',
$.gnu_asm_expression,
$.using_declaration,
$.namespace_alias_definition,
$.static_assert_declaration,
$.alias_declaration,
seq($._type_specifier, ';'),
),

module_declaration: $ => seq(
optional('export'),
'module',
field('name', $.module_name),
field('partition', optional($.module_partition)),
optional($.attribute_declaration),
';'
),
export_declaration: $ => seq(
'export',
choice($._exportable_item, seq('{', repeat($._exportable_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
16 changes: 16 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
(this) @variable.builtin
(null "nullptr" @constant)

((identifier) @constant
(#set! "priority"129)
(#lua-match? @constant "^[A-Z][A-Z%d_]*$"))

((field_identifier) @constant
(#set! "priority" 129)
(#lua-match? @constant "^[A-Z][A-Z%d_]*$"))


; Modules
(module_name
(identifier) @module)

; Keywords

[
Expand Down Expand Up @@ -62,6 +75,9 @@
"using"
"concept"
"requires"
"import"
"export"
"module"
(virtual)
] @keyword

Expand Down
Loading