typescript.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Should match the keys of textToKeywordObj in
  66. // https://github.com/microsoft/TypeScript/blob/master/src/compiler/scanner.ts
  67. 'abstract', 'any', 'as', 'asserts', 'bigint', 'boolean', 'break', 'case', 'catch',
  68. 'class', 'continue', 'const', 'constructor', 'debugger', 'declare', 'default',
  69. 'delete', 'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for',
  70. 'from', 'function', 'get', 'if', 'implements', 'import', 'in', 'infer',
  71. 'instanceof', 'interface', 'is', 'keyof', 'let', 'module', 'namespace', 'never',
  72. 'new', 'null', 'number', 'object', 'package', 'private', 'protected', 'public',
  73. 'readonly', 'require', 'global', 'return', 'set', 'static', 'string', 'super',
  74. 'switch', 'symbol', 'this', 'throw', 'true', 'try', 'type', 'typeof', 'undefined',
  75. 'unique', 'unknown', 'var', 'void', 'while', 'with', 'yield', 'async', 'await', 'of'
  76. ],
  77. operators: [
  78. '<=', '>=', '==', '!=', '===', '!==', '=>', '+', '-', '**',
  79. '*', '/', '%', '++', '--', '<<', '</', '>>', '>>>', '&',
  80. '|', '^', '!', '~', '&&', '||', '??', '?', ':', '=', '+=', '-=',
  81. '*=', '**=', '/=', '%=', '<<=', '>>=', '>>>=', '&=', '|=',
  82. '^=', '@',
  83. ],
  84. // we include these common regular expressions
  85. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  86. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  87. digits: /\d+(_+\d+)*/,
  88. octaldigits: /[0-7]+(_+[0-7]+)*/,
  89. binarydigits: /[0-1]+(_+[0-1]+)*/,
  90. hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
  91. regexpctl: /[(){}\[\]\$\^|\-*+?\.]/,
  92. regexpesc: /\\(?:[bBdDfnrstvwWn0\\\/]|@regexpctl|c[A-Z]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/,
  93. // The main tokenizer for our languages
  94. tokenizer: {
  95. root: [
  96. [/[{}]/, 'delimiter.bracket'],
  97. { include: 'common' }
  98. ],
  99. common: [
  100. // identifiers and keywords
  101. [/[a-z_$][\w$]*/, {
  102. cases: {
  103. '@keywords': 'keyword',
  104. '@default': 'identifier'
  105. }
  106. }],
  107. [/[A-Z][\w\$]*/, 'type.identifier'], // to show class names nicely
  108. // [/[A-Z][\w\$]*/, 'identifier'],
  109. // whitespace
  110. { include: '@whitespace' },
  111. // regular expression: ensure it is terminated before beginning (otherwise it is an opeator)
  112. [/\/(?=([^\\\/]|\\.)+\/([gimsuy]*)(\s*)(\.|;|,|\)|\]|\}|$))/, { token: 'regexp', bracket: '@open', next: '@regexp' }],
  113. // delimiters and operators
  114. [/[()\[\]]/, '@brackets'],
  115. [/[<>](?!@symbols)/, '@brackets'],
  116. [/!(?=([^=]|$))/, 'delimiter'],
  117. [/@symbols/, {
  118. cases: {
  119. '@operators': 'delimiter',
  120. '@default': ''
  121. }
  122. }],
  123. // numbers
  124. [/(@digits)[eE]([\-+]?(@digits))?/, 'number.float'],
  125. [/(@digits)\.(@digits)([eE][\-+]?(@digits))?/, 'number.float'],
  126. [/0[xX](@hexdigits)n?/, 'number.hex'],
  127. [/0[oO]?(@octaldigits)n?/, 'number.octal'],
  128. [/0[bB](@binarydigits)n?/, 'number.binary'],
  129. [/(@digits)n?/, 'number'],
  130. // delimiter: after number because of .\d floats
  131. [/[;,.]/, 'delimiter'],
  132. // strings
  133. [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  134. [/'([^'\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  135. [/"/, 'string', '@string_double'],
  136. [/'/, 'string', '@string_single'],
  137. [/`/, 'string', '@string_backtick'],
  138. ],
  139. whitespace: [
  140. [/[ \t\r\n]+/, ''],
  141. [/\/\*\*(?!\/)/, 'comment.doc', '@jsdoc'],
  142. [/\/\*/, 'comment', '@comment'],
  143. [/\/\/.*$/, 'comment'],
  144. ],
  145. comment: [
  146. [/[^\/*]+/, 'comment'],
  147. [/\*\//, 'comment', '@pop'],
  148. [/[\/*]/, 'comment']
  149. ],
  150. jsdoc: [
  151. [/[^\/*]+/, 'comment.doc'],
  152. [/\*\//, 'comment.doc', '@pop'],
  153. [/[\/*]/, 'comment.doc']
  154. ],
  155. // We match regular expression quite precisely
  156. regexp: [
  157. [/(\{)(\d+(?:,\d*)?)(\})/, ['regexp.escape.control', 'regexp.escape.control', 'regexp.escape.control']],
  158. [/(\[)(\^?)(?=(?:[^\]\\\/]|\\.)+)/, ['regexp.escape.control', { token: 'regexp.escape.control', next: '@regexrange' }]],
  159. [/(\()(\?:|\?=|\?!)/, ['regexp.escape.control', 'regexp.escape.control']],
  160. [/[()]/, 'regexp.escape.control'],
  161. [/@regexpctl/, 'regexp.escape.control'],
  162. [/[^\\\/]/, 'regexp'],
  163. [/@regexpesc/, 'regexp.escape'],
  164. [/\\\./, 'regexp.invalid'],
  165. [/(\/)([gimsuy]*)/, [{ token: 'regexp', bracket: '@close', next: '@pop' }, 'keyword.other']],
  166. ],
  167. regexrange: [
  168. [/-/, 'regexp.escape.control'],
  169. [/\^/, 'regexp.invalid'],
  170. [/@regexpesc/, 'regexp.escape'],
  171. [/[^\]]/, 'regexp'],
  172. [/\]/, { token: 'regexp.escape.control', next: '@pop', bracket: '@close' }]
  173. ],
  174. string_double: [
  175. [/[^\\"]+/, 'string'],
  176. [/@escapes/, 'string.escape'],
  177. [/\\./, 'string.escape.invalid'],
  178. [/"/, 'string', '@pop']
  179. ],
  180. string_single: [
  181. [/[^\\']+/, 'string'],
  182. [/@escapes/, 'string.escape'],
  183. [/\\./, 'string.escape.invalid'],
  184. [/'/, 'string', '@pop']
  185. ],
  186. string_backtick: [
  187. [/\$\{/, { token: 'delimiter.bracket', next: '@bracketCounting' }],
  188. [/[^\\`$]+/, 'string'],
  189. [/@escapes/, 'string.escape'],
  190. [/\\./, 'string.escape.invalid'],
  191. [/`/, 'string', '@pop']
  192. ],
  193. bracketCounting: [
  194. [/\{/, 'delimiter.bracket', '@bracketCounting'],
  195. [/\}/, 'delimiter.bracket', '@pop'],
  196. { include: 'common' }
  197. ],
  198. },
  199. };