c.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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
  31. Category: common, system
  32. Website: https://en.wikipedia.org/wiki/C_(programming_language)
  33. */
  34. /** @type LanguageFn */
  35. function c(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. },
  117. C_LINE_COMMENT_MODE,
  118. hljs.C_BLOCK_COMMENT_MODE
  119. ]
  120. };
  121. const TITLE_MODE = {
  122. className: 'title',
  123. begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
  124. relevance: 0
  125. };
  126. const FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
  127. const CPP_KEYWORDS = {
  128. keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
  129. 'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
  130. 'unsigned long volatile static protected bool template mutable if public friend ' +
  131. 'do goto auto void enum else break extern using asm case typeid wchar_t ' +
  132. 'short reinterpret_cast|10 default double register explicit signed typename try this ' +
  133. 'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
  134. 'concept co_await co_return co_yield requires ' +
  135. 'noexcept static_assert thread_local restrict final override ' +
  136. 'atomic_bool atomic_char atomic_schar ' +
  137. 'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
  138. 'atomic_ullong new throw return ' +
  139. 'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
  140. built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
  141. 'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set ' +
  142. 'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos ' +
  143. 'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
  144. 'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
  145. 'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
  146. 'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
  147. 'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
  148. 'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
  149. literal: 'true false nullptr NULL'
  150. };
  151. const EXPRESSION_CONTAINS = [
  152. PREPROCESSOR,
  153. CPP_PRIMITIVE_TYPES,
  154. C_LINE_COMMENT_MODE,
  155. hljs.C_BLOCK_COMMENT_MODE,
  156. NUMBERS,
  157. STRINGS
  158. ];
  159. const EXPRESSION_CONTEXT = {
  160. // This mode covers expression context where we can't expect a function
  161. // definition and shouldn't highlight anything that looks like one:
  162. // `return some()`, `else if()`, `(x*sum(1, 2))`
  163. variants: [
  164. {
  165. begin: /=/,
  166. end: /;/
  167. },
  168. {
  169. begin: /\(/,
  170. end: /\)/
  171. },
  172. {
  173. beginKeywords: 'new throw return else',
  174. end: /;/
  175. }
  176. ],
  177. keywords: CPP_KEYWORDS,
  178. contains: EXPRESSION_CONTAINS.concat([
  179. {
  180. begin: /\(/,
  181. end: /\)/,
  182. keywords: CPP_KEYWORDS,
  183. contains: EXPRESSION_CONTAINS.concat([ 'self' ]),
  184. relevance: 0
  185. }
  186. ]),
  187. relevance: 0
  188. };
  189. const FUNCTION_DECLARATION = {
  190. className: 'function',
  191. begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
  192. returnBegin: true,
  193. end: /[{;=]/,
  194. excludeEnd: true,
  195. keywords: CPP_KEYWORDS,
  196. illegal: /[^\w\s\*&:<>.]/,
  197. contains: [
  198. { // to prevent it from being confused as the function title
  199. begin: DECLTYPE_AUTO_RE,
  200. keywords: CPP_KEYWORDS,
  201. relevance: 0
  202. },
  203. {
  204. begin: FUNCTION_TITLE,
  205. returnBegin: true,
  206. contains: [ TITLE_MODE ],
  207. relevance: 0
  208. },
  209. {
  210. className: 'params',
  211. begin: /\(/,
  212. end: /\)/,
  213. keywords: CPP_KEYWORDS,
  214. relevance: 0,
  215. contains: [
  216. C_LINE_COMMENT_MODE,
  217. hljs.C_BLOCK_COMMENT_MODE,
  218. STRINGS,
  219. NUMBERS,
  220. CPP_PRIMITIVE_TYPES,
  221. // Count matching parentheses.
  222. {
  223. begin: /\(/,
  224. end: /\)/,
  225. keywords: CPP_KEYWORDS,
  226. relevance: 0,
  227. contains: [
  228. 'self',
  229. C_LINE_COMMENT_MODE,
  230. hljs.C_BLOCK_COMMENT_MODE,
  231. STRINGS,
  232. NUMBERS,
  233. CPP_PRIMITIVE_TYPES
  234. ]
  235. }
  236. ]
  237. },
  238. CPP_PRIMITIVE_TYPES,
  239. C_LINE_COMMENT_MODE,
  240. hljs.C_BLOCK_COMMENT_MODE,
  241. PREPROCESSOR
  242. ]
  243. };
  244. return {
  245. name: "C",
  246. aliases: [
  247. 'h'
  248. ],
  249. keywords: CPP_KEYWORDS,
  250. // Until differentiations are added between `c` and `cpp`, `c` will
  251. // not be auto-detected to avoid auto-detect conflicts between C and C++
  252. disableAutodetect: true,
  253. illegal: '</',
  254. contains: [].concat(
  255. EXPRESSION_CONTEXT,
  256. FUNCTION_DECLARATION,
  257. EXPRESSION_CONTAINS,
  258. [
  259. PREPROCESSOR,
  260. { // containers: ie, `vector <int> rooms (9);`
  261. 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*<',
  262. end: '>',
  263. keywords: CPP_KEYWORDS,
  264. contains: [
  265. 'self',
  266. CPP_PRIMITIVE_TYPES
  267. ]
  268. },
  269. {
  270. begin: hljs.IDENT_RE + '::',
  271. keywords: CPP_KEYWORDS
  272. },
  273. {
  274. className: 'class',
  275. beginKeywords: 'enum class struct union',
  276. end: /[{;:<>=]/,
  277. contains: [
  278. {
  279. beginKeywords: "final class struct"
  280. },
  281. hljs.TITLE_MODE
  282. ]
  283. }
  284. ]),
  285. exports: {
  286. preprocessor: PREPROCESSOR,
  287. strings: STRINGS,
  288. keywords: CPP_KEYWORDS
  289. }
  290. };
  291. }
  292. module.exports = c;