cpp.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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', 'comment'] },
  21. { open: '"', close: '"', notIn: ['string'] }
  22. ],
  23. surroundingPairs: [
  24. { open: '{', close: '}' },
  25. { open: '[', close: ']' },
  26. { open: '(', close: ')' },
  27. { open: '"', close: '"' },
  28. { open: "'", close: "'" }
  29. ],
  30. folding: {
  31. markers: {
  32. start: new RegExp('^\\s*#pragma\\s+region\\b'),
  33. end: new RegExp('^\\s*#pragma\\s+endregion\\b')
  34. }
  35. }
  36. };
  37. export const language = <languages.IMonarchLanguage>{
  38. defaultToken: '',
  39. tokenPostfix: '.cpp',
  40. brackets: [
  41. { token: 'delimiter.curly', open: '{', close: '}' },
  42. { token: 'delimiter.parenthesis', open: '(', close: ')' },
  43. { token: 'delimiter.square', open: '[', close: ']' },
  44. { token: 'delimiter.angle', open: '<', close: '>' }
  45. ],
  46. keywords: [
  47. 'abstract',
  48. 'amp',
  49. 'array',
  50. 'auto',
  51. 'bool',
  52. 'break',
  53. 'case',
  54. 'catch',
  55. 'char',
  56. 'class',
  57. 'const',
  58. 'constexpr',
  59. 'const_cast',
  60. 'continue',
  61. 'cpu',
  62. 'decltype',
  63. 'default',
  64. 'delegate',
  65. 'delete',
  66. 'do',
  67. 'double',
  68. 'dynamic_cast',
  69. 'each',
  70. 'else',
  71. 'enum',
  72. 'event',
  73. 'explicit',
  74. 'export',
  75. 'extern',
  76. 'false',
  77. 'final',
  78. 'finally',
  79. 'float',
  80. 'for',
  81. 'friend',
  82. 'gcnew',
  83. 'generic',
  84. 'goto',
  85. 'if',
  86. 'in',
  87. 'initonly',
  88. 'inline',
  89. 'int',
  90. 'interface',
  91. 'interior_ptr',
  92. 'internal',
  93. 'literal',
  94. 'long',
  95. 'mutable',
  96. 'namespace',
  97. 'new',
  98. 'noexcept',
  99. 'nullptr',
  100. '__nullptr',
  101. 'operator',
  102. 'override',
  103. 'partial',
  104. 'pascal',
  105. 'pin_ptr',
  106. 'private',
  107. 'property',
  108. 'protected',
  109. 'public',
  110. 'ref',
  111. 'register',
  112. 'reinterpret_cast',
  113. 'restrict',
  114. 'return',
  115. 'safe_cast',
  116. 'sealed',
  117. 'short',
  118. 'signed',
  119. 'sizeof',
  120. 'static',
  121. 'static_assert',
  122. 'static_cast',
  123. 'struct',
  124. 'switch',
  125. 'template',
  126. 'this',
  127. 'thread_local',
  128. 'throw',
  129. 'tile_static',
  130. 'true',
  131. 'try',
  132. 'typedef',
  133. 'typeid',
  134. 'typename',
  135. 'union',
  136. 'unsigned',
  137. 'using',
  138. 'virtual',
  139. 'void',
  140. 'volatile',
  141. 'wchar_t',
  142. 'where',
  143. 'while',
  144. '_asm', // reserved word with one underscores
  145. '_based',
  146. '_cdecl',
  147. '_declspec',
  148. '_fastcall',
  149. '_if_exists',
  150. '_if_not_exists',
  151. '_inline',
  152. '_multiple_inheritance',
  153. '_pascal',
  154. '_single_inheritance',
  155. '_stdcall',
  156. '_virtual_inheritance',
  157. '_w64',
  158. '__abstract', // reserved word with two underscores
  159. '__alignof',
  160. '__asm',
  161. '__assume',
  162. '__based',
  163. '__box',
  164. '__builtin_alignof',
  165. '__cdecl',
  166. '__clrcall',
  167. '__declspec',
  168. '__delegate',
  169. '__event',
  170. '__except',
  171. '__fastcall',
  172. '__finally',
  173. '__forceinline',
  174. '__gc',
  175. '__hook',
  176. '__identifier',
  177. '__if_exists',
  178. '__if_not_exists',
  179. '__inline',
  180. '__int128',
  181. '__int16',
  182. '__int32',
  183. '__int64',
  184. '__int8',
  185. '__interface',
  186. '__leave',
  187. '__m128',
  188. '__m128d',
  189. '__m128i',
  190. '__m256',
  191. '__m256d',
  192. '__m256i',
  193. '__m512',
  194. '__m512d',
  195. '__m512i',
  196. '__m64',
  197. '__multiple_inheritance',
  198. '__newslot',
  199. '__nogc',
  200. '__noop',
  201. '__nounwind',
  202. '__novtordisp',
  203. '__pascal',
  204. '__pin',
  205. '__pragma',
  206. '__property',
  207. '__ptr32',
  208. '__ptr64',
  209. '__raise',
  210. '__restrict',
  211. '__resume',
  212. '__sealed',
  213. '__single_inheritance',
  214. '__stdcall',
  215. '__super',
  216. '__thiscall',
  217. '__try',
  218. '__try_cast',
  219. '__typeof',
  220. '__unaligned',
  221. '__unhook',
  222. '__uuidof',
  223. '__value',
  224. '__virtual_inheritance',
  225. '__w64',
  226. '__wchar_t'
  227. ],
  228. operators: [
  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. '+=',
  256. '-=',
  257. '*=',
  258. '/=',
  259. '&=',
  260. '|=',
  261. '^=',
  262. '%=',
  263. '<<=',
  264. '>>=',
  265. '>>>='
  266. ],
  267. // we include these common regular expressions
  268. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  269. escapes: /\\(?:[0abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  270. integersuffix: /([uU](ll|LL|l|L)|(ll|LL|l|L)?[uU]?)/,
  271. floatsuffix: /[fFlL]?/,
  272. encoding: /u|u8|U|L/,
  273. // The main tokenizer for our languages
  274. tokenizer: {
  275. root: [
  276. // C++ 11 Raw String
  277. [/@encoding?R\"(?:([^ ()\\\t]*))\(/, { token: 'string.raw.begin', next: '@raw.$1' }],
  278. // identifiers and keywords
  279. [
  280. /[a-zA-Z_]\w*/,
  281. {
  282. cases: {
  283. '@keywords': { token: 'keyword.$0' },
  284. '@default': 'identifier'
  285. }
  286. }
  287. ],
  288. // The preprocessor checks must be before whitespace as they check /^\s*#/ which
  289. // otherwise fails to match later after other whitespace has been removed.
  290. // Inclusion
  291. [/^\s*#\s*include/, { token: 'keyword.directive.include', next: '@include' }],
  292. // Preprocessor directive
  293. [/^\s*#\s*\w+/, 'keyword.directive'],
  294. // whitespace
  295. { include: '@whitespace' },
  296. // [[ attributes ]].
  297. [/\[\s*\[/, { token: 'annotation', next: '@annotation' }],
  298. // delimiters and operators
  299. [/[{}()<>\[\]]/, '@brackets'],
  300. [
  301. /@symbols/,
  302. {
  303. cases: {
  304. '@operators': 'delimiter',
  305. '@default': ''
  306. }
  307. }
  308. ],
  309. // numbers
  310. [/\d*\d+[eE]([\-+]?\d+)?(@floatsuffix)/, 'number.float'],
  311. [/\d*\.\d+([eE][\-+]?\d+)?(@floatsuffix)/, 'number.float'],
  312. [/0[xX][0-9a-fA-F']*[0-9a-fA-F](@integersuffix)/, 'number.hex'],
  313. [/0[0-7']*[0-7](@integersuffix)/, 'number.octal'],
  314. [/0[bB][0-1']*[0-1](@integersuffix)/, 'number.binary'],
  315. [/\d[\d']*\d(@integersuffix)/, 'number'],
  316. [/\d(@integersuffix)/, 'number'],
  317. // delimiter: after number because of .\d floats
  318. [/[;,.]/, 'delimiter'],
  319. // strings
  320. [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  321. [/"/, 'string', '@string'],
  322. // characters
  323. [/'[^\\']'/, 'string'],
  324. [/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
  325. [/'/, 'string.invalid']
  326. ],
  327. whitespace: [
  328. [/[ \t\r\n]+/, ''],
  329. [/\/\*\*(?!\/)/, 'comment.doc', '@doccomment'],
  330. [/\/\*/, 'comment', '@comment'],
  331. [/\/\/.*\\$/, 'comment', '@linecomment'],
  332. [/\/\/.*$/, 'comment']
  333. ],
  334. comment: [
  335. [/[^\/*]+/, 'comment'],
  336. [/\*\//, 'comment', '@pop'],
  337. [/[\/*]/, 'comment']
  338. ],
  339. //For use with continuous line comments
  340. linecomment: [
  341. [/.*[^\\]$/, 'comment', '@pop'],
  342. [/[^]+/, 'comment']
  343. ],
  344. //Identical copy of comment above, except for the addition of .doc
  345. doccomment: [
  346. [/[^\/*]+/, 'comment.doc'],
  347. [/\*\//, 'comment.doc', '@pop'],
  348. [/[\/*]/, 'comment.doc']
  349. ],
  350. string: [
  351. [/[^\\"]+/, 'string'],
  352. [/@escapes/, 'string.escape'],
  353. [/\\./, 'string.escape.invalid'],
  354. [/"/, 'string', '@pop']
  355. ],
  356. raw: [
  357. [
  358. /(.*)(\))(?:([^ ()\\\t"]*))(\")/,
  359. {
  360. cases: {
  361. '$3==$S2': [
  362. 'string.raw',
  363. 'string.raw.end',
  364. 'string.raw.end',
  365. { token: 'string.raw.end', next: '@pop' }
  366. ],
  367. '@default': ['string.raw', 'string.raw', 'string.raw', 'string.raw']
  368. }
  369. }
  370. ],
  371. [/.*/, 'string.raw']
  372. ],
  373. annotation: [
  374. { include: '@whitespace' },
  375. [/using|alignas/, 'keyword'],
  376. [/[a-zA-Z0-9_]+/, 'annotation'],
  377. [/[,:]/, 'delimiter'],
  378. [/[()]/, '@brackets'],
  379. [/\]\s*\]/, { token: 'annotation', next: '@pop' }]
  380. ],
  381. include: [
  382. [
  383. /(\s*)(<)([^<>]*)(>)/,
  384. [
  385. '',
  386. 'keyword.directive.include.begin',
  387. 'string.include.identifier',
  388. { token: 'keyword.directive.include.end', next: '@pop' }
  389. ]
  390. ],
  391. [
  392. /(\s*)(")([^"]*)(")/,
  393. [
  394. '',
  395. 'keyword.directive.include.begin',
  396. 'string.include.identifier',
  397. { token: 'keyword.directive.include.end', next: '@pop' }
  398. ]
  399. ]
  400. ]
  401. }
  402. };