Skip to content

Commit

Permalink
C++: Added support for generic functions and made :: punctuation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Apr 3, 2021
1 parent 7e8cd40 commit 3df62fd
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 17 deletions.
26 changes: 24 additions & 2 deletions components/prism-cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
pattern: /(?:\b0b[01']+|\b0x(?:[\da-f']+(?:\.[\da-f']*)?|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+(?:\.[\d']*)?|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]{0,4}/i,
greedy: true
},
'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
'operator': />>=?|<<=?|->|--|\+\+|&&|\|\||[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
'boolean': /\b(?:true|false)\b/
});

Expand Down Expand Up @@ -59,6 +59,27 @@
}
});

Prism.languages.insertBefore('cpp', 'keyword', {
'generic-function': {
pattern: /\b[a-z_]\w*\s*<(?:[^<>]|<(?:[^<>])*>)*>(?=\s*\()/i,
inside: {
'function': /^\w+/,
'generic': {
pattern: /<[\s\S]+/,
alias: 'class-name',
inside: Prism.languages.cpp
}
}
}
});

Prism.languages.insertBefore('cpp', 'operator', {
'double-colon': {
pattern: /::/,
alias: 'punctuation'
}
});

Prism.languages.insertBefore('cpp', 'class-name', {
// the base clause is an optional list of parent classes
// https://en.cppreference.com/w/cpp/language/class
Expand All @@ -69,7 +90,8 @@
inside: Prism.languages.extend('cpp', {})
}
});
Prism.languages.insertBefore('inside', 'operator', {

Prism.languages.insertBefore('inside', 'double-colon', {
// All untokenized words that are not namespaces should be class names
'class-name': /\b[a-z_]\w*\b(?!\s*::)/i
}, Prism.languages.cpp['base-clause']);
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.

17 changes: 13 additions & 4 deletions tests/languages/cpp/base-clause_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class service : private Transport // comment
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "struct"],
["class-name", "Derived"],
["operator", ":"],
Expand All @@ -27,6 +28,7 @@ class service : private Transport // comment
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "struct"],
["class-name", "Derived"],
["operator", ":"],
Expand All @@ -35,6 +37,7 @@ class service : private Transport // comment
["class-name", "Base"]
]],
["punctuation", ";"],

["keyword", "class"],
["class-name", "X"],
["operator", ":"],
Expand All @@ -46,6 +49,7 @@ class service : private Transport // comment
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "Y"],
["operator", ":"],
Expand All @@ -57,18 +61,20 @@ class service : private Transport // comment
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "Y"],
["operator", ":"],
["base-clause", [
["keyword", "virtual"],
" baz",
["operator", "::"],
["double-colon", "::"],
["class-name", "B"]
]],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "Z"],
["operator", ":"],
Expand All @@ -77,11 +83,12 @@ class service : private Transport // comment
["class-name", "B"],
["operator", "<"],
"foo",
["operator", "::"],
["double-colon", "::"],
["class-name", "T"],
["operator", ">"]
]],
["punctuation", ";"],

["keyword", "struct"],
["class-name", "AA"],
["operator", ":"],
Expand All @@ -91,14 +98,15 @@ class service : private Transport // comment
["class-name", "Y"],
["punctuation", ","],
" foo",
["operator", "::"],
["double-colon", "::"],
"bar",
["operator", "::"],
["double-colon", "::"],
["class-name", "Z"]
]],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "service"],
["operator", ":"],
Expand All @@ -107,6 +115,7 @@ class service : private Transport // comment
["class-name", "Transport"],
["comment", "// comment"]
]],

["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"]
Expand Down
13 changes: 9 additions & 4 deletions tests/languages/cpp/class-name_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ concept Foo_bar
struct foo
enum bar
enum class FooBar

template<typename FooBar>

void Foo::bar() {}
Expand All @@ -19,20 +20,24 @@ void Foo<int>::bar() {}
["keyword", "struct"], ["class-name", "foo"],
["keyword", "enum"], ["class-name", "bar"],
["keyword", "enum"], ["keyword", "class"], ["class-name", "FooBar"],
["keyword", "template"], ["operator", "<"], ["keyword", "typename"], ["class-name", "FooBar"], ["operator", ">"],

["keyword", "template"],
["operator", "<"],
["keyword", "typename"],
["class-name", "FooBar"],
["operator", ">"],

["keyword", "void"],
["class-name", "Foo"],
["operator", "::"],
["double-colon", "::"],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],

["class-name", "Foo"],
["operator", "::"],
["double-colon", "::"],
["operator", "~"],
["function", "Foo"],
["punctuation", "("],
Expand All @@ -45,7 +50,7 @@ void Foo<int>::bar() {}
["operator", "<"],
["keyword", "int"],
["operator", ">"],
["operator", "::"],
["double-colon", "::"],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
Expand Down
93 changes: 93 additions & 0 deletions tests/languages/cpp/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
foo();
line.substr(0, separator_index);
boost::trim(key);
bpo::value<std::string>()
bpo::value<std::vector<std::string>>()->multitoken()
std::make_unique<service::UniqueMap>(std::move(entries));

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

[
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

"\r\nline",
["punctuation", "."],
["function", "substr"],
["punctuation", "("],
["number", "0"],
["punctuation", ","],
" separator_index",
["punctuation", ")"],
["punctuation", ";"],

"\r\nboost",
["double-colon", "::"],
["function", "trim"],
["punctuation", "("],
"key",
["punctuation", ")"],
["punctuation", ";"],

"\r\nbpo",
["double-colon", "::"],
["generic-function", [
["function", "value"],
["generic", [
["operator", "<"],
"std",
["double-colon", "::"],
"string",
["operator", ">"]
]]
]],
["punctuation", "("],
["punctuation", ")"],

"\r\nbpo",
["double-colon", "::"],
["generic-function", [
["function", "value"],
["generic", [
["operator", "<"],
"std",
["double-colon", "::"],
"vector",
["operator", "<"],
"std",
["double-colon", "::"],
"string",
["operator", ">>"]
]]
]],
["punctuation", "("],
["punctuation", ")"],
["operator", "->"],
["function", "multitoken"],
["punctuation", "("],
["punctuation", ")"],

"\r\nstd",
["double-colon", "::"],
["generic-function", [
["function", "make_unique"],
["generic", [
["operator", "<"],
"service",
["double-colon", "::"],
"UniqueMap",
["operator", ">"]
]]
]],
["punctuation", "("],
"std",
["double-colon", "::"],
["function", "move"],
["punctuation", "("],
"entries",
["punctuation", ")"],
["punctuation", ")"],
["punctuation", ";"]
]
6 changes: 2 additions & 4 deletions tests/languages/cpp/issue2347.test
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void MainWindow::changeWindowTitle()

["keyword", "void"],
["class-name", "MainWindow"],
["operator", "::"],
["double-colon", "::"],
["function", "changeWindowTitle"],
["punctuation", "("],
["punctuation", ")"],
Expand All @@ -67,6 +67,4 @@ void MainWindow::changeWindowTitle()
["punctuation", ";"],

["punctuation", "}"]
]

----------------------------------------------------
]
3 changes: 1 addition & 2 deletions tests/languages/cpp/operator_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
~ & | ^
+= -= *= /= %= >>= <<= &= |= ^=
! && ||
-> ::
->
? :
= == != < > <= >= <=>
and and_eq bitand bitor not not_eq or or_eq xor xor_eq
Expand Down Expand Up @@ -43,7 +43,6 @@ and and_eq bitand bitor not not_eq or or_eq xor xor_eq
["operator", "||"],

["operator", "->"],
["operator", "::"],

["operator", "?"],
["operator", ":"],
Expand Down
18 changes: 18 additions & 0 deletions tests/languages/cpp/punctuation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
( ) [ ] { }
, ; . ::

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

[
["punctuation", "("],
["punctuation", ")"],
["punctuation", "["],
["punctuation", "]"],
["punctuation", "{"],
["punctuation", "}"],

["punctuation", ","],
["punctuation", ";"],
["punctuation", "."],
["double-colon", "::"]
]

0 comments on commit 3df62fd

Please sign in to comment.