UNPKG

9.11 kBJavaScriptView Raw
1/*
2Language: Julia
3Description: Julia is a high-level, high-performance, dynamic programming language.
4Author: Kenta Sato <bicycle1885@gmail.com>
5Contributors: Alex Arslan <ararslan@comcast.net>, Fredrik Ekre <ekrefredrik@gmail.com>
6Website: https://julialang.org
7*/
8
9function julia(hljs) {
10 // Since there are numerous special names in Julia, it is too much trouble
11 // to maintain them by hand. Hence these names (i.e. keywords, literals and
12 // built-ins) are automatically generated from Julia 1.5.2 itself through
13 // the following scripts for each.
14
15 // ref: https://docs.julialang.org/en/v1/manual/variables/#Allowed-Variable-Names
16 const VARIABLE_NAME_RE = '[A-Za-z_\\u00A1-\\uFFFF][A-Za-z_0-9\\u00A1-\\uFFFF]*';
17
18 // # keyword generator, multi-word keywords handled manually below (Julia 1.5.2)
19 // import REPL.REPLCompletions
20 // res = String["in", "isa", "where"]
21 // for kw in collect(x.keyword for x in REPLCompletions.complete_keyword(""))
22 // if !(contains(kw, " ") || kw == "struct")
23 // push!(res, kw)
24 // end
25 // end
26 // sort!(unique!(res))
27 // foreach(x -> println("\'", x, "\',"), res)
28 const KEYWORD_LIST = [
29 'baremodule',
30 'begin',
31 'break',
32 'catch',
33 'ccall',
34 'const',
35 'continue',
36 'do',
37 'else',
38 'elseif',
39 'end',
40 'export',
41 'false',
42 'finally',
43 'for',
44 'function',
45 'global',
46 'if',
47 'import',
48 'in',
49 'isa',
50 'let',
51 'local',
52 'macro',
53 'module',
54 'quote',
55 'return',
56 'true',
57 'try',
58 'using',
59 'where',
60 'while',
61 ];
62
63 // # literal generator (Julia 1.5.2)
64 // import REPL.REPLCompletions
65 // res = String["true", "false"]
66 // for compl in filter!(x -> isa(x, REPLCompletions.ModuleCompletion) && (x.parent === Base || x.parent === Core),
67 // REPLCompletions.completions("", 0)[1])
68 // try
69 // v = eval(Symbol(compl.mod))
70 // if !(v isa Function || v isa Type || v isa TypeVar || v isa Module || v isa Colon)
71 // push!(res, compl.mod)
72 // end
73 // catch e
74 // end
75 // end
76 // sort!(unique!(res))
77 // foreach(x -> println("\'", x, "\',"), res)
78 const LITERAL_LIST = [
79 'ARGS',
80 'C_NULL',
81 'DEPOT_PATH',
82 'ENDIAN_BOM',
83 'ENV',
84 'Inf',
85 'Inf16',
86 'Inf32',
87 'Inf64',
88 'InsertionSort',
89 'LOAD_PATH',
90 'MergeSort',
91 'NaN',
92 'NaN16',
93 'NaN32',
94 'NaN64',
95 'PROGRAM_FILE',
96 'QuickSort',
97 'RoundDown',
98 'RoundFromZero',
99 'RoundNearest',
100 'RoundNearestTiesAway',
101 'RoundNearestTiesUp',
102 'RoundToZero',
103 'RoundUp',
104 'VERSION|0',
105 'devnull',
106 'false',
107 'im',
108 'missing',
109 'nothing',
110 'pi',
111 'stderr',
112 'stdin',
113 'stdout',
114 'true',
115 'undef',
116 'π',
117 'ℯ',
118 ];
119
120 // # built_in generator (Julia 1.5.2)
121 // import REPL.REPLCompletions
122 // res = String[]
123 // for compl in filter!(x -> isa(x, REPLCompletions.ModuleCompletion) && (x.parent === Base || x.parent === Core),
124 // REPLCompletions.completions("", 0)[1])
125 // try
126 // v = eval(Symbol(compl.mod))
127 // if (v isa Type || v isa TypeVar) && (compl.mod != "=>")
128 // push!(res, compl.mod)
129 // end
130 // catch e
131 // end
132 // end
133 // sort!(unique!(res))
134 // foreach(x -> println("\'", x, "\',"), res)
135 const BUILT_IN_LIST = [
136 'AbstractArray',
137 'AbstractChannel',
138 'AbstractChar',
139 'AbstractDict',
140 'AbstractDisplay',
141 'AbstractFloat',
142 'AbstractIrrational',
143 'AbstractMatrix',
144 'AbstractRange',
145 'AbstractSet',
146 'AbstractString',
147 'AbstractUnitRange',
148 'AbstractVecOrMat',
149 'AbstractVector',
150 'Any',
151 'ArgumentError',
152 'Array',
153 'AssertionError',
154 'BigFloat',
155 'BigInt',
156 'BitArray',
157 'BitMatrix',
158 'BitSet',
159 'BitVector',
160 'Bool',
161 'BoundsError',
162 'CapturedException',
163 'CartesianIndex',
164 'CartesianIndices',
165 'Cchar',
166 'Cdouble',
167 'Cfloat',
168 'Channel',
169 'Char',
170 'Cint',
171 'Cintmax_t',
172 'Clong',
173 'Clonglong',
174 'Cmd',
175 'Colon',
176 'Complex',
177 'ComplexF16',
178 'ComplexF32',
179 'ComplexF64',
180 'CompositeException',
181 'Condition',
182 'Cptrdiff_t',
183 'Cshort',
184 'Csize_t',
185 'Cssize_t',
186 'Cstring',
187 'Cuchar',
188 'Cuint',
189 'Cuintmax_t',
190 'Culong',
191 'Culonglong',
192 'Cushort',
193 'Cvoid',
194 'Cwchar_t',
195 'Cwstring',
196 'DataType',
197 'DenseArray',
198 'DenseMatrix',
199 'DenseVecOrMat',
200 'DenseVector',
201 'Dict',
202 'DimensionMismatch',
203 'Dims',
204 'DivideError',
205 'DomainError',
206 'EOFError',
207 'Enum',
208 'ErrorException',
209 'Exception',
210 'ExponentialBackOff',
211 'Expr',
212 'Float16',
213 'Float32',
214 'Float64',
215 'Function',
216 'GlobalRef',
217 'HTML',
218 'IO',
219 'IOBuffer',
220 'IOContext',
221 'IOStream',
222 'IdDict',
223 'IndexCartesian',
224 'IndexLinear',
225 'IndexStyle',
226 'InexactError',
227 'InitError',
228 'Int',
229 'Int128',
230 'Int16',
231 'Int32',
232 'Int64',
233 'Int8',
234 'Integer',
235 'InterruptException',
236 'InvalidStateException',
237 'Irrational',
238 'KeyError',
239 'LinRange',
240 'LineNumberNode',
241 'LinearIndices',
242 'LoadError',
243 'MIME',
244 'Matrix',
245 'Method',
246 'MethodError',
247 'Missing',
248 'MissingException',
249 'Module',
250 'NTuple',
251 'NamedTuple',
252 'Nothing',
253 'Number',
254 'OrdinalRange',
255 'OutOfMemoryError',
256 'OverflowError',
257 'Pair',
258 'PartialQuickSort',
259 'PermutedDimsArray',
260 'Pipe',
261 'ProcessFailedException',
262 'Ptr',
263 'QuoteNode',
264 'Rational',
265 'RawFD',
266 'ReadOnlyMemoryError',
267 'Real',
268 'ReentrantLock',
269 'Ref',
270 'Regex',
271 'RegexMatch',
272 'RoundingMode',
273 'SegmentationFault',
274 'Set',
275 'Signed',
276 'Some',
277 'StackOverflowError',
278 'StepRange',
279 'StepRangeLen',
280 'StridedArray',
281 'StridedMatrix',
282 'StridedVecOrMat',
283 'StridedVector',
284 'String',
285 'StringIndexError',
286 'SubArray',
287 'SubString',
288 'SubstitutionString',
289 'Symbol',
290 'SystemError',
291 'Task',
292 'TaskFailedException',
293 'Text',
294 'TextDisplay',
295 'Timer',
296 'Tuple',
297 'Type',
298 'TypeError',
299 'TypeVar',
300 'UInt',
301 'UInt128',
302 'UInt16',
303 'UInt32',
304 'UInt64',
305 'UInt8',
306 'UndefInitializer',
307 'UndefKeywordError',
308 'UndefRefError',
309 'UndefVarError',
310 'Union',
311 'UnionAll',
312 'UnitRange',
313 'Unsigned',
314 'Val',
315 'Vararg',
316 'VecElement',
317 'VecOrMat',
318 'Vector',
319 'VersionNumber',
320 'WeakKeyDict',
321 'WeakRef',
322 ];
323
324 const KEYWORDS = {
325 $pattern: VARIABLE_NAME_RE,
326 keyword: KEYWORD_LIST,
327 literal: LITERAL_LIST,
328 built_in: BUILT_IN_LIST,
329 };
330
331 // placeholder for recursive self-reference
332 const DEFAULT = {
333 keywords: KEYWORDS,
334 illegal: /<\//
335 };
336
337 // ref: https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/
338 const NUMBER = {
339 className: 'number',
340 // supported numeric literals:
341 // * binary literal (e.g. 0x10)
342 // * octal literal (e.g. 0o76543210)
343 // * hexadecimal literal (e.g. 0xfedcba876543210)
344 // * hexadecimal floating point literal (e.g. 0x1p0, 0x1.2p2)
345 // * decimal literal (e.g. 9876543210, 100_000_000)
346 // * floating pointe literal (e.g. 1.2, 1.2f, .2, 1., 1.2e10, 1.2e-10)
347 begin: /(\b0x[\d_]*(\.[\d_]*)?|0x\.\d[\d_]*)p[-+]?\d+|\b0[box][a-fA-F0-9][a-fA-F0-9_]*|(\b\d[\d_]*(\.[\d_]*)?|\.\d[\d_]*)([eEfF][-+]?\d+)?/,
348 relevance: 0
349 };
350
351 const CHAR = {
352 className: 'string',
353 begin: /'(.|\\[xXuU][a-zA-Z0-9]+)'/
354 };
355
356 const INTERPOLATION = {
357 className: 'subst',
358 begin: /\$\(/,
359 end: /\)/,
360 keywords: KEYWORDS
361 };
362
363 const INTERPOLATED_VARIABLE = {
364 className: 'variable',
365 begin: '\\$' + VARIABLE_NAME_RE
366 };
367
368 // TODO: neatly escape normal code in string literal
369 const STRING = {
370 className: 'string',
371 contains: [
372 hljs.BACKSLASH_ESCAPE,
373 INTERPOLATION,
374 INTERPOLATED_VARIABLE
375 ],
376 variants: [
377 {
378 begin: /\w*"""/,
379 end: /"""\w*/,
380 relevance: 10
381 },
382 {
383 begin: /\w*"/,
384 end: /"\w*/
385 }
386 ]
387 };
388
389 const COMMAND = {
390 className: 'string',
391 contains: [
392 hljs.BACKSLASH_ESCAPE,
393 INTERPOLATION,
394 INTERPOLATED_VARIABLE
395 ],
396 begin: '`',
397 end: '`'
398 };
399
400 const MACROCALL = {
401 className: 'meta',
402 begin: '@' + VARIABLE_NAME_RE
403 };
404
405 const COMMENT = {
406 className: 'comment',
407 variants: [
408 {
409 begin: '#=',
410 end: '=#',
411 relevance: 10
412 },
413 {
414 begin: '#',
415 end: '$'
416 }
417 ]
418 };
419
420 DEFAULT.name = 'Julia';
421 DEFAULT.contains = [
422 NUMBER,
423 CHAR,
424 STRING,
425 COMMAND,
426 MACROCALL,
427 COMMENT,
428 hljs.HASH_COMMENT_MODE,
429 {
430 className: 'keyword',
431 begin:
432 '\\b(((abstract|primitive)\\s+)type|(mutable\\s+)?struct)\\b'
433 },
434 { begin: /<:/ } // relevance booster
435 ];
436 INTERPOLATION.contains = DEFAULT.contains;
437
438 return DEFAULT;
439}
440
441module.exports = julia;