zk.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.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',
  36. 'poseidon_hash', 'merkle_root',
  37. 'range_check', 'less_than_strict', 'less_than_loose', 'bool_check',
  38. 'witness_base',
  39. 'constrain_equal_base', 'constrain_equal_point',
  40. 'constrain_instance',
  41. })
  42. -- Identifiers.
  43. local identifier = token(l.IDENTIFIER, l.word)
  44. -- Operators.
  45. local operator = token(l.OPERATOR, S('(){}=;,'))
  46. M._rules = {
  47. {'whitespace', ws},
  48. {'comment', comment},
  49. {'keyword', keyword},
  50. {'type', type},
  51. {'constant', constant},
  52. {'string', string},
  53. {'number', number},
  54. {'instruction', instruction},
  55. {'identifier', identifier},
  56. {'operator', operator},
  57. }
  58. return M