-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml.lua
81 lines (66 loc) · 2.42 KB
/
toml.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
--- TOML LPeg lexer.
-- Used yaml.lua and lua.lua for reference.
-- @author [Alejandro Baez](https://keybase.io/baez)
-- copyright 2016
-- @license MIT (see LICENSE)
-- @module toml
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 = 'toml'}
-- Whitespace
local indent = #l.starts_line(S(' \t')) *
(token(l.WHITESPACE, ' ') + token('indent_error', '\t'))^1
local ws = token(l.WHITESPACE, S(' \t')^1 + l.newline^1)
-- Comments.
local comment = token(l.COMMENT, '#' * l.nonnewline^0)
-- Strings.
local dq_str = P('U')^-1 * l.delimited_range('"', true)
local raw_sq_str = P('u')^-1 * l.delimited_range("'", false, false)
local multi_dq_str = '"""' * (l.any - '"""')^0 * P('"""')^-1
local multi_raw_sq_str = "'''" * (l.any - "'''")^0 * P("'''")^-1
local string = token(l.STRING, multi_raw_sq_str + multi_dq_str +
raw_sq_str + dq_str)
-- Numbers.
local number = token(l.NUMBER, l.float + l.integer)
-- Datetime.
local ts = token('timestamp', l.digit * l.digit * l.digit * l.digit * -- year
'-' * l.digit * l.digit^-1 * -- month
'-' * l.digit * l.digit^-1 * -- day
((S(' \t')^1 + S('tT'))^-1 * -- separator
l.digit * l.digit^-1 * -- hour
':' * l.digit * l.digit * -- minute
':' * l.digit * l.digit * -- second
('.' * l.digit^0)^-1 * -- fraction
('Z' + -- timezone
S(' \t')^0 * S('-+') * l.digit * l.digit^-1 *
(':' * l.digit * l.digit)^-1)^-1)^-1)
-- kewwords.
local keyword = token(l.KEYWORD, word_match{
'true', 'false'
})
-- Identifiers.
local identifier = token(l.IDENTIFIER, l.word)
-- Operators.
local operator = token(l.OPERATOR, S('#=+-,.{}[]()'))
-- table
local tab = token("table", S'['^1 * (l.word + S'.')^1 * S']'^1 )
M._rules = {
{'indent', indent},
{'whitespace', ws},
{'comment', comment},
{'keyword', keyword},
{'timestamp', ts},
{'string', string},
{'number', number},
{'table', tab},
{'operator', operator},
{'identifier', identifier},
}
M._tokenstyles = {
indent_error = 'back:%(color.red)',
timestamp = l.STYLE_NUMBER,
table = l.STYLE_LABEL
}
M._FOLDBYINDENTATION = true
return M