cpp.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {RegExp | string } re
  16. * @returns {string}
  17. */
  18. function optional(re) {
  19. return concat('(', re, ')?');
  20. }
  21. /**
  22. * @param {...(RegExp | string) } args
  23. * @returns {string}
  24. */
  25. function concat(...args) {
  26. const joined = args.map((x) => source(x)).join("");
  27. return joined;
  28. }
  29. /*
  30. Language: C-like foundation grammar for C/C++ grammars
  31. Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
  32. Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Zaven Muradyan <megalivoithos@gmail.com>, Roel Deckers <admin@codingcat.nl>, Sam Wu <samsam2310@gmail.com>, Jordi Petit <jordi.petit@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com>, Google Inc. (David Benjamin) <davidben@google.com>
  33. */
  34. /** @type LanguageFn */
  35. function cLike(hljs) {
  36. // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does
  37. // not include such support nor can we be sure all the grammars depending
  38. // on it would desire this behavior
  39. const C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$', {
  40. contains: [
  41. {
  42. begin: /\\\n/
  43. }
  44. ]
  45. });
  46. const DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';
  47. const NAMESPACE_RE = '[a-zA-Z_]\\w*::';
  48. const TEMPLATE_ARGUMENT_RE = '<[^<>]+>';
  49. const FUNCTION_TYPE_RE = '(' +
  50. DECLTYPE_AUTO_RE + '|' +
  51. optional(NAMESPACE_RE) +
  52. '[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
  53. ')';
  54. const CPP_PRIMITIVE_TYPES = {
  55. className: 'keyword',
  56. begin: '\\b[a-z\\d_]*_t\\b'
  57. };
  58. // https://en.cppreference.com/w/cpp/language/escape
  59. // \\ \x \xFF \u2837 \u00323747 \374
  60. const CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
  61. const STRINGS = {
  62. className: 'string',
  63. variants: [
  64. {
  65. begin: '(u8?|U|L)?"',
  66. end: '"',
  67. illegal: '\\n',
  68. contains: [ hljs.BACKSLASH_ESCAPE ]
  69. },
  70. {
  71. begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)",
  72. end: '\'',
  73. illegal: '.'
  74. },
  75. hljs.END_SAME_AS_BEGIN({
  76. begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,
  77. end: /\)([^()\\ ]{0,16})"/
  78. })
  79. ]
  80. };
  81. const NUMBERS = {
  82. className: 'number',
  83. variants: [
  84. {
  85. begin: '\\b(0b[01\']+)'
  86. },
  87. {
  88. begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)((ll|LL|l|L)(u|U)?|(u|U)(ll|LL|l|L)?|f|F|b|B)'
  89. },
  90. {
  91. begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)'
  92. }
  93. ],
  94. relevance: 0
  95. };
  96. const PREPROCESSOR = {
  97. className: 'meta',
  98. begin: /#\s*[a-z]+\b/,
  99. end: /$/,
  100. keywords: {
  101. 'meta-keyword':
  102. 'if else elif endif define undef warning error line ' +
  103. 'pragma _Pragma ifdef ifndef include'
  104. },
  105. contains: [
  106. {
  107. begin: /\\\n/,
  108. relevance: 0
  109. },
  110. hljs.inherit(STRINGS, {
  111. className: 'meta-string'
  112. }),
  113. {
  114. className: 'meta-string',
  115. begin: /<.*?>/,
  116. end: /$/,
  117. illegal: '\\n'
  118. },
  119. C_LINE_COMMENT_MODE,
  120. hljs.C_BLOCK_COMMENT_MODE
  121. ]
  122. };
  123. const TITLE_MODE = {
  124. className: 'title',
  125. begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
  126. relevance: 0
  127. };
  128. const FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
  129. const CPP_KEYWORDS = {
  130. keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
  131. 'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
  132. 'unsigned long volatile static protected bool template mutable if public friend ' +
  133. 'do goto auto void enum else break extern using asm case typeid wchar_t ' +
  134. 'short reinterpret_cast|10 default double register explicit signed typename try this ' +
  135. 'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
  136. 'concept co_await co_return co_yield requires ' +
  137. 'noexcept static_assert thread_local restrict final override ' +
  138. 'atomic_bool atomic_char atomic_schar ' +
  139. 'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
  140. 'atomic_ullong new throw return ' +
  141. 'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
  142. built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
  143. 'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set ' +
  144. 'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos ' +
  145. 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
  146. 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
  147. 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
  148. 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
  149. 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
  150. 'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
  151. literal: 'true false nullptr NULL'
  152. };
  153. const EXPRESSION_CONTAINS = [
  154. PREPROCESSOR,
  155. CPP_PRIMITIVE_TYPES,
  156. C_LINE_COMMENT_MODE,
  157. hljs.C_BLOCK_COMMENT_MODE,
  158. NUMBERS,
  159. STRINGS
  160. ];
  161. const EXPRESSION_CONTEXT = {
  162. // This mode covers expression context where we can't expect a function
  163. // definition and shouldn't highlight anything that looks like one:
  164. // `return some()`, `else if()`, `(x*sum(1, 2))`
  165. variants: [
  166. {
  167. begin: /=/,
  168. end: /;/
  169. },
  170. {
  171. begin: /\(/,
  172. end: /\)/
  173. },
  174. {
  175. beginKeywords: 'new throw return else',
  176. end: /;/
  177. }
  178. ],
  179. keywords: CPP_KEYWORDS,
  180. contains: EXPRESSION_CONTAINS.concat([
  181. {
  182. begin: /\(/,
  183. end: /\)/,
  184. keywords: CPP_KEYWORDS,
  185. contains: EXPRESSION_CONTAINS.concat([ 'self' ]),
  186. relevance: 0
  187. }
  188. ]),
  189. relevance: 0
  190. };
  191. const FUNCTION_DECLARATION = {
  192. className: 'function',
  193. begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
  194. returnBegin: true,
  195. end: /[{;=]/,
  196. excludeEnd: true,
  197. keywords: CPP_KEYWORDS,
  198. illegal: /[^\w\s\*&:<>.]/,
  199. contains: [
  200. { // to prevent it from being confused as the function title
  201. begin: DECLTYPE_AUTO_RE,
  202. keywords: CPP_KEYWORDS,
  203. relevance: 0
  204. },
  205. {
  206. begin: FUNCTION_TITLE,
  207. returnBegin: true,
  208. contains: [ TITLE_MODE ],
  209. relevance: 0
  210. },
  211. {
  212. className: 'params',
  213. begin: /\(/,
  214. end: /\)/,
  215. keywords: CPP_KEYWORDS,
  216. relevance: 0,
  217. contains: [
  218. C_LINE_COMMENT_MODE,
  219. hljs.C_BLOCK_COMMENT_MODE,
  220. STRINGS,
  221. NUMBERS,
  222. CPP_PRIMITIVE_TYPES,
  223. // Count matching parentheses.
  224. {
  225. begin: /\(/,
  226. end: /\)/,
  227. keywords: CPP_KEYWORDS,
  228. relevance: 0,
  229. contains: [
  230. 'self',
  231. C_LINE_COMMENT_MODE,
  232. hljs.C_BLOCK_COMMENT_MODE,
  233. STRINGS,
  234. NUMBERS,
  235. CPP_PRIMITIVE_TYPES
  236. ]
  237. }
  238. ]
  239. },
  240. CPP_PRIMITIVE_TYPES,
  241. C_LINE_COMMENT_MODE,
  242. hljs.C_BLOCK_COMMENT_MODE,
  243. PREPROCESSOR
  244. ]
  245. };
  246. return {
  247. aliases: [
  248. 'c',
  249. 'cc',
  250. 'h',
  251. 'c++',
  252. 'h++',
  253. 'hpp',
  254. 'hh',
  255. 'hxx',
  256. 'cxx'
  257. ],
  258. keywords: CPP_KEYWORDS,
  259. // the base c-like language will NEVER be auto-detected, rather the
  260. // derivitives: c, c++, arduino turn auto-detect back on for themselves
  261. disableAutodetect: true,
  262. illegal: '</',
  263. contains: [].concat(
  264. EXPRESSION_CONTEXT,
  265. FUNCTION_DECLARATION,
  266. EXPRESSION_CONTAINS,
  267. [
  268. PREPROCESSOR,
  269. { // containers: ie, `vector <int> rooms (9);`
  270. 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*<',
  271. end: '>',
  272. keywords: CPP_KEYWORDS,
  273. contains: [
  274. 'self',
  275. CPP_PRIMITIVE_TYPES
  276. ]
  277. },
  278. {
  279. begin: hljs.IDENT_RE + '::',
  280. keywords: CPP_KEYWORDS
  281. },
  282. {
  283. className: 'class',
  284. beginKeywords: 'enum class struct union',
  285. end: /[{;:<>=]/,
  286. contains: [
  287. {
  288. beginKeywords: "final class struct"
  289. },
  290. hljs.TITLE_MODE
  291. ]
  292. }
  293. ]),
  294. exports: {
  295. preprocessor: PREPROCESSOR,
  296. strings: STRINGS,
  297. keywords: CPP_KEYWORDS
  298. }
  299. };
  300. }
  301. /*
  302. Language: C++
  303. Category: common, system
  304. Website: https://isocpp.org
  305. */
  306. /** @type LanguageFn */
  307. function cpp(hljs) {
  308. const lang = cLike(hljs);
  309. // return auto-detection back on
  310. lang.disableAutodetect = false;
  311. lang.name = 'C++';
  312. lang.aliases = ['cc', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'];
  313. return lang;
  314. }
  315. module.exports = cpp;