zk.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. -- Numbers.
  14. local number = token(l.NUMBER, l.integer)
  15. -- Keywords.
  16. local keyword = token(l.KEYWORD, word_match{
  17. 'constant', 'contract', 'circuit',
  18. })
  19. -- Constants.
  20. local constant = token(l.CONSTANT, word_match{
  21. 'true', 'false',
  22. 'VALUE_COMMIT_VALUE', 'VALUE_COMMIT_RANDOM', 'NULLIFIER_K',
  23. })
  24. -- Types.
  25. local type = token(l.TYPE, word_match{
  26. 'EcPoint', 'EcFixedPoint', 'EcFixedPointBase', 'EcFixedPointShort',
  27. 'Base', 'BaseArray', 'Scalar', 'ScalarArray',
  28. 'MerklePath',
  29. 'Uint32', 'Uint64',
  30. })
  31. -- Instructions.
  32. local instruction = token('instruction', word_match{
  33. 'ec_add', 'ec_mul', 'ec_mul_base', 'ec_mul_short',
  34. 'ec_get_x', 'ec_get_y',
  35. 'base_add', 'base_mul', 'base_sub', 'greater_than',
  36. 'poseidon_hash', 'merkle_root', 'constrain_instance',
  37. 'range_check', 'less_than', 'bool_check',
  38. 'witness_base',
  39. })
  40. -- Identifiers.
  41. local identifier = token(l.IDENTIFIER, l.word)
  42. -- Operators.
  43. local operator = token(l.OPERATOR, S('(){}=;,'))
  44. M._rules = {
  45. {'whitespace', ws},
  46. {'comment', comment},
  47. {'keyword', keyword},
  48. {'type', type},
  49. {'constant', constant},
  50. {'string', string},
  51. {'number', number},
  52. {'instruction', instruction},
  53. {'identifier', identifier},
  54. {'operator', operator},
  55. }
  56. return M