zk.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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', 'EcFixedPointBase', 'EcFixedPointShort',
  20. 'Base', 'BaseArray', 'Scalar', 'ScalarArray',
  21. 'MerklePath',
  22. 'Uint32', 'Uint64',
  23. })
  24. -- Instructions.
  25. local instruction = token('instruction', word_match{
  26. 'ec_add', 'ec_mul', 'ec_mul_base', 'ec_mul_short',
  27. 'ec_get_x', 'ec_get_y',
  28. 'base_add', 'base_mul',
  29. 'poseidon_hash', 'calculate_merkle_root',
  30. 'constrain_instance',
  31. })
  32. -- Identifiers.
  33. local identifier = token(l.IDENTIFIER, l.word)
  34. -- Operators.
  35. local operator = token(l.OPERATOR, S('(){}=;,'))
  36. M._rules = {
  37. {'whitespace', ws},
  38. {'comment', comment},
  39. {'string', string},
  40. {'keyword', keyword},
  41. {'type', type},
  42. {'instruction', instruction},
  43. {'identifier', identifier},
  44. {'operator', operator},
  45. }
  46. return M