cpp.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. module.exports = function(hljs) {
  2. function optional(s) {
  3. return '(?:' + s + ')?';
  4. }
  5. var DECLTYPE_AUTO_RE = 'decltype\\(auto\\)'
  6. var NAMESPACE_RE = '[a-zA-Z_]\\w*::'
  7. var TEMPLATE_ARGUMENT_RE = '<.*?>';
  8. var FUNCTION_TYPE_RE = '(' +
  9. DECLTYPE_AUTO_RE + '|' +
  10. optional(NAMESPACE_RE) +'[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
  11. ')';
  12. var CPP_PRIMITIVE_TYPES = {
  13. className: 'keyword',
  14. begin: '\\b[a-z\\d_]*_t\\b'
  15. };
  16. // https://en.cppreference.com/w/cpp/language/escape
  17. // \\ \x \xFF \u2837 \u00323747 \374
  18. var CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)'
  19. var STRINGS = {
  20. className: 'string',
  21. variants: [
  22. {
  23. begin: '(u8?|U|L)?"', end: '"',
  24. illegal: '\\n',
  25. contains: [hljs.BACKSLASH_ESCAPE]
  26. },
  27. {
  28. begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)", end: '\'',
  29. illegal: '.'
  30. },
  31. { begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\((?:.|\n)*?\)\1"/ }
  32. ]
  33. };
  34. var NUMBERS = {
  35. className: 'number',
  36. variants: [
  37. { begin: '\\b(0b[01\']+)' },
  38. { begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
  39. { begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
  40. ],
  41. relevance: 0
  42. };
  43. var PREPROCESSOR = {
  44. className: 'meta',
  45. begin: /#\s*[a-z]+\b/, end: /$/,
  46. keywords: {
  47. 'meta-keyword':
  48. 'if else elif endif define undef warning error line ' +
  49. 'pragma _Pragma ifdef ifndef include'
  50. },
  51. contains: [
  52. {
  53. begin: /\\\n/, relevance: 0
  54. },
  55. hljs.inherit(STRINGS, {className: 'meta-string'}),
  56. {
  57. className: 'meta-string',
  58. begin: /<.*?>/, end: /$/,
  59. illegal: '\\n',
  60. },
  61. hljs.C_LINE_COMMENT_MODE,
  62. hljs.C_BLOCK_COMMENT_MODE
  63. ]
  64. };
  65. var TITLE_MODE = {
  66. className: 'title',
  67. begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
  68. relevance: 0
  69. };
  70. var FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
  71. var CPP_KEYWORDS = {
  72. keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
  73. 'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
  74. 'unsigned long volatile static protected bool template mutable if public friend ' +
  75. 'do goto auto void enum else break extern using asm case typeid wchar_t' +
  76. 'short reinterpret_cast|10 default double register explicit signed typename try this ' +
  77. 'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
  78. 'concept co_await co_return co_yield requires ' +
  79. 'noexcept static_assert thread_local restrict final override ' +
  80. 'atomic_bool atomic_char atomic_schar ' +
  81. 'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
  82. 'atomic_ullong new throw return ' +
  83. 'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
  84. built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
  85. 'auto_ptr deque list queue stack vector map set bitset multiset multimap unordered_set ' +
  86. 'unordered_map unordered_multiset unordered_multimap array shared_ptr abort terminate abs acos ' +
  87. 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
  88. 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
  89. 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
  90. 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
  91. 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
  92. 'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
  93. literal: 'true false nullptr NULL'
  94. };
  95. var EXPRESSION_CONTAINS = [
  96. CPP_PRIMITIVE_TYPES,
  97. hljs.C_LINE_COMMENT_MODE,
  98. hljs.C_BLOCK_COMMENT_MODE,
  99. NUMBERS,
  100. STRINGS
  101. ];
  102. var EXPRESSION_CONTEXT = {
  103. // This mode covers expression context where we can't expect a function
  104. // definition and shouldn't highlight anything that looks like one:
  105. // `return some()`, `else if()`, `(x*sum(1, 2))`
  106. variants: [
  107. {begin: /=/, end: /;/},
  108. {begin: /\(/, end: /\)/},
  109. {beginKeywords: 'new throw return else', end: /;/}
  110. ],
  111. keywords: CPP_KEYWORDS,
  112. contains: EXPRESSION_CONTAINS.concat([
  113. {
  114. begin: /\(/, end: /\)/,
  115. keywords: CPP_KEYWORDS,
  116. contains: EXPRESSION_CONTAINS.concat(['self']),
  117. relevance: 0
  118. }
  119. ]),
  120. relevance: 0
  121. };
  122. var FUNCTION_DECLARATION = {
  123. className: 'function',
  124. begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
  125. returnBegin: true, end: /[{;=]/,
  126. excludeEnd: true,
  127. keywords: CPP_KEYWORDS,
  128. illegal: /[^\w\s\*&:<>]/,
  129. contains: [
  130. { // to prevent it from being confused as the function title
  131. begin: DECLTYPE_AUTO_RE,
  132. keywords: CPP_KEYWORDS,
  133. relevance: 0,
  134. },
  135. {
  136. begin: FUNCTION_TITLE, returnBegin: true,
  137. contains: [TITLE_MODE],
  138. relevance: 0
  139. },
  140. {
  141. className: 'params',
  142. begin: /\(/, end: /\)/,
  143. keywords: CPP_KEYWORDS,
  144. relevance: 0,
  145. contains: [
  146. hljs.C_LINE_COMMENT_MODE,
  147. hljs.C_BLOCK_COMMENT_MODE,
  148. STRINGS,
  149. NUMBERS,
  150. CPP_PRIMITIVE_TYPES,
  151. // Count matching parentheses.
  152. {
  153. begin: /\(/, end: /\)/,
  154. keywords: CPP_KEYWORDS,
  155. relevance: 0,
  156. contains: [
  157. 'self',
  158. hljs.C_LINE_COMMENT_MODE,
  159. hljs.C_BLOCK_COMMENT_MODE,
  160. STRINGS,
  161. NUMBERS,
  162. CPP_PRIMITIVE_TYPES
  163. ]
  164. }
  165. ]
  166. },
  167. CPP_PRIMITIVE_TYPES,
  168. hljs.C_LINE_COMMENT_MODE,
  169. hljs.C_BLOCK_COMMENT_MODE,
  170. PREPROCESSOR
  171. ]
  172. };
  173. return {
  174. aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
  175. keywords: CPP_KEYWORDS,
  176. illegal: '</',
  177. contains: [].concat(
  178. EXPRESSION_CONTEXT,
  179. FUNCTION_DECLARATION,
  180. EXPRESSION_CONTAINS,
  181. [
  182. PREPROCESSOR,
  183. {
  184. begin: '\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
  185. keywords: CPP_KEYWORDS,
  186. contains: ['self', CPP_PRIMITIVE_TYPES]
  187. },
  188. {
  189. begin: hljs.IDENT_RE + '::',
  190. keywords: CPP_KEYWORDS
  191. },
  192. {
  193. className: 'class',
  194. beginKeywords: 'class struct', end: /[{;:]/,
  195. contains: [
  196. {begin: /</, end: />/, contains: ['self']}, // skip generic stuff
  197. hljs.TITLE_MODE
  198. ]
  199. }
  200. ]),
  201. exports: {
  202. preprocessor: PREPROCESSOR,
  203. strings: STRINGS,
  204. keywords: CPP_KEYWORDS
  205. }
  206. };
  207. };