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