vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
protobuf.lua
(1571B)
1 -- Copyright 2016-2025 David B. Lamkins <david@lamkins.net>. See LICENSE.
2 -- Protocol Buffer IDL LPeg lexer.
3 -- <https://developers.google.com/protocol-buffers/>
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('protobuf')
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 'contained', 'syntax', 'import', 'option', 'package', 'message', 'group', 'oneof', 'optional',
17 'required', 'repeated', 'default', 'extend', 'extensions', 'to', 'max', 'reserved', 'service',
18 'rpc', 'returns'
19 }))
20
21 -- Types.
22 lex:add_rule('type', token(lexer.TYPE, word_match{
23 'int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64', 'fixed32', 'fixed64', 'sfixed32',
24 'sfixed64', 'float', 'double', 'bool', 'string', 'bytes', 'enum', 'true', 'false'
25 }))
26
27 -- Strings.
28 local sq_str = P('L')^-1 * lexer.range("'", true)
29 local dq_str = P('L')^-1 * lexer.range('"', true)
30 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
31
32 -- Identifiers.
33 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
34
35 -- Comments.
36 local line_comment = lexer.to_eol('//', true)
37 local block_comment = lexer.range('/*', '*/')
38 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
39
40 -- Numbers.
41 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
42
43 -- Operators.
44 lex:add_rule('operator', token(lexer.OPERATOR, S('<>=|;,.()[]{}')))
45
46 lexer.property['scintillua.comment'] = '//'
47
48 return lex