prismjs_components_prism-csharp.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // node_modules/prismjs/components/prism-csharp.js
  2. (function(Prism2) {
  3. function replace(pattern, replacements) {
  4. return pattern.replace(/<<(\d+)>>/g, function(m, index) {
  5. return "(?:" + replacements[+index] + ")";
  6. });
  7. }
  8. function re(pattern, replacements, flags) {
  9. return RegExp(replace(pattern, replacements), flags || "");
  10. }
  11. function nested(pattern, depthLog2) {
  12. for (var i = 0; i < depthLog2; i++) {
  13. pattern = pattern.replace(/<<self>>/g, function() {
  14. return "(?:" + pattern + ")";
  15. });
  16. }
  17. return pattern.replace(/<<self>>/g, "[^\\s\\S]");
  18. }
  19. var keywordKinds = {
  20. // keywords which represent a return or variable type
  21. type: "bool byte char decimal double dynamic float int long object sbyte short string uint ulong ushort var void",
  22. // keywords which are used to declare a type
  23. typeDeclaration: "class enum interface record struct",
  24. // contextual keywords
  25. // ("var" and "dynamic" are missing because they are used like types)
  26. contextual: "add alias and ascending async await by descending from(?=\\s*(?:\\w|$)) get global group into init(?=\\s*;) join let nameof not notnull on or orderby partial remove select set unmanaged value when where with(?=\\s*{)",
  27. // all other keywords
  28. other: "abstract as base break case catch checked const continue default delegate do else event explicit extern finally fixed for foreach goto if implicit in internal is lock namespace new null operator out override params private protected public readonly ref return sealed sizeof stackalloc static switch this throw try typeof unchecked unsafe using virtual volatile while yield"
  29. };
  30. function keywordsToPattern(words) {
  31. return "\\b(?:" + words.trim().replace(/ /g, "|") + ")\\b";
  32. }
  33. var typeDeclarationKeywords = keywordsToPattern(keywordKinds.typeDeclaration);
  34. var keywords = RegExp(keywordsToPattern(keywordKinds.type + " " + keywordKinds.typeDeclaration + " " + keywordKinds.contextual + " " + keywordKinds.other));
  35. var nonTypeKeywords = keywordsToPattern(keywordKinds.typeDeclaration + " " + keywordKinds.contextual + " " + keywordKinds.other);
  36. var nonContextualKeywords = keywordsToPattern(keywordKinds.type + " " + keywordKinds.typeDeclaration + " " + keywordKinds.other);
  37. var generic = nested(/<(?:[^<>;=+\-*/%&|^]|<<self>>)*>/.source, 2);
  38. var nestedRound = nested(/\((?:[^()]|<<self>>)*\)/.source, 2);
  39. var name = /@?\b[A-Za-z_]\w*\b/.source;
  40. var genericName = replace(/<<0>>(?:\s*<<1>>)?/.source, [name, generic]);
  41. var identifier = replace(/(?!<<0>>)<<1>>(?:\s*\.\s*<<1>>)*/.source, [nonTypeKeywords, genericName]);
  42. var array = /\[\s*(?:,\s*)*\]/.source;
  43. var typeExpressionWithoutTuple = replace(/<<0>>(?:\s*(?:\?\s*)?<<1>>)*(?:\s*\?)?/.source, [identifier, array]);
  44. var tupleElement = replace(/[^,()<>[\];=+\-*/%&|^]|<<0>>|<<1>>|<<2>>/.source, [generic, nestedRound, array]);
  45. var tuple = replace(/\(<<0>>+(?:,<<0>>+)+\)/.source, [tupleElement]);
  46. var typeExpression = replace(/(?:<<0>>|<<1>>)(?:\s*(?:\?\s*)?<<2>>)*(?:\s*\?)?/.source, [tuple, identifier, array]);
  47. var typeInside = {
  48. "keyword": keywords,
  49. "punctuation": /[<>()?,.:[\]]/
  50. };
  51. var character = /'(?:[^\r\n'\\]|\\.|\\[Uux][\da-fA-F]{1,8})'/.source;
  52. var regularString = /"(?:\\.|[^\\"\r\n])*"/.source;
  53. var verbatimString = /@"(?:""|\\[\s\S]|[^\\"])*"(?!")/.source;
  54. Prism2.languages.csharp = Prism2.languages.extend("clike", {
  55. "string": [
  56. {
  57. pattern: re(/(^|[^$\\])<<0>>/.source, [verbatimString]),
  58. lookbehind: true,
  59. greedy: true
  60. },
  61. {
  62. pattern: re(/(^|[^@$\\])<<0>>/.source, [regularString]),
  63. lookbehind: true,
  64. greedy: true
  65. }
  66. ],
  67. "class-name": [
  68. {
  69. // Using static
  70. // using static System.Math;
  71. pattern: re(/(\busing\s+static\s+)<<0>>(?=\s*;)/.source, [identifier]),
  72. lookbehind: true,
  73. inside: typeInside
  74. },
  75. {
  76. // Using alias (type)
  77. // using Project = PC.MyCompany.Project;
  78. pattern: re(/(\busing\s+<<0>>\s*=\s*)<<1>>(?=\s*;)/.source, [name, typeExpression]),
  79. lookbehind: true,
  80. inside: typeInside
  81. },
  82. {
  83. // Using alias (alias)
  84. // using Project = PC.MyCompany.Project;
  85. pattern: re(/(\busing\s+)<<0>>(?=\s*=)/.source, [name]),
  86. lookbehind: true
  87. },
  88. {
  89. // Type declarations
  90. // class Foo<A, B>
  91. // interface Foo<out A, B>
  92. pattern: re(/(\b<<0>>\s+)<<1>>/.source, [typeDeclarationKeywords, genericName]),
  93. lookbehind: true,
  94. inside: typeInside
  95. },
  96. {
  97. // Single catch exception declaration
  98. // catch(Foo)
  99. // (things like catch(Foo e) is covered by variable declaration)
  100. pattern: re(/(\bcatch\s*\(\s*)<<0>>/.source, [identifier]),
  101. lookbehind: true,
  102. inside: typeInside
  103. },
  104. {
  105. // Name of the type parameter of generic constraints
  106. // where Foo : class
  107. pattern: re(/(\bwhere\s+)<<0>>/.source, [name]),
  108. lookbehind: true
  109. },
  110. {
  111. // Casts and checks via as and is.
  112. // as Foo<A>, is Bar<B>
  113. // (things like if(a is Foo b) is covered by variable declaration)
  114. pattern: re(/(\b(?:is(?:\s+not)?|as)\s+)<<0>>/.source, [typeExpressionWithoutTuple]),
  115. lookbehind: true,
  116. inside: typeInside
  117. },
  118. {
  119. // Variable, field and parameter declaration
  120. // (Foo bar, Bar baz, Foo[,,] bay, Foo<Bar, FooBar<Bar>> bax)
  121. pattern: re(/\b<<0>>(?=\s+(?!<<1>>|with\s*\{)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
  122. inside: typeInside
  123. }
  124. ],
  125. "keyword": keywords,
  126. // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#literals
  127. "number": /(?:\b0(?:x[\da-f_]*[\da-f]|b[01_]*[01])|(?:\B\.\d+(?:_+\d+)*|\b\d+(?:_+\d+)*(?:\.\d+(?:_+\d+)*)?)(?:e[-+]?\d+(?:_+\d+)*)?)(?:[dflmu]|lu|ul)?\b/i,
  128. "operator": />>=?|<<=?|[-=]>|([-+&|])\1|~|\?\?=?|[-+*/%&|^!=<>]=?/,
  129. "punctuation": /\?\.?|::|[{}[\];(),.:]/
  130. });
  131. Prism2.languages.insertBefore("csharp", "number", {
  132. "range": {
  133. pattern: /\.\./,
  134. alias: "operator"
  135. }
  136. });
  137. Prism2.languages.insertBefore("csharp", "punctuation", {
  138. "named-parameter": {
  139. pattern: re(/([(,]\s*)<<0>>(?=\s*:)/.source, [name]),
  140. lookbehind: true,
  141. alias: "punctuation"
  142. }
  143. });
  144. Prism2.languages.insertBefore("csharp", "class-name", {
  145. "namespace": {
  146. // namespace Foo.Bar {}
  147. // using Foo.Bar;
  148. pattern: re(/(\b(?:namespace|using)\s+)<<0>>(?:\s*\.\s*<<0>>)*(?=\s*[;{])/.source, [name]),
  149. lookbehind: true,
  150. inside: {
  151. "punctuation": /\./
  152. }
  153. },
  154. "type-expression": {
  155. // default(Foo), typeof(Foo<Bar>), sizeof(int)
  156. pattern: re(/(\b(?:default|sizeof|typeof)\s*\(\s*(?!\s))(?:[^()\s]|\s(?!\s)|<<0>>)*(?=\s*\))/.source, [nestedRound]),
  157. lookbehind: true,
  158. alias: "class-name",
  159. inside: typeInside
  160. },
  161. "return-type": {
  162. // Foo<Bar> ForBar(); Foo IFoo.Bar() => 0
  163. // int this[int index] => 0; T IReadOnlyList<T>.this[int index] => this[index];
  164. // int Foo => 0; int Foo { get; set } = 0;
  165. pattern: re(/<<0>>(?=\s+(?:<<1>>\s*(?:=>|[({]|\.\s*this\s*\[)|this\s*\[))/.source, [typeExpression, identifier]),
  166. inside: typeInside,
  167. alias: "class-name"
  168. },
  169. "constructor-invocation": {
  170. // new List<Foo<Bar[]>> { }
  171. pattern: re(/(\bnew\s+)<<0>>(?=\s*[[({])/.source, [typeExpression]),
  172. lookbehind: true,
  173. inside: typeInside,
  174. alias: "class-name"
  175. },
  176. /*'explicit-implementation': {
  177. // int IFoo<Foo>.Bar => 0; void IFoo<Foo<Foo>>.Foo<T>();
  178. pattern: replace(/\b<<0>>(?=\.<<1>>)/, className, methodOrPropertyDeclaration),
  179. inside: classNameInside,
  180. alias: 'class-name'
  181. },*/
  182. "generic-method": {
  183. // foo<Bar>()
  184. pattern: re(/<<0>>\s*<<1>>(?=\s*\()/.source, [name, generic]),
  185. inside: {
  186. "function": re(/^<<0>>/.source, [name]),
  187. "generic": {
  188. pattern: RegExp(generic),
  189. alias: "class-name",
  190. inside: typeInside
  191. }
  192. }
  193. },
  194. "type-list": {
  195. // The list of types inherited or of generic constraints
  196. // class Foo<F> : Bar, IList<FooBar>
  197. // where F : Bar, IList<int>
  198. pattern: re(
  199. /\b((?:<<0>>\s+<<1>>|record\s+<<1>>\s*<<5>>|where\s+<<2>>)\s*:\s*)(?:<<3>>|<<4>>|<<1>>\s*<<5>>|<<6>>)(?:\s*,\s*(?:<<3>>|<<4>>|<<6>>))*(?=\s*(?:where|[{;]|=>|$))/.source,
  200. [typeDeclarationKeywords, genericName, name, typeExpression, keywords.source, nestedRound, /\bnew\s*\(\s*\)/.source]
  201. ),
  202. lookbehind: true,
  203. inside: {
  204. "record-arguments": {
  205. pattern: re(/(^(?!new\s*\()<<0>>\s*)<<1>>/.source, [genericName, nestedRound]),
  206. lookbehind: true,
  207. greedy: true,
  208. inside: Prism2.languages.csharp
  209. },
  210. "keyword": keywords,
  211. "class-name": {
  212. pattern: RegExp(typeExpression),
  213. greedy: true,
  214. inside: typeInside
  215. },
  216. "punctuation": /[,()]/
  217. }
  218. },
  219. "preprocessor": {
  220. pattern: /(^[\t ]*)#.*/m,
  221. lookbehind: true,
  222. alias: "property",
  223. inside: {
  224. // highlight preprocessor directives as keywords
  225. "directive": {
  226. pattern: /(#)\b(?:define|elif|else|endif|endregion|error|if|line|nullable|pragma|region|undef|warning)\b/,
  227. lookbehind: true,
  228. alias: "keyword"
  229. }
  230. }
  231. }
  232. });
  233. var regularStringOrCharacter = regularString + "|" + character;
  234. var regularStringCharacterOrComment = replace(/\/(?![*/])|\/\/[^\r\n]*[\r\n]|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>/.source, [regularStringOrCharacter]);
  235. var roundExpression = nested(replace(/[^"'/()]|<<0>>|\(<<self>>*\)/.source, [regularStringCharacterOrComment]), 2);
  236. var attrTarget = /\b(?:assembly|event|field|method|module|param|property|return|type)\b/.source;
  237. var attr = replace(/<<0>>(?:\s*\(<<1>>*\))?/.source, [identifier, roundExpression]);
  238. Prism2.languages.insertBefore("csharp", "class-name", {
  239. "attribute": {
  240. // Attributes
  241. // [Foo], [Foo(1), Bar(2, Prop = "foo")], [return: Foo(1), Bar(2)], [assembly: Foo(Bar)]
  242. pattern: re(/((?:^|[^\s\w>)?])\s*\[\s*)(?:<<0>>\s*:\s*)?<<1>>(?:\s*,\s*<<1>>)*(?=\s*\])/.source, [attrTarget, attr]),
  243. lookbehind: true,
  244. greedy: true,
  245. inside: {
  246. "target": {
  247. pattern: re(/^<<0>>(?=\s*:)/.source, [attrTarget]),
  248. alias: "keyword"
  249. },
  250. "attribute-arguments": {
  251. pattern: re(/\(<<0>>*\)/.source, [roundExpression]),
  252. inside: Prism2.languages.csharp
  253. },
  254. "class-name": {
  255. pattern: RegExp(identifier),
  256. inside: {
  257. "punctuation": /\./
  258. }
  259. },
  260. "punctuation": /[:,]/
  261. }
  262. }
  263. });
  264. var formatString = /:[^}\r\n]+/.source;
  265. var mInterpolationRound = nested(replace(/[^"'/()]|<<0>>|\(<<self>>*\)/.source, [regularStringCharacterOrComment]), 2);
  266. var mInterpolation = replace(/\{(?!\{)(?:(?![}:])<<0>>)*<<1>>?\}/.source, [mInterpolationRound, formatString]);
  267. var sInterpolationRound = nested(replace(/[^"'/()]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>|\(<<self>>*\)/.source, [regularStringOrCharacter]), 2);
  268. var sInterpolation = replace(/\{(?!\{)(?:(?![}:])<<0>>)*<<1>>?\}/.source, [sInterpolationRound, formatString]);
  269. function createInterpolationInside(interpolation, interpolationRound) {
  270. return {
  271. "interpolation": {
  272. pattern: re(/((?:^|[^{])(?:\{\{)*)<<0>>/.source, [interpolation]),
  273. lookbehind: true,
  274. inside: {
  275. "format-string": {
  276. pattern: re(/(^\{(?:(?![}:])<<0>>)*)<<1>>(?=\}$)/.source, [interpolationRound, formatString]),
  277. lookbehind: true,
  278. inside: {
  279. "punctuation": /^:/
  280. }
  281. },
  282. "punctuation": /^\{|\}$/,
  283. "expression": {
  284. pattern: /[\s\S]+/,
  285. alias: "language-csharp",
  286. inside: Prism2.languages.csharp
  287. }
  288. }
  289. },
  290. "string": /[\s\S]+/
  291. };
  292. }
  293. Prism2.languages.insertBefore("csharp", "string", {
  294. "interpolation-string": [
  295. {
  296. pattern: re(/(^|[^\\])(?:\$@|@\$)"(?:""|\\[\s\S]|\{\{|<<0>>|[^\\{"])*"/.source, [mInterpolation]),
  297. lookbehind: true,
  298. greedy: true,
  299. inside: createInterpolationInside(mInterpolation, mInterpolationRound)
  300. },
  301. {
  302. pattern: re(/(^|[^@\\])\$"(?:\\.|\{\{|<<0>>|[^\\"{])*"/.source, [sInterpolation]),
  303. lookbehind: true,
  304. greedy: true,
  305. inside: createInterpolationInside(sInterpolation, sInterpolationRound)
  306. }
  307. ],
  308. "char": {
  309. pattern: RegExp(character),
  310. greedy: true
  311. }
  312. });
  313. Prism2.languages.dotnet = Prism2.languages.cs = Prism2.languages.csharp;
  314. })(Prism);
  315. //# sourceMappingURL=prismjs_components_prism-csharp.js.map