csharp.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/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. 'catch',
  62. 'class',
  63. 'const',
  64. 'continue',
  65. 'do',
  66. 'else',
  67. 'event',
  68. 'explicit',
  69. 'extern',
  70. 'finally',
  71. 'fixed',
  72. 'for',
  73. 'foreach',
  74. 'goto',
  75. 'if',
  76. 'implicit',
  77. 'in',
  78. 'interface',
  79. 'internal',
  80. 'is',
  81. 'lock',
  82. 'namespace',
  83. 'new',
  84. 'operator',
  85. 'out',
  86. 'override',
  87. 'params',
  88. 'private',
  89. 'protected',
  90. 'public',
  91. 'readonly',
  92. 'record',
  93. 'ref',
  94. 'return',
  95. 'scoped',
  96. 'sealed',
  97. 'sizeof',
  98. 'stackalloc',
  99. 'static',
  100. 'struct',
  101. 'switch',
  102. 'this',
  103. 'throw',
  104. 'try',
  105. 'typeof',
  106. 'unchecked',
  107. 'unsafe',
  108. 'using',
  109. 'virtual',
  110. 'void',
  111. 'volatile',
  112. 'while'
  113. ];
  114. const CONTEXTUAL_KEYWORDS = [
  115. 'add',
  116. 'alias',
  117. 'and',
  118. 'ascending',
  119. 'async',
  120. 'await',
  121. 'by',
  122. 'descending',
  123. 'equals',
  124. 'from',
  125. 'get',
  126. 'global',
  127. 'group',
  128. 'init',
  129. 'into',
  130. 'join',
  131. 'let',
  132. 'nameof',
  133. 'not',
  134. 'notnull',
  135. 'on',
  136. 'or',
  137. 'orderby',
  138. 'partial',
  139. 'remove',
  140. 'select',
  141. 'set',
  142. 'unmanaged',
  143. 'value|0',
  144. 'var',
  145. 'when',
  146. 'where',
  147. 'with',
  148. 'yield'
  149. ];
  150. const KEYWORDS = {
  151. keyword: NORMAL_KEYWORDS.concat(CONTEXTUAL_KEYWORDS),
  152. built_in: BUILT_IN_KEYWORDS,
  153. literal: LITERAL_KEYWORDS
  154. };
  155. const TITLE_MODE = hljs.inherit(hljs.TITLE_MODE, { begin: '[a-zA-Z](\\.?\\w)*' });
  156. const NUMBERS = {
  157. className: 'number',
  158. variants: [
  159. { begin: '\\b(0b[01\']+)' },
  160. { begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
  161. { begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
  162. ],
  163. relevance: 0
  164. };
  165. const RAW_STRING = {
  166. className: 'string',
  167. begin: /"""("*)(?!")(.|\n)*?"""\1/,
  168. relevance: 1
  169. };
  170. const VERBATIM_STRING = {
  171. className: 'string',
  172. begin: '@"',
  173. end: '"',
  174. contains: [ { begin: '""' } ]
  175. };
  176. const VERBATIM_STRING_NO_LF = hljs.inherit(VERBATIM_STRING, { illegal: /\n/ });
  177. const SUBST = {
  178. className: 'subst',
  179. begin: /\{/,
  180. end: /\}/,
  181. keywords: KEYWORDS
  182. };
  183. const SUBST_NO_LF = hljs.inherit(SUBST, { illegal: /\n/ });
  184. const INTERPOLATED_STRING = {
  185. className: 'string',
  186. begin: /\$"/,
  187. end: '"',
  188. illegal: /\n/,
  189. contains: [
  190. { begin: /\{\{/ },
  191. { begin: /\}\}/ },
  192. hljs.BACKSLASH_ESCAPE,
  193. SUBST_NO_LF
  194. ]
  195. };
  196. const INTERPOLATED_VERBATIM_STRING = {
  197. className: 'string',
  198. begin: /\$@"/,
  199. end: '"',
  200. contains: [
  201. { begin: /\{\{/ },
  202. { begin: /\}\}/ },
  203. { begin: '""' },
  204. SUBST
  205. ]
  206. };
  207. const INTERPOLATED_VERBATIM_STRING_NO_LF = hljs.inherit(INTERPOLATED_VERBATIM_STRING, {
  208. illegal: /\n/,
  209. contains: [
  210. { begin: /\{\{/ },
  211. { begin: /\}\}/ },
  212. { begin: '""' },
  213. SUBST_NO_LF
  214. ]
  215. });
  216. SUBST.contains = [
  217. INTERPOLATED_VERBATIM_STRING,
  218. INTERPOLATED_STRING,
  219. VERBATIM_STRING,
  220. hljs.APOS_STRING_MODE,
  221. hljs.QUOTE_STRING_MODE,
  222. NUMBERS,
  223. hljs.C_BLOCK_COMMENT_MODE
  224. ];
  225. SUBST_NO_LF.contains = [
  226. INTERPOLATED_VERBATIM_STRING_NO_LF,
  227. INTERPOLATED_STRING,
  228. VERBATIM_STRING_NO_LF,
  229. hljs.APOS_STRING_MODE,
  230. hljs.QUOTE_STRING_MODE,
  231. NUMBERS,
  232. hljs.inherit(hljs.C_BLOCK_COMMENT_MODE, { illegal: /\n/ })
  233. ];
  234. const STRING = { variants: [
  235. RAW_STRING,
  236. INTERPOLATED_VERBATIM_STRING,
  237. INTERPOLATED_STRING,
  238. VERBATIM_STRING,
  239. hljs.APOS_STRING_MODE,
  240. hljs.QUOTE_STRING_MODE
  241. ] };
  242. const GENERIC_MODIFIER = {
  243. begin: "<",
  244. end: ">",
  245. contains: [
  246. { beginKeywords: "in out" },
  247. TITLE_MODE
  248. ]
  249. };
  250. const TYPE_IDENT_RE = hljs.IDENT_RE + '(<' + hljs.IDENT_RE + '(\\s*,\\s*' + hljs.IDENT_RE + ')*>)?(\\[\\])?';
  251. const AT_IDENTIFIER = {
  252. // prevents expressions like `@class` from incorrect flagging
  253. // `class` as a keyword
  254. begin: "@" + hljs.IDENT_RE,
  255. relevance: 0
  256. };
  257. return {
  258. name: 'C#',
  259. aliases: [
  260. 'cs',
  261. 'c#'
  262. ],
  263. keywords: KEYWORDS,
  264. illegal: /::/,
  265. contains: [
  266. hljs.COMMENT(
  267. '///',
  268. '$',
  269. {
  270. returnBegin: true,
  271. contains: [
  272. {
  273. className: 'doctag',
  274. variants: [
  275. {
  276. begin: '///',
  277. relevance: 0
  278. },
  279. { begin: '<!--|-->' },
  280. {
  281. begin: '</?',
  282. end: '>'
  283. }
  284. ]
  285. }
  286. ]
  287. }
  288. ),
  289. hljs.C_LINE_COMMENT_MODE,
  290. hljs.C_BLOCK_COMMENT_MODE,
  291. {
  292. className: 'meta',
  293. begin: '#',
  294. end: '$',
  295. keywords: { keyword: 'if else elif endif define undef warning error line region endregion pragma checksum' }
  296. },
  297. STRING,
  298. NUMBERS,
  299. {
  300. beginKeywords: 'class interface',
  301. relevance: 0,
  302. end: /[{;=]/,
  303. illegal: /[^\s:,]/,
  304. contains: [
  305. { beginKeywords: "where class" },
  306. TITLE_MODE,
  307. GENERIC_MODIFIER,
  308. hljs.C_LINE_COMMENT_MODE,
  309. hljs.C_BLOCK_COMMENT_MODE
  310. ]
  311. },
  312. {
  313. beginKeywords: 'namespace',
  314. relevance: 0,
  315. end: /[{;=]/,
  316. illegal: /[^\s:]/,
  317. contains: [
  318. TITLE_MODE,
  319. hljs.C_LINE_COMMENT_MODE,
  320. hljs.C_BLOCK_COMMENT_MODE
  321. ]
  322. },
  323. {
  324. beginKeywords: 'record',
  325. relevance: 0,
  326. end: /[{;=]/,
  327. illegal: /[^\s:]/,
  328. contains: [
  329. TITLE_MODE,
  330. GENERIC_MODIFIER,
  331. hljs.C_LINE_COMMENT_MODE,
  332. hljs.C_BLOCK_COMMENT_MODE
  333. ]
  334. },
  335. {
  336. // [Attributes("")]
  337. className: 'meta',
  338. begin: '^\\s*\\[(?=[\\w])',
  339. excludeBegin: true,
  340. end: '\\]',
  341. excludeEnd: true,
  342. contains: [
  343. {
  344. className: 'string',
  345. begin: /"/,
  346. end: /"/
  347. }
  348. ]
  349. },
  350. {
  351. // Expression keywords prevent 'keyword Name(...)' from being
  352. // recognized as a function definition
  353. beginKeywords: 'new return throw await else',
  354. relevance: 0
  355. },
  356. {
  357. className: 'function',
  358. begin: '(' + TYPE_IDENT_RE + '\\s+)+' + hljs.IDENT_RE + '\\s*(<[^=]+>\\s*)?\\(',
  359. returnBegin: true,
  360. end: /\s*[{;=]/,
  361. excludeEnd: true,
  362. keywords: KEYWORDS,
  363. contains: [
  364. // prevents these from being highlighted `title`
  365. {
  366. beginKeywords: FUNCTION_MODIFIERS.join(" "),
  367. relevance: 0
  368. },
  369. {
  370. begin: hljs.IDENT_RE + '\\s*(<[^=]+>\\s*)?\\(',
  371. returnBegin: true,
  372. contains: [
  373. hljs.TITLE_MODE,
  374. GENERIC_MODIFIER
  375. ],
  376. relevance: 0
  377. },
  378. { match: /\(\)/ },
  379. {
  380. className: 'params',
  381. begin: /\(/,
  382. end: /\)/,
  383. excludeBegin: true,
  384. excludeEnd: true,
  385. keywords: KEYWORDS,
  386. relevance: 0,
  387. contains: [
  388. STRING,
  389. NUMBERS,
  390. hljs.C_BLOCK_COMMENT_MODE
  391. ]
  392. },
  393. hljs.C_LINE_COMMENT_MODE,
  394. hljs.C_BLOCK_COMMENT_MODE
  395. ]
  396. },
  397. AT_IDENTIFIER
  398. ]
  399. };
  400. }
  401. export { csharp as default };