csharp.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. Language: C#
  3. Author: Jason Diamond <jason@diamond.name>
  4. Contributor: Nicolas LLOBERA <nllobera@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com>, David Pine <david.pine@microsoft.com>
  5. Website: https://docs.microsoft.com/en-us/dotnet/csharp/
  6. Category: common
  7. */
  8. /** @type LanguageFn */
  9. function csharp(hljs) {
  10. const BUILT_IN_KEYWORDS = [
  11. 'bool',
  12. 'byte',
  13. 'char',
  14. 'decimal',
  15. 'delegate',
  16. 'double',
  17. 'dynamic',
  18. 'enum',
  19. 'float',
  20. 'int',
  21. 'long',
  22. 'nint',
  23. 'nuint',
  24. 'object',
  25. 'sbyte',
  26. 'short',
  27. 'string',
  28. 'ulong',
  29. 'uint',
  30. 'ushort'
  31. ];
  32. const FUNCTION_MODIFIERS = [
  33. 'public',
  34. 'private',
  35. 'protected',
  36. 'static',
  37. 'internal',
  38. 'protected',
  39. 'abstract',
  40. 'async',
  41. 'extern',
  42. 'override',
  43. 'unsafe',
  44. 'virtual',
  45. 'new',
  46. 'sealed',
  47. 'partial'
  48. ];
  49. const LITERAL_KEYWORDS = [
  50. 'default',
  51. 'false',
  52. 'null',
  53. 'true'
  54. ];
  55. const NORMAL_KEYWORDS = [
  56. 'abstract',
  57. 'as',
  58. 'base',
  59. 'break',
  60. 'case',
  61. 'class',
  62. 'const',
  63. 'continue',
  64. 'do',
  65. 'else',
  66. 'event',
  67. 'explicit',
  68. 'extern',
  69. 'finally',
  70. 'fixed',
  71. 'for',
  72. 'foreach',
  73. 'goto',
  74. 'if',
  75. 'implicit',
  76. 'in',
  77. 'interface',
  78. 'internal',
  79. 'is',
  80. 'lock',
  81. 'namespace',
  82. 'new',
  83. 'operator',
  84. 'out',
  85. 'override',
  86. 'params',
  87. 'private',
  88. 'protected',
  89. 'public',
  90. 'readonly',
  91. 'record',
  92. 'ref',
  93. 'return',
  94. 'sealed',
  95. 'sizeof',
  96. 'stackalloc',
  97. 'static',
  98. 'struct',
  99. 'switch',
  100. 'this',
  101. 'throw',
  102. 'try',
  103. 'typeof',
  104. 'unchecked',
  105. 'unsafe',
  106. 'using',
  107. 'virtual',
  108. 'void',
  109. 'volatile',
  110. 'while'
  111. ];
  112. const CONTEXTUAL_KEYWORDS = [
  113. 'add',
  114. 'alias',
  115. 'and',
  116. 'ascending',
  117. 'async',
  118. 'await',
  119. 'by',
  120. 'descending',
  121. 'equals',
  122. 'from',
  123. 'get',
  124. 'global',
  125. 'group',
  126. 'init',
  127. 'into',
  128. 'join',
  129. 'let',
  130. 'nameof',
  131. 'not',
  132. 'notnull',
  133. 'on',
  134. 'or',
  135. 'orderby',
  136. 'partial',
  137. 'remove',
  138. 'select',
  139. 'set',
  140. 'unmanaged',
  141. 'value|0',
  142. 'var',
  143. 'when',
  144. 'where',
  145. 'with',
  146. 'yield'
  147. ];
  148. const KEYWORDS = {
  149. keyword: NORMAL_KEYWORDS.concat(CONTEXTUAL_KEYWORDS),
  150. built_in: BUILT_IN_KEYWORDS,
  151. literal: LITERAL_KEYWORDS
  152. };
  153. const TITLE_MODE = hljs.inherit(hljs.TITLE_MODE, {
  154. begin: '[a-zA-Z](\\.?\\w)*'
  155. });
  156. const NUMBERS = {
  157. className: 'number',
  158. variants: [
  159. {
  160. begin: '\\b(0b[01\']+)'
  161. },
  162. {
  163. begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)'
  164. },
  165. {
  166. begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)'
  167. }
  168. ],
  169. relevance: 0
  170. };
  171. const VERBATIM_STRING = {
  172. className: 'string',
  173. begin: '@"',
  174. end: '"',
  175. contains: [
  176. {
  177. begin: '""'
  178. }
  179. ]
  180. };
  181. const VERBATIM_STRING_NO_LF = hljs.inherit(VERBATIM_STRING, {
  182. illegal: /\n/
  183. });
  184. const SUBST = {
  185. className: 'subst',
  186. begin: /\{/,
  187. end: /\}/,
  188. keywords: KEYWORDS
  189. };
  190. const SUBST_NO_LF = hljs.inherit(SUBST, {
  191. illegal: /\n/
  192. });
  193. const INTERPOLATED_STRING = {
  194. className: 'string',
  195. begin: /\$"/,
  196. end: '"',
  197. illegal: /\n/,
  198. contains: [
  199. {
  200. begin: /\{\{/
  201. },
  202. {
  203. begin: /\}\}/
  204. },
  205. hljs.BACKSLASH_ESCAPE,
  206. SUBST_NO_LF
  207. ]
  208. };
  209. const INTERPOLATED_VERBATIM_STRING = {
  210. className: 'string',
  211. begin: /\$@"/,
  212. end: '"',
  213. contains: [
  214. {
  215. begin: /\{\{/
  216. },
  217. {
  218. begin: /\}\}/
  219. },
  220. {
  221. begin: '""'
  222. },
  223. SUBST
  224. ]
  225. };
  226. const INTERPOLATED_VERBATIM_STRING_NO_LF = hljs.inherit(INTERPOLATED_VERBATIM_STRING, {
  227. illegal: /\n/,
  228. contains: [
  229. {
  230. begin: /\{\{/
  231. },
  232. {
  233. begin: /\}\}/
  234. },
  235. {
  236. begin: '""'
  237. },
  238. SUBST_NO_LF
  239. ]
  240. });
  241. SUBST.contains = [
  242. INTERPOLATED_VERBATIM_STRING,
  243. INTERPOLATED_STRING,
  244. VERBATIM_STRING,
  245. hljs.APOS_STRING_MODE,
  246. hljs.QUOTE_STRING_MODE,
  247. NUMBERS,
  248. hljs.C_BLOCK_COMMENT_MODE
  249. ];
  250. SUBST_NO_LF.contains = [
  251. INTERPOLATED_VERBATIM_STRING_NO_LF,
  252. INTERPOLATED_STRING,
  253. VERBATIM_STRING_NO_LF,
  254. hljs.APOS_STRING_MODE,
  255. hljs.QUOTE_STRING_MODE,
  256. NUMBERS,
  257. hljs.inherit(hljs.C_BLOCK_COMMENT_MODE, {
  258. illegal: /\n/
  259. })
  260. ];
  261. const STRING = {
  262. variants: [
  263. INTERPOLATED_VERBATIM_STRING,
  264. INTERPOLATED_STRING,
  265. VERBATIM_STRING,
  266. hljs.APOS_STRING_MODE,
  267. hljs.QUOTE_STRING_MODE
  268. ]
  269. };
  270. const GENERIC_MODIFIER = {
  271. begin: "<",
  272. end: ">",
  273. contains: [
  274. {
  275. beginKeywords: "in out"
  276. },
  277. TITLE_MODE
  278. ]
  279. };
  280. const TYPE_IDENT_RE = hljs.IDENT_RE + '(<' + hljs.IDENT_RE + '(\\s*,\\s*' + hljs.IDENT_RE + ')*>)?(\\[\\])?';
  281. const AT_IDENTIFIER = {
  282. // prevents expressions like `@class` from incorrect flagging
  283. // `class` as a keyword
  284. begin: "@" + hljs.IDENT_RE,
  285. relevance: 0
  286. };
  287. return {
  288. name: 'C#',
  289. aliases: [
  290. 'cs',
  291. 'c#'
  292. ],
  293. keywords: KEYWORDS,
  294. illegal: /::/,
  295. contains: [
  296. hljs.COMMENT(
  297. '///',
  298. '$',
  299. {
  300. returnBegin: true,
  301. contains: [
  302. {
  303. className: 'doctag',
  304. variants: [
  305. {
  306. begin: '///',
  307. relevance: 0
  308. },
  309. {
  310. begin: '<!--|-->'
  311. },
  312. {
  313. begin: '</?',
  314. end: '>'
  315. }
  316. ]
  317. }
  318. ]
  319. }
  320. ),
  321. hljs.C_LINE_COMMENT_MODE,
  322. hljs.C_BLOCK_COMMENT_MODE,
  323. {
  324. className: 'meta',
  325. begin: '#',
  326. end: '$',
  327. keywords: {
  328. 'meta-keyword': 'if else elif endif define undef warning error line region endregion pragma checksum'
  329. }
  330. },
  331. STRING,
  332. NUMBERS,
  333. {
  334. beginKeywords: 'class interface',
  335. relevance: 0,
  336. end: /[{;=]/,
  337. illegal: /[^\s:,]/,
  338. contains: [
  339. {
  340. beginKeywords: "where class"
  341. },
  342. TITLE_MODE,
  343. GENERIC_MODIFIER,
  344. hljs.C_LINE_COMMENT_MODE,
  345. hljs.C_BLOCK_COMMENT_MODE
  346. ]
  347. },
  348. {
  349. beginKeywords: 'namespace',
  350. relevance: 0,
  351. end: /[{;=]/,
  352. illegal: /[^\s:]/,
  353. contains: [
  354. TITLE_MODE,
  355. hljs.C_LINE_COMMENT_MODE,
  356. hljs.C_BLOCK_COMMENT_MODE
  357. ]
  358. },
  359. {
  360. beginKeywords: 'record',
  361. relevance: 0,
  362. end: /[{;=]/,
  363. illegal: /[^\s:]/,
  364. contains: [
  365. TITLE_MODE,
  366. GENERIC_MODIFIER,
  367. hljs.C_LINE_COMMENT_MODE,
  368. hljs.C_BLOCK_COMMENT_MODE
  369. ]
  370. },
  371. {
  372. // [Attributes("")]
  373. className: 'meta',
  374. begin: '^\\s*\\[',
  375. excludeBegin: true,
  376. end: '\\]',
  377. excludeEnd: true,
  378. contains: [
  379. {
  380. className: 'meta-string',
  381. begin: /"/,
  382. end: /"/
  383. }
  384. ]
  385. },
  386. {
  387. // Expression keywords prevent 'keyword Name(...)' from being
  388. // recognized as a function definition
  389. beginKeywords: 'new return throw await else',
  390. relevance: 0
  391. },
  392. {
  393. className: 'function',
  394. begin: '(' + TYPE_IDENT_RE + '\\s+)+' + hljs.IDENT_RE + '\\s*(<.+>\\s*)?\\(',
  395. returnBegin: true,
  396. end: /\s*[{;=]/,
  397. excludeEnd: true,
  398. keywords: KEYWORDS,
  399. contains: [
  400. // prevents these from being highlighted `title`
  401. {
  402. beginKeywords: FUNCTION_MODIFIERS.join(" "),
  403. relevance: 0
  404. },
  405. {
  406. begin: hljs.IDENT_RE + '\\s*(<.+>\\s*)?\\(',
  407. returnBegin: true,
  408. contains: [
  409. hljs.TITLE_MODE,
  410. GENERIC_MODIFIER
  411. ],
  412. relevance: 0
  413. },
  414. {
  415. className: 'params',
  416. begin: /\(/,
  417. end: /\)/,
  418. excludeBegin: true,
  419. excludeEnd: true,
  420. keywords: KEYWORDS,
  421. relevance: 0,
  422. contains: [
  423. STRING,
  424. NUMBERS,
  425. hljs.C_BLOCK_COMMENT_MODE
  426. ]
  427. },
  428. hljs.C_LINE_COMMENT_MODE,
  429. hljs.C_BLOCK_COMMENT_MODE
  430. ]
  431. },
  432. AT_IDENTIFIER
  433. ]
  434. };
  435. }
  436. module.exports = csharp;