scala.js 13 KB

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