Skip to content

Commit 3df62fd

Browse files
C++: Added support for generic functions and made :: punctuation (#2814)
1 parent 7e8cd40 commit 3df62fd

8 files changed

+161
-17
lines changed

components/prism-cpp.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
pattern: /(?:\b0b[01']+|\b0x(?:[\da-f']+(?:\.[\da-f']*)?|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+(?:\.[\d']*)?|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]{0,4}/i,
2828
greedy: true
2929
},
30-
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
30+
'operator': />>=?|<<=?|->|--|\+\+|&&|\|\||[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
3131
'boolean': /\b(?:true|false)\b/
3232
});
3333

@@ -59,6 +59,27 @@
5959
}
6060
});
6161

62+
Prism.languages.insertBefore('cpp', 'keyword', {
63+
'generic-function': {
64+
pattern: /\b[a-z_]\w*\s*<(?:[^<>]|<(?:[^<>])*>)*>(?=\s*\()/i,
65+
inside: {
66+
'function': /^\w+/,
67+
'generic': {
68+
pattern: /<[\s\S]+/,
69+
alias: 'class-name',
70+
inside: Prism.languages.cpp
71+
}
72+
}
73+
}
74+
});
75+
76+
Prism.languages.insertBefore('cpp', 'operator', {
77+
'double-colon': {
78+
pattern: /::/,
79+
alias: 'punctuation'
80+
}
81+
});
82+
6283
Prism.languages.insertBefore('cpp', 'class-name', {
6384
// the base clause is an optional list of parent classes
6485
// https://en.cppreference.com/w/cpp/language/class
@@ -69,7 +90,8 @@
6990
inside: Prism.languages.extend('cpp', {})
7091
}
7192
});
72-
Prism.languages.insertBefore('inside', 'operator', {
93+
94+
Prism.languages.insertBefore('inside', 'double-colon', {
7395
// All untokenized words that are not namespaces should be class names
7496
'class-name': /\b[a-z_]\w*\b(?!\s*::)/i
7597
}, Prism.languages.cpp['base-clause']);

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/base-clause_feature.test

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class service : private Transport // comment
1818
["punctuation", "{"],
1919
["punctuation", "}"],
2020
["punctuation", ";"],
21+
2122
["keyword", "struct"],
2223
["class-name", "Derived"],
2324
["operator", ":"],
@@ -27,6 +28,7 @@ class service : private Transport // comment
2728
["punctuation", "{"],
2829
["punctuation", "}"],
2930
["punctuation", ";"],
31+
3032
["keyword", "struct"],
3133
["class-name", "Derived"],
3234
["operator", ":"],
@@ -35,6 +37,7 @@ class service : private Transport // comment
3537
["class-name", "Base"]
3638
]],
3739
["punctuation", ";"],
40+
3841
["keyword", "class"],
3942
["class-name", "X"],
4043
["operator", ":"],
@@ -46,6 +49,7 @@ class service : private Transport // comment
4649
["punctuation", "{"],
4750
["punctuation", "}"],
4851
["punctuation", ";"],
52+
4953
["keyword", "class"],
5054
["class-name", "Y"],
5155
["operator", ":"],
@@ -57,18 +61,20 @@ class service : private Transport // comment
5761
["punctuation", "{"],
5862
["punctuation", "}"],
5963
["punctuation", ";"],
64+
6065
["keyword", "class"],
6166
["class-name", "Y"],
6267
["operator", ":"],
6368
["base-clause", [
6469
["keyword", "virtual"],
6570
" baz",
66-
["operator", "::"],
71+
["double-colon", "::"],
6772
["class-name", "B"]
6873
]],
6974
["punctuation", "{"],
7075
["punctuation", "}"],
7176
["punctuation", ";"],
77+
7278
["keyword", "class"],
7379
["class-name", "Z"],
7480
["operator", ":"],
@@ -77,11 +83,12 @@ class service : private Transport // comment
7783
["class-name", "B"],
7884
["operator", "<"],
7985
"foo",
80-
["operator", "::"],
86+
["double-colon", "::"],
8187
["class-name", "T"],
8288
["operator", ">"]
8389
]],
8490
["punctuation", ";"],
91+
8592
["keyword", "struct"],
8693
["class-name", "AA"],
8794
["operator", ":"],
@@ -91,14 +98,15 @@ class service : private Transport // comment
9198
["class-name", "Y"],
9299
["punctuation", ","],
93100
" foo",
94-
["operator", "::"],
101+
["double-colon", "::"],
95102
"bar",
96-
["operator", "::"],
103+
["double-colon", "::"],
97104
["class-name", "Z"]
98105
]],
99106
["punctuation", "{"],
100107
["punctuation", "}"],
101108
["punctuation", ";"],
109+
102110
["keyword", "class"],
103111
["class-name", "service"],
104112
["operator", ":"],
@@ -107,6 +115,7 @@ class service : private Transport // comment
107115
["class-name", "Transport"],
108116
["comment", "// comment"]
109117
]],
118+
110119
["punctuation", "{"],
111120
["punctuation", "}"],
112121
["punctuation", ";"]

tests/languages/cpp/class-name_feature.test

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ concept Foo_bar
44
struct foo
55
enum bar
66
enum class FooBar
7+
78
template<typename FooBar>
89

910
void Foo::bar() {}
@@ -19,20 +20,24 @@ void Foo<int>::bar() {}
1920
["keyword", "struct"], ["class-name", "foo"],
2021
["keyword", "enum"], ["class-name", "bar"],
2122
["keyword", "enum"], ["keyword", "class"], ["class-name", "FooBar"],
22-
["keyword", "template"], ["operator", "<"], ["keyword", "typename"], ["class-name", "FooBar"], ["operator", ">"],
2323

24+
["keyword", "template"],
25+
["operator", "<"],
26+
["keyword", "typename"],
27+
["class-name", "FooBar"],
28+
["operator", ">"],
2429

2530
["keyword", "void"],
2631
["class-name", "Foo"],
27-
["operator", "::"],
32+
["double-colon", "::"],
2833
["function", "bar"],
2934
["punctuation", "("],
3035
["punctuation", ")"],
3136
["punctuation", "{"],
3237
["punctuation", "}"],
3338

3439
["class-name", "Foo"],
35-
["operator", "::"],
40+
["double-colon", "::"],
3641
["operator", "~"],
3742
["function", "Foo"],
3843
["punctuation", "("],
@@ -45,7 +50,7 @@ void Foo<int>::bar() {}
4550
["operator", "<"],
4651
["keyword", "int"],
4752
["operator", ">"],
48-
["operator", "::"],
53+
["double-colon", "::"],
4954
["function", "bar"],
5055
["punctuation", "("],
5156
["punctuation", ")"],
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
foo();
2+
line.substr(0, separator_index);
3+
boost::trim(key);
4+
bpo::value<std::string>()
5+
bpo::value<std::vector<std::string>>()->multitoken()
6+
std::make_unique<service::UniqueMap>(std::move(entries));
7+
8+
----------------------------------------------------
9+
10+
[
11+
["function", "foo"],
12+
["punctuation", "("],
13+
["punctuation", ")"],
14+
["punctuation", ";"],
15+
16+
"\r\nline",
17+
["punctuation", "."],
18+
["function", "substr"],
19+
["punctuation", "("],
20+
["number", "0"],
21+
["punctuation", ","],
22+
" separator_index",
23+
["punctuation", ")"],
24+
["punctuation", ";"],
25+
26+
"\r\nboost",
27+
["double-colon", "::"],
28+
["function", "trim"],
29+
["punctuation", "("],
30+
"key",
31+
["punctuation", ")"],
32+
["punctuation", ";"],
33+
34+
"\r\nbpo",
35+
["double-colon", "::"],
36+
["generic-function", [
37+
["function", "value"],
38+
["generic", [
39+
["operator", "<"],
40+
"std",
41+
["double-colon", "::"],
42+
"string",
43+
["operator", ">"]
44+
]]
45+
]],
46+
["punctuation", "("],
47+
["punctuation", ")"],
48+
49+
"\r\nbpo",
50+
["double-colon", "::"],
51+
["generic-function", [
52+
["function", "value"],
53+
["generic", [
54+
["operator", "<"],
55+
"std",
56+
["double-colon", "::"],
57+
"vector",
58+
["operator", "<"],
59+
"std",
60+
["double-colon", "::"],
61+
"string",
62+
["operator", ">>"]
63+
]]
64+
]],
65+
["punctuation", "("],
66+
["punctuation", ")"],
67+
["operator", "->"],
68+
["function", "multitoken"],
69+
["punctuation", "("],
70+
["punctuation", ")"],
71+
72+
"\r\nstd",
73+
["double-colon", "::"],
74+
["generic-function", [
75+
["function", "make_unique"],
76+
["generic", [
77+
["operator", "<"],
78+
"service",
79+
["double-colon", "::"],
80+
"UniqueMap",
81+
["operator", ">"]
82+
]]
83+
]],
84+
["punctuation", "("],
85+
"std",
86+
["double-colon", "::"],
87+
["function", "move"],
88+
["punctuation", "("],
89+
"entries",
90+
["punctuation", ")"],
91+
["punctuation", ")"],
92+
["punctuation", ";"]
93+
]

tests/languages/cpp/issue2347.test

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void MainWindow::changeWindowTitle()
4040

4141
["keyword", "void"],
4242
["class-name", "MainWindow"],
43-
["operator", "::"],
43+
["double-colon", "::"],
4444
["function", "changeWindowTitle"],
4545
["punctuation", "("],
4646
["punctuation", ")"],
@@ -67,6 +67,4 @@ void MainWindow::changeWindowTitle()
6767
["punctuation", ";"],
6868

6969
["punctuation", "}"]
70-
]
71-
72-
----------------------------------------------------
70+
]

tests/languages/cpp/operator_feature.test

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
~ & | ^
44
+= -= *= /= %= >>= <<= &= |= ^=
55
! && ||
6-
-> ::
6+
->
77
? :
88
= == != < > <= >= <=>
99
and and_eq bitand bitor not not_eq or or_eq xor xor_eq
@@ -43,7 +43,6 @@ and and_eq bitand bitor not not_eq or or_eq xor xor_eq
4343
["operator", "||"],
4444

4545
["operator", "->"],
46-
["operator", "::"],
4746

4847
["operator", "?"],
4948
["operator", ":"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
( ) [ ] { }
2+
, ; . ::
3+
4+
----------------------------------------------------
5+
6+
[
7+
["punctuation", "("],
8+
["punctuation", ")"],
9+
["punctuation", "["],
10+
["punctuation", "]"],
11+
["punctuation", "{"],
12+
["punctuation", "}"],
13+
14+
["punctuation", ","],
15+
["punctuation", ";"],
16+
["punctuation", "."],
17+
["double-colon", "::"]
18+
]

0 commit comments

Comments
 (0)