elixir.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. export var conf = {
  6. comments: {
  7. lineComment: '#'
  8. },
  9. brackets: [
  10. ['{', '}'],
  11. ['[', ']'],
  12. ['(', ')']
  13. ],
  14. surroundingPairs: [
  15. { open: '{', close: '}' },
  16. { open: '[', close: ']' },
  17. { open: '(', close: ')' },
  18. { open: "'", close: "'" },
  19. { open: '"', close: '"' }
  20. ],
  21. autoClosingPairs: [
  22. { open: "'", close: "'", notIn: ['string', 'comment'] },
  23. { open: '"', close: '"', notIn: ['comment'] },
  24. { open: '"""', close: '"""' },
  25. { open: '`', close: '`', notIn: ['string', 'comment'] },
  26. { open: '(', close: ')' },
  27. { open: '{', close: '}' },
  28. { open: '[', close: ']' },
  29. { open: '<<', close: '>>' }
  30. ],
  31. indentationRules: {
  32. increaseIndentPattern: /^\s*(after|else|catch|rescue|fn|[^#]*(do|<\-|\->|\{|\[|\=))\s*$/,
  33. decreaseIndentPattern: /^\s*((\}|\])\s*$|(after|else|catch|rescue|end)\b)/
  34. }
  35. };
  36. /**
  37. * A Monarch lexer for the Elixir language.
  38. *
  39. * References:
  40. *
  41. * * Monarch documentation - https://microsoft.github.io/monaco-editor/monarch.html
  42. * * Elixir lexer - https://github.com/elixir-makeup/makeup_elixir/blob/master/lib/makeup/lexers/elixir_lexer.ex
  43. * * TextMate lexer (elixir-tmbundle) - https://github.com/elixir-editors/elixir-tmbundle/blob/master/Syntaxes/Elixir.tmLanguage
  44. * * TextMate lexer (vscode-elixir-ls) - https://github.com/elixir-lsp/vscode-elixir-ls/blob/master/syntaxes/elixir.json
  45. */
  46. export var language = {
  47. defaultToken: 'source',
  48. tokenPostfix: '.elixir',
  49. brackets: [
  50. { open: '[', close: ']', token: 'delimiter.square' },
  51. { open: '(', close: ')', token: 'delimiter.parenthesis' },
  52. { open: '{', close: '}', token: 'delimiter.curly' },
  53. { open: '<<', close: '>>', token: 'delimiter.angle.special' }
  54. ],
  55. // Below are lists/regexps to which we reference later.
  56. declarationKeywords: [
  57. 'def',
  58. 'defp',
  59. 'defn',
  60. 'defnp',
  61. 'defguard',
  62. 'defguardp',
  63. 'defmacro',
  64. 'defmacrop',
  65. 'defdelegate',
  66. 'defcallback',
  67. 'defmacrocallback',
  68. 'defmodule',
  69. 'defprotocol',
  70. 'defexception',
  71. 'defimpl',
  72. 'defstruct'
  73. ],
  74. operatorKeywords: ['and', 'in', 'not', 'or', 'when'],
  75. namespaceKeywords: ['alias', 'import', 'require', 'use'],
  76. otherKeywords: [
  77. 'after',
  78. 'case',
  79. 'catch',
  80. 'cond',
  81. 'do',
  82. 'else',
  83. 'end',
  84. 'fn',
  85. 'for',
  86. 'if',
  87. 'quote',
  88. 'raise',
  89. 'receive',
  90. 'rescue',
  91. 'super',
  92. 'throw',
  93. 'try',
  94. 'unless',
  95. 'unquote_splicing',
  96. 'unquote',
  97. 'with'
  98. ],
  99. constants: ['true', 'false', 'nil'],
  100. nameBuiltin: ['__MODULE__', '__DIR__', '__ENV__', '__CALLER__', '__STACKTRACE__'],
  101. // Matches any of the operator names:
  102. // <<< >>> ||| &&& ^^^ ~~~ === !== ~>> <~> |~> <|> == != <= >= && || \\ <> ++ -- |> =~ -> <- ~> <~ :: .. = < > + - * / | . ^ & !
  103. operator: /-[->]?|!={0,2}|\*|\/|\\\\|&{1,3}|\.\.?|\^(?:\^\^)?|\+\+?|<(?:-|<<|=|>|\|>|~>?)?|=~|={1,3}|>(?:=|>>)?|\|~>|\|>|\|{1,3}|~>>?|~~~|::/,
  104. // See https://hexdocs.pm/elixir/syntax-reference.html#variables
  105. variableName: /[a-z_][a-zA-Z0-9_]*[?!]?/,
  106. // See https://hexdocs.pm/elixir/syntax-reference.html#atoms
  107. atomName: /[a-zA-Z_][a-zA-Z0-9_@]*[?!]?|@specialAtomName|@operator/,
  108. specialAtomName: /\.\.\.|<<>>|%\{\}|%|\{\}/,
  109. aliasPart: /[A-Z][a-zA-Z0-9_]*/,
  110. moduleName: /@aliasPart(?:\.@aliasPart)*/,
  111. // Sigil pairs are: """ """, ''' ''', " ", ' ', / /, | |, < >, { }, [ ], ( )
  112. sigilSymmetricDelimiter: /"""|'''|"|'|\/|\|/,
  113. sigilStartDelimiter: /@sigilSymmetricDelimiter|<|\{|\[|\(/,
  114. sigilEndDelimiter: /@sigilSymmetricDelimiter|>|\}|\]|\)/,
  115. decimal: /\d(?:_?\d)*/,
  116. hex: /[0-9a-fA-F](_?[0-9a-fA-F])*/,
  117. octal: /[0-7](_?[0-7])*/,
  118. binary: /[01](_?[01])*/,
  119. // See https://hexdocs.pm/elixir/master/String.html#module-escape-characters
  120. escape: /\\u[0-9a-fA-F]{4}|\\x[0-9a-fA-F]{2}|\\./,
  121. // The keys below correspond to tokenizer states.
  122. // We start from the root state and match against its rules
  123. // until we explicitly transition into another state.
  124. // The `include` simply brings in all operations from the given state
  125. // and is useful for improving readability.
  126. tokenizer: {
  127. root: [
  128. { include: '@whitespace' },
  129. { include: '@comments' },
  130. // Keywords start as either an identifier or a string,
  131. // but end with a : so it's important to match this first.
  132. { include: '@keywordsShorthand' },
  133. { include: '@numbers' },
  134. { include: '@identifiers' },
  135. { include: '@strings' },
  136. { include: '@atoms' },
  137. { include: '@sigils' },
  138. { include: '@attributes' },
  139. { include: '@symbols' }
  140. ],
  141. // Whitespace
  142. whitespace: [[/\s+/, 'white']],
  143. // Comments
  144. comments: [[/(#)(.*)/, ['comment.punctuation', 'comment']]],
  145. // Keyword list shorthand
  146. keywordsShorthand: [
  147. [/(@atomName)(:)/, ['constant', 'constant.punctuation']],
  148. // Use positive look-ahead to ensure the string is followed by :
  149. // and should be considered a keyword.
  150. [
  151. /"(?=([^"]|#\{.*?\}|\\")*":)/,
  152. { token: 'constant.delimiter', next: '@doubleQuotedStringKeyword' }
  153. ],
  154. [
  155. /'(?=([^']|#\{.*?\}|\\')*':)/,
  156. { token: 'constant.delimiter', next: '@singleQuotedStringKeyword' }
  157. ]
  158. ],
  159. doubleQuotedStringKeyword: [
  160. [/":/, { token: 'constant.delimiter', next: '@pop' }],
  161. { include: '@stringConstantContentInterpol' }
  162. ],
  163. singleQuotedStringKeyword: [
  164. [/':/, { token: 'constant.delimiter', next: '@pop' }],
  165. { include: '@stringConstantContentInterpol' }
  166. ],
  167. // Numbers
  168. numbers: [
  169. [/0b@binary/, 'number.binary'],
  170. [/0o@octal/, 'number.octal'],
  171. [/0x@hex/, 'number.hex'],
  172. [/@decimal\.@decimal([eE]-?@decimal)?/, 'number.float'],
  173. [/@decimal/, 'number']
  174. ],
  175. // Identifiers
  176. identifiers: [
  177. // Tokenize identifier name in function-like definitions.
  178. // Note: given `def a + b, do: nil`, `a` is not a function name,
  179. // so we use negative look-ahead to ensure there's no operator.
  180. [
  181. /\b(defp?|defnp?|defmacrop?|defguardp?|defdelegate)(\s+)(@variableName)(?!\s+@operator)/,
  182. [
  183. 'keyword.declaration',
  184. 'white',
  185. {
  186. cases: {
  187. unquote: 'keyword',
  188. '@default': 'function'
  189. }
  190. }
  191. ]
  192. ],
  193. // Tokenize function calls
  194. [
  195. // In-scope call - an identifier followed by ( or .(
  196. /(@variableName)(?=\s*\.?\s*\()/,
  197. {
  198. cases: {
  199. // Tokenize as keyword in cases like `if(..., do: ..., else: ...)`
  200. '@declarationKeywords': 'keyword.declaration',
  201. '@namespaceKeywords': 'keyword',
  202. '@otherKeywords': 'keyword',
  203. '@default': 'function.call'
  204. }
  205. }
  206. ],
  207. [
  208. // Referencing function in a module
  209. /(@moduleName)(\s*)(\.)(\s*)(@variableName)/,
  210. ['type.identifier', 'white', 'operator', 'white', 'function.call']
  211. ],
  212. [
  213. // Referencing function in an Erlang module
  214. /(:)(@atomName)(\s*)(\.)(\s*)(@variableName)/,
  215. ['constant.punctuation', 'constant', 'white', 'operator', 'white', 'function.call']
  216. ],
  217. [
  218. // Piping into a function (tokenized separately as it may not have parentheses)
  219. /(\|>)(\s*)(@variableName)/,
  220. [
  221. 'operator',
  222. 'white',
  223. {
  224. cases: {
  225. '@otherKeywords': 'keyword',
  226. '@default': 'function.call'
  227. }
  228. }
  229. ]
  230. ],
  231. [
  232. // Function reference passed to another function
  233. /(&)(\s*)(@variableName)/,
  234. ['operator', 'white', 'function.call']
  235. ],
  236. // Language keywords, builtins, constants and variables
  237. [
  238. /@variableName/,
  239. {
  240. cases: {
  241. '@declarationKeywords': 'keyword.declaration',
  242. '@operatorKeywords': 'keyword.operator',
  243. '@namespaceKeywords': 'keyword',
  244. '@otherKeywords': 'keyword',
  245. '@constants': 'constant.language',
  246. '@nameBuiltin': 'variable.language',
  247. '_.*': 'comment.unused',
  248. '@default': 'identifier'
  249. }
  250. }
  251. ],
  252. // Module names
  253. [/@moduleName/, 'type.identifier']
  254. ],
  255. // Strings
  256. strings: [
  257. [/"""/, { token: 'string.delimiter', next: '@doubleQuotedHeredoc' }],
  258. [/'''/, { token: 'string.delimiter', next: '@singleQuotedHeredoc' }],
  259. [/"/, { token: 'string.delimiter', next: '@doubleQuotedString' }],
  260. [/'/, { token: 'string.delimiter', next: '@singleQuotedString' }]
  261. ],
  262. doubleQuotedHeredoc: [
  263. [/"""/, { token: 'string.delimiter', next: '@pop' }],
  264. { include: '@stringContentInterpol' }
  265. ],
  266. singleQuotedHeredoc: [
  267. [/'''/, { token: 'string.delimiter', next: '@pop' }],
  268. { include: '@stringContentInterpol' }
  269. ],
  270. doubleQuotedString: [
  271. [/"/, { token: 'string.delimiter', next: '@pop' }],
  272. { include: '@stringContentInterpol' }
  273. ],
  274. singleQuotedString: [
  275. [/'/, { token: 'string.delimiter', next: '@pop' }],
  276. { include: '@stringContentInterpol' }
  277. ],
  278. // Atoms
  279. atoms: [
  280. [/(:)(@atomName)/, ['constant.punctuation', 'constant']],
  281. [/:"/, { token: 'constant.delimiter', next: '@doubleQuotedStringAtom' }],
  282. [/:'/, { token: 'constant.delimiter', next: '@singleQuotedStringAtom' }]
  283. ],
  284. doubleQuotedStringAtom: [
  285. [/"/, { token: 'constant.delimiter', next: '@pop' }],
  286. { include: '@stringConstantContentInterpol' }
  287. ],
  288. singleQuotedStringAtom: [
  289. [/'/, { token: 'constant.delimiter', next: '@pop' }],
  290. { include: '@stringConstantContentInterpol' }
  291. ],
  292. // Sigils
  293. // See https://elixir-lang.org/getting-started/sigils.html
  294. // Sigils allow for typing values using their textual representation.
  295. // All sigils start with ~ followed by a letter indicating sigil type
  296. // and then a delimiter pair enclosing the textual representation.
  297. // Optional modifiers are allowed after the closing delimiter.
  298. // For instance a regular expressions can be written as:
  299. // ~r/foo|bar/ ~r{foo|bar} ~r/foo|bar/g
  300. //
  301. // In general lowercase sigils allow for interpolation
  302. // and escaped characters, whereas uppercase sigils don't
  303. //
  304. // During tokenization we want to distinguish some
  305. // specific sigil types, namely string and regexp,
  306. // so that they cen be themed separately.
  307. //
  308. // To reasonably handle all those combinations we leverage
  309. // dot-separated states, so if we transition to @sigilStart.interpol.s.{.}
  310. // then "sigilStart.interpol.s" state will match and also all
  311. // the individual dot-separated parameters can be accessed.
  312. sigils: [
  313. [/~[a-z]@sigilStartDelimiter/, { token: '@rematch', next: '@sigil.interpol' }],
  314. [/~[A-Z]@sigilStartDelimiter/, { token: '@rematch', next: '@sigil.noInterpol' }]
  315. ],
  316. sigil: [
  317. [/~([a-zA-Z])\{/, { token: '@rematch', switchTo: '@sigilStart.$S2.$1.{.}' }],
  318. [/~([a-zA-Z])\[/, { token: '@rematch', switchTo: '@sigilStart.$S2.$1.[.]' }],
  319. [/~([a-zA-Z])\(/, { token: '@rematch', switchTo: '@sigilStart.$S2.$1.(.)' }],
  320. [/~([a-zA-Z])\</, { token: '@rematch', switchTo: '@sigilStart.$S2.$1.<.>' }],
  321. [
  322. /~([a-zA-Z])(@sigilSymmetricDelimiter)/,
  323. { token: '@rematch', switchTo: '@sigilStart.$S2.$1.$2.$2' }
  324. ]
  325. ],
  326. // The definitions below expect states to be of the form:
  327. //
  328. // sigilStart.<interpol-or-noInterpol>.<sigil-letter>.<start-delimiter>.<end-delimiter>
  329. // sigilContinue.<interpol-or-noInterpol>.<sigil-letter>.<start-delimiter>.<end-delimiter>
  330. //
  331. // The sigilStart state is used only to properly classify the token (as string/regex/sigil)
  332. // and immediately switches to the sigilContinue sate, which handles the actual content
  333. // and waits for the corresponding end delimiter.
  334. 'sigilStart.interpol.s': [
  335. [
  336. /~s@sigilStartDelimiter/,
  337. {
  338. token: 'string.delimiter',
  339. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  340. }
  341. ]
  342. ],
  343. 'sigilContinue.interpol.s': [
  344. [
  345. /(@sigilEndDelimiter)[a-zA-Z]*/,
  346. {
  347. cases: {
  348. '$1==$S5': { token: 'string.delimiter', next: '@pop' },
  349. '@default': 'string'
  350. }
  351. }
  352. ],
  353. { include: '@stringContentInterpol' }
  354. ],
  355. 'sigilStart.noInterpol.S': [
  356. [
  357. /~S@sigilStartDelimiter/,
  358. {
  359. token: 'string.delimiter',
  360. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  361. }
  362. ]
  363. ],
  364. 'sigilContinue.noInterpol.S': [
  365. // Ignore escaped sigil end
  366. [/(^|[^\\])\\@sigilEndDelimiter/, 'string'],
  367. [
  368. /(@sigilEndDelimiter)[a-zA-Z]*/,
  369. {
  370. cases: {
  371. '$1==$S5': { token: 'string.delimiter', next: '@pop' },
  372. '@default': 'string'
  373. }
  374. }
  375. ],
  376. { include: '@stringContent' }
  377. ],
  378. 'sigilStart.interpol.r': [
  379. [
  380. /~r@sigilStartDelimiter/,
  381. {
  382. token: 'regexp.delimiter',
  383. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  384. }
  385. ]
  386. ],
  387. 'sigilContinue.interpol.r': [
  388. [
  389. /(@sigilEndDelimiter)[a-zA-Z]*/,
  390. {
  391. cases: {
  392. '$1==$S5': { token: 'regexp.delimiter', next: '@pop' },
  393. '@default': 'regexp'
  394. }
  395. }
  396. ],
  397. { include: '@regexpContentInterpol' }
  398. ],
  399. 'sigilStart.noInterpol.R': [
  400. [
  401. /~R@sigilStartDelimiter/,
  402. {
  403. token: 'regexp.delimiter',
  404. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  405. }
  406. ]
  407. ],
  408. 'sigilContinue.noInterpol.R': [
  409. // Ignore escaped sigil end
  410. [/(^|[^\\])\\@sigilEndDelimiter/, 'regexp'],
  411. [
  412. /(@sigilEndDelimiter)[a-zA-Z]*/,
  413. {
  414. cases: {
  415. '$1==$S5': { token: 'regexp.delimiter', next: '@pop' },
  416. '@default': 'regexp'
  417. }
  418. }
  419. ],
  420. { include: '@regexpContent' }
  421. ],
  422. // Fallback to the generic sigil by default
  423. 'sigilStart.interpol': [
  424. [
  425. /~([a-zA-Z])@sigilStartDelimiter/,
  426. {
  427. token: 'sigil.delimiter',
  428. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  429. }
  430. ]
  431. ],
  432. 'sigilContinue.interpol': [
  433. [
  434. /(@sigilEndDelimiter)[a-zA-Z]*/,
  435. {
  436. cases: {
  437. '$1==$S5': { token: 'sigil.delimiter', next: '@pop' },
  438. '@default': 'sigil'
  439. }
  440. }
  441. ],
  442. { include: '@sigilContentInterpol' }
  443. ],
  444. 'sigilStart.noInterpol': [
  445. [
  446. /~([a-zA-Z])@sigilStartDelimiter/,
  447. {
  448. token: 'sigil.delimiter',
  449. switchTo: '@sigilContinue.$S2.$S3.$S4.$S5'
  450. }
  451. ]
  452. ],
  453. 'sigilContinue.noInterpol': [
  454. // Ignore escaped sigil end
  455. [/(^|[^\\])\\@sigilEndDelimiter/, 'sigil'],
  456. [
  457. /(@sigilEndDelimiter)[a-zA-Z]*/,
  458. {
  459. cases: {
  460. '$1==$S5': { token: 'sigil.delimiter', next: '@pop' },
  461. '@default': 'sigil'
  462. }
  463. }
  464. ],
  465. { include: '@sigilContent' }
  466. ],
  467. // Attributes
  468. attributes: [
  469. // Module @doc* attributes - tokenized as comments
  470. [
  471. /\@(module|type)?doc (~[sS])?"""/,
  472. {
  473. token: 'comment.block.documentation',
  474. next: '@doubleQuotedHeredocDocstring'
  475. }
  476. ],
  477. [
  478. /\@(module|type)?doc (~[sS])?"/,
  479. {
  480. token: 'comment.block.documentation',
  481. next: '@doubleQuotedStringDocstring'
  482. }
  483. ],
  484. [/\@(module|type)?doc false/, 'comment.block.documentation'],
  485. // Module attributes
  486. [/\@(@variableName)/, 'variable']
  487. ],
  488. doubleQuotedHeredocDocstring: [
  489. [/"""/, { token: 'comment.block.documentation', next: '@pop' }],
  490. { include: '@docstringContent' }
  491. ],
  492. doubleQuotedStringDocstring: [
  493. [/"/, { token: 'comment.block.documentation', next: '@pop' }],
  494. { include: '@docstringContent' }
  495. ],
  496. // Operators, punctuation, brackets
  497. symbols: [
  498. // Code point operator (either with regular character ?a or an escaped one ?\n)
  499. [/\?(\\.|[^\\\s])/, 'number.constant'],
  500. // Anonymous function arguments
  501. [/&\d+/, 'operator'],
  502. // Bitshift operators (must go before delimiters, so that << >> don't match first)
  503. [/<<<|>>>/, 'operator'],
  504. // Delimiter pairs
  505. [/[()\[\]\{\}]|<<|>>/, '@brackets'],
  506. // Triple dot is a valid name (must go before operators, so that .. doesn't match instead)
  507. [/\.\.\./, 'identifier'],
  508. // Punctuation => (must go before operators, so it's not tokenized as = then >)
  509. [/=>/, 'punctuation'],
  510. // Operators
  511. [/@operator/, 'operator'],
  512. // Punctuation
  513. [/[:;,.%]/, 'punctuation']
  514. ],
  515. // Generic helpers
  516. stringContentInterpol: [
  517. { include: '@interpolation' },
  518. { include: '@escapeChar' },
  519. { include: '@stringContent' }
  520. ],
  521. stringContent: [[/./, 'string']],
  522. stringConstantContentInterpol: [
  523. { include: '@interpolation' },
  524. { include: '@escapeChar' },
  525. { include: '@stringConstantContent' }
  526. ],
  527. stringConstantContent: [[/./, 'constant']],
  528. regexpContentInterpol: [
  529. { include: '@interpolation' },
  530. { include: '@escapeChar' },
  531. { include: '@regexpContent' }
  532. ],
  533. regexpContent: [
  534. // # may be a regular regexp char, so we use a heuristic
  535. // assuming a # surrounded by whitespace is actually a comment.
  536. [/(\s)(#)(\s.*)$/, ['white', 'comment.punctuation', 'comment']],
  537. [/./, 'regexp']
  538. ],
  539. sigilContentInterpol: [
  540. { include: '@interpolation' },
  541. { include: '@escapeChar' },
  542. { include: '@sigilContent' }
  543. ],
  544. sigilContent: [[/./, 'sigil']],
  545. docstringContent: [[/./, 'comment.block.documentation']],
  546. escapeChar: [[/@escape/, 'constant.character.escape']],
  547. interpolation: [
  548. [/#{/, { token: 'delimiter.bracket.embed', next: '@interpolationContinue' }]
  549. ],
  550. interpolationContinue: [
  551. [/}/, { token: 'delimiter.bracket.embed', next: '@pop' }],
  552. // Interpolation brackets may contain arbitrary code,
  553. // so we simply match against all the root rules,
  554. // until we reach interpolation end (the above matches).
  555. { include: '@root' }
  556. ]
  557. }
  558. };