vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
julia.lua
(4474B)
1 -- Copyright 2020-2025 Tobias Frilling. See LICENSE.
2 -- Julia lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local B, P, S = lpeg.B, lpeg.P, lpeg.S
7
8 local lex = lexer.new('julia')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 local id = lexer.word * P('!')^0
14
15 -- Keyword
16 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
17 'baremodule', 'begin', 'break', 'catch', 'const', 'continue', 'do', 'else', 'elseif', 'end',
18 'export', 'finally', 'for', 'function', 'global', 'if', 'in', 'isa', 'import', 'let', 'local',
19 'macro', 'module', 'quote', 'return', 'struct', 'try', 'using', 'where', 'while'
20 } + 'abstract type' + 'mutable struct' + 'primitive type'))
21
22 -- Constant
23 local const_bool = word_match('true false')
24 local const_numerical = (P('Inf') + 'NaN') * (P('16') + '32' + '64')^-1 * -lexer.alnum
25 local const_special = word_match('nothing undef missing')
26 local const_env = word_match('ARGS ENV ENDIAN_BOM LOAD_PATH VERSION PROGRAM_FILE DEPOT_PATH')
27 local const_io = word_match('stdout stdin stderr devnull')
28 lex:add_rule('constant', token(lexer.CONSTANT,
29 const_bool + const_numerical + const_special + const_env + const_io))
30
31 -- Type
32 local type_annotated = (B('::') + B(':: ')) * id
33 local type_para = id * #P('{')
34 local type_subtyping = id * #(lexer.space^0 * '<:') + (B('<:') + B('<: ')) * id
35 local type_struct = B('struct ') * id
36 -- LuaFormatter off
37 local type_builtin_numerical = ((P('Abstract') + 'Big') * 'Float' +
38 'Float' * (P('16') + '32' + '64') +
39 P('U')^-1 * 'Int' * (P('8') + '16' + '32' + '64' + '128')^-1 +
40 P('Abstract')^-1 * 'Irrational'
41 ) * -lexer.alnum + word_match('Number Complex Real Integer Bool Signed Unsigned Rational')
42 -- LuaFormatter on
43 local type_builtin_range = ((P('Lin') + 'Ordinal' + (P('Abstract')^-1 * P('Unit')^-1)) * 'Range' +
44 'StepRange' * P('Len')^-1 - 'Range'
45 ) * -lexer.alnum
46 local type_builtin_array = ((P('Abstract') + 'Bit' + 'Dense' + 'PermutedDims' + 'Sub')^-1 *
47 word_match('Array Vector Matrix VecOrMat') +
48 (P('Abstract') + 'Sym' + (P('Unit')^-1 * (P('Lower') + 'Upper')))^-1 * 'Triangular'
49 ) * -lexer.alnum +
50 word_match('Adjoint Bidiagonal Diagonal Hermitian LQPackedQ Symmetric Transpose UpperHessenberg')
51 lex:add_rule('type', token(lexer.TYPE,
52 type_para + type_annotated + type_subtyping + type_struct + type_builtin_numerical +
53 type_builtin_range + type_builtin_array))
54
55 -- Macro
56 lex:add_rule('macro', token(lexer.PREPROCESSOR, '@' * (id + '.')))
57
58 -- Symbol
59 lex:add_rule('symbol', token('symbol', -B(P(':') + '<') * ':' * id))
60 lex:add_style('symbol', lexer.styles.string)
61
62 -- Function
63 lex:add_rule('function', token(lexer.FUNCTION, id * #(P('.')^-1 * '(')))
64
65 -- Identifier
66 lex:add_rule('identifier', token(lexer.IDENTIFIER, id))
67
68 -- Comment
69 local line_comment = lexer.to_eol('#')
70 local block_comment = lexer.range('#=', '=#')
71 lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
72
73 -- Number
74 local decimal = lexer.digit^1 * ('_' * lexer.digit^1)^0
75 local hex_digits = lexer.xdigit^1 * ('_' * lexer.xdigit^1)^0
76 local hexadecimal = '0x' * hex_digits
77 local binary = '0b' * S('01')^1 * ('_' * S('01')^1)^0
78 local integer = binary + hexadecimal + decimal
79
80 local float_dec_coeff = decimal^0 * '.' * decimal + decimal * '.' * decimal^0
81 local float_dec_expon = S('eEf') * S('+-')^-1 * lexer.digit^1
82 local float_dec = float_dec_coeff * float_dec_expon^-1 + decimal * float_dec_expon
83
84 local float_hex_coeff = '0x' * (hex_digits^0 * '.' * hex_digits + hex_digits * '.' * hex_digits^0)
85 local float_hex_expon = 'p' * S('+-')^-1 * lexer.digit^1
86 local float_hex = float_hex_coeff * float_hex_expon^-1 + hexadecimal * float_hex_expon
87
88 local float = float_dec + float_hex
89
90 local imaginary = (float_dec + decimal) * 'im'
91
92 lex:add_rule('number',
93 token(lexer.NUMBER, S('+-')^-1 * (imaginary + float + integer) * -lexer.alpha))
94
95 -- String & Character
96 local doc_str = lexer.range('"""')
97 local str = lexer.range('"')
98 lex:add_rule('string', token(lexer.STRING, doc_str + str))
99
100 local c_esc = '\\' * S('\\"\'nrbtfav')
101 local unicode = '\\' * S('uU') * lexer.xdigit^1
102 local char = "'" * (lexer.alnum + c_esc + unicode) * "'"
103 lex:add_rule('character', token('character', char))
104 lex:add_style('character', lexer.styles.constant)
105
106 -- Operator
107 lex:add_rule('operator', token(lexer.OPERATOR, S('+-*/<>=!%^&|~\\\':?.') + '÷' + '≠' + '≈' +
108 '≤' + '≥' + '⊻' + '√'))
109
110 lexer.property['scintillua.comment'] = '#'
111
112 return lex