cpp.js 6.0 KB

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