🌐 AI搜索 & 代理 主页
Skip to content

Commit ee8cf44

Browse files
authored
enh(cpp) Misc fixes and improvements for function detection
- enh(cpp) Properly detect decltype(auto) - enh(cpp) recognize primitive types (`int8_t`, etc.) as function types - enh(cpp) Detect namespaced function types (`A::typeName func(...)`) - enh(cpp) Detect namespaced functions also (`A::functionName`) - enh(cpp) template type in function declaration (`vector<int> func(...)`) Closes #1730. Closes #1222. Closes #1502.
1 parent b33ef93 commit ee8cf44

File tree

4 files changed

+123
-58
lines changed

4 files changed

+123
-58
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Language Improvements:
1919
- (ini) support TOML arrays, clean up grammar (#2335) [Josh Goebel][]
2020
- (vbnet) add nameof operator to the keywords (#2329) [Youssef Victor][]
2121
- (stan) updated with improved coverage of language keywords and patterns. (#1829) [Jeffrey Arnold][]
22+
- enh(cpp) Detect namespaced function types (`A::typeName func(...)`) (#2332) [Josh Goebel][]
23+
- enh(cpp) Detect namespaced functions also (`A::functionName`) (#2332) [Josh Goebel][]
24+
- enh(cpp) Properly detect decltype(auto) (#2332) [Josh Goebel][]
25+
- enh(cpp) recognize primitive types (`int8_t`, etc.) as function types (#2332) [Josh Goebel][]
2226

2327
Developer Tools:
2428

src/languages/cpp.js

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ Website: https://isocpp.org
77
*/
88

99
function(hljs) {
10+
function optional(s) {
11+
return '(?:' + s + ')?';
12+
}
13+
var DECLTYPE_AUTO_RE = 'decltype\\(auto\\)'
14+
var NAMESPACE_RE = '[a-zA-Z_]\\w*::'
15+
var TEMPLATE_ARGUMENT_RE = '<.*?>';
16+
var FUNCTION_TYPE_RE = '(' +
17+
DECLTYPE_AUTO_RE + '|' +
18+
optional(NAMESPACE_RE) +'[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
19+
')';
1020
var CPP_PRIMITIVE_TYPES = {
1121
className: 'keyword',
1222
begin: '\\b[a-z\\d_]*_t\\b'
@@ -64,7 +74,13 @@ function(hljs) {
6474
]
6575
};
6676

67-
var FUNCTION_TITLE = hljs.IDENT_RE + '\\s*\\(';
77+
var TITLE_MODE = {
78+
className: 'title',
79+
begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
80+
relevance: 0
81+
};
82+
83+
var FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
6884

6985
var CPP_KEYWORDS = {
7086
keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
@@ -99,86 +115,99 @@ function(hljs) {
99115
STRINGS
100116
];
101117

102-
return {
103-
aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
118+
var EXPRESSION_CONTEXT = {
119+
// This mode covers expression context where we can't expect a function
120+
// definition and shouldn't highlight anything that looks like one:
121+
// `return some()`, `else if()`, `(x*sum(1, 2))`
122+
variants: [
123+
{begin: /=/, end: /;/},
124+
{begin: /\(/, end: /\)/},
125+
{beginKeywords: 'new throw return else', end: /;/}
126+
],
104127
keywords: CPP_KEYWORDS,
105-
illegal: '</',
106128
contains: EXPRESSION_CONTAINS.concat([
107-
PREPROCESSOR,
108129
{
109-
begin: '\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
130+
begin: /\(/, end: /\)/,
110131
keywords: CPP_KEYWORDS,
111-
contains: ['self', CPP_PRIMITIVE_TYPES]
112-
},
113-
{
114-
begin: hljs.IDENT_RE + '::',
115-
keywords: CPP_KEYWORDS
132+
contains: EXPRESSION_CONTAINS.concat(['self']),
133+
relevance: 0
134+
}
135+
]),
136+
relevance: 0
137+
};
138+
139+
var FUNCTION_DECLARATION = {
140+
className: 'function',
141+
begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
142+
returnBegin: true, end: /[{;=]/,
143+
excludeEnd: true,
144+
keywords: CPP_KEYWORDS,
145+
illegal: /[^\w\s\*&:<>]/,
146+
contains: [
147+
148+
{ // to prevent it from being confused as the function title
149+
begin: DECLTYPE_AUTO_RE,
150+
keywords: CPP_KEYWORDS,
151+
relevance: 0,
116152
},
117153
{
118-
// This mode covers expression context where we can't expect a function
119-
// definition and shouldn't highlight anything that looks like one:
120-
// `return some()`, `else if()`, `(x*sum(1, 2))`
121-
variants: [
122-
{begin: /=/, end: /;/},
123-
{begin: /\(/, end: /\)/},
124-
{beginKeywords: 'new throw return else', end: /;/}
125-
],
126-
keywords: CPP_KEYWORDS,
127-
contains: EXPRESSION_CONTAINS.concat([
128-
{
129-
begin: /\(/, end: /\)/,
130-
keywords: CPP_KEYWORDS,
131-
contains: EXPRESSION_CONTAINS.concat(['self']),
132-
relevance: 0
133-
}
134-
]),
154+
begin: FUNCTION_TITLE, returnBegin: true,
155+
contains: [TITLE_MODE],
135156
relevance: 0
136157
},
137158
{
138-
className: 'function',
139-
begin: '(' + hljs.IDENT_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
140-
returnBegin: true, end: /[{;=]/,
141-
excludeEnd: true,
159+
className: 'params',
160+
begin: /\(/, end: /\)/,
142161
keywords: CPP_KEYWORDS,
143-
illegal: /[^\w\s\*&]/,
162+
relevance: 0,
144163
contains: [
164+
hljs.C_LINE_COMMENT_MODE,
165+
hljs.C_BLOCK_COMMENT_MODE,
166+
STRINGS,
167+
NUMBERS,
168+
CPP_PRIMITIVE_TYPES,
169+
// Count matching parentheses.
145170
{
146-
begin: FUNCTION_TITLE, returnBegin: true,
147-
contains: [hljs.TITLE_MODE],
148-
relevance: 0
149-
},
150-
{
151-
className: 'params',
152171
begin: /\(/, end: /\)/,
153172
keywords: CPP_KEYWORDS,
154173
relevance: 0,
155174
contains: [
175+
'self',
156176
hljs.C_LINE_COMMENT_MODE,
157177
hljs.C_BLOCK_COMMENT_MODE,
158178
STRINGS,
159179
NUMBERS,
160-
CPP_PRIMITIVE_TYPES,
161-
// Count matching parentheses.
162-
{
163-
begin: /\(/, end: /\)/,
164-
keywords: CPP_KEYWORDS,
165-
relevance: 0,
166-
contains: [
167-
'self',
168-
hljs.C_LINE_COMMENT_MODE,
169-
hljs.C_BLOCK_COMMENT_MODE,
170-
STRINGS,
171-
NUMBERS,
172-
CPP_PRIMITIVE_TYPES
173-
]
174-
}
180+
CPP_PRIMITIVE_TYPES
175181
]
176-
},
177-
hljs.C_LINE_COMMENT_MODE,
178-
hljs.C_BLOCK_COMMENT_MODE,
179-
PREPROCESSOR
182+
}
180183
]
181184
},
185+
CPP_PRIMITIVE_TYPES,
186+
hljs.C_LINE_COMMENT_MODE,
187+
hljs.C_BLOCK_COMMENT_MODE,
188+
PREPROCESSOR
189+
]
190+
};
191+
192+
return {
193+
aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
194+
keywords: CPP_KEYWORDS,
195+
illegal: '</',
196+
contains: [].concat(
197+
EXPRESSION_CONTEXT,
198+
FUNCTION_DECLARATION,
199+
EXPRESSION_CONTAINS,
200+
[
201+
PREPROCESSOR,
202+
{
203+
begin: '\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
204+
keywords: CPP_KEYWORDS,
205+
contains: ['self', CPP_PRIMITIVE_TYPES]
206+
},
207+
{
208+
begin: hljs.IDENT_RE + '::',
209+
keywords: CPP_KEYWORDS
210+
},
182211
{
183212
className: 'class',
184213
beginKeywords: 'class struct', end: /[{;:]/,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<span class="hljs-function"><span class="hljs-keyword">decltype</span>(<span class="hljs-keyword">auto</span>) <span class="hljs-title">look_up_a_string_1</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> lookup1(); }
2+
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">look_up_a_string_2</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> lookup2(); }
3+
<span class="hljs-function"><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> <span class="hljs-title">A::showB</span><span class="hljs-params">(B x)</span> </span>{}
4+
<span class="hljs-function"><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> <span class="hljs-title">showB</span><span class="hljs-params">(B x)</span> </span>{}
5+
<span class="hljs-function"><span class="hljs-keyword">friend</span> <span class="hljs-keyword">void</span> <span class="hljs-title">showB</span><span class="hljs-params">(B::SomeType x)</span> </span>{}
6+
<span class="hljs-function"><span class="hljs-keyword">inline</span> <span class="hljs-keyword">int</span> <span class="hljs-title">add</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span> </span>{}
7+
<span class="hljs-function">int8t <span class="hljs-title">Get_Tile_Value</span><span class="hljs-params">()</span> </span>{}
8+
9+
<span class="hljs-function"><span class="hljs-keyword">int8_t</span> <span class="hljs-title">Get_Tile_Value</span><span class="hljs-params">()</span> </span>{}
10+
11+
<span class="hljs-function">B::type <span class="hljs-title">test</span><span class="hljs-params">()</span> </span>{};
12+
13+
<span class="hljs-comment">// template</span>
14+
<span class="hljs-function">boost::optional&lt;application&gt; <span class="hljs-title">handle_key</span><span class="hljs-params">(application state, key_code key, coord size)</span></span>;
15+
16+
test();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
decltype(auto) look_up_a_string_1() { return lookup1(); }
2+
void look_up_a_string_2() { return lookup2(); }
3+
friend void A::showB(B x) {}
4+
friend void showB(B x) {}
5+
friend void showB(B::SomeType x) {}
6+
inline int add(int a, int b) {}
7+
int8t Get_Tile_Value() {}
8+
9+
int8_t Get_Tile_Value() {}
10+
11+
B::type test() {};
12+
13+
// template
14+
boost::optional<application> handle_key(application state, key_code key, coord size);
15+
16+
test();

0 commit comments

Comments
 (0)