vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
gap.lua
(1341B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Gap LPeg lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('gap')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
15 'and', 'break', 'continue', 'do', 'elif', 'else', 'end', 'fail', 'false', 'fi', 'for', 'function',
16 'if', 'in', 'infinity', 'local', 'not', 'od', 'or', 'rec', 'repeat', 'return', 'then', 'true',
17 'until', 'while'
18 }))
19
20 -- Identifiers.
21 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
22
23 -- Strings.
24 local sq_str = lexer.range("'", true)
25 local dq_str = lexer.range('"', true)
26 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
27
28 -- Comments.
29 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
30
31 -- Numbers.
32 lex:add_rule('number', token(lexer.NUMBER, lexer.dec_num * -lexer.alpha))
33
34 -- Operators.
35 lex:add_rule('operator', token(lexer.OPERATOR, S('*+-,./:;<=>~^#()[]{}')))
36
37 -- Fold points.
38 lex:add_fold_point(lexer.KEYWORD, 'function', 'end')
39 lex:add_fold_point(lexer.KEYWORD, 'do', 'od')
40 lex:add_fold_point(lexer.KEYWORD, 'if', 'fi')
41 lex:add_fold_point(lexer.KEYWORD, 'repeat', 'until')
42
43 lexer.property['scintillua.comment'] = '#'
44
45 return lex