cpp.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. '__m64',
  194. '__multiple_inheritance',
  195. '__newslot',
  196. '__nogc',
  197. '__noop',
  198. '__nounwind',
  199. '__novtordisp',
  200. '__pascal',
  201. '__pin',
  202. '__pragma',
  203. '__property',
  204. '__ptr32',
  205. '__ptr64',
  206. '__raise',
  207. '__restrict',
  208. '__resume',
  209. '__sealed',
  210. '__single_inheritance',
  211. '__stdcall',
  212. '__super',
  213. '__thiscall',
  214. '__try',
  215. '__try_cast',
  216. '__typeof',
  217. '__unaligned',
  218. '__unhook',
  219. '__uuidof',
  220. '__value',
  221. '__virtual_inheritance',
  222. '__w64',
  223. '__wchar_t'
  224. ],
  225. operators: [
  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. '/=',
  256. '&=',
  257. '|=',
  258. '^=',
  259. '%=',
  260. '<<=',
  261. '>>=',
  262. '>>>='
  263. ],
  264. // we include these common regular expressions
  265. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  266. escapes: /\\(?:[0abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  267. integersuffix: /([uU](ll|LL|l|L)|(ll|LL|l|L)?[uU]?)/,
  268. floatsuffix: /[fFlL]?/,
  269. encoding: /u|u8|U|L/,
  270. // The main tokenizer for our languages
  271. tokenizer: {
  272. root: [
  273. // C++ 11 Raw String
  274. [/@encoding?R\"(?:([^ ()\\\t]*))\(/, { token: 'string.raw.begin', next: '@raw.$1' }],
  275. // identifiers and keywords
  276. [
  277. /[a-zA-Z_]\w*/,
  278. {
  279. cases: {
  280. '@keywords': { token: 'keyword.$0' },
  281. '@default': 'identifier'
  282. }
  283. }
  284. ],
  285. // The preprocessor checks must be before whitespace as they check /^\s*#/ which
  286. // otherwise fails to match later after other whitespace has been removed.
  287. // Inclusion
  288. [/^\s*#\s*include/, { token: 'keyword.directive.include', next: '@include' }],
  289. // Preprocessor directive
  290. [/^\s*#\s*\w+/, 'keyword.directive'],
  291. // whitespace
  292. { include: '@whitespace' },
  293. // [[ attributes ]].
  294. [/\[\s*\[/, { token: 'annotation', next: '@annotation' }],
  295. // delimiters and operators
  296. [/[{}()\[\]]/, '@brackets'],
  297. [/[<>](?!@symbols)/, '@brackets'],
  298. [
  299. /@symbols/,
  300. {
  301. cases: {
  302. '@operators': 'delimiter',
  303. '@default': ''
  304. }
  305. }
  306. ],
  307. // numbers
  308. [/\d*\d+[eE]([\-+]?\d+)?(@floatsuffix)/, 'number.float'],
  309. [/\d*\.\d+([eE][\-+]?\d+)?(@floatsuffix)/, 'number.float'],
  310. [/0[xX][0-9a-fA-F']*[0-9a-fA-F](@integersuffix)/, 'number.hex'],
  311. [/0[0-7']*[0-7](@integersuffix)/, 'number.octal'],
  312. [/0[bB][0-1']*[0-1](@integersuffix)/, 'number.binary'],
  313. [/\d[\d']*\d(@integersuffix)/, 'number'],
  314. [/\d(@integersuffix)/, 'number'],
  315. // delimiter: after number because of .\d floats
  316. [/[;,.]/, 'delimiter'],
  317. // strings
  318. [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  319. [/"/, 'string', '@string'],
  320. // characters
  321. [/'[^\\']'/, 'string'],
  322. [/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
  323. [/'/, 'string.invalid']
  324. ],
  325. whitespace: [
  326. [/[ \t\r\n]+/, ''],
  327. [/\/\*\*(?!\/)/, 'comment.doc', '@doccomment'],
  328. [/\/\*/, 'comment', '@comment'],
  329. [/\/\/.*\\$/, 'comment', '@linecomment'],
  330. [/\/\/.*$/, 'comment']
  331. ],
  332. comment: [
  333. [/[^\/*]+/, 'comment'],
  334. [/\*\//, 'comment', '@pop'],
  335. [/[\/*]/, 'comment']
  336. ],
  337. //For use with continuous line comments
  338. linecomment: [
  339. [/.*[^\\]$/, 'comment', '@pop'],
  340. [/[^]+/, 'comment']
  341. ],
  342. //Identical copy of comment above, except for the addition of .doc
  343. doccomment: [
  344. [/[^\/*]+/, 'comment.doc'],
  345. [/\*\//, 'comment.doc', '@pop'],
  346. [/[\/*]/, 'comment.doc']
  347. ],
  348. string: [
  349. [/[^\\"]+/, 'string'],
  350. [/@escapes/, 'string.escape'],
  351. [/\\./, 'string.escape.invalid'],
  352. [/"/, 'string', '@pop']
  353. ],
  354. raw: [
  355. [
  356. /(.*)(\))(?:([^ ()\\\t"]*))(\")/,
  357. {
  358. cases: {
  359. '$3==$S2': [
  360. 'string.raw',
  361. 'string.raw.end',
  362. 'string.raw.end',
  363. { token: 'string.raw.end', next: '@pop' }
  364. ],
  365. '@default': ['string.raw', 'string.raw', 'string.raw', 'string.raw']
  366. }
  367. }
  368. ],
  369. [/.*/, 'string.raw']
  370. ],
  371. annotation: [
  372. { include: '@whitespace' },
  373. [/using|alignas/, 'keyword'],
  374. [/[a-zA-Z0-9_]+/, 'annotation'],
  375. [/[,:]/, 'delimiter'],
  376. [/[()]/, '@brackets'],
  377. [/\]\s*\]/, { token: 'annotation', next: '@pop' }]
  378. ],
  379. include: [
  380. [
  381. /(\s*)(<)([^<>]*)(>)/,
  382. [
  383. '',
  384. 'keyword.directive.include.begin',
  385. 'string.include.identifier',
  386. { token: 'keyword.directive.include.end', next: '@pop' }
  387. ]
  388. ],
  389. [
  390. /(\s*)(")([^"]*)(")/,
  391. [
  392. '',
  393. 'keyword.directive.include.begin',
  394. 'string.include.identifier',
  395. { token: 'keyword.directive.include.end', next: '@pop' }
  396. ]
  397. ]
  398. ]
  399. }
  400. };