zk.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. 'k', "field", 'constant', 'witness', '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. 'EcNiPoint', 'Base', 'BaseArray', 'Scalar', 'ScalarArray',
  28. 'MerklePath', 'Uint32', 'Uint64',
  29. })
  30. -- Instructions.
  31. local instruction = token('instruction', word_match{
  32. 'ec_add', 'ec_mul', 'ec_mul_base', 'ec_mul_short', 'ec_mul_var_base',
  33. 'ec_get_x', 'ec_get_y',
  34. 'base_add', 'base_mul', 'base_sub',
  35. 'poseidon_hash', 'merkle_root',
  36. 'range_check', 'less_than_strict', 'less_than_loose', 'bool_check',
  37. 'cond_select', 'zero_cond', 'witness_base',
  38. 'constrain_equal_base', 'constrain_equal_point',
  39. 'constrain_instance', 'debug',
  40. })
  41. -- Identifiers.
  42. local identifier = token(l.IDENTIFIER, l.word)
  43. -- Operators.
  44. local operator = token(l.OPERATOR, S('(){}=;,'))
  45. M._rules = {
  46. {'whitespace', ws},
  47. {'comment', comment},
  48. {'keyword', keyword},
  49. {'type', type},
  50. {'constant', constant},
  51. {'string', string},
  52. {'number', number},
  53. {'instruction', instruction},
  54. {'identifier', identifier},
  55. {'operator', operator},
  56. }
  57. return M