csharp.js (6523B)
1 /* 2 Language: C# 3 Author: Jason Diamond <jason@diamond.name> 4 Contributor: Nicolas LLOBERA <nllobera@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com> 5 Website: https://docs.microsoft.com/en-us/dotnet/csharp/ 6 Category: common 7 */ 8 9 /** @type LanguageFn */ 10 function csharp(hljs) { 11 var KEYWORDS = { 12 keyword: 13 // Normal keywords. 14 'abstract as base bool break byte case catch char checked const continue decimal ' + 15 'default delegate do double enum event explicit extern finally fixed float ' + 16 'for foreach goto if implicit in init int interface internal is lock long ' + 17 'object operator out override params private protected public readonly ref sbyte ' + 18 'sealed short sizeof stackalloc static string struct switch this try typeof ' + 19 'uint ulong unchecked unsafe ushort using virtual void volatile while ' + 20 // Contextual keywords. 21 'add alias ascending async await by descending dynamic equals from get global group into join ' + 22 'let nameof on orderby partial remove select set value var when where yield', 23 literal: 24 'null false true' 25 }; 26 var TITLE_MODE = hljs.inherit(hljs.TITLE_MODE, {begin: '[a-zA-Z](\\.?\\w)*'}); 27 var NUMBERS = { 28 className: 'number', 29 variants: [ 30 { begin: '\\b(0b[01\']+)' }, 31 { begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' }, 32 { begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' } 33 ], 34 relevance: 0 35 }; 36 var VERBATIM_STRING = { 37 className: 'string', 38 begin: '@"', end: '"', 39 contains: [{begin: '""'}] 40 }; 41 var VERBATIM_STRING_NO_LF = hljs.inherit(VERBATIM_STRING, {illegal: /\n/}); 42 var SUBST = { 43 className: 'subst', 44 begin: '{', end: '}', 45 keywords: KEYWORDS 46 }; 47 var SUBST_NO_LF = hljs.inherit(SUBST, {illegal: /\n/}); 48 var INTERPOLATED_STRING = { 49 className: 'string', 50 begin: /\$"/, end: '"', 51 illegal: /\n/, 52 contains: [{begin: '{{'}, {begin: '}}'}, hljs.BACKSLASH_ESCAPE, SUBST_NO_LF] 53 }; 54 var INTERPOLATED_VERBATIM_STRING = { 55 className: 'string', 56 begin: /\$@"/, end: '"', 57 contains: [{begin: '{{'}, {begin: '}}'}, {begin: '""'}, SUBST] 58 }; 59 var INTERPOLATED_VERBATIM_STRING_NO_LF = hljs.inherit(INTERPOLATED_VERBATIM_STRING, { 60 illegal: /\n/, 61 contains: [{begin: '{{'}, {begin: '}}'}, {begin: '""'}, SUBST_NO_LF] 62 }); 63 SUBST.contains = [ 64 INTERPOLATED_VERBATIM_STRING, 65 INTERPOLATED_STRING, 66 VERBATIM_STRING, 67 hljs.APOS_STRING_MODE, 68 hljs.QUOTE_STRING_MODE, 69 NUMBERS, 70 hljs.C_BLOCK_COMMENT_MODE 71 ]; 72 SUBST_NO_LF.contains = [ 73 INTERPOLATED_VERBATIM_STRING_NO_LF, 74 INTERPOLATED_STRING, 75 VERBATIM_STRING_NO_LF, 76 hljs.APOS_STRING_MODE, 77 hljs.QUOTE_STRING_MODE, 78 NUMBERS, 79 hljs.inherit(hljs.C_BLOCK_COMMENT_MODE, {illegal: /\n/}) 80 ]; 81 var STRING = { 82 variants: [ 83 INTERPOLATED_VERBATIM_STRING, 84 INTERPOLATED_STRING, 85 VERBATIM_STRING, 86 hljs.APOS_STRING_MODE, 87 hljs.QUOTE_STRING_MODE 88 ] 89 }; 90 91 var GENERIC_MODIFIER = { 92 begin: "<", 93 end: ">", 94 contains: [ 95 { beginKeywords: "in out"}, 96 TITLE_MODE 97 ] 98 }; 99 var TYPE_IDENT_RE = hljs.IDENT_RE + '(<' + hljs.IDENT_RE + '(\\s*,\\s*' + hljs.IDENT_RE + ')*>)?(\\[\\])?'; 100 var AT_IDENTIFIER = { 101 // prevents expressions like `@class` from incorrect flagging 102 // `class` as a keyword 103 begin: "@" + hljs.IDENT_RE, 104 relevance: 0 105 }; 106 107 return { 108 name: 'C#', 109 aliases: ['cs', 'c#'], 110 keywords: KEYWORDS, 111 illegal: /::/, 112 contains: [ 113 hljs.COMMENT( 114 '///', 115 '$', 116 { 117 returnBegin: true, 118 contains: [ 119 { 120 className: 'doctag', 121 variants: [ 122 { 123 begin: '///', relevance: 0 124 }, 125 { 126 begin: '<!--|-->' 127 }, 128 { 129 begin: '</?', end: '>' 130 } 131 ] 132 } 133 ] 134 } 135 ), 136 hljs.C_LINE_COMMENT_MODE, 137 hljs.C_BLOCK_COMMENT_MODE, 138 { 139 className: 'meta', 140 begin: '#', end: '$', 141 keywords: { 142 'meta-keyword': 'if else elif endif define undef warning error line region endregion pragma checksum' 143 } 144 }, 145 STRING, 146 NUMBERS, 147 { 148 beginKeywords: 'class interface', end: /[{;=]/, 149 illegal: /[^\s:,]/, 150 contains: [ 151 { beginKeywords: "where class" }, 152 TITLE_MODE, 153 GENERIC_MODIFIER, 154 hljs.C_LINE_COMMENT_MODE, 155 hljs.C_BLOCK_COMMENT_MODE 156 ] 157 }, 158 { 159 beginKeywords: 'namespace', end: /[{;=]/, 160 illegal: /[^\s:]/, 161 contains: [ 162 TITLE_MODE, 163 hljs.C_LINE_COMMENT_MODE, 164 hljs.C_BLOCK_COMMENT_MODE 165 ] 166 }, 167 { 168 beginKeywords: 'record', end: /[{;=]/, 169 illegal: /[^\s:]/, 170 contains: [ 171 TITLE_MODE, 172 GENERIC_MODIFIER, 173 hljs.C_LINE_COMMENT_MODE, 174 hljs.C_BLOCK_COMMENT_MODE 175 ] 176 }, 177 { 178 // [Attributes("")] 179 className: 'meta', 180 begin: '^\\s*\\[', excludeBegin: true, end: '\\]', excludeEnd: true, 181 contains: [ 182 {className: 'meta-string', begin: /"/, end: /"/} 183 ] 184 }, 185 { 186 // Expression keywords prevent 'keyword Name(...)' from being 187 // recognized as a function definition 188 beginKeywords: 'new return throw await else', 189 relevance: 0 190 }, 191 { 192 className: 'function', 193 begin: '(' + TYPE_IDENT_RE + '\\s+)+' + hljs.IDENT_RE + '\\s*(\\<.+\\>)?\\s*\\(', returnBegin: true, 194 end: /\s*[{;=]/, excludeEnd: true, 195 keywords: KEYWORDS, 196 contains: [ 197 { 198 begin: hljs.IDENT_RE + '\\s*(\\<.+\\>)?\\s*\\(', returnBegin: true, 199 contains: [ 200 hljs.TITLE_MODE, 201 GENERIC_MODIFIER 202 ], 203 relevance: 0 204 }, 205 { 206 className: 'params', 207 begin: /\(/, end: /\)/, 208 excludeBegin: true, 209 excludeEnd: true, 210 keywords: KEYWORDS, 211 relevance: 0, 212 contains: [ 213 STRING, 214 NUMBERS, 215 hljs.C_BLOCK_COMMENT_MODE 216 ] 217 }, 218 hljs.C_LINE_COMMENT_MODE, 219 hljs.C_BLOCK_COMMENT_MODE 220 ] 221 }, 222 AT_IDENTIFIER 223 ] 224 }; 225 } 226 227 module.exports = csharp;