vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
elixir.lua
(4392B)
1 -- Copyright 2015-2025 Mitchell. See LICENSE.
2 -- Contributed by Richard Philips.
3 -- Elixir LPeg lexer.
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local B, P, S = lpeg.B, lpeg.P, lpeg.S
8
9 local lex = lexer.new('elixir', {fold_by_indentation = true})
10
11 -- Whitespace.
12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
13
14 -- Sigils.
15 local sigil11 = '~' * S('CRSW') * lexer.range('<', '>')
16 local sigil12 = '~' * S('CRSW') * lexer.range('{', '}')
17 local sigil13 = '~' * S('CRSW') * lexer.range('[', ']')
18 local sigil14 = '~' * S('CRSW') * lexer.range('(', ')')
19 local sigil15 = '~' * S('CRSW') * lexer.range('|', false, false)
20 local sigil16 = '~' * S('CRSW') * lexer.range('/', false, false)
21 local sigil17 = '~' * S('CRSW') * lexer.range('"', false, false)
22 local sigil18 = '~' * S('CRSW') * lexer.range("'", false, false)
23 local sigil19 = '~' * S('CRSW') * lexer.range('"""')
24 local sigil10 = '~' * S('CRSW') * lexer.range("'''")
25 local sigil21 = '~' * S('crsw') * lexer.range('<', '>')
26 local sigil22 = '~' * S('crsw') * lexer.range('{', '}')
27 local sigil23 = '~' * S('crsw') * lexer.range('[', ']')
28 local sigil24 = '~' * S('crsw') * lexer.range('(', ')')
29 local sigil25 = '~' * S('crsw') * lexer.range('|')
30 local sigil26 = '~' * S('crsw') * lexer.range('/')
31 local sigil27 = '~' * S('crsw') * lexer.range('"')
32 local sigil28 = '~' * S('crsw') * lexer.range("'")
33 local sigil29 = '~' * S('crsw') * lexer.range('"""')
34 local sigil20 = '~' * S('crsw') * lexer.range("'''")
35 local sigil_token = token(lexer.REGEX,
36 sigil10 + sigil19 + sigil11 + sigil12 + sigil13 + sigil14 + sigil15 + sigil16 + sigil17 + sigil18 +
37 sigil20 + sigil29 + sigil21 + sigil22 + sigil23 + sigil24 + sigil25 + sigil26 + sigil27 +
38 sigil28)
39 local sigiladdon_token = token(lexer.EMBEDDED, lexer.alpha^0)
40 lex:add_rule('sigil', sigil_token * sigiladdon_token)
41
42 -- Atoms.
43 local atom1 = B(1 - P(':')) * ':' * lexer.range('"')
44 local atom2 = B(1 - P(':')) * ':' * lexer.alpha * (lexer.alnum + S('_@'))^0 * S('?!')^-1
45 local atom3 = B(1 - (lexer.alnum + S('_:'))) * lexer.upper * (lexer.alnum + S('_@'))^0 * S('?!')^-1
46 lex:add_rule('atom', token(lexer.CONSTANT, atom1 + atom2 + atom3))
47
48 -- Strings.
49 local dq_str = lexer.range('"')
50 local triple_dq_str = lexer.range('"""')
51 lex:add_rule('string', token(lexer.STRING, triple_dq_str + dq_str))
52
53 -- Comments.
54 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
55
56 -- Attributes.
57 lex:add_rule('attribute', token(lexer.LABEL, B(1 - (lexer.alnum + '_')) * '@' * lexer.alpha *
58 (lexer.alnum + '_')^0))
59
60 -- Booleans.
61 lex:add_rule('boolean', token(lexer.NUMBER, P(':')^-1 * word_match('true false nil')))
62
63 -- Functions.
64 lex:add_rule('function', token(lexer.FUNCTION, word_match{
65 'defstruct', 'defrecordp', 'defrecord', 'defprotocol', 'defp', 'defoverridable', 'defmodule',
66 'defmacrop', 'defmacro', 'defimpl', 'defexception', 'defdelegate', 'defcallback', 'def'
67 }))
68
69 -- Keywords.
70 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
71 'is_atom', 'is_binary', 'is_bitstring', 'is_boolean', 'is_float', 'is_function', 'is_integer',
72 'is_list', 'is_map', 'is_number', 'is_pid', 'is_port', 'is_record', 'is_reference', 'is_tuple',
73 'is_exception', 'case', 'when', 'cond', 'for', 'if', 'unless', 'try', 'receive', 'send', 'exit',
74 'raise', 'throw', 'after', 'rescue', 'catch', 'else', 'do', 'end', 'quote', 'unquote', 'super',
75 'import', 'require', 'alias', 'use', 'self', 'with', 'fn'
76 }))
77
78 -- Operators
79 local operator1 = word_match('and or not when xor in')
80 local operator2 = P('!==') + '!=' + '!' + '=~' + '===' + '==' + '=' + '<<<' + '<<' + '<=' + '<-' +
81 '<' + '>>>' + '>>' + '>=' + '>' + '->' + '--' + '-' + '++' + '+' + '&&&' + '&&' + '&' + '|||' +
82 '||' + '|>' + '|' + '..' + '.' + '^^^' + '^' + '\\\\' + '::' + '*' + '/' + '~~~' + '@'
83 lex:add_rule('operator', token(lexer.OPERATOR, operator1 + operator2))
84
85 -- Identifiers
86 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word * S('?!')^-1))
87
88 -- Numbers
89 local dec = lexer.digit * (lexer.digit + '_')^0
90 local bin = '0b' * S('01')^1
91 local oct = '0o' * lpeg.R('07')^1
92 local integer = bin + lexer.hex_num + oct + dec
93 local float = lexer.digit^1 * '.' * lexer.digit^1 * S('eE') * (S('+-')^-1 * lexer.digit^1)^-1
94 lex:add_rule('number',
95 B(1 - (lexer.alpha + '_')) * S('+-')^-1 * token(lexer.NUMBER, float + integer))
96
97 lexer.property['scintillua.comment'] = '#'
98
99 return lex