scala.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. import type { languages } from '../fillers/monaco-editor-core';
  6. export const conf: languages.LanguageConfiguration = {
  7. /*
  8. * `...` is allowed as an identifier.
  9. * $ is allowed in identifiers.
  10. * unary_<op> is allowed as an identifier.
  11. * <name>_= is allowed as an identifier.
  12. */
  13. wordPattern:
  14. /(unary_[@~!#%^&*()\-=+\\|:<>\/?]+)|([a-zA-Z_$][\w$]*?_=)|(`[^`]+`)|([a-zA-Z_$][\w$]*)/g,
  15. comments: {
  16. lineComment: '//',
  17. blockComment: ['/*', '*/']
  18. },
  19. brackets: [
  20. ['{', '}'],
  21. ['[', ']'],
  22. ['(', ')']
  23. ],
  24. autoClosingPairs: [
  25. { open: '{', close: '}' },
  26. { open: '[', close: ']' },
  27. { open: '(', close: ')' },
  28. { open: '"', close: '"' },
  29. { open: "'", close: "'" }
  30. ],
  31. surroundingPairs: [
  32. { open: '{', close: '}' },
  33. { open: '[', close: ']' },
  34. { open: '(', close: ')' },
  35. { open: '"', close: '"' },
  36. { open: "'", close: "'" }
  37. ],
  38. folding: {
  39. markers: {
  40. start: new RegExp('^\\s*//\\s*(?:(?:#?region\\b)|(?:<editor-fold\\b))'),
  41. end: new RegExp('^\\s*//\\s*(?:(?:#?endregion\\b)|(?:</editor-fold>))')
  42. }
  43. }
  44. };
  45. export const language = <languages.IMonarchLanguage>{
  46. tokenPostfix: '.scala',
  47. // We can't easily add everything from Dotty, but we can at least add some of its keywords
  48. keywords: [
  49. 'asInstanceOf',
  50. 'catch',
  51. 'class',
  52. 'classOf',
  53. 'def',
  54. 'do',
  55. 'else',
  56. 'extends',
  57. 'finally',
  58. 'for',
  59. 'foreach',
  60. 'forSome',
  61. 'if',
  62. 'import',
  63. 'isInstanceOf',
  64. 'macro',
  65. 'match',
  66. 'new',
  67. 'object',
  68. 'package',
  69. 'return',
  70. 'throw',
  71. 'trait',
  72. 'try',
  73. 'type',
  74. 'until',
  75. 'val',
  76. 'var',
  77. 'while',
  78. 'with',
  79. 'yield',
  80. // Dotty-specific:
  81. 'given',
  82. 'enum',
  83. 'then'
  84. ],
  85. // Dotty-specific:
  86. softKeywords: ['as', 'export', 'extension', 'end', 'derives', 'on'],
  87. constants: ['true', 'false', 'null', 'this', 'super'],
  88. modifiers: [
  89. 'abstract',
  90. 'final',
  91. 'implicit',
  92. 'lazy',
  93. 'override',
  94. 'private',
  95. 'protected',
  96. 'sealed'
  97. ],
  98. // Dotty-specific:
  99. softModifiers: ['inline', 'opaque', 'open', 'transparent', 'using'],
  100. name: /(?:[a-z_$][\w$]*|`[^`]+`)/,
  101. type: /(?:[A-Z][\w$]*)/,
  102. // we include these common regular expressions
  103. symbols: /[=><!~?:&|+\-*\/^\\%@#]+/,
  104. digits: /\d+(_+\d+)*/,
  105. hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
  106. // C# style strings
  107. escapes: /\\(?:[btnfr\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  108. fstring_conv: /[bBhHsScCdoxXeEfgGaAt]|[Tn](?:[HIklMSLNpzZsQ]|[BbhAaCYyjmde]|[RTrDFC])/,
  109. // The main tokenizer for our languages
  110. tokenizer: {
  111. root: [
  112. // strings
  113. [/\braw"""/, { token: 'string.quote', bracket: '@open', next: '@rawstringt' }],
  114. [/\braw"/, { token: 'string.quote', bracket: '@open', next: '@rawstring' }],
  115. [/\bs"""/, { token: 'string.quote', bracket: '@open', next: '@sstringt' }],
  116. [/\bs"/, { token: 'string.quote', bracket: '@open', next: '@sstring' }],
  117. [/\bf""""/, { token: 'string.quote', bracket: '@open', next: '@fstringt' }],
  118. [/\bf"/, { token: 'string.quote', bracket: '@open', next: '@fstring' }],
  119. [/"""/, { token: 'string.quote', bracket: '@open', next: '@stringt' }],
  120. [/"/, { token: 'string.quote', bracket: '@open', next: '@string' }],
  121. // numbers
  122. [/(@digits)[eE]([\-+]?(@digits))?[fFdD]?/, 'number.float', '@allowMethod'],
  123. [/(@digits)\.(@digits)([eE][\-+]?(@digits))?[fFdD]?/, 'number.float', '@allowMethod'],
  124. [/0[xX](@hexdigits)[Ll]?/, 'number.hex', '@allowMethod'],
  125. [/(@digits)[fFdD]/, 'number.float', '@allowMethod'],
  126. [/(@digits)[lL]?/, 'number', '@allowMethod'],
  127. [/\b_\*/, 'key'],
  128. [/\b(_)\b/, 'keyword', '@allowMethod'],
  129. // identifiers and keywords
  130. [/\bimport\b/, 'keyword', '@import'],
  131. [/\b(case)([ \t]+)(class)\b/, ['keyword.modifier', 'white', 'keyword']],
  132. [/\bcase\b/, 'keyword', '@case'],
  133. [/\bva[lr]\b/, 'keyword', '@vardef'],
  134. [
  135. /\b(def)([ \t]+)((?:unary_)?@symbols|@name(?:_=)|@name)/,
  136. ['keyword', 'white', 'identifier']
  137. ],
  138. [/@name(?=[ \t]*:(?!:))/, 'variable'],
  139. [/(\.)(@name|@symbols)/, ['operator', { token: '@rematch', next: '@allowMethod' }]],
  140. [/([{(])(\s*)(@name(?=\s*=>))/, ['@brackets', 'white', 'variable']],
  141. [
  142. /@name/,
  143. {
  144. cases: {
  145. '@keywords': 'keyword',
  146. '@softKeywords': 'keyword',
  147. '@modifiers': 'keyword.modifier',
  148. '@softModifiers': 'keyword.modifier',
  149. '@constants': {
  150. token: 'constant',
  151. next: '@allowMethod'
  152. },
  153. '@default': {
  154. token: 'identifier',
  155. next: '@allowMethod'
  156. }
  157. }
  158. }
  159. ],
  160. [/@type/, 'type', '@allowMethod'],
  161. // whitespace
  162. { include: '@whitespace' },
  163. // @ annotations.
  164. [/@[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*/, 'annotation'],
  165. // delimiters and operators
  166. [/[{(]/, '@brackets'],
  167. [/[})]/, '@brackets', '@allowMethod'],
  168. [/\[/, 'operator.square'],
  169. [/](?!\s*(?:va[rl]|def|type)\b)/, 'operator.square', '@allowMethod'],
  170. [/]/, 'operator.square'],
  171. [/([=-]>|<-|>:|<:|:>|<%)(?=[\s\w()[\]{},\."'`])/, 'keyword'],
  172. [/@symbols/, 'operator'],
  173. // delimiter: after number because of .\d floats
  174. [/[;,\.]/, 'delimiter'],
  175. // symbols
  176. [/'[a-zA-Z$][\w$]*(?!')/, 'attribute.name'],
  177. // characters
  178. [/'[^\\']'/, 'string', '@allowMethod'],
  179. [
  180. /(')(@escapes)(')/,
  181. ['string', 'string.escape', { token: 'string', next: '@allowMethod' }]
  182. ],
  183. [/'/, 'string.invalid']
  184. ],
  185. import: [
  186. [/;/, 'delimiter', '@pop'],
  187. [/^|$/, '', '@pop'],
  188. [/[ \t]+/, 'white'],
  189. [/[\n\r]+/, 'white', '@pop'],
  190. [/\/\*/, 'comment', '@comment'],
  191. [/@name|@type/, 'type'],
  192. [/[(){}]/, '@brackets'],
  193. [/[[\]]/, 'operator.square'],
  194. [/[\.,]/, 'delimiter']
  195. ],
  196. allowMethod: [
  197. [/^|$/, '', '@pop'],
  198. [/[ \t]+/, 'white'],
  199. [/[\n\r]+/, 'white', '@pop'],
  200. [/\/\*/, 'comment', '@comment'],
  201. [/(?==>[\s\w([{])/, 'keyword', '@pop'],
  202. [
  203. /(@name|@symbols)(?=[ \t]*[[({"'`]|[ \t]+(?:[+-]?\.?\d|\w))/,
  204. {
  205. cases: {
  206. '@keywords': { token: 'keyword', next: '@pop' },
  207. '->|<-|>:|<:|<%': { token: 'keyword', next: '@pop' },
  208. '@default': { token: '@rematch', next: '@pop' }
  209. }
  210. }
  211. ],
  212. ['', '', '@pop']
  213. ],
  214. comment: [
  215. [/[^\/*]+/, 'comment'],
  216. [/\/\*/, 'comment', '@push'], // nested comment
  217. [/\*\//, 'comment', '@pop'],
  218. [/[\/*]/, 'comment']
  219. ],
  220. case: [
  221. [/\b_\*/, 'key'],
  222. [/\b(_|true|false|null|this|super)\b/, 'keyword', '@allowMethod'],
  223. [/\bif\b|=>/, 'keyword', '@pop'],
  224. [/`[^`]+`/, 'identifier', '@allowMethod'],
  225. [/@name/, 'variable', '@allowMethod'],
  226. [/:::?|\||@(?![a-z_$])/, 'keyword'],
  227. { include: '@root' }
  228. ],
  229. vardef: [
  230. [/\b_\*/, 'key'],
  231. [/\b(_|true|false|null|this|super)\b/, 'keyword'],
  232. [/@name/, 'variable'],
  233. [/:::?|\||@(?![a-z_$])/, 'keyword'],
  234. [/=|:(?!:)/, 'operator', '@pop'],
  235. [/$/, 'white', '@pop'],
  236. { include: '@root' }
  237. ],
  238. string: [
  239. [/[^\\"\n\r]+/, 'string'],
  240. [/@escapes/, 'string.escape'],
  241. [/\\./, 'string.escape.invalid'],
  242. [
  243. /"/,
  244. {
  245. token: 'string.quote',
  246. bracket: '@close',
  247. switchTo: '@allowMethod'
  248. }
  249. ]
  250. ],
  251. stringt: [
  252. [/[^\\"\n\r]+/, 'string'],
  253. [/@escapes/, 'string.escape'],
  254. [/\\./, 'string.escape.invalid'],
  255. [/"(?=""")/, 'string'],
  256. [
  257. /"""/,
  258. {
  259. token: 'string.quote',
  260. bracket: '@close',
  261. switchTo: '@allowMethod'
  262. }
  263. ],
  264. [/"/, 'string']
  265. ],
  266. fstring: [
  267. [/@escapes/, 'string.escape'],
  268. [
  269. /"/,
  270. {
  271. token: 'string.quote',
  272. bracket: '@close',
  273. switchTo: '@allowMethod'
  274. }
  275. ],
  276. [/\$\$/, 'string'],
  277. [/(\$)([a-z_]\w*)/, ['operator', 'identifier']],
  278. [/\$\{/, 'operator', '@interp'],
  279. [/%%/, 'string'],
  280. [
  281. /(%)([\-#+ 0,(])(\d+|\.\d+|\d+\.\d+)(@fstring_conv)/,
  282. ['metatag', 'keyword.modifier', 'number', 'metatag']
  283. ],
  284. [/(%)(\d+|\.\d+|\d+\.\d+)(@fstring_conv)/, ['metatag', 'number', 'metatag']],
  285. [/(%)([\-#+ 0,(])(@fstring_conv)/, ['metatag', 'keyword.modifier', 'metatag']],
  286. [/(%)(@fstring_conv)/, ['metatag', 'metatag']],
  287. [/./, 'string']
  288. ],
  289. fstringt: [
  290. [/@escapes/, 'string.escape'],
  291. [/"(?=""")/, 'string'],
  292. [
  293. /"""/,
  294. {
  295. token: 'string.quote',
  296. bracket: '@close',
  297. switchTo: '@allowMethod'
  298. }
  299. ],
  300. [/\$\$/, 'string'],
  301. [/(\$)([a-z_]\w*)/, ['operator', 'identifier']],
  302. [/\$\{/, 'operator', '@interp'],
  303. [/%%/, 'string'],
  304. [
  305. /(%)([\-#+ 0,(])(\d+|\.\d+|\d+\.\d+)(@fstring_conv)/,
  306. ['metatag', 'keyword.modifier', 'number', 'metatag']
  307. ],
  308. [/(%)(\d+|\.\d+|\d+\.\d+)(@fstring_conv)/, ['metatag', 'number', 'metatag']],
  309. [/(%)([\-#+ 0,(])(@fstring_conv)/, ['metatag', 'keyword.modifier', 'metatag']],
  310. [/(%)(@fstring_conv)/, ['metatag', 'metatag']],
  311. [/./, 'string']
  312. ],
  313. sstring: [
  314. [/@escapes/, 'string.escape'],
  315. [
  316. /"/,
  317. {
  318. token: 'string.quote',
  319. bracket: '@close',
  320. switchTo: '@allowMethod'
  321. }
  322. ],
  323. [/\$\$/, 'string'],
  324. [/(\$)([a-z_]\w*)/, ['operator', 'identifier']],
  325. [/\$\{/, 'operator', '@interp'],
  326. [/./, 'string']
  327. ],
  328. sstringt: [
  329. [/@escapes/, 'string.escape'],
  330. [/"(?=""")/, 'string'],
  331. [
  332. /"""/,
  333. {
  334. token: 'string.quote',
  335. bracket: '@close',
  336. switchTo: '@allowMethod'
  337. }
  338. ],
  339. [/\$\$/, 'string'],
  340. [/(\$)([a-z_]\w*)/, ['operator', 'identifier']],
  341. [/\$\{/, 'operator', '@interp'],
  342. [/./, 'string']
  343. ],
  344. interp: [[/{/, 'operator', '@push'], [/}/, 'operator', '@pop'], { include: '@root' }],
  345. rawstring: [
  346. [/[^"]/, 'string'],
  347. [
  348. /"/,
  349. {
  350. token: 'string.quote',
  351. bracket: '@close',
  352. switchTo: '@allowMethod'
  353. }
  354. ]
  355. ],
  356. rawstringt: [
  357. [/[^"]/, 'string'],
  358. [/"(?=""")/, 'string'],
  359. [
  360. /"""/,
  361. {
  362. token: 'string.quote',
  363. bracket: '@close',
  364. switchTo: '@allowMethod'
  365. }
  366. ],
  367. [/"/, 'string']
  368. ],
  369. whitespace: [
  370. [/[ \t\r\n]+/, 'white'],
  371. [/\/\*/, 'comment', '@comment'],
  372. [/\/\/.*$/, 'comment']
  373. ]
  374. }
  375. };