vis: move text object definitions to separate file
[vis.git] / lexers / ledger.lua
blob16322411c1e4d2a285e254a0b5f0b6a907ddf5e3
1 -- Copyright 2015-2016 Charles Lehner. See LICENSE.
2 -- ledger journal LPeg lexer, see http://www.ledger-cli.org/
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 = 'ledger'}
10 local delim = P('\t') + P(' ')
12 -- Whitespace.
13 local ws = token(l.WHITESPACE, l.space^1)
15 -- Comments.
16 local comment = token(l.COMMENT, S(';#') * l.nonnewline^0)
18 -- Strings.
19 local sq_str = l.delimited_range("'")
20 local dq_str = l.delimited_range('"')
21 local label = l.delimited_range('[]', true, true)
22 local string = token(l.STRING, sq_str + dq_str + label)
24 -- Date.
25 local date = token(l.CONSTANT, l.starts_line((l.digit + S('/-')) ^1))
27 -- Account.
28 local account = token(l.VARIABLE,
29 l.starts_line(S(' \t')^1 * (l.print - delim)^1))
31 -- Amount.
32 local amount = token(l.NUMBER, delim * (1 - S(';\r\n'))^1)
34 -- Automated transactions.
35 local auto_tx = token(l.PREPROCESSOR, l.starts_line(S('=~') * l.nonnewline^0))
37 -- Directives.
38 local directive_word = word_match{
39 'account', 'alias', 'assert', 'bucket', 'capture', 'check', 'comment',
40 'commodity', 'define', 'end', 'fixed', 'endfixed', 'include', 'payee',
41 'apply', 'tag', 'test', 'year'
42 } + S('AYNDCIiOobh')
43 local directive = token(l.KEYWORD, l.starts_line(S('!@')^-1 * directive_word))
45 M._rules = {
46 {'account', account},
47 {'amount', amount},
48 {'comment', comment},
49 {'whitespace', ws},
50 {'date', date},
51 {'auto_tx', auto_tx},
52 {'directive', directive},
55 M._LEXBYLINE = true
57 return M