apex.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. // the default separators except `@$`
  8. wordPattern:
  9. /(-?\d*\.\d\w*)|([^\`\~\!\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
  10. comments: {
  11. lineComment: '//',
  12. blockComment: ['/*', '*/']
  13. },
  14. brackets: [
  15. ['{', '}'],
  16. ['[', ']'],
  17. ['(', ')']
  18. ],
  19. autoClosingPairs: [
  20. { open: '{', close: '}' },
  21. { open: '[', close: ']' },
  22. { open: '(', close: ')' },
  23. { open: '"', close: '"' },
  24. { open: "'", close: "'" }
  25. ],
  26. surroundingPairs: [
  27. { open: '{', close: '}' },
  28. { open: '[', close: ']' },
  29. { open: '(', close: ')' },
  30. { open: '"', close: '"' },
  31. { open: "'", close: "'" },
  32. { open: '<', close: '>' }
  33. ],
  34. folding: {
  35. markers: {
  36. start: new RegExp('^\\s*//\\s*(?:(?:#?region\\b)|(?:<editor-fold\\b))'),
  37. end: new RegExp('^\\s*//\\s*(?:(?:#?endregion\\b)|(?:</editor-fold>))')
  38. }
  39. }
  40. };
  41. const keywords = [
  42. 'abstract',
  43. 'activate',
  44. 'and',
  45. 'any',
  46. 'array',
  47. 'as',
  48. 'asc',
  49. 'assert',
  50. 'autonomous',
  51. 'begin',
  52. 'bigdecimal',
  53. 'blob',
  54. 'boolean',
  55. 'break',
  56. 'bulk',
  57. 'by',
  58. 'case',
  59. 'cast',
  60. 'catch',
  61. 'char',
  62. 'class',
  63. 'collect',
  64. 'commit',
  65. 'const',
  66. 'continue',
  67. 'convertcurrency',
  68. 'decimal',
  69. 'default',
  70. 'delete',
  71. 'desc',
  72. 'do',
  73. 'double',
  74. 'else',
  75. 'end',
  76. 'enum',
  77. 'exception',
  78. 'exit',
  79. 'export',
  80. 'extends',
  81. 'false',
  82. 'final',
  83. 'finally',
  84. 'float',
  85. 'for',
  86. 'from',
  87. 'future',
  88. 'get',
  89. 'global',
  90. 'goto',
  91. 'group',
  92. 'having',
  93. 'hint',
  94. 'if',
  95. 'implements',
  96. 'import',
  97. 'in',
  98. 'inner',
  99. 'insert',
  100. 'instanceof',
  101. 'int',
  102. 'interface',
  103. 'into',
  104. 'join',
  105. 'last_90_days',
  106. 'last_month',
  107. 'last_n_days',
  108. 'last_week',
  109. 'like',
  110. 'limit',
  111. 'list',
  112. 'long',
  113. 'loop',
  114. 'map',
  115. 'merge',
  116. 'native',
  117. 'new',
  118. 'next_90_days',
  119. 'next_month',
  120. 'next_n_days',
  121. 'next_week',
  122. 'not',
  123. 'null',
  124. 'nulls',
  125. 'number',
  126. 'object',
  127. 'of',
  128. 'on',
  129. 'or',
  130. 'outer',
  131. 'override',
  132. 'package',
  133. 'parallel',
  134. 'pragma',
  135. 'private',
  136. 'protected',
  137. 'public',
  138. 'retrieve',
  139. 'return',
  140. 'returning',
  141. 'rollback',
  142. 'savepoint',
  143. 'search',
  144. 'select',
  145. 'set',
  146. 'short',
  147. 'sort',
  148. 'stat',
  149. 'static',
  150. 'strictfp',
  151. 'super',
  152. 'switch',
  153. 'synchronized',
  154. 'system',
  155. 'testmethod',
  156. 'then',
  157. 'this',
  158. 'this_month',
  159. 'this_week',
  160. 'throw',
  161. 'throws',
  162. 'today',
  163. 'tolabel',
  164. 'tomorrow',
  165. 'transaction',
  166. 'transient',
  167. 'trigger',
  168. 'true',
  169. 'try',
  170. 'type',
  171. 'undelete',
  172. 'update',
  173. 'upsert',
  174. 'using',
  175. 'virtual',
  176. 'void',
  177. 'volatile',
  178. 'webservice',
  179. 'when',
  180. 'where',
  181. 'while',
  182. 'yesterday'
  183. ];
  184. // create case variations of the keywords - apex is case insensitive, but we can't make the highlighter case insensitive
  185. // because we use a heuristic to assume that identifiers starting with an upper case letter are types.
  186. const uppercaseFirstLetter = (lowercase: string) =>
  187. lowercase.charAt(0).toUpperCase() + lowercase.substr(1);
  188. let keywordsWithCaseVariations: string[] = [];
  189. keywords.forEach((lowercase) => {
  190. keywordsWithCaseVariations.push(lowercase);
  191. keywordsWithCaseVariations.push(lowercase.toUpperCase());
  192. keywordsWithCaseVariations.push(uppercaseFirstLetter(lowercase));
  193. });
  194. export const language = <languages.IMonarchLanguage>{
  195. defaultToken: '',
  196. tokenPostfix: '.apex',
  197. keywords: keywordsWithCaseVariations,
  198. operators: [
  199. '=',
  200. '>',
  201. '<',
  202. '!',
  203. '~',
  204. '?',
  205. ':',
  206. '==',
  207. '<=',
  208. '>=',
  209. '!=',
  210. '&&',
  211. '||',
  212. '++',
  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. // we include these common regular expressions
  238. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  239. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  240. digits: /\d+(_+\d+)*/,
  241. octaldigits: /[0-7]+(_+[0-7]+)*/,
  242. binarydigits: /[0-1]+(_+[0-1]+)*/,
  243. hexdigits: /[[0-9a-fA-F]+(_+[0-9a-fA-F]+)*/,
  244. // The main tokenizer for our languages
  245. tokenizer: {
  246. root: [
  247. // identifiers and keywords
  248. [
  249. /[a-z_$][\w$]*/,
  250. {
  251. cases: {
  252. '@keywords': { token: 'keyword.$0' },
  253. '@default': 'identifier'
  254. }
  255. }
  256. ],
  257. // assume that identifiers starting with an uppercase letter are types
  258. [
  259. /[A-Z][\w\$]*/,
  260. {
  261. cases: {
  262. '@keywords': { token: 'keyword.$0' },
  263. '@default': 'type.identifier'
  264. }
  265. }
  266. ],
  267. // whitespace
  268. { include: '@whitespace' },
  269. // delimiters and operators
  270. [/[{}()\[\]]/, '@brackets'],
  271. [/[<>](?!@symbols)/, '@brackets'],
  272. [
  273. /@symbols/,
  274. {
  275. cases: {
  276. '@operators': 'delimiter',
  277. '@default': ''
  278. }
  279. }
  280. ],
  281. // @ annotations.
  282. [/@\s*[a-zA-Z_\$][\w\$]*/, 'annotation'],
  283. // numbers
  284. [/(@digits)[eE]([\-+]?(@digits))?[fFdD]?/, 'number.float'],
  285. [/(@digits)\.(@digits)([eE][\-+]?(@digits))?[fFdD]?/, 'number.float'],
  286. [/(@digits)[fFdD]/, 'number.float'],
  287. [/(@digits)[lL]?/, 'number'],
  288. // delimiter: after number because of .\d floats
  289. [/[;,.]/, 'delimiter'],
  290. // strings
  291. [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  292. [/'([^'\\]|\\.)*$/, 'string.invalid'], // non-teminated string
  293. [/"/, 'string', '@string."'],
  294. [/'/, 'string', "@string.'"],
  295. // characters
  296. [/'[^\\']'/, 'string'],
  297. [/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
  298. [/'/, 'string.invalid']
  299. ],
  300. whitespace: [
  301. [/[ \t\r\n]+/, ''],
  302. [/\/\*\*(?!\/)/, 'comment.doc', '@apexdoc'],
  303. [/\/\*/, 'comment', '@comment'],
  304. [/\/\/.*$/, 'comment']
  305. ],
  306. comment: [
  307. [/[^\/*]+/, 'comment'],
  308. // [/\/\*/, 'comment', '@push' ], // nested comment not allowed :-(
  309. // [/\/\*/, 'comment.invalid' ], // this breaks block comments in the shape of /* //*/
  310. [/\*\//, 'comment', '@pop'],
  311. [/[\/*]/, 'comment']
  312. ],
  313. //Identical copy of comment above, except for the addition of .doc
  314. apexdoc: [
  315. [/[^\/*]+/, 'comment.doc'],
  316. [/\*\//, 'comment.doc', '@pop'],
  317. [/[\/*]/, 'comment.doc']
  318. ],
  319. string: [
  320. [/[^\\"']+/, 'string'],
  321. [/@escapes/, 'string.escape'],
  322. [/\\./, 'string.escape.invalid'],
  323. [
  324. /["']/,
  325. {
  326. cases: {
  327. '$#==$S2': { token: 'string', next: '@pop' },
  328. '@default': 'string'
  329. }
  330. }
  331. ]
  332. ]
  333. }
  334. };