build: set version to 0.5
[vis.git] / lua / lexers / protobuf.lua
blob41cba2a8c32c3b4516445c7dd4416026fb94eb53
1 -- Copyright 2016-2017 David B. Lamkins <david@lamkins.net>. See LICENSE.
2 -- Protocol Buffer IDL LPeg lexer.
3 -- <https://developers.google.com/protocol-buffers/>
5 local l = require('lexer')
6 local token, word_match = l.token, l.word_match
7 local P, R, S = lpeg.P, lpeg.R, lpeg.S
9 local M = {_NAME = 'protobuf'}
11 -- Whitespace.
12 local ws = token(l.WHITESPACE, l.space^1)
14 -- Comments.
15 local line_comment = '//' * l.nonnewline_esc^0
16 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
17 local comment = token(l.COMMENT, line_comment + block_comment)
19 -- Strings.
20 local sq_str = P('L')^-1 * l.delimited_range("'", true)
21 local dq_str = P('L')^-1 * l.delimited_range('"', true)
22 local string = token(l.STRING, sq_str + dq_str)
24 -- Numbers.
25 local number = token(l.NUMBER, l.float + l.integer)
27 -- Keywords.
28 local keyword = token(l.KEYWORD, word_match{
29 'contained', 'syntax', 'import', 'option', 'package', 'message', 'group',
30 'oneof', 'optional', 'required', 'repeated', 'default', 'extend',
31 'extensions', 'to', 'max', 'reserved', 'service', 'rpc', 'returns'
34 -- Types.
35 local type = token(l.TYPE, word_match{
36 'int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64', 'fixed32',
37 'fixed64', 'sfixed32', 'sfixed64', 'float', 'double', 'bool', 'string',
38 'bytes', 'enum', 'true', 'false'
41 -- Identifiers.
42 local identifier = token(l.IDENTIFIER, l.word)
44 -- Operators.
45 local operator = token(l.OPERATOR, S('<>=|;,.()[]{}'))
47 M._rules = {
48 {'whitespace', ws},
49 {'keyword', keyword},
50 {'type', type},
51 {'identifier', identifier},
52 {'string', string},
53 {'comment', comment},
54 {'number', number},
55 {'operator', operator},
58 return M