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 | /** @type LanguageFn */
|
---|
10 | function elixir(hljs) {
|
---|
11 | const ELIXIR_IDENT_RE = '[a-zA-Z_][a-zA-Z0-9_.]*(!|\\?)?';
|
---|
12 | const ELIXIR_METHOD_RE = '[a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?';
|
---|
13 | const ELIXIR_KEYWORDS = {
|
---|
14 | $pattern: ELIXIR_IDENT_RE,
|
---|
15 | keyword: 'and false then defined module in return redo retry end for true self when ' +
|
---|
16 | 'next until do begin unless nil break not case cond alias while ensure or ' +
|
---|
17 | 'include use alias fn quote require import with|0'
|
---|
18 | };
|
---|
19 | const SUBST = {
|
---|
20 | className: 'subst',
|
---|
21 | begin: /#\{/,
|
---|
22 | end: /\}/,
|
---|
23 | keywords: ELIXIR_KEYWORDS
|
---|
24 | };
|
---|
25 | const NUMBER = {
|
---|
26 | className: 'number',
|
---|
27 | begin: '(\\b0o[0-7_]+)|(\\b0b[01_]+)|(\\b0x[0-9a-fA-F_]+)|(-?\\b[1-9][0-9_]*(\\.[0-9_]+([eE][-+]?[0-9]+)?)?)',
|
---|
28 | relevance: 0
|
---|
29 | };
|
---|
30 | const SIGIL_DELIMITERS = '[/|([{<"\']';
|
---|
31 | const LOWERCASE_SIGIL = {
|
---|
32 | className: 'string',
|
---|
33 | begin: '~[a-z]' + '(?=' + SIGIL_DELIMITERS + ')',
|
---|
34 | contains: [
|
---|
35 | {
|
---|
36 | endsParent: true,
|
---|
37 | contains: [
|
---|
38 | {
|
---|
39 | contains: [
|
---|
40 | hljs.BACKSLASH_ESCAPE,
|
---|
41 | SUBST
|
---|
42 | ],
|
---|
43 | variants: [
|
---|
44 | {
|
---|
45 | begin: /"/,
|
---|
46 | end: /"/
|
---|
47 | },
|
---|
48 | {
|
---|
49 | begin: /'/,
|
---|
50 | end: /'/
|
---|
51 | },
|
---|
52 | {
|
---|
53 | begin: /\//,
|
---|
54 | end: /\//
|
---|
55 | },
|
---|
56 | {
|
---|
57 | begin: /\|/,
|
---|
58 | end: /\|/
|
---|
59 | },
|
---|
60 | {
|
---|
61 | begin: /\(/,
|
---|
62 | end: /\)/
|
---|
63 | },
|
---|
64 | {
|
---|
65 | begin: /\[/,
|
---|
66 | end: /\]/
|
---|
67 | },
|
---|
68 | {
|
---|
69 | begin: /\{/,
|
---|
70 | end: /\}/
|
---|
71 | },
|
---|
72 | {
|
---|
73 | begin: /</,
|
---|
74 | end: />/
|
---|
75 | }
|
---|
76 | ]
|
---|
77 | }
|
---|
78 | ]
|
---|
79 | }
|
---|
80 | ]
|
---|
81 | };
|
---|
82 |
|
---|
83 | const UPCASE_SIGIL = {
|
---|
84 | className: 'string',
|
---|
85 | begin: '~[A-Z]' + '(?=' + SIGIL_DELIMITERS + ')',
|
---|
86 | contains: [
|
---|
87 | {
|
---|
88 | begin: /"/,
|
---|
89 | end: /"/
|
---|
90 | },
|
---|
91 | {
|
---|
92 | begin: /'/,
|
---|
93 | end: /'/
|
---|
94 | },
|
---|
95 | {
|
---|
96 | begin: /\//,
|
---|
97 | end: /\//
|
---|
98 | },
|
---|
99 | {
|
---|
100 | begin: /\|/,
|
---|
101 | end: /\|/
|
---|
102 | },
|
---|
103 | {
|
---|
104 | begin: /\(/,
|
---|
105 | end: /\)/
|
---|
106 | },
|
---|
107 | {
|
---|
108 | begin: /\[/,
|
---|
109 | end: /\]/
|
---|
110 | },
|
---|
111 | {
|
---|
112 | begin: /\{/,
|
---|
113 | end: /\}/
|
---|
114 | },
|
---|
115 | {
|
---|
116 | begin: /</,
|
---|
117 | end: />/
|
---|
118 | }
|
---|
119 | ]
|
---|
120 | };
|
---|
121 |
|
---|
122 | const STRING = {
|
---|
123 | className: 'string',
|
---|
124 | contains: [
|
---|
125 | hljs.BACKSLASH_ESCAPE,
|
---|
126 | SUBST
|
---|
127 | ],
|
---|
128 | variants: [
|
---|
129 | {
|
---|
130 | begin: /"""/,
|
---|
131 | end: /"""/
|
---|
132 | },
|
---|
133 | {
|
---|
134 | begin: /'''/,
|
---|
135 | end: /'''/
|
---|
136 | },
|
---|
137 | {
|
---|
138 | begin: /~S"""/,
|
---|
139 | end: /"""/,
|
---|
140 | contains: [] // override default
|
---|
141 | },
|
---|
142 | {
|
---|
143 | begin: /~S"/,
|
---|
144 | end: /"/,
|
---|
145 | contains: [] // override default
|
---|
146 | },
|
---|
147 | {
|
---|
148 | begin: /~S'''/,
|
---|
149 | end: /'''/,
|
---|
150 | contains: [] // override default
|
---|
151 | },
|
---|
152 | {
|
---|
153 | begin: /~S'/,
|
---|
154 | end: /'/,
|
---|
155 | contains: [] // override default
|
---|
156 | },
|
---|
157 | {
|
---|
158 | begin: /'/,
|
---|
159 | end: /'/
|
---|
160 | },
|
---|
161 | {
|
---|
162 | begin: /"/,
|
---|
163 | end: /"/
|
---|
164 | }
|
---|
165 | ]
|
---|
166 | };
|
---|
167 | const FUNCTION = {
|
---|
168 | className: 'function',
|
---|
169 | beginKeywords: 'def defp defmacro',
|
---|
170 | end: /\B\b/, // the mode is ended by the title
|
---|
171 | contains: [
|
---|
172 | hljs.inherit(hljs.TITLE_MODE, {
|
---|
173 | begin: ELIXIR_IDENT_RE,
|
---|
174 | endsParent: true
|
---|
175 | })
|
---|
176 | ]
|
---|
177 | };
|
---|
178 | const CLASS = hljs.inherit(FUNCTION, {
|
---|
179 | className: 'class',
|
---|
180 | beginKeywords: 'defimpl defmodule defprotocol defrecord',
|
---|
181 | end: /\bdo\b|$|;/
|
---|
182 | });
|
---|
183 | const ELIXIR_DEFAULT_CONTAINS = [
|
---|
184 | STRING,
|
---|
185 | UPCASE_SIGIL,
|
---|
186 | LOWERCASE_SIGIL,
|
---|
187 | hljs.HASH_COMMENT_MODE,
|
---|
188 | CLASS,
|
---|
189 | FUNCTION,
|
---|
190 | {
|
---|
191 | begin: '::'
|
---|
192 | },
|
---|
193 | {
|
---|
194 | className: 'symbol',
|
---|
195 | begin: ':(?![\\s:])',
|
---|
196 | contains: [
|
---|
197 | STRING,
|
---|
198 | {
|
---|
199 | begin: ELIXIR_METHOD_RE
|
---|
200 | }
|
---|
201 | ],
|
---|
202 | relevance: 0
|
---|
203 | },
|
---|
204 | {
|
---|
205 | className: 'symbol',
|
---|
206 | begin: ELIXIR_IDENT_RE + ':(?!:)',
|
---|
207 | relevance: 0
|
---|
208 | },
|
---|
209 | NUMBER,
|
---|
210 | {
|
---|
211 | className: 'variable',
|
---|
212 | begin: '(\\$\\W)|((\\$|@@?)(\\w+))'
|
---|
213 | },
|
---|
214 | {
|
---|
215 | begin: '->'
|
---|
216 | },
|
---|
217 | { // regexp container
|
---|
218 | begin: '(' + hljs.RE_STARTERS_RE + ')\\s*',
|
---|
219 | contains: [
|
---|
220 | hljs.HASH_COMMENT_MODE,
|
---|
221 | {
|
---|
222 | // to prevent false regex triggers for the division function:
|
---|
223 | // /:
|
---|
224 | begin: /\/: (?=\d+\s*[,\]])/,
|
---|
225 | relevance: 0,
|
---|
226 | contains: [NUMBER]
|
---|
227 | },
|
---|
228 | {
|
---|
229 | className: 'regexp',
|
---|
230 | illegal: '\\n',
|
---|
231 | contains: [
|
---|
232 | hljs.BACKSLASH_ESCAPE,
|
---|
233 | SUBST
|
---|
234 | ],
|
---|
235 | variants: [
|
---|
236 | {
|
---|
237 | begin: '/',
|
---|
238 | end: '/[a-z]*'
|
---|
239 | },
|
---|
240 | {
|
---|
241 | begin: '%r\\[',
|
---|
242 | end: '\\][a-z]*'
|
---|
243 | }
|
---|
244 | ]
|
---|
245 | }
|
---|
246 | ],
|
---|
247 | relevance: 0
|
---|
248 | }
|
---|
249 | ];
|
---|
250 | SUBST.contains = ELIXIR_DEFAULT_CONTAINS;
|
---|
251 |
|
---|
252 | return {
|
---|
253 | name: 'Elixir',
|
---|
254 | keywords: ELIXIR_KEYWORDS,
|
---|
255 | contains: ELIXIR_DEFAULT_CONTAINS
|
---|
256 | };
|
---|
257 | }
|
---|
258 |
|
---|
259 | module.exports = elixir;
|
---|