build: set version to 0.5
[vis.git] / lua / lexers / toml.lua
bloba738bb47521607b804d7ba8d152e35d9e55f5353
1 -- Copyright 2015-2017 Alejandro Baez (https://keybase.io/baez). See LICENSE.
2 -- TOML 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 = 'toml'}
10 -- Whitespace
11 local indent = #l.starts_line(S(' \t')) *
12 (token(l.WHITESPACE, ' ') + token('indent_error', '\t'))^1
13 local ws = token(l.WHITESPACE, S(' \t')^1 + l.newline^1)
15 -- Comments.
16 local comment = token(l.COMMENT, '#' * l.nonnewline^0)
18 -- Strings.
19 local string = token(l.STRING, l.delimited_range("'") + l.delimited_range('"'))
21 -- Numbers.
22 local number = token(l.NUMBER, l.float + l.integer)
24 -- Datetime.
25 local ts = token('timestamp', l.digit * l.digit * l.digit * l.digit * -- year
26 '-' * l.digit * l.digit^-1 * -- month
27 '-' * l.digit * l.digit^-1 * -- day
28 ((S(' \t')^1 + S('tT'))^-1 * -- separator
29 l.digit * l.digit^-1 * -- hour
30 ':' * l.digit * l.digit * -- minute
31 ':' * l.digit * l.digit * -- second
32 ('.' * l.digit^0)^-1 * -- fraction
33 ('Z' + -- timezone
34 S(' \t')^0 * S('-+') * l.digit * l.digit^-1 *
35 (':' * l.digit * l.digit)^-1)^-1)^-1)
37 -- kewwords.
38 local keyword = token(l.KEYWORD, word_match{
39 'true', 'false'
43 -- Identifiers.
44 local identifier = token(l.IDENTIFIER, l.word)
46 -- Operators.
47 local operator = token(l.OPERATOR, S('#=+-,.{}[]()'))
49 M._rules = {
50 {'indent', indent},
51 {'whitespace', ws},
52 {'keyword', keyword},
53 {'identifier', identifier},
54 {'operator', operator},
55 {'string', string},
56 {'comment', comment},
57 {'number', number},
58 {'timestamp', ts},
61 M._tokenstyles = {
62 indent_error = 'back:red',
63 timestamp = l.STYLE_NUMBER,
66 M._FOLDBYINDENTATION = true
68 return M