vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
crystal.lua
(3913B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Copyright 2017 Michel Martens.
3 -- Crystal LPeg lexer (based on Ruby).
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local P, S = lpeg.P, lpeg.S
8
9 local lex = lexer.new('crystal')
10
11 -- Whitespace.
12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
13
14 -- Keywords.
15 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
16 'alias', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else', 'elsif', 'end',
17 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', 'redo', 'rescue', 'retry',
18 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield',
19 '__FILE__', '__LINE__'
20 }))
21
22 -- Functions.
23 lex:add_rule('function', token(lexer.FUNCTION, word_match{
24 'abort', 'at_exit', 'caller', 'delay', 'exit', 'fork', 'future', 'get_stack_top', 'gets', 'lazy',
25 'loop', 'main', 'p', 'print', 'printf', 'puts', 'raise', 'rand', 'read_line', 'require', 'sleep',
26 'spawn', 'sprintf', 'system', 'with_color',
27 -- Macros.
28 'assert_responds_to', 'debugger', 'parallel', 'pp', 'record', 'redefine_main'
29 }) * -S('.:|'))
30
31 -- Identifiers.
32 local word_char = lexer.alnum + S('_!?')
33 local word = (lexer.alpha + '_') * word_char^0
34 lex:add_rule('identifier', token(lexer.IDENTIFIER, word))
35
36 -- Comments.
37 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
38
39 -- Strings.
40 local cmd_str = lexer.range('`')
41 local sq_str = lexer.range("'")
42 local dq_str = lexer.range('"')
43 local heredoc = '<<' * P(function(input, index)
44 local _, e, indented, _, delimiter = input:find('^(%-?)(["`]?)([%a_][%w_]*)%2[\n\r\f;]+', index)
45 if not delimiter then return end
46 local end_heredoc = (#indented > 0 and '[\n\r\f]+ *' or '[\n\r\f]+')
47 _, e = input:find(end_heredoc .. delimiter, e)
48 return e and e + 1 or #input + 1
49 end)
50 local string = token(lexer.STRING, (sq_str + dq_str + heredoc + cmd_str) * S('f')^-1)
51 -- TODO: regex_str fails with `obj.method /patt/` syntax.
52 local regex_str = lexer.after_set('!%^&*([{-=+|:;,?<>~', lexer.range('/', true) * S('iomx')^0)
53 local regex = token(lexer.REGEX, regex_str)
54 lex:add_rule('string', string + regex)
55
56 -- Numbers.
57 local numeric_literal = '?' * (lexer.any - lexer.space) * -word_char -- TODO: meta, control, etc.
58 lex:add_rule('number', token(lexer.NUMBER, lexer.number_('_') * S('ri')^-1 + numeric_literal))
59
60 -- Variables.
61 local global_var = '$' *
62 (word + S('!@L+`\'=~/\\,.;<>_*"$?:') + lexer.digit + '-' * S('0FadiIKlpvw'))
63 local class_var = '@@' * word
64 local inst_var = '@' * word
65 lex:add_rule('variable', token(lexer.VARIABLE, global_var + class_var + inst_var))
66
67 -- Symbols.
68 lex:add_rule('symbol', token('symbol', ':' * P(function(input, index)
69 if input:sub(index - 2, index - 2) ~= ':' then return true end
70 end) * (word_char^1 + sq_str + dq_str)))
71 lex:add_style('symbol', lexer.styles.constant)
72
73 -- Operators.
74 lex:add_rule('operator', token(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~')))
75
76 -- Fold points.
77 local function disambiguate(text, pos, line, s)
78 return line:sub(1, s - 1):match('^%s*$') and not text:sub(1, pos - 1):match('\\[ \t]*\r?\n$') and
79 1 or 0
80 end
81 lex:add_fold_point(lexer.KEYWORD, 'begin', 'end')
82 lex:add_fold_point(lexer.KEYWORD, 'case', 'end')
83 lex:add_fold_point(lexer.KEYWORD, 'class', 'end')
84 lex:add_fold_point(lexer.KEYWORD, 'def', 'end')
85 lex:add_fold_point(lexer.KEYWORD, 'do', 'end')
86 lex:add_fold_point(lexer.KEYWORD, 'for', 'end')
87 lex:add_fold_point(lexer.KEYWORD, 'module', 'end')
88 lex:add_fold_point(lexer.KEYWORD, 'if', disambiguate)
89 lex:add_fold_point(lexer.KEYWORD, 'while', disambiguate)
90 lex:add_fold_point(lexer.KEYWORD, 'unless', disambiguate)
91 lex:add_fold_point(lexer.KEYWORD, 'until', disambiguate)
92 lex:add_fold_point(lexer.OPERATOR, '(', ')')
93 lex:add_fold_point(lexer.OPERATOR, '[', ']')
94 lex:add_fold_point(lexer.OPERATOR, '{', '}')
95
96 lexer.property['scintillua.comment'] = '#'
97
98 return lex