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(' ')
13 local ws
= token(l
.WHITESPACE
, l
.space^
1)
16 local comment
= token(l
.COMMENT
, S(';#') * l
.nonnewline^
0)
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
)
25 local date = token(l
.CONSTANT
, l
.starts_line((l
.digit
+ S('/-')) ^
1))
28 local account
= token(l
.VARIABLE
,
29 l
.starts_line(S(' \t')^
1 * (l
.print - delim
)^
1))
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))
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'
43 local directive
= token(l
.KEYWORD
, l
.starts_line(S('!@')^
-1 * directive_word
))
52 {'directive', directive
},