logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

ledger.lua (1485B)


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