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