vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

ledger.lua

(1483B)


      1 -- Copyright 2015-2025 Charles Lehner. See LICENSE.
      2 -- ledger journal LPeg lexer, see http://www.ledger-cli.org/
      3 
      4 local lexer = require('lexer')
      5 local token, word_match = lexer.token, lexer.word_match
      6 local P, S = lpeg.P, lpeg.S
      7 
      8 local lex = lexer.new('ledger', {lex_by_line = true})
      9 
     10 local delim = P('\t') + P('  ')
     11 
     12 -- Account.
     13 lex:add_rule('account', token(lexer.VARIABLE, lexer.starts_line(S(' \t')^1 * lexer.graph^1)))
     14 
     15 -- Amount.
     16 lex:add_rule('amount', token(lexer.NUMBER, delim * (1 - S(';\r\n'))^1))
     17 
     18 -- Comments.
     19 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol(S(';#'))))
     20 
     21 -- Whitespace.
     22 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     23 
     24 -- Strings.
     25 local sq_str = lexer.range("'")
     26 local dq_str = lexer.range('"')
     27 local label = lexer.range('[', ']', true)
     28 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + label))
     29 
     30 -- Date.
     31 lex:add_rule('date', token(lexer.CONSTANT, lexer.starts_line((lexer.digit + S('/-'))^1)))
     32 
     33 -- Automated transactions.
     34 lex:add_rule('auto_tx', token(lexer.PREPROCESSOR, lexer.to_eol(lexer.starts_line(S('=~')))))
     35 
     36 -- Directives.
     37 local directive_word = word_match{
     38 	'	account', 'alias', 'assert', 'bucket', 'capture', 'check', 'comment', 'commodity', 'define',
     39 	'end', 'fixed', 'endfixed', 'include', 'payee', 'apply', 'tag', 'test', 'year'
     40 } + S('AYNDCIiOobh')
     41 lex:add_rule('directive', token(lexer.KEYWORD, lexer.starts_line(S('!@')^-1 * directive_word)))
     42 
     43 lexer.property['scintillua.comment'] = '#'
     44 
     45 return lex