zk.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 'poseidon_hash', 'calculate_merkle_root',
  29. 'constrain_instance',
  30. })
  31. -- Identifiers.
  32. local identifier = token(l.IDENTIFIER, l.word)
  33. -- Operators.
  34. local operator = token(l.OPERATOR, S('(){}=;,'))
  35. M._rules = {
  36. {'whitespace', ws},
  37. {'comment', comment},
  38. {'string', string},
  39. {'keyword', keyword},
  40. {'type', type},
  41. {'instruction', instruction},
  42. {'identifier', identifier},
  43. {'operator', operator},
  44. }
  45. return M