1 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
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'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local line_comment
= '//' * l
.nonnewline^
0
15 local block_comment
= '/*' * (l
.any
- '*/')^
0 * '*/'
16 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
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
)
25 local number = token(l
.NUMBER
, (l
.float
+ l
.integer
) * P('i')^
-1)
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',
36 local constant
= token(l
.CONSTANT
, word_match
{
37 'true', 'false', 'iota', 'nil'
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'
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'
54 local identifier
= token(l
.IDENTIFIER
, l
.word
)
57 local operator
= token(l
.OPERATOR
, S('+-*/%&|^<>=!:;.,()[]{}'))
62 {'constant', constant
},
65 {'identifier', identifier
},
69 {'operator', operator
},
73 _patterns
= {'[{}]', '/%*', '%*/', '//'},
74 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1},
75 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')}