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 generic functions and made :: punctuation #2814

Merged
merged 4 commits into from Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion components/prism-cpp.js
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,6 +90,7 @@
inside: Prism.languages.extend('cpp', {})
}
});

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

25 changes: 17 additions & 8 deletions tests/languages/cpp/base-clause_feature.test
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,31 +61,34 @@ class service : private Transport // comment
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "Y"],
["operator", ":"],
["base-clause", [
["keyword", "virtual"],
" baz",
["operator", "::"],
["class-name", "baz"],
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
["double-colon", "::"],
["class-name", "B"]
]],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "class"],
["class-name", "Z"],
["operator", ":"],
["base-clause", [
["keyword", "public"],
["class-name", "B"],
["operator", "<"],
"foo",
["operator", "::"],
["class-name", "foo"],
["double-colon", "::"],
["class-name", "T"],
["operator", ">"]
]],
["punctuation", ";"],

["keyword", "struct"],
["class-name", "AA"],
["operator", ":"],
Expand All @@ -90,15 +97,16 @@ class service : private Transport // comment
["punctuation", ","],
["class-name", "Y"],
["punctuation", ","],
" foo",
["operator", "::"],
"bar",
["operator", "::"],
["class-name", "foo"],
["double-colon", "::"],
["class-name", "bar"],
["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
36 changes: 26 additions & 10 deletions tests/languages/cpp/class-name_feature.test
Expand Up @@ -13,26 +13,42 @@ void Foo<int>::bar() {}
----------------------------------------------------

[
["keyword", "class"], ["class-name", "Foo"],
["keyword", "class"], ["class-name", "Foo_bar"],
["keyword", "concept"], ["class-name", "Foo_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", "class"],
["class-name", "Foo"],

["keyword", "class"],
["class-name", "Foo_bar"],

["keyword", "concept"],
["class-name", "Foo_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", "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 +61,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
@@ -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
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
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
@@ -0,0 +1,18 @@
( ) [ ] { }
, ; . ::

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

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

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