razor.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. define(["require", "exports"], function (require, exports) {
  6. 'use strict';
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. // Allow for running under nodejs/requirejs in tests
  9. var _monaco = (typeof monaco === 'undefined' ? self.monaco : monaco);
  10. var EMPTY_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'];
  11. exports.conf = {
  12. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
  13. comments: {
  14. blockComment: ['<!--', '-->']
  15. },
  16. brackets: [
  17. ['<!--', '-->'],
  18. ['<', '>'],
  19. ['{', '}'],
  20. ['(', ')']
  21. ],
  22. autoClosingPairs: [
  23. { open: '{', close: '}' },
  24. { open: '[', close: ']' },
  25. { open: '(', close: ')' },
  26. { open: '"', close: '"' },
  27. { open: '\'', close: '\'' }
  28. ],
  29. surroundingPairs: [
  30. { open: '"', close: '"' },
  31. { open: '\'', close: '\'' },
  32. { open: '<', close: '>' }
  33. ],
  34. onEnterRules: [
  35. {
  36. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  37. afterText: /^<\/(\w[\w\d]*)\s*>$/i,
  38. action: { indentAction: _monaco.languages.IndentAction.IndentOutdent }
  39. },
  40. {
  41. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  42. action: { indentAction: _monaco.languages.IndentAction.Indent }
  43. }
  44. ],
  45. };
  46. exports.language = {
  47. defaultToken: '',
  48. tokenPostfix: '',
  49. // ignoreCase: true,
  50. // The main tokenizer for our languages
  51. tokenizer: {
  52. root: [
  53. [/@@/],
  54. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.root' }],
  55. [/<!DOCTYPE/, 'metatag.html', '@doctype'],
  56. [/<!--/, 'comment.html', '@comment'],
  57. [/(<)(\w+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  58. [/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]],
  59. [/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]],
  60. [/(<)([:\w]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  61. [/(<\/)(\w+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  62. [/</, 'delimiter.html'],
  63. [/[ \t\r\n]+/],
  64. [/[^<@]+/],
  65. ],
  66. doctype: [
  67. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.comment' }],
  68. [/[^>]+/, 'metatag.content.html'],
  69. [/>/, 'metatag.html', '@pop'],
  70. ],
  71. comment: [
  72. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.comment' }],
  73. [/-->/, 'comment.html', '@pop'],
  74. [/[^-]+/, 'comment.content.html'],
  75. [/./, 'comment.content.html']
  76. ],
  77. otherTag: [
  78. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.otherTag' }],
  79. [/\/?>/, 'delimiter.html', '@pop'],
  80. [/"([^"]*)"/, 'attribute.value'],
  81. [/'([^']*)'/, 'attribute.value'],
  82. [/[\w\-]+/, 'attribute.name'],
  83. [/=/, 'delimiter'],
  84. [/[ \t\r\n]+/],
  85. ],
  86. // -- BEGIN <script> tags handling
  87. // After <script
  88. script: [
  89. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.script' }],
  90. [/type/, 'attribute.name', '@scriptAfterType'],
  91. [/"([^"]*)"/, 'attribute.value'],
  92. [/'([^']*)'/, 'attribute.value'],
  93. [/[\w\-]+/, 'attribute.name'],
  94. [/=/, 'delimiter'],
  95. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  96. [/[ \t\r\n]+/],
  97. [/(<\/)(script\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  98. ],
  99. // After <script ... type
  100. scriptAfterType: [
  101. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.scriptAfterType' }],
  102. [/=/, 'delimiter', '@scriptAfterTypeEquals'],
  103. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  104. [/[ \t\r\n]+/],
  105. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  106. ],
  107. // After <script ... type =
  108. scriptAfterTypeEquals: [
  109. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.scriptAfterTypeEquals' }],
  110. [/"([^"]*)"/, { token: 'attribute.value', switchTo: '@scriptWithCustomType.$1' }],
  111. [/'([^']*)'/, { token: 'attribute.value', switchTo: '@scriptWithCustomType.$1' }],
  112. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  113. [/[ \t\r\n]+/],
  114. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  115. ],
  116. // After <script ... type = $S2
  117. scriptWithCustomType: [
  118. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.scriptWithCustomType.$S2' }],
  119. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.$S2', nextEmbedded: '$S2' }],
  120. [/"([^"]*)"/, 'attribute.value'],
  121. [/'([^']*)'/, 'attribute.value'],
  122. [/[\w\-]+/, 'attribute.name'],
  123. [/=/, 'delimiter'],
  124. [/[ \t\r\n]+/],
  125. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  126. ],
  127. scriptEmbedded: [
  128. [/@[^@]/, { token: '@rematch', switchTo: '@razorInEmbeddedState.scriptEmbedded.$S2', nextEmbedded: '@pop' }],
  129. [/<\/script/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }]
  130. ],
  131. // -- END <script> tags handling
  132. // -- BEGIN <style> tags handling
  133. // After <style
  134. style: [
  135. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.style' }],
  136. [/type/, 'attribute.name', '@styleAfterType'],
  137. [/"([^"]*)"/, 'attribute.value'],
  138. [/'([^']*)'/, 'attribute.value'],
  139. [/[\w\-]+/, 'attribute.name'],
  140. [/=/, 'delimiter'],
  141. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  142. [/[ \t\r\n]+/],
  143. [/(<\/)(style\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  144. ],
  145. // After <style ... type
  146. styleAfterType: [
  147. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.styleAfterType' }],
  148. [/=/, 'delimiter', '@styleAfterTypeEquals'],
  149. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  150. [/[ \t\r\n]+/],
  151. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  152. ],
  153. // After <style ... type =
  154. styleAfterTypeEquals: [
  155. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.styleAfterTypeEquals' }],
  156. [/"([^"]*)"/, { token: 'attribute.value', switchTo: '@styleWithCustomType.$1' }],
  157. [/'([^']*)'/, { token: 'attribute.value', switchTo: '@styleWithCustomType.$1' }],
  158. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  159. [/[ \t\r\n]+/],
  160. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  161. ],
  162. // After <style ... type = $S2
  163. styleWithCustomType: [
  164. [/@[^@]/, { token: '@rematch', switchTo: '@razorInSimpleState.styleWithCustomType.$S2' }],
  165. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.$S2', nextEmbedded: '$S2' }],
  166. [/"([^"]*)"/, 'attribute.value'],
  167. [/'([^']*)'/, 'attribute.value'],
  168. [/[\w\-]+/, 'attribute.name'],
  169. [/=/, 'delimiter'],
  170. [/[ \t\r\n]+/],
  171. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  172. ],
  173. styleEmbedded: [
  174. [/@[^@]/, { token: '@rematch', switchTo: '@razorInEmbeddedState.styleEmbedded.$S2', nextEmbedded: '@pop' }],
  175. [/<\/style/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }]
  176. ],
  177. // -- END <style> tags handling
  178. razorInSimpleState: [
  179. [/@\*/, 'comment.cs', '@razorBlockCommentTopLevel'],
  180. [/@[{(]/, 'metatag.cs', '@razorRootTopLevel'],
  181. [/(@)(\s*[\w]+)/, ['metatag.cs', { token: 'identifier.cs', switchTo: '@$S2.$S3' }]],
  182. [/[})]/, { token: 'metatag.cs', switchTo: '@$S2.$S3' }],
  183. [/\*@/, { token: 'comment.cs', switchTo: '@$S2.$S3' }],
  184. ],
  185. razorInEmbeddedState: [
  186. [/@\*/, 'comment.cs', '@razorBlockCommentTopLevel'],
  187. [/@[{(]/, 'metatag.cs', '@razorRootTopLevel'],
  188. [/(@)(\s*[\w]+)/, ['metatag.cs', { token: 'identifier.cs', switchTo: '@$S2.$S3', nextEmbedded: '$S3' }]],
  189. [/[})]/, { token: 'metatag.cs', switchTo: '@$S2.$S3', nextEmbedded: '$S3' }],
  190. [/\*@/, { token: 'comment.cs', switchTo: '@$S2.$S3', nextEmbedded: '$S3' }],
  191. ],
  192. razorBlockCommentTopLevel: [
  193. [/\*@/, '@rematch', '@pop'],
  194. [/[^*]+/, 'comment.cs'],
  195. [/./, 'comment.cs']
  196. ],
  197. razorBlockComment: [
  198. [/\*@/, 'comment.cs', '@pop'],
  199. [/[^*]+/, 'comment.cs'],
  200. [/./, 'comment.cs']
  201. ],
  202. razorRootTopLevel: [
  203. [/\{/, 'delimiter.bracket.cs', '@razorRoot'],
  204. [/\(/, 'delimiter.parenthesis.cs', '@razorRoot'],
  205. [/[})]/, '@rematch', '@pop'],
  206. { include: 'razorCommon' }
  207. ],
  208. razorRoot: [
  209. [/\{/, 'delimiter.bracket.cs', '@razorRoot'],
  210. [/\(/, 'delimiter.parenthesis.cs', '@razorRoot'],
  211. [/\}/, 'delimiter.bracket.cs', '@pop'],
  212. [/\)/, 'delimiter.parenthesis.cs', '@pop'],
  213. { include: 'razorCommon' }
  214. ],
  215. razorCommon: [
  216. [/[a-zA-Z_]\w*/, {
  217. cases: {
  218. '@razorKeywords': { token: 'keyword.cs' },
  219. '@default': 'identifier.cs'
  220. }
  221. }],
  222. // brackets
  223. [/[\[\]]/, 'delimiter.array.cs'],
  224. // whitespace
  225. [/[ \t\r\n]+/],
  226. // comments
  227. [/\/\/.*$/, 'comment.cs'],
  228. [/@\*/, 'comment.cs', '@razorBlockComment'],
  229. // strings
  230. [/"([^"]*)"/, 'string.cs'],
  231. [/'([^']*)'/, 'string.cs'],
  232. // simple html
  233. [/(<)(\w+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  234. [/(<)(\w+)(>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  235. [/(<\/)(\w+)(>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  236. // delimiters
  237. [/[\+\-\*\%\&\|\^\~\!\=\<\>\/\?\;\:\.\,]/, 'delimiter.cs'],
  238. // numbers
  239. [/\d*\d+[eE]([\-+]?\d+)?/, 'number.float.cs'],
  240. [/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float.cs'],
  241. [/0[xX][0-9a-fA-F']*[0-9a-fA-F]/, 'number.hex.cs'],
  242. [/0[0-7']*[0-7]/, 'number.octal.cs'],
  243. [/0[bB][0-1']*[0-1]/, 'number.binary.cs'],
  244. [/\d[\d']*/, 'number.cs'],
  245. [/\d/, 'number.cs'],
  246. ]
  247. },
  248. razorKeywords: [
  249. 'abstract', 'as', 'async', 'await', 'base', 'bool',
  250. 'break', 'by', 'byte', 'case',
  251. 'catch', 'char', 'checked', 'class',
  252. 'const', 'continue', 'decimal', 'default',
  253. 'delegate', 'do', 'double', 'descending',
  254. 'explicit', 'event', 'extern', 'else',
  255. 'enum', 'false', 'finally', 'fixed',
  256. 'float', 'for', 'foreach', 'from',
  257. 'goto', 'group', 'if', 'implicit',
  258. 'in', 'int', 'interface', 'internal',
  259. 'into', 'is', 'lock', 'long', 'nameof',
  260. 'new', 'null', 'namespace', 'object',
  261. 'operator', 'out', 'override', 'orderby',
  262. 'params', 'private', 'protected', 'public',
  263. 'readonly', 'ref', 'return', 'switch',
  264. 'struct', 'sbyte', 'sealed', 'short',
  265. 'sizeof', 'stackalloc', 'static', 'string',
  266. 'select', 'this', 'throw', 'true',
  267. 'try', 'typeof', 'uint', 'ulong',
  268. 'unchecked', 'unsafe', 'ushort', 'using',
  269. 'var', 'virtual', 'volatile', 'void', 'when',
  270. 'while', 'where', 'yield',
  271. 'model', 'inject' // Razor specific
  272. ],
  273. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  274. };
  275. });