Skip to content

Commit

Permalink
detect namespaced function types
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Dec 23, 2019
1 parent 2ebab5f commit 2027309
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,7 @@ Core Changes:

Language Improvements:

- enh(cpp) Detect namespaced function types (`A::typeName func(...)`) (#2332) [Josh Goebel][]
- enh(cpp) Detect namespaced functions also (`A::functionName`) (#2332) [Josh Goebel][]
- enh(cpp) Properly detect decltype(auto) (#2332) [Josh Goebel][]
- enh(cpp) recognize primitive types (`int8_t`, etc.) as function types (#2332) [Josh Goebel][]
Expand Down
19 changes: 13 additions & 6 deletions src/languages/cpp.js
Expand Up @@ -7,8 +7,13 @@ Website: https://isocpp.org
*/

function(hljs) {
function optional(s) {
return '(' + s + ')?';
}
var DECLTYPE_AUTO_RE = 'decltype\\(auto\\)'
var FUNCTION_TYPE_RE = '(' + DECLTYPE_AUTO_RE + '|[a-zA-Z_]\\w*)';
var NAMESPACE_RE = '[a-zA-Z_]\\w*::'
// (<.*?>)?
var FUNCTION_TYPE_RE = '(' + DECLTYPE_AUTO_RE + '|' + optional(NAMESPACE_RE) +'[a-zA-Z_]\\w*)';
var CPP_PRIMITIVE_TYPES = {
className: 'keyword',
begin: '\\b[a-z\\d_]*_t\\b'
Expand Down Expand Up @@ -68,11 +73,11 @@ function(hljs) {

var TITLE_MODE = {
className: 'title',
begin: '(' + hljs.IDENT_RE + '::)?' + hljs.IDENT_RE,
begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
relevance: 0
};

var FUNCTION_TITLE = '(' + hljs.IDENT_RE + '::)?' + hljs.IDENT_RE + '\\s*\\(';
var FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';

var CPP_KEYWORDS = {
keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
Expand Down Expand Up @@ -134,11 +139,13 @@ function(hljs) {
returnBegin: true, end: /[{;=]/,
excludeEnd: true,
keywords: CPP_KEYWORDS,
illegal: /[^\w\s\*&]/,
illegal: /[^\w\s\*&:]/,
contains: [
{

{ // to prevent it from being confused as the function title
begin: DECLTYPE_AUTO_RE,
keywords: CPP_KEYWORDS
keywords: CPP_KEYWORDS,
relevance: 0,
},
{
begin: FUNCTION_TITLE, returnBegin: true,
Expand Down
3 changes: 3 additions & 0 deletions test/markup/cpp/function-declarations.expect.txt
Expand Up @@ -5,6 +5,9 @@
<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>{}
<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>{}
<span class="hljs-function">int8t <span class="hljs-title">Get_Tile_Value</span><span class="hljs-params">()</span> </span>{}

<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>{}

<span class="hljs-function">B::type <span class="hljs-title">test</span><span class="hljs-params">()</span> </span>{};

test();
3 changes: 3 additions & 0 deletions test/markup/cpp/function-declarations.txt
Expand Up @@ -5,6 +5,9 @@ friend void showB(B x) {}
friend void showB(B::SomeType x) {}
inline int add(int a, int b) {}
int8t Get_Tile_Value() {}

int8_t Get_Tile_Value() {}

B::type test() {};

test();

0 comments on commit 2027309

Please sign in to comment.