vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
go.lua
(2354B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Go LPeg lexer.
3
4 local lexer = lexer
5 local P, S = lpeg.P, lpeg.S
6
7 local lex = lexer.new(...)
8
9 -- Keywords.
10 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
11
12 -- Constants.
13 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
14
15 -- Types.
16 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
17
18 -- Functions.
19 local builtin_func = -lpeg.B('.') *
20 lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
21 local func = lex:tag(lexer.FUNCTION, lexer.word)
22 local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
23 lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
24
25 -- Identifiers.
26 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
27
28 -- Strings.
29 local sq_str = lexer.range("'", true)
30 local dq_str = lexer.range('"', true)
31 local raw_str = lexer.range('`', false, false)
32 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
33
34 -- Comments.
35 local line_comment = lexer.to_eol('//')
36 local block_comment = lexer.range('/*', '*/')
37 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
38
39 -- Numbers.
40 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * P('i')^-1))
41
42 -- Operators.
43 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-*/%&|^<>=!~:;.,()[]{}')))
44
45 -- Fold points.
46 lex:add_fold_point(lexer.OPERATOR, '{', '}')
47 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
48
49 -- Word lists.
50 lex:set_word_list(lexer.KEYWORD, {
51 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', 'fallthrough', 'for',
52 'func', 'go', 'goto', 'if', 'import', 'interface', 'map', 'package', 'range', 'return', 'select',
53 'struct', 'switch', 'type', 'var'
54 })
55
56 lex:set_word_list(lexer.CONSTANT_BUILTIN, 'true false iota nil')
57
58 lex:set_word_list(lexer.TYPE, {
59 'any', 'bool', 'byte', 'comparable', 'complex64', 'complex128', 'error', 'float32', 'float64',
60 'int', 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8', 'uint16', 'uint32',
61 'uint64', 'uintptr'
62 })
63
64 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
65 'append', 'cap', 'close', 'complex', 'copy', 'delete', 'imag', 'len', 'make', 'new', 'panic',
66 'print', 'println', 'real', 'recover'
67 })
68
69 lexer.property['scintillua.comment'] = '//'
70
71 return lex