zk.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- LPEG lexer for the zkas zk language
  2. local l = require('lexer')
  3. local token, word_match = l.token, l.word_match
  4. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  5. local M = {_NAME = 'zk'}
  6. -- Whitespace.
  7. local ws = token(l.WHITESPACE, l.space^1)
  8. -- Comments.
  9. local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
  10. -- Strings.
  11. local dq_str = P('U')^-1 * l.delimited_range('"', true)
  12. local string = token(l.STRING, dq_str)
  13. -- Keywords.
  14. local keyword = token(l.KEYWORD, word_match{
  15. 'constant', 'contract', 'circuit',
  16. })
  17. -- Types.
  18. local type = token(l.TYPE, word_match{
  19. 'EcPoint', 'EcFixedPoint', 'Base', 'BaseArray',
  20. 'Scalar', 'ScalarArray', 'MerklePath', 'Uint32',
  21. })
  22. -- Instructions.
  23. local instruction = token('instruction', word_match{
  24. 'ec_add', 'ec_mul', 'ec_mul_base', 'ec_mul_short',
  25. 'ec_get_x', 'ec_get_y',
  26. 'poseidon_hash', 'calculate_merkle_root',
  27. 'constrain_instance',
  28. })
  29. -- Identifiers.
  30. local identifier = token(l.IDENTIFIER, l.word)
  31. -- Operators.
  32. local operator = token(l.OPERATOR, S('(){}=;,'))
  33. M._rules = {
  34. {'whitespace', ws},
  35. {'comment', comment},
  36. {'string', string},
  37. {'keyword', keyword},
  38. {'type', type},
  39. {'instruction', instruction},
  40. {'identifier', identifier},
  41. {'operator', operator},
  42. }
  43. return M