typescript.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 'use strict';
  6. import IRichLanguageConfiguration = monaco.languages.LanguageConfiguration;
  7. import ILanguage = monaco.languages.IMonarchLanguage;
  8. // Allow for running under nodejs/requirejs in tests
  9. const _monaco: typeof monaco = (typeof monaco === 'undefined' ? (<any>self).monaco : monaco);
  10. export const conf: IRichLanguageConfiguration = {
  11. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
  12. comments: {
  13. lineComment: '//',
  14. blockComment: ['/*', '*/']
  15. },
  16. brackets: [
  17. ['{', '}'],
  18. ['[', ']'],
  19. ['(', ')']
  20. ],
  21. onEnterRules: [
  22. {
  23. // e.g. /** | */
  24. beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
  25. afterText: /^\s*\*\/$/,
  26. action: { indentAction: _monaco.languages.IndentAction.IndentOutdent, appendText: ' * ' }
  27. },
  28. {
  29. // e.g. /** ...|
  30. beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
  31. action: { indentAction: _monaco.languages.IndentAction.None, appendText: ' * ' }
  32. },
  33. {
  34. // e.g. * ...|
  35. beforeText: /^(\t|(\ \ ))*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
  36. action: { indentAction: _monaco.languages.IndentAction.None, appendText: '* ' }
  37. },
  38. {
  39. // e.g. */|
  40. beforeText: /^(\t|(\ \ ))*\ \*\/\s*$/,
  41. action: { indentAction: _monaco.languages.IndentAction.None, removeText: 1 }
  42. }
  43. ],
  44. autoClosingPairs: [
  45. { open: '{', close: '}' },
  46. { open: '[', close: ']' },
  47. { open: '(', close: ')' },
  48. { open: '"', close: '"', notIn: ['string'] },
  49. { open: '\'', close: '\'', notIn: ['string', 'comment'] },
  50. { open: '`', close: '`', notIn: ['string', 'comment'] },
  51. { open: "/**", close: " */", notIn: ["string"] }
  52. ],
  53. folding: {
  54. markers: {
  55. start: new RegExp("^\\s*//\\s*#?region\\b"),
  56. end: new RegExp("^\\s*//\\s*#?endregion\\b")
  57. }
  58. }
  59. };
  60. export const language = {
  61. // Set defaultToken to invalid to see what you do not tokenize yet
  62. defaultToken: 'invalid',
  63. tokenPostfix: '.ts',
  64. keywords: [
  65. 'abstract', 'as', 'break', 'case', 'catch', 'class', 'continue', 'const',
  66. 'constructor', 'debugger', 'declare', 'default', 'delete', 'do', 'else',
  67. 'enum', 'export', 'extends', 'false', 'finally', 'for', 'from', 'function',
  68. 'get', 'if', 'implements', 'import', 'in', 'infer', 'instanceof', 'interface',
  69. 'is', 'keyof', 'let', 'module', 'namespace', 'never', 'new', 'null', 'package',
  70. 'private', 'protected', 'public', 'readonly', 'require', 'global', 'return',
  71. 'set', 'static', 'super', 'switch', 'symbol', 'this', 'throw', 'true', 'try',
  72. 'type', 'typeof', 'unique', 'var', 'void', 'while', 'with', 'yield', 'async',
  73. 'await', 'of'
  74. ],
  75. typeKeywords: [
  76. 'any', 'boolean', 'number', 'object', 'string', 'undefined'
  77. ],
  78. operators: [
  79. '<=', '>=', '==', '!=', '===', '!==', '=>', '+', '-', '**',
  80. '*', '/', '%', '++', '--', '<<', '</', '>>', '>>>', '&',
  81. '|', '^', '!', '~', '&&', '||', '?', ':', '=', '+=', '-=',
  82. '*=', '**=', '/=', '%=', '<<=', '>>=', '>>>=', '&=', '|=',
  83. '^=', '@',
  84. ],
  85. // we include these common regular expressions
  86. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  87. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  88. digits: /\d+(_+\d+)*/,
  89. octaldigits: /[0-7]+(_+[0-7]+)*/,
  90. binarydigits: /[0-1]+(_+[0-1]+)*/,
  91. hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
  92. regexpctl: /[(){}\[\]\$\^|\-*+?\.]/,
  93. regexpesc: /\\(?:[bBdDfnrstvwWn0\\\/]|@regexpctl|c[A-Z]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/,
  94. // The main tokenizer for our languages
  95. tokenizer: {
  96. root: [
  97. [/[{}]/, 'delimiter.bracket'],
  98. { include: 'common' }
  99. ],
  100. common: [
  101. // identifiers and keywords
  102. [/[a-z_$][\w$]*/, {
  103. cases: {
  104. '@typeKeywords': 'keyword',
  105. '@keywords': 'keyword',
  106. '@default': 'identifier'
  107. }
  108. }],
  109. [/[A-Z][\w\$]*/, 'type.identifier'], // to show class names nicely
  110. // [/[A-Z][\w\$]*/, 'identifier'],
  111. // whitespace
  112. { include: '@whitespace' },
  113. // regular expression: ensure it is terminated before beginning (otherwise it is an opeator)
  114. [/\/(?=([^\\\/]|\\.)+\/([gimsuy]*)(\s*)(\.|;|\/|,|\)|\]|\}|$))/, { token: 'regexp', bracket: '@open', next: '@regexp' }],
  115. // delimiters and operators
  116. [/[()\[\]]/, '@brackets'],
  117. [/[<>](?!@symbols)/, '@brackets'],
  118. [/!(?=([^=]|$))/, 'delimiter'],
  119. [/@symbols/, {
  120. cases: {
  121. '@operators': 'delimiter',
  122. '@default': ''
  123. }
  124. }],
  125. // numbers
  126. [/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'],
  127. [/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'],
  128. [/0[xX](@hexdigits)n?/, 'number.hex'],
  129. [/0[oO]?(@octaldigits)n?/, 'number.octal'],
  130. [/0[bB](@binarydigits)n?/, 'number.binary'],
  131. [/(@digits)n?/, 'number'],
  132. // delimiter: after number because of .\d floats
  133. [/[;,.]/, 'delimiter'],
  134. // strings
  135. [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  136. [/'([^'\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  137. [/"/, 'string', '@string_double'],
  138. [/'/, 'string', '@string_single'],
  139. [/`/, 'string', '@string_backtick'],
  140. ],
  141. whitespace: [
  142. [/[ \t\r\n]+/, ''],
  143. [/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'],
  144. [/\/\*/, 'comment', '@comment'],
  145. [/\/\/.*$/, 'comment'],
  146. ],
  147. comment: [
  148. [/[^\/*]+/, 'comment'],
  149. [/\*\//, 'comment', '@pop'],
  150. [/[\/*]/, 'comment']
  151. ],
  152. jsdoc: [
  153. [/[^\/*]+/, 'comment.doc'],
  154. [/\*\//, 'comment.doc', '@pop'],
  155. [/[\/*]/, 'comment.doc']
  156. ],
  157. // We match regular expression quite precisely
  158. regexp: [
  159. [/(\{)(\d+(?:,\d*)?)(\})/, ['regexp.escape.control', 'regexp.escape.control', 'regexp.escape.control']],
  160. [/(\[)(\^?)(?=(?:[^\]\\\/]|\\.)+)/, ['regexp.escape.control', { token: 'regexp.escape.control', next: '@regexrange' }]],
  161. [/(\()(\?:|\?=|\?!)/, ['regexp.escape.control', 'regexp.escape.control']],
  162. [/[()]/, 'regexp.escape.control'],
  163. [/@regexpctl/, 'regexp.escape.control'],
  164. [/[^\\\/]/, 'regexp'],
  165. [/@regexpesc/, 'regexp.escape'],
  166. [/\\\./, 'regexp.invalid'],
  167. [/(\/)([gimsuy]*)/, [{ token: 'regexp', bracket: '@close', next: '@pop' }, 'keyword.other']],
  168. ],
  169. regexrange: [
  170. [/-/, 'regexp.escape.control'],
  171. [/\^/, 'regexp.invalid'],
  172. [/@regexpesc/, 'regexp.escape'],
  173. [/[^\]]/, 'regexp'],
  174. [/\]/, { token: 'regexp.escape.control', next: '@pop', bracket: '@close' }]
  175. ],
  176. string_double: [
  177. [/[^\\"]+/, 'string'],
  178. [/@escapes/, 'string.escape'],
  179. [/\\./, 'string.escape.invalid'],
  180. [/"/, 'string', '@pop']
  181. ],
  182. string_single: [
  183. [/[^\\']+/, 'string'],
  184. [/@escapes/, 'string.escape'],
  185. [/\\./, 'string.escape.invalid'],
  186. [/'/, 'string', '@pop']
  187. ],
  188. string_backtick: [
  189. [/\$\{/, { token: 'delimiter.bracket', next: '@bracketCounting' }],
  190. [/[^\\`$]+/, 'string'],
  191. [/@escapes/, 'string.escape'],
  192. [/\\./, 'string.escape.invalid'],
  193. [/`/, 'string', '@pop']
  194. ],
  195. bracketCounting: [
  196. [/\{/, 'delimiter.bracket', '@bracketCounting'],
  197. [/\}/, 'delimiter.bracket', '@pop'],
  198. { include: 'common' }
  199. ],
  200. },
  201. };