elixir.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*!-----------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Version: 0.31.1(337587859b1c171314b40503171188b6cea6a32a)
  4. * Released under the MIT license
  5. * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt
  6. *-----------------------------------------------------------------------------*/
  7. // src/basic-languages/elixir/elixir.ts
  8. var conf = {
  9. comments: {
  10. lineComment: "#"
  11. },
  12. brackets: [
  13. ["{", "}"],
  14. ["[", "]"],
  15. ["(", ")"]
  16. ],
  17. surroundingPairs: [
  18. { open: "{", close: "}" },
  19. { open: "[", close: "]" },
  20. { open: "(", close: ")" },
  21. { open: "'", close: "'" },
  22. { open: '"', close: '"' }
  23. ],
  24. autoClosingPairs: [
  25. { open: "'", close: "'", notIn: ["string", "comment"] },
  26. { open: '"', close: '"', notIn: ["comment"] },
  27. { open: '"""', close: '"""' },
  28. { open: "`", close: "`", notIn: ["string", "comment"] },
  29. { open: "(", close: ")" },
  30. { open: "{", close: "}" },
  31. { open: "[", close: "]" },
  32. { open: "<<", close: ">>" }
  33. ],
  34. indentationRules: {
  35. increaseIndentPattern: /^\s*(after|else|catch|rescue|fn|[^#]*(do|<\-|\->|\{|\[|\=))\s*$/,
  36. decreaseIndentPattern: /^\s*((\}|\])\s*$|(after|else|catch|rescue|end)\b)/
  37. }
  38. };
  39. var language = {
  40. defaultToken: "source",
  41. tokenPostfix: ".elixir",
  42. brackets: [
  43. { open: "[", close: "]", token: "delimiter.square" },
  44. { open: "(", close: ")", token: "delimiter.parenthesis" },
  45. { open: "{", close: "}", token: "delimiter.curly" },
  46. { open: "<<", close: ">>", token: "delimiter.angle.special" }
  47. ],
  48. declarationKeywords: [
  49. "def",
  50. "defp",
  51. "defn",
  52. "defnp",
  53. "defguard",
  54. "defguardp",
  55. "defmacro",
  56. "defmacrop",
  57. "defdelegate",
  58. "defcallback",
  59. "defmacrocallback",
  60. "defmodule",
  61. "defprotocol",
  62. "defexception",
  63. "defimpl",
  64. "defstruct"
  65. ],
  66. operatorKeywords: ["and", "in", "not", "or", "when"],
  67. namespaceKeywords: ["alias", "import", "require", "use"],
  68. otherKeywords: [
  69. "after",
  70. "case",
  71. "catch",
  72. "cond",
  73. "do",
  74. "else",
  75. "end",
  76. "fn",
  77. "for",
  78. "if",
  79. "quote",
  80. "raise",
  81. "receive",
  82. "rescue",
  83. "super",
  84. "throw",
  85. "try",
  86. "unless",
  87. "unquote_splicing",
  88. "unquote",
  89. "with"
  90. ],
  91. constants: ["true", "false", "nil"],
  92. nameBuiltin: ["__MODULE__", "__DIR__", "__ENV__", "__CALLER__", "__STACKTRACE__"],
  93. operator: /-[->]?|!={0,2}|\*{1,2}|\/|\\\\|&{1,3}|\.\.?|\^(?:\^\^)?|\+\+?|<(?:-|<<|=|>|\|>|~>?)?|=~|={1,3}|>(?:=|>>)?|\|~>|\|>|\|{1,3}|~>>?|~~~|::/,
  94. variableName: /[a-z_][a-zA-Z0-9_]*[?!]?/,
  95. atomName: /[a-zA-Z_][a-zA-Z0-9_@]*[?!]?|@specialAtomName|@operator/,
  96. specialAtomName: /\.\.\.|<<>>|%\{\}|%|\{\}/,
  97. aliasPart: /[A-Z][a-zA-Z0-9_]*/,
  98. moduleName: /@aliasPart(?:\.@aliasPart)*/,
  99. sigilSymmetricDelimiter: /"""|'''|"|'|\/|\|/,
  100. sigilStartDelimiter: /@sigilSymmetricDelimiter|<|\{|\[|\(/,
  101. sigilEndDelimiter: /@sigilSymmetricDelimiter|>|\}|\]|\)/,
  102. sigilModifiers: /[a-zA-Z0-9]*/,
  103. decimal: /\d(?:_?\d)*/,
  104. hex: /[0-9a-fA-F](_?[0-9a-fA-F])*/,
  105. octal: /[0-7](_?[0-7])*/,
  106. binary: /[01](_?[01])*/,
  107. escape: /\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2}|\\./,
  108. tokenizer: {
  109. root: [
  110. { include: "@whitespace" },
  111. { include: "@comments" },
  112. { include: "@keywordsShorthand" },
  113. { include: "@numbers" },
  114. { include: "@identifiers" },
  115. { include: "@strings" },
  116. { include: "@atoms" },
  117. { include: "@sigils" },
  118. { include: "@attributes" },
  119. { include: "@symbols" }
  120. ],
  121. whitespace: [[/\s+/, "white"]],
  122. comments: [[/(#)(.*)/, ["comment.punctuation", "comment"]]],
  123. keywordsShorthand: [
  124. [/(@atomName)(:)/, ["constant", "constant.punctuation"]],
  125. [
  126. /"(?=([^"]|#\{.*?\}|\\")*":)/,
  127. { token: "constant.delimiter", next: "@doubleQuotedStringKeyword" }
  128. ],
  129. [
  130. /'(?=([^']|#\{.*?\}|\\')*':)/,
  131. { token: "constant.delimiter", next: "@singleQuotedStringKeyword" }
  132. ]
  133. ],
  134. doubleQuotedStringKeyword: [
  135. [/":/, { token: "constant.delimiter", next: "@pop" }],
  136. { include: "@stringConstantContentInterpol" }
  137. ],
  138. singleQuotedStringKeyword: [
  139. [/':/, { token: "constant.delimiter", next: "@pop" }],
  140. { include: "@stringConstantContentInterpol" }
  141. ],
  142. numbers: [
  143. [/0b@binary/, "number.binary"],
  144. [/0o@octal/, "number.octal"],
  145. [/0x@hex/, "number.hex"],
  146. [/@decimal\.@decimal([eE]-?@decimal)?/, "number.float"],
  147. [/@decimal/, "number"]
  148. ],
  149. identifiers: [
  150. [
  151. /\b(defp?|defnp?|defmacrop?|defguardp?|defdelegate)(\s+)(@variableName)(?!\s+@operator)/,
  152. [
  153. "keyword.declaration",
  154. "white",
  155. {
  156. cases: {
  157. unquote: "keyword",
  158. "@default": "function"
  159. }
  160. }
  161. ]
  162. ],
  163. [
  164. /(@variableName)(?=\s*\.?\s*\()/,
  165. {
  166. cases: {
  167. "@declarationKeywords": "keyword.declaration",
  168. "@namespaceKeywords": "keyword",
  169. "@otherKeywords": "keyword",
  170. "@default": "function.call"
  171. }
  172. }
  173. ],
  174. [
  175. /(@moduleName)(\s*)(\.)(\s*)(@variableName)/,
  176. ["type.identifier", "white", "operator", "white", "function.call"]
  177. ],
  178. [
  179. /(:)(@atomName)(\s*)(\.)(\s*)(@variableName)/,
  180. ["constant.punctuation", "constant", "white", "operator", "white", "function.call"]
  181. ],
  182. [
  183. /(\|>)(\s*)(@variableName)/,
  184. [
  185. "operator",
  186. "white",
  187. {
  188. cases: {
  189. "@otherKeywords": "keyword",
  190. "@default": "function.call"
  191. }
  192. }
  193. ]
  194. ],
  195. [
  196. /(&)(\s*)(@variableName)/,
  197. ["operator", "white", "function.call"]
  198. ],
  199. [
  200. /@variableName/,
  201. {
  202. cases: {
  203. "@declarationKeywords": "keyword.declaration",
  204. "@operatorKeywords": "keyword.operator",
  205. "@namespaceKeywords": "keyword",
  206. "@otherKeywords": "keyword",
  207. "@constants": "constant.language",
  208. "@nameBuiltin": "variable.language",
  209. "_.*": "comment.unused",
  210. "@default": "identifier"
  211. }
  212. }
  213. ],
  214. [/@moduleName/, "type.identifier"]
  215. ],
  216. strings: [
  217. [/"""/, { token: "string.delimiter", next: "@doubleQuotedHeredoc" }],
  218. [/'''/, { token: "string.delimiter", next: "@singleQuotedHeredoc" }],
  219. [/"/, { token: "string.delimiter", next: "@doubleQuotedString" }],
  220. [/'/, { token: "string.delimiter", next: "@singleQuotedString" }]
  221. ],
  222. doubleQuotedHeredoc: [
  223. [/"""/, { token: "string.delimiter", next: "@pop" }],
  224. { include: "@stringContentInterpol" }
  225. ],
  226. singleQuotedHeredoc: [
  227. [/'''/, { token: "string.delimiter", next: "@pop" }],
  228. { include: "@stringContentInterpol" }
  229. ],
  230. doubleQuotedString: [
  231. [/"/, { token: "string.delimiter", next: "@pop" }],
  232. { include: "@stringContentInterpol" }
  233. ],
  234. singleQuotedString: [
  235. [/'/, { token: "string.delimiter", next: "@pop" }],
  236. { include: "@stringContentInterpol" }
  237. ],
  238. atoms: [
  239. [/(:)(@atomName)/, ["constant.punctuation", "constant"]],
  240. [/:"/, { token: "constant.delimiter", next: "@doubleQuotedStringAtom" }],
  241. [/:'/, { token: "constant.delimiter", next: "@singleQuotedStringAtom" }]
  242. ],
  243. doubleQuotedStringAtom: [
  244. [/"/, { token: "constant.delimiter", next: "@pop" }],
  245. { include: "@stringConstantContentInterpol" }
  246. ],
  247. singleQuotedStringAtom: [
  248. [/'/, { token: "constant.delimiter", next: "@pop" }],
  249. { include: "@stringConstantContentInterpol" }
  250. ],
  251. sigils: [
  252. [/~[a-z]@sigilStartDelimiter/, { token: "@rematch", next: "@sigil.interpol" }],
  253. [/~[A-Z]@sigilStartDelimiter/, { token: "@rematch", next: "@sigil.noInterpol" }]
  254. ],
  255. sigil: [
  256. [/~([a-zA-Z])\{/, { token: "@rematch", switchTo: "@sigilStart.$S2.$1.{.}" }],
  257. [/~([a-zA-Z])\[/, { token: "@rematch", switchTo: "@sigilStart.$S2.$1.[.]" }],
  258. [/~([a-zA-Z])\(/, { token: "@rematch", switchTo: "@sigilStart.$S2.$1.(.)" }],
  259. [/~([a-zA-Z])\</, { token: "@rematch", switchTo: "@sigilStart.$S2.$1.<.>" }],
  260. [
  261. /~([a-zA-Z])(@sigilSymmetricDelimiter)/,
  262. { token: "@rematch", switchTo: "@sigilStart.$S2.$1.$2.$2" }
  263. ]
  264. ],
  265. "sigilStart.interpol.s": [
  266. [
  267. /~s@sigilStartDelimiter/,
  268. {
  269. token: "string.delimiter",
  270. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  271. }
  272. ]
  273. ],
  274. "sigilContinue.interpol.s": [
  275. [
  276. /(@sigilEndDelimiter)@sigilModifiers/,
  277. {
  278. cases: {
  279. "$1==$S5": { token: "string.delimiter", next: "@pop" },
  280. "@default": "string"
  281. }
  282. }
  283. ],
  284. { include: "@stringContentInterpol" }
  285. ],
  286. "sigilStart.noInterpol.S": [
  287. [
  288. /~S@sigilStartDelimiter/,
  289. {
  290. token: "string.delimiter",
  291. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  292. }
  293. ]
  294. ],
  295. "sigilContinue.noInterpol.S": [
  296. [/(^|[^\\])\\@sigilEndDelimiter/, "string"],
  297. [
  298. /(@sigilEndDelimiter)@sigilModifiers/,
  299. {
  300. cases: {
  301. "$1==$S5": { token: "string.delimiter", next: "@pop" },
  302. "@default": "string"
  303. }
  304. }
  305. ],
  306. { include: "@stringContent" }
  307. ],
  308. "sigilStart.interpol.r": [
  309. [
  310. /~r@sigilStartDelimiter/,
  311. {
  312. token: "regexp.delimiter",
  313. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  314. }
  315. ]
  316. ],
  317. "sigilContinue.interpol.r": [
  318. [
  319. /(@sigilEndDelimiter)@sigilModifiers/,
  320. {
  321. cases: {
  322. "$1==$S5": { token: "regexp.delimiter", next: "@pop" },
  323. "@default": "regexp"
  324. }
  325. }
  326. ],
  327. { include: "@regexpContentInterpol" }
  328. ],
  329. "sigilStart.noInterpol.R": [
  330. [
  331. /~R@sigilStartDelimiter/,
  332. {
  333. token: "regexp.delimiter",
  334. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  335. }
  336. ]
  337. ],
  338. "sigilContinue.noInterpol.R": [
  339. [/(^|[^\\])\\@sigilEndDelimiter/, "regexp"],
  340. [
  341. /(@sigilEndDelimiter)@sigilModifiers/,
  342. {
  343. cases: {
  344. "$1==$S5": { token: "regexp.delimiter", next: "@pop" },
  345. "@default": "regexp"
  346. }
  347. }
  348. ],
  349. { include: "@regexpContent" }
  350. ],
  351. "sigilStart.interpol": [
  352. [
  353. /~([a-zA-Z])@sigilStartDelimiter/,
  354. {
  355. token: "sigil.delimiter",
  356. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  357. }
  358. ]
  359. ],
  360. "sigilContinue.interpol": [
  361. [
  362. /(@sigilEndDelimiter)@sigilModifiers/,
  363. {
  364. cases: {
  365. "$1==$S5": { token: "sigil.delimiter", next: "@pop" },
  366. "@default": "sigil"
  367. }
  368. }
  369. ],
  370. { include: "@sigilContentInterpol" }
  371. ],
  372. "sigilStart.noInterpol": [
  373. [
  374. /~([a-zA-Z])@sigilStartDelimiter/,
  375. {
  376. token: "sigil.delimiter",
  377. switchTo: "@sigilContinue.$S2.$S3.$S4.$S5"
  378. }
  379. ]
  380. ],
  381. "sigilContinue.noInterpol": [
  382. [/(^|[^\\])\\@sigilEndDelimiter/, "sigil"],
  383. [
  384. /(@sigilEndDelimiter)@sigilModifiers/,
  385. {
  386. cases: {
  387. "$1==$S5": { token: "sigil.delimiter", next: "@pop" },
  388. "@default": "sigil"
  389. }
  390. }
  391. ],
  392. { include: "@sigilContent" }
  393. ],
  394. attributes: [
  395. [
  396. /\@(module|type)?doc (~[sS])?"""/,
  397. {
  398. token: "comment.block.documentation",
  399. next: "@doubleQuotedHeredocDocstring"
  400. }
  401. ],
  402. [
  403. /\@(module|type)?doc (~[sS])?"/,
  404. {
  405. token: "comment.block.documentation",
  406. next: "@doubleQuotedStringDocstring"
  407. }
  408. ],
  409. [/\@(module|type)?doc false/, "comment.block.documentation"],
  410. [/\@(@variableName)/, "variable"]
  411. ],
  412. doubleQuotedHeredocDocstring: [
  413. [/"""/, { token: "comment.block.documentation", next: "@pop" }],
  414. { include: "@docstringContent" }
  415. ],
  416. doubleQuotedStringDocstring: [
  417. [/"/, { token: "comment.block.documentation", next: "@pop" }],
  418. { include: "@docstringContent" }
  419. ],
  420. symbols: [
  421. [/\?(\\.|[^\\\s])/, "number.constant"],
  422. [/&\d+/, "operator"],
  423. [/<<<|>>>/, "operator"],
  424. [/[()\[\]\{\}]|<<|>>/, "@brackets"],
  425. [/\.\.\./, "identifier"],
  426. [/=>/, "punctuation"],
  427. [/@operator/, "operator"],
  428. [/[:;,.%]/, "punctuation"]
  429. ],
  430. stringContentInterpol: [
  431. { include: "@interpolation" },
  432. { include: "@escapeChar" },
  433. { include: "@stringContent" }
  434. ],
  435. stringContent: [[/./, "string"]],
  436. stringConstantContentInterpol: [
  437. { include: "@interpolation" },
  438. { include: "@escapeChar" },
  439. { include: "@stringConstantContent" }
  440. ],
  441. stringConstantContent: [[/./, "constant"]],
  442. regexpContentInterpol: [
  443. { include: "@interpolation" },
  444. { include: "@escapeChar" },
  445. { include: "@regexpContent" }
  446. ],
  447. regexpContent: [
  448. [/(\s)(#)(\s.*)$/, ["white", "comment.punctuation", "comment"]],
  449. [/./, "regexp"]
  450. ],
  451. sigilContentInterpol: [
  452. { include: "@interpolation" },
  453. { include: "@escapeChar" },
  454. { include: "@sigilContent" }
  455. ],
  456. sigilContent: [[/./, "sigil"]],
  457. docstringContent: [[/./, "comment.block.documentation"]],
  458. escapeChar: [[/@escape/, "constant.character.escape"]],
  459. interpolation: [[/#{/, { token: "delimiter.bracket.embed", next: "@interpolationContinue" }]],
  460. interpolationContinue: [
  461. [/}/, { token: "delimiter.bracket.embed", next: "@pop" }],
  462. { include: "@root" }
  463. ]
  464. }
  465. };
  466. export {
  467. conf,
  468. language
  469. };