]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/cpp.js
1 /*! `cpp` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar
= (function () {
7 Category: common, system
8 Website: https://isocpp.org
11 /** @type LanguageFn */
13 const regex
= hljs
.regex
;
14 // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
15 // not include such support nor can we be sure all the grammars depending
16 // on it would desire this behavior
17 const C_LINE_COMMENT_MODE
= hljs
.COMMENT('//', '$', { contains: [ { begin: /\\\n/ } ] });
18 const DECLTYPE_AUTO_RE
= 'decltype\\(auto\\)';
19 const NAMESPACE_RE
= '[a-zA-Z_]\\w*::';
20 const TEMPLATE_ARGUMENT_RE
= '<[^<>]+>';
21 const FUNCTION_TYPE_RE
= '(?!struct)('
22 + DECLTYPE_AUTO_RE
+ '|'
23 + regex
.optional(NAMESPACE_RE
)
24 + '[a-zA-Z_]\\w*' + regex
.optional(TEMPLATE_ARGUMENT_RE
)
27 const CPP_PRIMITIVE_TYPES
= {
29 begin: '\\b[a-z\\d_]*_t\\b'
32 // https://en.cppreference.com/w/cpp/language/escape
33 // \\ \x \xFF \u2837 \u00323747 \374
34 const CHARACTER_ESCAPES
= '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
42 contains: [ hljs
.BACKSLASH_ESCAPE
]
45 begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES
+ '|.)',
49 hljs
.END_SAME_AS_BEGIN({
50 begin: /(?:u8
?|U
|L
)?R
"([^()\\ ]{0,16})\(/,
51 end: /\)([^()\\ ]{0,16})"/
59 // Floating-point literal.
61 "[+-]?(?:" // Leading sign.
64 +"[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"
65 + "|\\.[0-9](?:'?[0-9])*"
66 + ")(?:[Ee][+-]?[0-9](?:'?[0-9])*)?"
67 + "|[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"
70 +"[0-9A-Fa-f](?:'?[0-9A-Fa-f])*(?:\\.(?:[0-9A-Fa-f](?:'?[0-9A-Fa-f])*)?)?"
71 + "|\\.[0-9A-Fa-f](?:'?[0-9A-Fa-f])*"
72 + ")[Pp][+-]?[0-9](?:'?[0-9])*"
73 + ")(?:" // Literal suffixes.
74 + "[Ff](?:16|32|64|128)?"
77 + "|" // Literal suffix is optional.
82 "[+-]?\\b(?:" // Leading sign.
83 + "0[Bb][01](?:'?[01])*" // Binary.
84 + "|0[Xx][0-9A-Fa-f](?:'?[0-9A-Fa-f])*" // Hexadecimal.
85 + "|0(?:'?[0-7])*" // Octal or just a lone zero.
86 + "|[1-9](?:'?[0-9])*" // Decimal.
87 + ")(?:" // Literal suffixes.
92 + "|" // Literal suffix is optional.
94 // Note: there are user-defined literal suffixes too, but perhaps having the custom suffix not part of the
95 // literal highlight actually makes it stand out more.
101 const PREPROCESSOR
= {
103 begin: /#\s*[a-z]+\b/,
106 'if else elif endif define undef warning error line '
107 + 'pragma _Pragma ifdef ifndef include' },
113 hljs
.inherit(STRINGS
, { className: 'string' }),
119 hljs
.C_BLOCK_COMMENT_MODE
125 begin: regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
,
129 const FUNCTION_TITLE
= regex
.optional(NAMESPACE_RE
) + hljs
.IDENT_RE
+ '\\s*\\(';
131 // https://en.cppreference.com/w/cpp/keyword
132 const RESERVED_KEYWORDS
= [
193 'reinterpret_cast|10',
207 'transaction_safe_dynamic',
222 // https://en.cppreference.com/w/cpp/keyword
223 const RESERVED_TYPES
= [
249 'condition_variable',
250 'condition_variable_any',
251 'counting_semaphore',
274 'recursive_timed_mutex',
280 'shared_timed_mutex',
292 'unordered_multimap',
293 'unordered_multiset',
302 const FUNCTION_HINTS
= [
353 'make_shared_for_overwrite',
413 // https://en.cppreference.com/w/cpp/keyword
414 const BUILT_IN
= [ '_Pragma' ];
416 const CPP_KEYWORDS
= {
417 type: RESERVED_TYPES
,
418 keyword: RESERVED_KEYWORDS
,
421 _type_hints: TYPE_HINTS
424 const FUNCTION_DISPATCH
= {
425 className: 'function.dispatch',
428 // Only for relevance, not highlighting.
429 _hint: FUNCTION_HINTS
},
438 regex
.lookahead(/(<[^<>]+>|)\s*\(/))
441 const EXPRESSION_CONTAINS
= [
446 hljs
.C_BLOCK_COMMENT_MODE
,
451 const EXPRESSION_CONTEXT
= {
452 // This mode covers expression context where we can't expect a function
453 // definition and shouldn't highlight anything that looks like one:
454 // `return some()`, `else if()`, `(x*sum(1, 2))`
465 beginKeywords: 'new throw return else',
469 keywords: CPP_KEYWORDS
,
470 contains: EXPRESSION_CONTAINS
.concat([
474 keywords: CPP_KEYWORDS
,
475 contains: EXPRESSION_CONTAINS
.concat([ 'self' ]),
482 const FUNCTION_DECLARATION
= {
483 className: 'function',
484 begin: '(' + FUNCTION_TYPE_RE
+ '[\\*&\\s]+)+' + FUNCTION_TITLE
,
488 keywords: CPP_KEYWORDS
,
489 illegal: /[^\w\s\*&:<>.]/,
491 { // to prevent it from being confused as the function title
492 begin: DECLTYPE_AUTO_RE
,
493 keywords: CPP_KEYWORDS
,
497 begin: FUNCTION_TITLE
,
499 contains: [ TITLE_MODE
],
502 // needed because we do not have look-behind on the below rule
503 // to prevent it from grabbing the final : in a :: pair
511 endsWithParent: true,
517 // allow for multiple declarations, e.g.:
518 // extern void f(int), g(char);
527 keywords: CPP_KEYWORDS
,
531 hljs
.C_BLOCK_COMMENT_MODE
,
535 // Count matching parentheses.
539 keywords: CPP_KEYWORDS
,
544 hljs
.C_BLOCK_COMMENT_MODE
,
554 hljs
.C_BLOCK_COMMENT_MODE
,
570 keywords: CPP_KEYWORDS
,
572 classNameAliases: { 'function.dispatch': 'built_in' },
575 FUNCTION_DECLARATION
,
580 { // containers: ie, `vector <int> rooms (9);`
581 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*<(?!<)',
583 keywords: CPP_KEYWORDS
,
590 begin: hljs
.IDENT_RE
+ '::',
591 keywords: CPP_KEYWORDS
595 // extra complexity to deal with `enum class` and `enum struct`
596 /\b(?:enum(?:\s+(?:class|struct))?|class|struct|union)/,
613 export default hljsGrammar
;