Skip to content

Commit 88fa72c

Browse files
C++: Added missing keywords and modules (#2763)
1 parent e931441 commit 88fa72c

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

components/prism-cpp.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(function (Prism) {
22

3-
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/;
3+
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/;
4+
var modName = /\b(?!<keyword>)\w+(?:\s*\.\s*\w+)*\b/.source.replace(/<keyword>/g, function () { return keyword.source; })
45

56
Prism.languages.cpp = Prism.languages.extend('c', {
67
'class-name': [
@@ -31,6 +32,26 @@
3132
});
3233

3334
Prism.languages.insertBefore('cpp', 'string', {
35+
'module': {
36+
// https://en.cppreference.com/w/cpp/language/modules
37+
pattern: RegExp(
38+
/(\b(?:module|import)\s+)/.source +
39+
'(?:' +
40+
// header-name
41+
/"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|<[^<>\r\n]*>/.source +
42+
'|' +
43+
// module name or partition or both
44+
/<mod-name>(?:\s*:\s*<mod-name>)?|:\s*<mod-name>/.source.replace(/<mod-name>/g, function () { return modName; }) +
45+
')'
46+
),
47+
lookbehind: true,
48+
greedy: true,
49+
inside: {
50+
'string': /^[<"][\s\S]+/,
51+
'operator': /:/,
52+
'punctuation': /\./
53+
}
54+
},
3455
'raw-string': {
3556
pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
3657
alias: 'string',

components/prism-cpp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/cpp/keyword_feature.test

+8
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ enum;
3333
explicit
3434
export
3535
extern
36+
final
3637
float
3738
for
3839
friend
3940
goto
4041
if
42+
import;
4143
inline
4244
int
4345
long
46+
module;
4447
mutable
4548
namespace
4649
new
4750
noexcept
4851
nullptr
4952
operator
53+
override
5054
private
5155
protected
5256
public
@@ -125,20 +129,24 @@ uint64_t
125129
["keyword", "explicit"],
126130
["keyword", "export"],
127131
["keyword", "extern"],
132+
["keyword", "final"],
128133
["keyword", "float"],
129134
["keyword", "for"],
130135
["keyword", "friend"],
131136
["keyword", "goto"],
132137
["keyword", "if"],
138+
["keyword", "import"], ["punctuation", ";"],
133139
["keyword", "inline"],
134140
["keyword", "int"],
135141
["keyword", "long"],
142+
["keyword", "module"], ["punctuation", ";"],
136143
["keyword", "mutable"],
137144
["keyword", "namespace"],
138145
["keyword", "new"],
139146
["keyword", "noexcept"],
140147
["keyword", "nullptr"],
141148
["keyword", "operator"],
149+
["keyword", "override"],
142150
["keyword", "private"],
143151
["keyword", "protected"],
144152
["keyword", "public"],
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
export module speech;
2+
3+
export const char* get_phrase_en() {
4+
return "Hello, world!";
5+
}
6+
7+
export module speech;
8+
9+
export import :english;
10+
export import :spanish;
11+
12+
export module speech:english;
13+
14+
import speech;
15+
import :PrivWidget;
16+
17+
import <iostream>;
18+
import <cstdlib>;
19+
import "foo.h";
20+
import <baz.h>;
21+
22+
module : private;
23+
24+
----------------------------------------------------
25+
26+
[
27+
["keyword", "export"],
28+
["keyword", "module"],
29+
["module", ["speech"]],
30+
["punctuation", ";"],
31+
32+
["keyword", "export"],
33+
["keyword", "const"],
34+
["keyword", "char"],
35+
["operator", "*"],
36+
["function", "get_phrase_en"],
37+
["punctuation", "("],
38+
["punctuation", ")"],
39+
["punctuation", "{"],
40+
41+
["keyword", "return"],
42+
["string", "\"Hello, world!\""],
43+
["punctuation", ";"],
44+
45+
["punctuation", "}"],
46+
47+
["keyword", "export"],
48+
["keyword", "module"],
49+
["module", ["speech"]],
50+
["punctuation", ";"],
51+
52+
["keyword", "export"],
53+
["keyword", "import"],
54+
["module", [
55+
["operator", ":"],
56+
"english"
57+
]],
58+
["punctuation", ";"],
59+
60+
["keyword", "export"],
61+
["keyword", "import"],
62+
["module", [
63+
["operator", ":"],
64+
"spanish"
65+
]],
66+
["punctuation", ";"],
67+
68+
["keyword", "export"],
69+
["keyword", "module"],
70+
["module", [
71+
"speech",
72+
["operator", ":"],
73+
"english"
74+
]],
75+
["punctuation", ";"],
76+
77+
["keyword", "import"],
78+
["module", ["speech"]],
79+
["punctuation", ";"],
80+
81+
["keyword", "import"],
82+
["module", [
83+
["operator", ":"],
84+
"PrivWidget"
85+
]],
86+
["punctuation", ";"],
87+
88+
["keyword", "import"],
89+
["module", [
90+
["string", "<iostream>"]
91+
]],
92+
["punctuation", ";"],
93+
94+
["keyword", "import"],
95+
["module", [
96+
["string", "<cstdlib>"]
97+
]],
98+
["punctuation", ";"],
99+
100+
["keyword", "import"],
101+
["module", [
102+
["string", "\"foo.h\""]
103+
]],
104+
["punctuation", ";"],
105+
106+
["keyword", "import"],
107+
["module", [
108+
["string", "<baz.h>"]
109+
]],
110+
["punctuation", ";"],
111+
112+
["keyword", "module"],
113+
["operator", ":"],
114+
["keyword", "private"],
115+
["punctuation", ";"]
116+
]

0 commit comments

Comments
 (0)