Skip to content

Commit

Permalink
C++: Added missing keywords and modules (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Feb 26, 2021
1 parent e931441 commit 88fa72c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
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", ";"]
]

0 comments on commit 88fa72c

Please sign in to comment.