-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrust.lua
110 lines (90 loc) · 3.21 KB
/
rust.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
--- Rust LPeg lexer.
-- See @{README.md} for details on usage.
-- @author [Alejandro Baez](https://keybase.io/baez)
-- @copyright 2016
-- @license MIT (see LICENSE)
-- @module rust
local l = require("lexer")
local token, word_match = l.token, l.word_match
local P, R, S = lpeg.P, lpeg.R, lpeg.S
local M = {_NAME = 'rust'}
-- Whitespace.
local ws = token(l.WHITESPACE, l.space^1)
-- Comments.
local line_comment = '//' * l.nonnewline_esc^0
local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
local comment = token(l.COMMENT, line_comment + block_comment)
-- Strings.
local dq_str = P('L')^-1 * l.delimited_range('"')
local raw_str = lpeg.Cmt('r' * lpeg.C(P('#')^0) * '"',
function (input, index, eq)
local _, e = input:find('"' .. eq, index, true)
return (e or #input) + 1
end)
local string = token(l.STRING, dq_str + raw_str)
-- Character.
local char = token('char', P("'") * (('\\' * l.any) + l.any) * P("'"))
-- Numbers.
local number = token(l.NUMBER, l.float + (l.dec_num + "_")^1 +
"0b" * (l.dec_num + "_")^1 + l.integer)
-- Keywords.
local keyword = token(l.KEYWORD, word_match{
'abstract', 'alignof', 'as', 'become', 'box',
'break', 'const', 'continue', 'crate', 'do',
'else', 'enum', 'extern', 'false', 'final',
'fn', 'for', 'if', 'impl', 'in',
'let', 'loop', 'macro', 'match', 'mod',
'move', 'mut', "offsetof", 'override', 'priv',
'proc', 'pub', 'pure', 'ref', 'return',
'Self', 'self', 'sizeof', 'static', 'struct',
'super', 'trait', 'true', 'type', 'typeof',
'unsafe', 'unsized', 'use', 'virtual', 'where',
'while', 'yield'
})
-- Library types
local library = token('library', l.upper * (l.lower + l.dec_num)^1)
-- syntax extensions
local extension = token('extension', l.word^1 * S("!"))
-- Lifetimes
local lifetime = token('lifetime', "'" * ("static" + l.alnum))
-- Types.
local type = token(l.TYPE, word_match{
'bool', 'isize', 'usize', 'char', 'str',
'u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64',
'f32','f64',
})
-- Identifiers.
local identifier = token(l.IDENTIFIER, l.word)
-- Operators.
local operator = token(l.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}'))
-- Attributes.
local attribute = token('attribute', (P('#![') + P('#[')) *
(l.nonnewline - ']')^0 * P("]")^-1)
M._rules = {
{'whitespace', ws},
{'keyword', keyword},
{'extension', extension},
{'library', library},
{'type', type},
{'string', string},
{'char', char},
{'lifetime', lifetime},
{'comment', comment},
{'attribute', attribute},
{'number', number},
{'operator', operator},
{'identifier', identifier},
}
M._tokenstyles = {
attribute = l.STYLE_PREPROCESSOR,
library = l.STYLE_CLASS,
extension = l.STYLE_FUNCTION,
char = l.STYLE_STRING,
lifetime = l.STYLE_TYPE
}
M._foldsymbols = {
_patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
[l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')},
[l.OPERATOR] = {['('] = 1, ['{'] = 1, [')'] = -1, ['}'] = -1}
}
return M