aboutsummaryrefslogtreecommitdiff
path: root/lexers/rust.lua
blob: f8ad5a8a151c94d5601c1e9092aeedd692ccb688 (plain) (blame)
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
-- Copyright 2015-2016 Alejandro Baez (https://twitter.com/a_baez). See LICENSE.
-- Rust LPeg lexer.

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 sq_str = P('L')^-1 * l.delimited_range("'")
local dq_str = P('L')^-1 * l.delimited_range('"')
local raw_str = "##" * (l.any - '##')^0 * P("##")^-1
local string = token(l.STRING, dq_str + raw_str)

-- Numbers.
local number = token(l.NUMBER, l.float +
                     "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',
  'pub',        'pure',       'ref',      'return',   'sizeof',
  'static',     'self',       'struct',   'super',    'true',
  'trait',      'type',       'typeof',   'unsafe',   'unsized',
  'use',        'virtual',    'where',    'while',    'yield'
})

-- Library types
local library = token(l.LABEL, l.upper * (l.lower + l.dec_num)^1)

-- syntax extensions
local extension = l.word^1 * S("!")

local func = token(l.FUNCTION, extension)

-- 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(l.PREPROCESSOR, "#[" *
                        (l.nonnewline - ']')^0 * P("]")^-1)

M._rules = {
  {'whitespace', ws},
  {'keyword', keyword},
  {'function', func},
  {'library', library},
  {'type', type},
  {'identifier', identifier},
  {'string', string},
  {'comment', comment},
  {'number', number},
  {'operator', operator},
  {'preprocessor', attribute},
}

M._foldsymbols = {
  _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
  [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')},
  [l.OPERATOR] = {['('] = 1, ['{'] = 1, [')'] = -1, ['}'] = -1}
}

return M