1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/highlight.js/lib/languages/cpp.js

465 lines
10 KiB
JavaScript
Raw Normal View History

2024-01-16 21:26:16 +08:00
/**
* @param {string} value
* @returns {RegExp}
* */
/**
* @param {RegExp | string } re
* @returns {string}
*/
function source(re) {
if (!re) return null;
if (typeof re === "string") return re;
return re.source;
}
/**
* @param {RegExp | string } re
* @returns {string}
*/
function lookahead(re) {
return concat('(?=', re, ')');
}
/**
* @param {RegExp | string } re
* @returns {string}
*/
function optional(re) {
return concat('(', re, ')?');
}
/**
* @param {...(RegExp | string) } args
* @returns {string}
*/
function concat(...args) {
const joined = args.map((x) => source(x)).join("");
return joined;
}
/*
Language: C++
Category: common, system
Website: https://isocpp.org
*/
/** @type LanguageFn */
function cpp(hljs) {
// added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
// not include such support nor can we be sure all the grammars depending
// on it would desire this behavior
const C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$', {
contains: [
{
begin: /\\\n/
}
]
});
const DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';
const NAMESPACE_RE = '[a-zA-Z_]\\w*::';
const TEMPLATE_ARGUMENT_RE = '<[^<>]+>';
const FUNCTION_TYPE_RE = '(' +
DECLTYPE_AUTO_RE + '|' +
optional(NAMESPACE_RE) +
'[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
')';
const CPP_PRIMITIVE_TYPES = {
2023-12-18 13:12:25 +08:00
className: 'keyword',
begin: '\\b[a-z\\d_]*_t\\b'
};
2024-01-16 21:26:16 +08:00
// https://en.cppreference.com/w/cpp/language/escape
// \\ \x \xFF \u2837 \u00323747 \374
const CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
const STRINGS = {
2023-12-18 13:12:25 +08:00
className: 'string',
variants: [
{
2024-01-16 21:26:16 +08:00
begin: '(u8?|U|L)?"',
end: '"',
2023-12-18 13:12:25 +08:00
illegal: '\\n',
2024-01-16 21:26:16 +08:00
contains: [ hljs.BACKSLASH_ESCAPE ]
2023-12-18 13:12:25 +08:00
},
{
2024-01-16 21:26:16 +08:00
begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)",
end: '\'',
2023-12-18 13:12:25 +08:00
illegal: '.'
2024-01-16 21:26:16 +08:00
},
hljs.END_SAME_AS_BEGIN({
begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,
end: /\)([^()\\ ]{0,16})"/
})
2023-12-18 13:12:25 +08:00
]
};
2024-01-16 21:26:16 +08:00
const NUMBERS = {
2023-12-18 13:12:25 +08:00
className: 'number',
variants: [
2024-01-16 21:26:16 +08:00
{
begin: '\\b(0b[01\']+)'
},
{
begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)'
},
{
begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)'
}
2023-12-18 13:12:25 +08:00
],
relevance: 0
};
2024-01-16 21:26:16 +08:00
const PREPROCESSOR = {
2023-12-18 13:12:25 +08:00
className: 'meta',
2024-01-16 21:26:16 +08:00
begin: /#\s*[a-z]+\b/,
end: /$/,
2023-12-18 13:12:25 +08:00
keywords: {
'meta-keyword':
'if else elif endif define undef warning error line ' +
2024-01-16 21:26:16 +08:00
'pragma _Pragma ifdef ifndef include'
2023-12-18 13:12:25 +08:00
},
contains: [
{
2024-01-16 21:26:16 +08:00
begin: /\\\n/,
relevance: 0
2023-12-18 13:12:25 +08:00
},
2024-01-16 21:26:16 +08:00
hljs.inherit(STRINGS, {
className: 'meta-string'
}),
2023-12-18 13:12:25 +08:00
{
className: 'meta-string',
2024-01-16 21:26:16 +08:00
begin: /<.*?>/
2023-12-18 13:12:25 +08:00
},
2024-01-16 21:26:16 +08:00
C_LINE_COMMENT_MODE,
2023-12-18 13:12:25 +08:00
hljs.C_BLOCK_COMMENT_MODE
]
};
2024-01-16 21:26:16 +08:00
const TITLE_MODE = {
className: 'title',
begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
relevance: 0
};
const FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
const COMMON_CPP_HINTS = [
'asin',
'atan2',
'atan',
'calloc',
'ceil',
'cosh',
'cos',
'exit',
'exp',
'fabs',
'floor',
'fmod',
'fprintf',
'fputs',
'free',
'frexp',
'auto_ptr',
'deque',
'list',
'queue',
'stack',
'vector',
'map',
'set',
'pair',
'bitset',
'multiset',
'multimap',
'unordered_set',
'fscanf',
'future',
'isalnum',
'isalpha',
'iscntrl',
'isdigit',
'isgraph',
'islower',
'isprint',
'ispunct',
'isspace',
'isupper',
'isxdigit',
'tolower',
'toupper',
'labs',
'ldexp',
'log10',
'log',
'malloc',
'realloc',
'memchr',
'memcmp',
'memcpy',
'memset',
'modf',
'pow',
'printf',
'putchar',
'puts',
'scanf',
'sinh',
'sin',
'snprintf',
'sprintf',
'sqrt',
'sscanf',
'strcat',
'strchr',
'strcmp',
'strcpy',
'strcspn',
'strlen',
'strncat',
'strncmp',
'strncpy',
'strpbrk',
'strrchr',
'strspn',
'strstr',
'tanh',
'tan',
'unordered_map',
'unordered_multiset',
'unordered_multimap',
'priority_queue',
'make_pair',
'array',
'shared_ptr',
'abort',
'terminate',
'abs',
'acos',
'vfprintf',
'vprintf',
'vsprintf',
'endl',
'initializer_list',
'unique_ptr',
'complex',
'imaginary',
'std',
'string',
'wstring',
'cin',
'cout',
'cerr',
'clog',
'stdin',
'stdout',
'stderr',
'stringstream',
'istringstream',
'ostringstream'
];
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
const CPP_KEYWORDS = {
keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
2023-12-18 13:12:25 +08:00
'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
'unsigned long volatile static protected bool template mutable if public friend ' +
2024-01-16 21:26:16 +08:00
'do goto auto void enum else break extern using asm case typeid wchar_t ' +
2023-12-18 13:12:25 +08:00
'short reinterpret_cast|10 default double register explicit signed typename try this ' +
2024-01-16 21:26:16 +08:00
'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
'concept co_await co_return co_yield requires ' +
'noexcept static_assert thread_local restrict final override ' +
2023-12-18 13:12:25 +08:00
'atomic_bool atomic_char atomic_schar ' +
'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
'atomic_ullong new throw return ' +
2024-01-16 21:26:16 +08:00
'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
built_in: '_Bool _Complex _Imaginary',
_relevance_hints: COMMON_CPP_HINTS,
2023-12-18 13:12:25 +08:00
literal: 'true false nullptr NULL'
};
2024-01-16 21:26:16 +08:00
const FUNCTION_DISPATCH = {
className: "function.dispatch",
relevance: 0,
keywords: CPP_KEYWORDS,
begin: concat(
/\b/,
/(?!decltype)/,
/(?!if)/,
/(?!for)/,
/(?!while)/,
hljs.IDENT_RE,
lookahead(/\s*\(/))
};
const EXPRESSION_CONTAINS = [
FUNCTION_DISPATCH,
PREPROCESSOR,
2023-12-18 13:12:25 +08:00
CPP_PRIMITIVE_TYPES,
2024-01-16 21:26:16 +08:00
C_LINE_COMMENT_MODE,
2023-12-18 13:12:25 +08:00
hljs.C_BLOCK_COMMENT_MODE,
NUMBERS,
STRINGS
];
2024-01-16 21:26:16 +08:00
const EXPRESSION_CONTEXT = {
// This mode covers expression context where we can't expect a function
// definition and shouldn't highlight anything that looks like one:
// `return some()`, `else if()`, `(x*sum(1, 2))`
variants: [
{
begin: /=/,
end: /;/
},
{
begin: /\(/,
end: /\)/
},
{
beginKeywords: 'new throw return else',
end: /;/
}
],
2023-12-18 13:12:25 +08:00
keywords: CPP_KEYWORDS,
contains: EXPRESSION_CONTAINS.concat([
{
2024-01-16 21:26:16 +08:00
begin: /\(/,
end: /\)/,
keywords: CPP_KEYWORDS,
contains: EXPRESSION_CONTAINS.concat([ 'self' ]),
relevance: 0
}
]),
relevance: 0
};
const FUNCTION_DECLARATION = {
className: 'function',
begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
returnBegin: true,
end: /[{;=]/,
excludeEnd: true,
keywords: CPP_KEYWORDS,
illegal: /[^\w\s\*&:<>.]/,
contains: [
{ // to prevent it from being confused as the function title
begin: DECLTYPE_AUTO_RE,
2023-12-18 13:12:25 +08:00
keywords: CPP_KEYWORDS,
2024-01-16 21:26:16 +08:00
relevance: 0
2023-12-18 13:12:25 +08:00
},
{
2024-01-16 21:26:16 +08:00
begin: FUNCTION_TITLE,
returnBegin: true,
contains: [ TITLE_MODE ],
relevance: 0
2023-12-18 13:12:25 +08:00
},
2024-01-16 21:26:16 +08:00
// needed because we do not have look-behind on the below rule
// to prevent it from grabbing the final : in a :: pair
2023-12-18 13:12:25 +08:00
{
2024-01-16 21:26:16 +08:00
begin: /::/,
2023-12-18 13:12:25 +08:00
relevance: 0
},
2024-01-16 21:26:16 +08:00
// initializers
{
begin: /:/,
endsWithParent: true,
contains: [
STRINGS,
NUMBERS
]
},
2023-12-18 13:12:25 +08:00
{
2024-01-16 21:26:16 +08:00
className: 'params',
begin: /\(/,
end: /\)/,
2023-12-18 13:12:25 +08:00
keywords: CPP_KEYWORDS,
2024-01-16 21:26:16 +08:00
relevance: 0,
2023-12-18 13:12:25 +08:00
contains: [
2024-01-16 21:26:16 +08:00
C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
STRINGS,
NUMBERS,
CPP_PRIMITIVE_TYPES,
// Count matching parentheses.
2023-12-18 13:12:25 +08:00
{
2024-01-16 21:26:16 +08:00
begin: /\(/,
end: /\)/,
2023-12-18 13:12:25 +08:00
keywords: CPP_KEYWORDS,
relevance: 0,
contains: [
2024-01-16 21:26:16 +08:00
'self',
C_LINE_COMMENT_MODE,
2023-12-18 13:12:25 +08:00
hljs.C_BLOCK_COMMENT_MODE,
STRINGS,
NUMBERS,
2024-01-16 21:26:16 +08:00
CPP_PRIMITIVE_TYPES
2023-12-18 13:12:25 +08:00
]
2024-01-16 21:26:16 +08:00
}
2023-12-18 13:12:25 +08:00
]
},
2024-01-16 21:26:16 +08:00
CPP_PRIMITIVE_TYPES,
C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
PREPROCESSOR
]
};
return {
name: 'C++',
aliases: [
'cc',
'c++',
'h++',
'hpp',
'hh',
'hxx',
'cxx'
],
keywords: CPP_KEYWORDS,
illegal: '</',
classNameAliases: {
"function.dispatch": "built_in"
},
contains: [].concat(
EXPRESSION_CONTEXT,
FUNCTION_DECLARATION,
FUNCTION_DISPATCH,
EXPRESSION_CONTAINS,
[
PREPROCESSOR,
{ // containers: ie, `vector <int> rooms (9);`
begin: '\\b(deque|list|queue|priority_queue|pair|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<',
end: '>',
keywords: CPP_KEYWORDS,
contains: [
'self',
CPP_PRIMITIVE_TYPES
]
},
{
begin: hljs.IDENT_RE + '::',
keywords: CPP_KEYWORDS
},
{
className: 'class',
beginKeywords: 'enum class struct union',
end: /[{;:<>=]/,
contains: [
{
beginKeywords: "final class struct"
},
hljs.TITLE_MODE
]
}
]),
2023-12-18 13:12:25 +08:00
exports: {
preprocessor: PREPROCESSOR,
strings: STRINGS,
keywords: CPP_KEYWORDS
}
};
2024-01-16 21:26:16 +08:00
}
module.exports = cpp;