build: set version to 0.5
[vis.git] / lua / lexers / go.lua
blob5eaeedb8b851bf2c801976200a3fbb29a7250e43
1 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Go LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'go'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = '//' * l.nonnewline^0
15 local block_comment = '/*' * (l.any - '*/')^0 * '*/'
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 -- Strings.
19 local sq_str = l.delimited_range("'", true)
20 local dq_str = l.delimited_range('"', true)
21 local raw_str = l.delimited_range('`', false, true)
22 local string = token(l.STRING, sq_str + dq_str + raw_str)
24 -- Numbers.
25 local number = token(l.NUMBER, (l.float + l.integer) * P('i')^-1)
27 -- Keywords.
28 local keyword = token(l.KEYWORD, word_match{
29 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else',
30 'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface',
31 'map', 'package', 'range', 'return', 'select', 'struct', 'switch', 'type',
32 'var'
35 -- Constants.
36 local constant = token(l.CONSTANT, word_match{
37 'true', 'false', 'iota', 'nil'
40 -- Types.
41 local type = token(l.TYPE, word_match{
42 'bool', 'byte', 'complex64', 'complex128', 'error', 'float32', 'float64',
43 'int', 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8',
44 'uint16', 'uint32', 'uint64', 'uintptr'
47 -- Functions.
48 local func = token(l.FUNCTION, word_match{
49 'append', 'cap', 'close', 'complex', 'copy', 'delete', 'imag', 'len', 'make',
50 'new', 'panic', 'print', 'println', 'real', 'recover'
53 -- Identifiers.
54 local identifier = token(l.IDENTIFIER, l.word)
56 -- Operators.
57 local operator = token(l.OPERATOR, S('+-*/%&|^<>=!:;.,()[]{}'))
59 M._rules = {
60 {'whitespace', ws},
61 {'keyword', keyword},
62 {'constant', constant},
63 {'type', type},
64 {'function', func},
65 {'identifier', identifier},
66 {'string', string},
67 {'comment', comment},
68 {'number', number},
69 {'operator', operator},
72 M._foldsymbols = {
73 _patterns = {'[{}]', '/%*', '%*/', '//'},
74 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
75 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
78 return M