]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/cpp.js
1 /*! `cpp` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
8 Category: common, system
9 Website: https://isocpp.org
12 /** @type LanguageFn */
14 const regex
= hljs
.regex
;
15 // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
16 // not include such support nor can we be sure all the grammars depending
17 // on it would desire this behavior
18 const C_LINE_COMMENT_MODE
= hljs
.COMMENT('//', '$', { contains: [ { begin: /\\\n/ } ] });
19 const DECLTYPE_AUTO_RE
= 'decltype\\(auto\\)';
20 const NAMESPACE_RE
= '[a-zA-Z_]\\w*::';
21 const TEMPLATE_ARGUMENT_RE
= '<[^<>]+>';
22 const FUNCTION_TYPE_RE
= '(?!struct)('
23 + DECLTYPE_AUTO_RE
+ '|'
24 + regex
.optional(NAMESPACE_RE
)
25 + '[a-zA-Z_]\\w*' + regex
.optional(TEMPLATE_ARGUMENT_RE
)
28 const CPP_PRIMITIVE_TYPES
= {
30 begin: '\\b[a-z\\d_]*_t\\b'
33 // https://en.cppreference.com/w/cpp/language/escape
34 // \\ \x \xFF \u2837 \u00323747 \374
35 const CHARACTER_ESCAPES
= '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
43 contains: [ hljs
.BACKSLASH_ESCAPE
]
46 begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES
+ '|.)',
50 hljs
.END_SAME_AS_BEGIN({
51 begin: /(?:u8
?|U
|L
)?R
"([^()\\ ]{0,16})\(/,
52 end: /\)([^()\\ ]{0,16})"/
60 // Floating-point literal.
62 "[+-]?(?:" // Leading sign.
65 +"[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"
66 + "|\\.[0-9](?:'?[0-9])*"
67 + ")(?:[Ee][+-]?[0-9](?:'?[0-9])*)?"
68 + "|[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"
71 +"[0-9A-Fa-f](?:'?[0-9A-Fa-f])*(?:\\.(?:[0-9A-Fa-f](?:'?[0-9A-Fa-f])*)?)?"
72 + "|\\.[0-9A-Fa-f](?:'?[0-9A-Fa-f])*"
73 + ")[Pp][+-]?[0-9](?:'?[0-9])*"
74 + ")(?:" // Literal suffixes.
75 + "[Ff](?:16|32|64|128)?"
78 + "|" // Literal suffix is optional.
83 "[+-]?\\b(?:" // Leading sign.
84 + "0[Bb][01](?:'?[01])*" // Binary.
85 + "|0[Xx][0-9A-Fa-f](?:'?[0-9A-Fa-f])*" // Hexadecimal.
86 + "|0(?:'?[0-7])*" // Octal or just a lone zero.
87 + "|[1-9](?:'?[0-9])*" // Decimal.
88 + ")(?:" // Literal suffixes.
93 + "|" // Literal suffix is optional.
95 // Note: there are user-defined literal suffixes too, but perhaps having the custom suffix not part of the
96 // literal highlight actually makes it stand out more.
102 const PREPROCESSOR
= {
104 begin: /#\s*[a-z]+\b/,
107 'if else elif endif define undef warning error line '
108 + 'pragma _Pragma ifdef ifndef include' },
114 hljs
.inherit(STRINGS
, { className: 'string' }),
120 hljs
.C_BLOCK_COMMENT_MODE
126 begin: regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
,
130 const FUNCTION_TITLE
= regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
+ '\\s*\\(';
132 // https://en.cppreference.com/w/cpp/keyword
133 const RESERVED_KEYWORDS
= [
194 'reinterpret_cast|10',
208 'transaction_safe_dynamic',
223 // https://en.cppreference.com/w/cpp/keyword
224 const RESERVED_TYPES
= [
250 'condition_variable',
251 'condition_variable_any',
252 'counting_semaphore',
275 'recursive_timed_mutex',
281 'shared_timed_mutex',
293 'unordered_multimap',
294 'unordered_multiset',
303 const FUNCTION_HINTS
= [
354 'make_shared_for_overwrite',
414 // https://en.cppreference.com/w/cpp/keyword
415 const BUILT_IN
= [ '_Pragma' ];
417 const CPP_KEYWORDS
= {
418 type: RESERVED_TYPES
,
419 keyword: RESERVED_KEYWORDS
,
422 _type_hints: TYPE_HINTS
425 const FUNCTION_DISPATCH
= {
426 className: 'function.dispatch',
429 // Only for relevance, not highlighting.
430 _hint: FUNCTION_HINTS
},
439 regex
.lookahead(/(<[^<>]+>|)\s*\(/))
442 const EXPRESSION_CONTAINS
= [
447 hljs
.C_BLOCK_COMMENT_MODE
,
452 const EXPRESSION_CONTEXT
= {
453 // This mode covers expression context where we can't expect a function
454 // definition and shouldn't highlight anything that looks like one:
455 // `return some()`, `else if()`, `(x*sum(1, 2))`
466 beginKeywords: 'new throw return else',
470 keywords: CPP_KEYWORDS
,
471 contains: EXPRESSION_CONTAINS
.concat([
475 keywords: CPP_KEYWORDS
,
476 contains: EXPRESSION_CONTAINS
.concat([ 'self' ]),
483 const FUNCTION_DECLARATION
= {
484 className: 'function',
485 begin: '(' + FUNCTION_TYPE_RE
+ '[\\*&\\s]+)+' + FUNCTION_TITLE
,
489 keywords: CPP_KEYWORDS
,
490 illegal: /[^\w\s\*&:<>.]/,
492 { // to prevent it from being confused as the function title
493 begin: DECLTYPE_AUTO_RE
,
494 keywords: CPP_KEYWORDS
,
498 begin: FUNCTION_TITLE
,
500 contains: [ TITLE_MODE
],
503 // needed because we do not have look-behind on the below rule
504 // to prevent it from grabbing the final : in a :: pair
512 endsWithParent: true,
518 // allow for multiple declarations, e.g.:
519 // extern void f(int), g(char);
528 keywords: CPP_KEYWORDS
,
532 hljs
.C_BLOCK_COMMENT_MODE
,
536 // Count matching parentheses.
540 keywords: CPP_KEYWORDS
,
545 hljs
.C_BLOCK_COMMENT_MODE
,
555 hljs
.C_BLOCK_COMMENT_MODE
,
571 keywords: CPP_KEYWORDS
,
573 classNameAliases: { 'function.dispatch': 'built_in' },
576 FUNCTION_DECLARATION
,
581 { // containers: ie, `vector <int> rooms (9);`
582 begin: '\\b(deque|list|queue|priority_queue|pair|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array|tuple|optional|variant|function|flat_map|flat_set)\\s*<(?!<)',
584 keywords: CPP_KEYWORDS
,
591 begin: hljs
.IDENT_RE
+ '::',
592 keywords: CPP_KEYWORDS
596 // extra complexity to deal with `enum class` and `enum struct`
597 /\b(?:enum(?:\s+(?:class|struct))?|class|struct|union)/,
614 hljs
.registerLanguage('cpp', hljsGrammar
);