elixir.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. module.exports = function(hljs) {
  2. var ELIXIR_IDENT_RE = '[a-zA-Z_][a-zA-Z0-9_.]*(\\!|\\?)?';
  3. var ELIXIR_METHOD_RE = '[a-zA-Z_]\\w*[!?=]?|[-+~]\\@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?';
  4. var ELIXIR_KEYWORDS =
  5. 'and false then defined module in return redo retry end for true self when ' +
  6. 'next until do begin unless nil break not case cond alias while ensure or ' +
  7. 'include use alias fn quote require import with|0';
  8. var SUBST = {
  9. className: 'subst',
  10. begin: '#\\{', end: '}',
  11. lexemes: ELIXIR_IDENT_RE,
  12. keywords: ELIXIR_KEYWORDS
  13. };
  14. var SIGIL_DELIMITERS = '[/|([{<"\']'
  15. var LOWERCASE_SIGIL = {
  16. className: 'string',
  17. begin: '~[a-z]' + '(?=' + SIGIL_DELIMITERS + ')',
  18. contains: [
  19. {
  20. endsParent:true,
  21. contains: [{
  22. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  23. variants: [
  24. { begin: /"/, end: /"/ },
  25. { begin: /'/, end: /'/ },
  26. { begin: /\//, end: /\// },
  27. { begin: /\|/, end: /\|/ },
  28. { begin: /\(/, end: /\)/ },
  29. { begin: /\[/, end: /\]/ },
  30. { begin: /\{/, end: /\}/ },
  31. { begin: /</, end: />/ }
  32. ]
  33. }]
  34. },
  35. ],
  36. };
  37. var UPCASE_SIGIL = {
  38. className: 'string',
  39. begin: '~[A-Z]' + '(?=' + SIGIL_DELIMITERS + ')',
  40. contains: [
  41. { begin: /"/, end: /"/ },
  42. { begin: /'/, end: /'/ },
  43. { begin: /\//, end: /\// },
  44. { begin: /\|/, end: /\|/ },
  45. { begin: /\(/, end: /\)/ },
  46. { begin: /\[/, end: /\]/ },
  47. { begin: /\{/, end: /\}/ },
  48. { begin: /\</, end: /\>/ }
  49. ]
  50. };
  51. var STRING = {
  52. className: 'string',
  53. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  54. variants: [
  55. {
  56. begin: /"""/, end: /"""/,
  57. },
  58. {
  59. begin: /'''/, end: /'''/,
  60. },
  61. {
  62. begin: /~S"""/, end: /"""/,
  63. contains: []
  64. },
  65. {
  66. begin: /~S"/, end: /"/,
  67. contains: []
  68. },
  69. {
  70. begin: /~S'''/, end: /'''/,
  71. contains: []
  72. },
  73. {
  74. begin: /~S'/, end: /'/,
  75. contains: []
  76. },
  77. {
  78. begin: /'/, end: /'/
  79. },
  80. {
  81. begin: /"/, end: /"/
  82. },
  83. ]
  84. };
  85. var FUNCTION = {
  86. className: 'function',
  87. beginKeywords: 'def defp defmacro', end: /\B\b/, // the mode is ended by the title
  88. contains: [
  89. hljs.inherit(hljs.TITLE_MODE, {
  90. begin: ELIXIR_IDENT_RE,
  91. endsParent: true
  92. })
  93. ]
  94. };
  95. var CLASS = hljs.inherit(FUNCTION, {
  96. className: 'class',
  97. beginKeywords: 'defimpl defmodule defprotocol defrecord', end: /\bdo\b|$|;/
  98. });
  99. var ELIXIR_DEFAULT_CONTAINS = [
  100. STRING,
  101. UPCASE_SIGIL,
  102. LOWERCASE_SIGIL,
  103. hljs.HASH_COMMENT_MODE,
  104. CLASS,
  105. FUNCTION,
  106. {
  107. begin: '::'
  108. },
  109. {
  110. className: 'symbol',
  111. begin: ':(?![\\s:])',
  112. contains: [STRING, {begin: ELIXIR_METHOD_RE}],
  113. relevance: 0
  114. },
  115. {
  116. className: 'symbol',
  117. begin: ELIXIR_IDENT_RE + ':(?!:)',
  118. relevance: 0
  119. },
  120. {
  121. className: 'number',
  122. begin: '(\\b0o[0-7_]+)|(\\b0b[01_]+)|(\\b0x[0-9a-fA-F_]+)|(-?\\b[1-9][0-9_]*(.[0-9_]+([eE][-+]?[0-9]+)?)?)',
  123. relevance: 0
  124. },
  125. {
  126. className: 'variable',
  127. begin: '(\\$\\W)|((\\$|\\@\\@?)(\\w+))'
  128. },
  129. {
  130. begin: '->'
  131. },
  132. { // regexp container
  133. begin: '(' + hljs.RE_STARTERS_RE + ')\\s*',
  134. contains: [
  135. hljs.HASH_COMMENT_MODE,
  136. {
  137. className: 'regexp',
  138. illegal: '\\n',
  139. contains: [hljs.BACKSLASH_ESCAPE, SUBST],
  140. variants: [
  141. {
  142. begin: '/', end: '/[a-z]*'
  143. },
  144. {
  145. begin: '%r\\[', end: '\\][a-z]*'
  146. }
  147. ]
  148. }
  149. ],
  150. relevance: 0
  151. }
  152. ];
  153. SUBST.contains = ELIXIR_DEFAULT_CONTAINS;
  154. return {
  155. lexemes: ELIXIR_IDENT_RE,
  156. keywords: ELIXIR_KEYWORDS,
  157. contains: ELIXIR_DEFAULT_CONTAINS
  158. };
  159. };