rust.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. comments: {
  8. lineComment: '//',
  9. blockComment: ['/*', '*/']
  10. },
  11. brackets: [
  12. ['{', '}'],
  13. ['[', ']'],
  14. ['(', ')']
  15. ],
  16. autoClosingPairs: [
  17. { open: '[', close: ']' },
  18. { open: '{', close: '}' },
  19. { open: '(', close: ')' },
  20. { open: '"', close: '"', notIn: ['string'] }
  21. ],
  22. surroundingPairs: [
  23. { open: '{', close: '}' },
  24. { open: '[', close: ']' },
  25. { open: '(', close: ')' },
  26. { open: '"', close: '"' },
  27. { open: "'", close: "'" }
  28. ],
  29. folding: {
  30. markers: {
  31. start: new RegExp('^\\s*#pragma\\s+region\\b'),
  32. end: new RegExp('^\\s*#pragma\\s+endregion\\b')
  33. }
  34. }
  35. };
  36. export const language = <languages.IMonarchLanguage>{
  37. tokenPostfix: '.rust',
  38. defaultToken: 'invalid',
  39. keywords: [
  40. 'as',
  41. 'async',
  42. 'await',
  43. 'box',
  44. 'break',
  45. 'const',
  46. 'continue',
  47. 'crate',
  48. 'dyn',
  49. 'else',
  50. 'enum',
  51. 'extern',
  52. 'false',
  53. 'fn',
  54. 'for',
  55. 'if',
  56. 'impl',
  57. 'in',
  58. 'let',
  59. 'loop',
  60. 'match',
  61. 'mod',
  62. 'move',
  63. 'mut',
  64. 'pub',
  65. 'ref',
  66. 'return',
  67. 'self',
  68. 'static',
  69. 'struct',
  70. 'super',
  71. 'trait',
  72. 'true',
  73. 'try',
  74. 'type',
  75. 'unsafe',
  76. 'use',
  77. 'where',
  78. 'while',
  79. 'catch',
  80. 'default',
  81. 'union',
  82. 'static',
  83. 'abstract',
  84. 'alignof',
  85. 'become',
  86. 'do',
  87. 'final',
  88. 'macro',
  89. 'offsetof',
  90. 'override',
  91. 'priv',
  92. 'proc',
  93. 'pure',
  94. 'sizeof',
  95. 'typeof',
  96. 'unsized',
  97. 'virtual',
  98. 'yield'
  99. ],
  100. typeKeywords: [
  101. 'Self',
  102. 'm32',
  103. 'm64',
  104. 'm128',
  105. 'f80',
  106. 'f16',
  107. 'f128',
  108. 'int',
  109. 'uint',
  110. 'float',
  111. 'char',
  112. 'bool',
  113. 'u8',
  114. 'u16',
  115. 'u32',
  116. 'u64',
  117. 'f32',
  118. 'f64',
  119. 'i8',
  120. 'i16',
  121. 'i32',
  122. 'i64',
  123. 'str',
  124. 'Option',
  125. 'Either',
  126. 'c_float',
  127. 'c_double',
  128. 'c_void',
  129. 'FILE',
  130. 'fpos_t',
  131. 'DIR',
  132. 'dirent',
  133. 'c_char',
  134. 'c_schar',
  135. 'c_uchar',
  136. 'c_short',
  137. 'c_ushort',
  138. 'c_int',
  139. 'c_uint',
  140. 'c_long',
  141. 'c_ulong',
  142. 'size_t',
  143. 'ptrdiff_t',
  144. 'clock_t',
  145. 'time_t',
  146. 'c_longlong',
  147. 'c_ulonglong',
  148. 'intptr_t',
  149. 'uintptr_t',
  150. 'off_t',
  151. 'dev_t',
  152. 'ino_t',
  153. 'pid_t',
  154. 'mode_t',
  155. 'ssize_t'
  156. ],
  157. constants: ['true', 'false', 'Some', 'None', 'Left', 'Right', 'Ok', 'Err'],
  158. supportConstants: [
  159. 'EXIT_FAILURE',
  160. 'EXIT_SUCCESS',
  161. 'RAND_MAX',
  162. 'EOF',
  163. 'SEEK_SET',
  164. 'SEEK_CUR',
  165. 'SEEK_END',
  166. '_IOFBF',
  167. '_IONBF',
  168. '_IOLBF',
  169. 'BUFSIZ',
  170. 'FOPEN_MAX',
  171. 'FILENAME_MAX',
  172. 'L_tmpnam',
  173. 'TMP_MAX',
  174. 'O_RDONLY',
  175. 'O_WRONLY',
  176. 'O_RDWR',
  177. 'O_APPEND',
  178. 'O_CREAT',
  179. 'O_EXCL',
  180. 'O_TRUNC',
  181. 'S_IFIFO',
  182. 'S_IFCHR',
  183. 'S_IFBLK',
  184. 'S_IFDIR',
  185. 'S_IFREG',
  186. 'S_IFMT',
  187. 'S_IEXEC',
  188. 'S_IWRITE',
  189. 'S_IREAD',
  190. 'S_IRWXU',
  191. 'S_IXUSR',
  192. 'S_IWUSR',
  193. 'S_IRUSR',
  194. 'F_OK',
  195. 'R_OK',
  196. 'W_OK',
  197. 'X_OK',
  198. 'STDIN_FILENO',
  199. 'STDOUT_FILENO',
  200. 'STDERR_FILENO'
  201. ],
  202. supportMacros: [
  203. 'format!',
  204. 'print!',
  205. 'println!',
  206. 'panic!',
  207. 'format_args!',
  208. 'unreachable!',
  209. 'write!',
  210. 'writeln!'
  211. ],
  212. operators: [
  213. '!',
  214. '!=',
  215. '%',
  216. '%=',
  217. '&',
  218. '&=',
  219. '&&',
  220. '*',
  221. '*=',
  222. '+',
  223. '+=',
  224. '-',
  225. '-=',
  226. '->',
  227. '.',
  228. '..',
  229. '...',
  230. '/',
  231. '/=',
  232. ':',
  233. ';',
  234. '<<',
  235. '<<=',
  236. '<',
  237. '<=',
  238. '=',
  239. '==',
  240. '=>',
  241. '>',
  242. '>=',
  243. '>>',
  244. '>>=',
  245. '@',
  246. '^',
  247. '^=',
  248. '|',
  249. '|=',
  250. '||',
  251. '_',
  252. '?',
  253. '#'
  254. ],
  255. escapes: /\\([nrt0\"''\\]|x\h{2}|u\{\h{1,6}\})/,
  256. delimiters: /[,]/,
  257. symbols: /[\#\!\%\&\*\+\-\.\/\:\;\<\=\>\@\^\|_\?]+/,
  258. intSuffixes: /[iu](8|16|32|64|128|size)/,
  259. floatSuffixes: /f(32|64)/,
  260. tokenizer: {
  261. root: [
  262. // Raw string literals
  263. [/r(#*)"/, { token: 'string.quote', bracket: '@open', next: '@stringraw.$1' }],
  264. [
  265. /[a-zA-Z][a-zA-Z0-9_]*!?|_[a-zA-Z0-9_]+/,
  266. {
  267. cases: {
  268. '@typeKeywords': 'keyword.type',
  269. '@keywords': 'keyword',
  270. '@supportConstants': 'keyword',
  271. '@supportMacros': 'keyword',
  272. '@constants': 'keyword',
  273. '@default': 'identifier'
  274. }
  275. }
  276. ],
  277. // Designator
  278. [/\$/, 'identifier'],
  279. // Lifetime annotations
  280. [/'[a-zA-Z_][a-zA-Z0-9_]*(?=[^\'])/, 'identifier'],
  281. // Byte literal
  282. [/'(\S|@escapes)'/, 'string.byteliteral'],
  283. // Strings
  284. [/"/, { token: 'string.quote', bracket: '@open', next: '@string' }],
  285. { include: '@numbers' },
  286. // Whitespace + comments
  287. { include: '@whitespace' },
  288. [
  289. /@delimiters/,
  290. {
  291. cases: {
  292. '@keywords': 'keyword',
  293. '@default': 'delimiter'
  294. }
  295. }
  296. ],
  297. [/[{}()\[\]<>]/, '@brackets'],
  298. [/@symbols/, { cases: { '@operators': 'operator', '@default': '' } }]
  299. ],
  300. whitespace: [
  301. [/[ \t\r\n]+/, 'white'],
  302. [/\/\*/, 'comment', '@comment'],
  303. [/\/\/.*$/, 'comment']
  304. ],
  305. comment: [
  306. [/[^\/*]+/, 'comment'],
  307. [/\/\*/, 'comment', '@push'],
  308. ['\\*/', 'comment', '@pop'],
  309. [/[\/*]/, 'comment']
  310. ],
  311. string: [
  312. [/[^\\"]+/, 'string'],
  313. [/@escapes/, 'string.escape'],
  314. [/\\./, 'string.escape.invalid'],
  315. [/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }]
  316. ],
  317. stringraw: [
  318. [/[^"#]+/, { token: 'string' }],
  319. [
  320. /"(#*)/,
  321. {
  322. cases: {
  323. '$1==$S2': { token: 'string.quote', bracket: '@close', next: '@pop' },
  324. '@default': { token: 'string' }
  325. }
  326. }
  327. ],
  328. [/["#]/, { token: 'string' }]
  329. ],
  330. numbers: [
  331. //Octal
  332. [/(0o[0-7_]+)(@intSuffixes)?/, { token: 'number' }],
  333. //Binary
  334. [/(0b[0-1_]+)(@intSuffixes)?/, { token: 'number' }],
  335. //Exponent
  336. [/[\d][\d_]*(\.[\d][\d_]*)?[eE][+-][\d_]+(@floatSuffixes)?/, { token: 'number' }],
  337. //Float
  338. [/\b(\d\.?[\d_]*)(@floatSuffixes)?\b/, { token: 'number' }],
  339. //Hexadecimal
  340. [/(0x[\da-fA-F]+)_?(@intSuffixes)?/, { token: 'number' }],
  341. //Integer
  342. [/[\d][\d_]*(@intSuffixes?)?/, { token: 'number' }]
  343. ]
  344. }
  345. };