mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 14:48:08 -05:00
contrib/zk.lua: Require spaces for indentation
This commit is contained in:
@@ -3,31 +3,40 @@ local l = require('lexer')
|
|||||||
local token, word_match = l.token, l.word_match
|
local token, word_match = l.token, l.word_match
|
||||||
local P, R, S = lpeg.P, lpeg.R, lpeg.S
|
local P, R, S = lpeg.P, lpeg.R, lpeg.S
|
||||||
|
|
||||||
local M = {_NAME = 'zk'}
|
local lex = l.new('zk', {fold_by_indentation = true})
|
||||||
|
|
||||||
-- Whitespace.
|
-- Whitespace.
|
||||||
local ws = token(l.WHITESPACE, l.space^1)
|
local indent = #l.starts_line(S(' \t')) *
|
||||||
|
(token(l.WHITESPACE, ' ') + token('indent_error', '\t'))^1
|
||||||
|
lex:add_rule('indent', indent)
|
||||||
|
lex:add_style('indent_error', {back = l.colors.red})
|
||||||
|
lex:add_rule('whitespace', token(l.WHITESPACE, S(' \t')^1 + l.newline^1))
|
||||||
|
|
||||||
-- Comments.
|
-- Comments.
|
||||||
local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
|
local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
|
||||||
|
lex:add_rule('comment', comment)
|
||||||
|
|
||||||
-- Strings.
|
-- Strings.
|
||||||
local dq_str = P('U')^-1 * l.range('"', true)
|
local dq_str = P('U')^-1 * l.range('"', true)
|
||||||
local string = token(l.STRING, dq_str)
|
local string = token(l.STRING, dq_str)
|
||||||
|
lex:add_rule('string', string)
|
||||||
|
|
||||||
-- Numbers.
|
-- Numbers.
|
||||||
local number = token(l.NUMBER, l.integer)
|
local number = token(l.NUMBER, l.integer)
|
||||||
|
lex:add_rule('number', number)
|
||||||
|
|
||||||
-- Keywords.
|
-- Keywords.
|
||||||
local keyword = token(l.KEYWORD, word_match{
|
local keyword = token(l.KEYWORD, word_match{
|
||||||
'k', "field", 'constant', 'witness', 'circuit',
|
'k', "field", 'constant', 'witness', 'circuit',
|
||||||
})
|
})
|
||||||
|
lex:add_rule('keyword', keyword)
|
||||||
|
|
||||||
-- Constants.
|
-- Constants.
|
||||||
local constant = token(l.CONSTANT, word_match{
|
local constant = token(l.CONSTANT, word_match{
|
||||||
'true', 'false',
|
'true', 'false',
|
||||||
'VALUE_COMMIT_VALUE', 'VALUE_COMMIT_RANDOM', 'NULLIFIER_K',
|
'VALUE_COMMIT_VALUE', 'VALUE_COMMIT_RANDOM', 'NULLIFIER_K',
|
||||||
})
|
})
|
||||||
|
lex:add_rule('constant', constant)
|
||||||
|
|
||||||
-- Types.
|
-- Types.
|
||||||
local type = token(l.TYPE, word_match{
|
local type = token(l.TYPE, word_match{
|
||||||
@@ -35,6 +44,7 @@ local type = token(l.TYPE, word_match{
|
|||||||
'EcNiPoint', 'Base', 'BaseArray', 'Scalar', 'ScalarArray',
|
'EcNiPoint', 'Base', 'BaseArray', 'Scalar', 'ScalarArray',
|
||||||
'MerklePath', 'Uint32', 'Uint64',
|
'MerklePath', 'Uint32', 'Uint64',
|
||||||
})
|
})
|
||||||
|
lex:add_rule('type', type)
|
||||||
|
|
||||||
-- Instructions.
|
-- Instructions.
|
||||||
local instruction = token('instruction', word_match{
|
local instruction = token('instruction', word_match{
|
||||||
@@ -47,24 +57,14 @@ local instruction = token('instruction', word_match{
|
|||||||
'constrain_equal_base', 'constrain_equal_point',
|
'constrain_equal_base', 'constrain_equal_point',
|
||||||
'constrain_instance', 'debug',
|
'constrain_instance', 'debug',
|
||||||
})
|
})
|
||||||
|
lex:add_rule('instruction', instruction)
|
||||||
|
|
||||||
-- Identifiers.
|
-- Identifiers.
|
||||||
local identifier = token(l.IDENTIFIER, l.word)
|
local identifier = token(l.IDENTIFIER, l.word)
|
||||||
|
lex:add_rule('identifier', identifier)
|
||||||
|
|
||||||
-- Operators.
|
-- Operators.
|
||||||
local operator = token(l.OPERATOR, S('(){}=;,'))
|
local operator = token(l.OPERATOR, S('(){}=;,'))
|
||||||
|
lex:add_rule('operator', operator)
|
||||||
|
|
||||||
M._rules = {
|
return lex
|
||||||
{'whitespace', ws},
|
|
||||||
{'comment', comment},
|
|
||||||
{'keyword', keyword},
|
|
||||||
{'type', type},
|
|
||||||
{'constant', constant},
|
|
||||||
{'string', string},
|
|
||||||
{'number', number},
|
|
||||||
{'instruction', instruction},
|
|
||||||
{'identifier', identifier},
|
|
||||||
{'operator', operator},
|
|
||||||
}
|
|
||||||
|
|
||||||
return M
|
|
||||||
|
|||||||
Reference in New Issue
Block a user