MediaWiki:Gadget-prism-language-csharp.js

来自萌娘共享
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
  1. /**
  2. * -------------------------------------------------------------------------
  3. * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
  4. * -------------------------------------------------------------------------
  5. */
  6. var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/prism-language-csharp/Gadget-prism-language-csharp.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]|longId=103d1a563ea4ccc8ff29fb55c9bcd88329a56eb5|shortId=103d1a56|summary=feat: rename (#594)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com></nowiki>}}';
  7. /* <pre> */
  8. "use strict";
  9. (function (Prism) {
  10. function replace(pattern, replacements) {
  11. return pattern.replace(/<<(\d+)>>/g, function (m, index) {
  12. return '(?:' + replacements[+index] + ')';
  13. });
  14. }
  15. function re(pattern, replacements, flags) {
  16. return RegExp(replace(pattern, replacements), flags || '');
  17. }
  18. function nested(pattern, depthLog2) {
  19. for (var i = 0; i < depthLog2; i++) {
  20. pattern = pattern.replace(/<<self>>/g, function () { return '(?:' + pattern + ')'; });
  21. }
  22. return pattern.replace(/<<self>>/g, '[^\\s\\S]');
  23. }
  24. var keywordKinds = {
  25. type: 'bool byte char decimal double dynamic float int long object sbyte short string uint ulong ushort var void',
  26. typeDeclaration: 'class enum interface record struct',
  27. 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*{)',
  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. Prism.languages.csharp = Prism.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. pattern: re(/(\busing\s+static\s+)<<0>>(?=\s*;)/.source, [identifier]),
  70. lookbehind: true,
  71. inside: typeInside
  72. },
  73. {
  74. pattern: re(/(\busing\s+<<0>>\s*=\s*)<<1>>(?=\s*;)/.source, [name, typeExpression]),
  75. lookbehind: true,
  76. inside: typeInside
  77. },
  78. {
  79. pattern: re(/(\busing\s+)<<0>>(?=\s*=)/.source, [name]),
  80. lookbehind: true
  81. },
  82. {
  83. pattern: re(/(\b<<0>>\s+)<<1>>/.source, [typeDeclarationKeywords, genericName]),
  84. lookbehind: true,
  85. inside: typeInside
  86. },
  87. {
  88. pattern: re(/(\bcatch\s*\(\s*)<<0>>/.source, [identifier]),
  89. lookbehind: true,
  90. inside: typeInside
  91. },
  92. {
  93. pattern: re(/(\bwhere\s+)<<0>>/.source, [name]),
  94. lookbehind: true
  95. },
  96. {
  97. pattern: re(/(\b(?:is(?:\s+not)?|as)\s+)<<0>>/.source, [typeExpressionWithoutTuple]),
  98. lookbehind: true,
  99. inside: typeInside
  100. },
  101. {
  102. pattern: re(/\b<<0>>(?=\s+(?!<<1>>|with\s*\{)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
  103. inside: typeInside
  104. }
  105. ],
  106. 'keyword': keywords,
  107. '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,
  108. 'operator': />>=?|<<=?|[-=]>|([-+&|])\1|~|\?\?=?|[-+*/%&|^!=<>]=?/,
  109. 'punctuation': /\?\.?|::|[{}[\];(),.:]/
  110. });
  111. Prism.languages.insertBefore('csharp', 'number', {
  112. 'range': {
  113. pattern: /\.\./,
  114. alias: 'operator'
  115. }
  116. });
  117. Prism.languages.insertBefore('csharp', 'punctuation', {
  118. 'named-parameter': {
  119. pattern: re(/([(,]\s*)<<0>>(?=\s*:)/.source, [name]),
  120. lookbehind: true,
  121. alias: 'punctuation'
  122. }
  123. });
  124. Prism.languages.insertBefore('csharp', 'class-name', {
  125. 'namespace': {
  126. pattern: re(/(\b(?:namespace|using)\s+)<<0>>(?:\s*\.\s*<<0>>)*(?=\s*[;{])/.source, [name]),
  127. lookbehind: true,
  128. inside: {
  129. 'punctuation': /\./
  130. }
  131. },
  132. 'type-expression': {
  133. pattern: re(/(\b(?:default|sizeof|typeof)\s*\(\s*(?!\s))(?:[^()\s]|\s(?!\s)|<<0>>)*(?=\s*\))/.source, [nestedRound]),
  134. lookbehind: true,
  135. alias: 'class-name',
  136. inside: typeInside
  137. },
  138. 'return-type': {
  139. pattern: re(/<<0>>(?=\s+(?:<<1>>\s*(?:=>|[({]|\.\s*this\s*\[)|this\s*\[))/.source, [typeExpression, identifier]),
  140. inside: typeInside,
  141. alias: 'class-name'
  142. },
  143. 'constructor-invocation': {
  144. pattern: re(/(\bnew\s+)<<0>>(?=\s*[[({])/.source, [typeExpression]),
  145. lookbehind: true,
  146. inside: typeInside,
  147. alias: 'class-name'
  148. },
  149. 'generic-method': {
  150. pattern: re(/<<0>>\s*<<1>>(?=\s*\()/.source, [name, generic]),
  151. inside: {
  152. 'function': re(/^<<0>>/.source, [name]),
  153. 'generic': {
  154. pattern: RegExp(generic),
  155. alias: 'class-name',
  156. inside: typeInside
  157. }
  158. }
  159. },
  160. 'type-list': {
  161. pattern: re(/\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, [typeDeclarationKeywords, genericName, name, typeExpression, keywords.source, nestedRound, /\bnew\s*\(\s*\)/.source]),
  162. lookbehind: true,
  163. inside: {
  164. 'record-arguments': {
  165. pattern: re(/(^(?!new\s*\()<<0>>\s*)<<1>>/.source, [genericName, nestedRound]),
  166. lookbehind: true,
  167. greedy: true,
  168. inside: Prism.languages.csharp
  169. },
  170. 'keyword': keywords,
  171. 'class-name': {
  172. pattern: RegExp(typeExpression),
  173. greedy: true,
  174. inside: typeInside
  175. },
  176. 'punctuation': /[,()]/
  177. }
  178. },
  179. 'preprocessor': {
  180. pattern: /(^[\t ]*)#.*/m,
  181. lookbehind: true,
  182. alias: 'property',
  183. inside: {
  184. 'directive': {
  185. pattern: /(#)\b(?:define|elif|else|endif|endregion|error|if|line|nullable|pragma|region|undef|warning)\b/,
  186. lookbehind: true,
  187. alias: 'keyword'
  188. }
  189. }
  190. }
  191. });
  192. var regularStringOrCharacter = regularString + '|' + character;
  193. var regularStringCharacterOrComment = replace(/\/(?![*/])|\/\/[^\r\n]*[\r\n]|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>/.source, [regularStringOrCharacter]);
  194. var roundExpression = nested(replace(/[^"'/()]|<<0>>|\(<<self>>*\)/.source, [regularStringCharacterOrComment]), 2);
  195. var attrTarget = /\b(?:assembly|event|field|method|module|param|property|return|type)\b/.source;
  196. var attr = replace(/<<0>>(?:\s*\(<<1>>*\))?/.source, [identifier, roundExpression]);
  197. Prism.languages.insertBefore('csharp', 'class-name', {
  198. 'attribute': {
  199. pattern: re(/((?:^|[^\s\w>)?])\s*\[\s*)(?:<<0>>\s*:\s*)?<<1>>(?:\s*,\s*<<1>>)*(?=\s*\])/.source, [attrTarget, attr]),
  200. lookbehind: true,
  201. greedy: true,
  202. inside: {
  203. 'target': {
  204. pattern: re(/^<<0>>(?=\s*:)/.source, [attrTarget]),
  205. alias: 'keyword'
  206. },
  207. 'attribute-arguments': {
  208. pattern: re(/\(<<0>>*\)/.source, [roundExpression]),
  209. inside: Prism.languages.csharp
  210. },
  211. 'class-name': {
  212. pattern: RegExp(identifier),
  213. inside: {
  214. 'punctuation': /\./
  215. }
  216. },
  217. 'punctuation': /[:,]/
  218. }
  219. }
  220. });
  221. var formatString = /:[^}\r\n]+/.source;
  222. var mInterpolationRound = nested(replace(/[^"'/()]|<<0>>|\(<<self>>*\)/.source, [regularStringCharacterOrComment]), 2);
  223. var mInterpolation = replace(/\{(?!\{)(?:(?![}:])<<0>>)*<<1>>?\}/.source, [mInterpolationRound, formatString]);
  224. var sInterpolationRound = nested(replace(/[^"'/()]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>|\(<<self>>*\)/.source, [regularStringOrCharacter]), 2);
  225. var sInterpolation = replace(/\{(?!\{)(?:(?![}:])<<0>>)*<<1>>?\}/.source, [sInterpolationRound, formatString]);
  226. function createInterpolationInside(interpolation, interpolationRound) {
  227. return {
  228. 'interpolation': {
  229. pattern: re(/((?:^|[^{])(?:\{\{)*)<<0>>/.source, [interpolation]),
  230. lookbehind: true,
  231. inside: {
  232. 'format-string': {
  233. pattern: re(/(^\{(?:(?![}:])<<0>>)*)<<1>>(?=\}$)/.source, [interpolationRound, formatString]),
  234. lookbehind: true,
  235. inside: {
  236. 'punctuation': /^:/
  237. }
  238. },
  239. 'punctuation': /^\{|\}$/,
  240. 'expression': {
  241. pattern: /[\s\S]+/,
  242. alias: 'language-csharp',
  243. inside: Prism.languages.csharp
  244. }
  245. }
  246. },
  247. 'string': /[\s\S]+/
  248. };
  249. }
  250. Prism.languages.insertBefore('csharp', 'string', {
  251. 'interpolation-string': [
  252. {
  253. pattern: re(/(^|[^\\])(?:\$@|@\$)"(?:""|\\[\s\S]|\{\{|<<0>>|[^\\{"])*"/.source, [mInterpolation]),
  254. lookbehind: true,
  255. greedy: true,
  256. inside: createInterpolationInside(mInterpolation, mInterpolationRound)
  257. },
  258. {
  259. pattern: re(/(^|[^@\\])\$"(?:\\.|\{\{|<<0>>|[^\\"{])*"/.source, [sInterpolation]),
  260. lookbehind: true,
  261. greedy: true,
  262. inside: createInterpolationInside(sInterpolation, sInterpolationRound)
  263. }
  264. ],
  265. 'char': {
  266. pattern: RegExp(character),
  267. greedy: true
  268. }
  269. });
  270. Prism.languages.dotnet = Prism.languages.cs = Prism.languages.csharp;
  271. }(Prism));
  272. /* </pre> */