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++: Added missing keywords and modules #2763

Merged
merged 1 commit into from Feb 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion components/prism-cpp.js
@@ -1,6 +1,7 @@
(function (Prism) {

var keyword = /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char8_t|char16_t|char32_t|class|compl|concept|const|consteval|constexpr|constinit|const_cast|continue|co_await|co_return|co_yield|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|mutable|namespace|new|noexcept|nullptr|operator|private|protected|public|register|reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/;
var keyword = /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char8_t|char16_t|char32_t|class|compl|concept|const|consteval|constexpr|constinit|const_cast|continue|co_await|co_return|co_yield|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|final|float|for|friend|goto|if|import|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|module|mutable|namespace|new|noexcept|nullptr|operator|override|private|protected|public|register|reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/;
var modName = /\b(?!<keyword>)\w+(?:\s*\.\s*\w+)*\b/.source.replace(/<keyword>/g, function () { return keyword.source; })

Prism.languages.cpp = Prism.languages.extend('c', {
'class-name': [
Expand Down Expand Up @@ -31,6 +32,26 @@
});

Prism.languages.insertBefore('cpp', 'string', {
'module': {
// https://en.cppreference.com/w/cpp/language/modules
pattern: RegExp(
/(\b(?:module|import)\s+)/.source +
'(?:' +
// header-name
/"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|<[^<>\r\n]*>/.source +
'|' +
// module name or partition or both
/<mod-name>(?:\s*:\s*<mod-name>)?|:\s*<mod-name>/.source.replace(/<mod-name>/g, function () { return modName; }) +
')'
),
lookbehind: true,
greedy: true,
inside: {
'string': /^[<"][\s\S]+/,
'operator': /:/,
'punctuation': /\./
}
},
'raw-string': {
pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
alias: 'string',
Expand Down
2 changes: 1 addition & 1 deletion components/prism-cpp.min.js

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

8 changes: 8 additions & 0 deletions tests/languages/cpp/keyword_feature.test
Expand Up @@ -33,20 +33,24 @@ enum;
explicit
export
extern
final
float
for
friend
goto
if
import;
inline
int
long
module;
mutable
namespace
new
noexcept
nullptr
operator
override
private
protected
public
Expand Down Expand Up @@ -125,20 +129,24 @@ uint64_t
["keyword", "explicit"],
["keyword", "export"],
["keyword", "extern"],
["keyword", "final"],
["keyword", "float"],
["keyword", "for"],
["keyword", "friend"],
["keyword", "goto"],
["keyword", "if"],
["keyword", "import"], ["punctuation", ";"],
["keyword", "inline"],
["keyword", "int"],
["keyword", "long"],
["keyword", "module"], ["punctuation", ";"],
["keyword", "mutable"],
["keyword", "namespace"],
["keyword", "new"],
["keyword", "noexcept"],
["keyword", "nullptr"],
["keyword", "operator"],
["keyword", "override"],
["keyword", "private"],
["keyword", "protected"],
["keyword", "public"],
Expand Down
116 changes: 116 additions & 0 deletions tests/languages/cpp/module_feature.test
@@ -0,0 +1,116 @@
export module speech;

export const char* get_phrase_en() {
return "Hello, world!";
}

export module speech;

export import :english;
export import :spanish;

export module speech:english;

import speech;
import :PrivWidget;

import <iostream>;
import <cstdlib>;
import "foo.h";
import <baz.h>;

module : private;

----------------------------------------------------

[
["keyword", "export"],
["keyword", "module"],
["module", ["speech"]],
["punctuation", ";"],

["keyword", "export"],
["keyword", "const"],
["keyword", "char"],
["operator", "*"],
["function", "get_phrase_en"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],

["keyword", "return"],
["string", "\"Hello, world!\""],
["punctuation", ";"],

["punctuation", "}"],

["keyword", "export"],
["keyword", "module"],
["module", ["speech"]],
["punctuation", ";"],

["keyword", "export"],
["keyword", "import"],
["module", [
["operator", ":"],
"english"
]],
["punctuation", ";"],

["keyword", "export"],
["keyword", "import"],
["module", [
["operator", ":"],
"spanish"
]],
["punctuation", ";"],

["keyword", "export"],
["keyword", "module"],
["module", [
"speech",
["operator", ":"],
"english"
]],
["punctuation", ";"],

["keyword", "import"],
["module", ["speech"]],
["punctuation", ";"],

["keyword", "import"],
["module", [
["operator", ":"],
"PrivWidget"
]],
["punctuation", ";"],

["keyword", "import"],
["module", [
["string", "<iostream>"]
]],
["punctuation", ";"],

["keyword", "import"],
["module", [
["string", "<cstdlib>"]
]],
["punctuation", ";"],

["keyword", "import"],
["module", [
["string", "\"foo.h\""]
]],
["punctuation", ";"],

["keyword", "import"],
["module", [
["string", "<baz.h>"]
]],
["punctuation", ";"],

["keyword", "module"],
["operator", ":"],
["keyword", "private"],
["punctuation", ";"]
]