mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 14:48:08 -05:00
contrib: Add LPEG lexer for the zkas language.
This commit is contained in:
54
contrib/zk.lua
Normal file
54
contrib/zk.lua
Normal file
@@ -0,0 +1,54 @@
|
||||
-- LPEG lexer for the zkas zk language
|
||||
local l = require('lexer')
|
||||
local token, word_match = l.token, l.word_match
|
||||
local P, R, S = lpeg.P, lpeg.R, lpeg.S
|
||||
|
||||
local M = {_NAME = 'zk'}
|
||||
|
||||
-- Whitespace.
|
||||
local ws = token(l.WHITESPACE, l.space^1)
|
||||
|
||||
-- Comments.
|
||||
local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
|
||||
|
||||
-- Strings.
|
||||
local dq_str = P('U')^-1 * l.delimited_range('"', true)
|
||||
local string = token(l.STRING, dq_str)
|
||||
|
||||
-- Keywords.
|
||||
local keyword = token(l.KEYWORD, word_match{
|
||||
'constant', 'contract', 'circuit',
|
||||
})
|
||||
|
||||
-- Types.
|
||||
local type = token(l.TYPE, word_match{
|
||||
'EcPoint', 'EcFixedPoint', 'Base', 'BaseArray',
|
||||
'Scalar', 'ScalarArray', 'MerklePath',
|
||||
})
|
||||
|
||||
-- Instructions.
|
||||
local instruction = token('instruction', word_match{
|
||||
'ec_add', 'ec_mul', 'ec_mul_base', 'ec_mul_short',
|
||||
'ec_get_x', 'ec_get_y',
|
||||
'poseidon_hash', 'calculate_merkle_root',
|
||||
'constrain_instance',
|
||||
})
|
||||
|
||||
-- Identifiers.
|
||||
local identifier = token(l.IDENTIFIER, l.word)
|
||||
|
||||
-- Operators.
|
||||
local operator = token(l.OPERATOR, S('(){}=;,'))
|
||||
|
||||
M._rules = {
|
||||
{'whitespace', ws},
|
||||
{'comment', comment},
|
||||
{'string', string},
|
||||
{'keyword', keyword},
|
||||
{'type', type},
|
||||
{'instruction', instruction},
|
||||
{'identifier', identifier},
|
||||
{'operator', operator},
|
||||
}
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user