-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluck.lua
More file actions
226 lines (187 loc) · 5.35 KB
/
luck.lua
File metadata and controls
226 lines (187 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
local tinsert = table.insert
local tremove = table.remove
local tunpack = table.unpack
local tconcat = table.concat
local sformat = string.format
local smatch = string.match
local function _new_env(userenv)
local CfgEnv = {}
for k,v in pairs(_G) do
CfgEnv[k] = v
end
for k,v in pairs(userenv) do
CfgEnv[k] = v
end
local _PATH = {}
local function dummy_template()
return true
end
local function _LITERAL(template)
return function (cfg)
return template == cfg, "wrong value"
end
end
-- declare local before
local function _check(template, v, path)
local f
if type(template) == "table" then
f = CfgEnv.STRUCT(template)
elseif type(template) == "function" then
f = template
else
f = _LITERAL(template)
end
tinsert(_PATH, path)
local ok,err = f(v)
if ok and path then
tremove(_PATH)
end
return ok, err
end
function CfgEnv.ANY()
return true
end
function CfgEnv.NIL(v)
return type(v) == "nil", "not nil"
end
function CfgEnv.BOOL(v)
return type(v) == "boolean", "not boolean"
end
function CfgEnv.NUM(v)
return type(v) == "number", "not number"
end
function CfgEnv.STRING(v)
return type(v) == "string", "not string"
end
function CfgEnv.PATTERN(pat)
return function (v)
if type(v) ~= "string" then
return false, "not string"
end
if not smatch(v, pat) then
return false, sformat("'%s' not match pattern '%s'", v, pat)
end
return true
end
end
function CfgEnv.RANGE(min, max)
local max = max or math.huge
local min = min or -math.huge
return function (v)
if type(v) ~= "number" then
return false, "not number"
end
if v > max or v < min then
return false, "not in range"
end
return true
end
end
function CfgEnv.LIST(...)
local temps = {...}
assert(next(temps))
return function (v)
if type(v) ~= "table" then
return false, "wrong type"
end
for i,temp in ipairs(temps) do
if not _check(temp, v[i], i) then
return false,"wrong list element"
end
end
for i=#temps+1, #v do
if not _check(temps[#temps], v[i], i) then
return false,"wrong list element"
end
end
return true
end
end
function CfgEnv.TABLE(ktemp, vtemp)
ktemp = ktemp or dummy_template
vtemp = vtemp or dummy_template
return function (value)
if type(value) ~= "table" then
return false, "wrong type"
end
for k,v in pairs(value) do
local ok,err = _check(ktemp, k, tostring(k))
if not ok then
return false, "wrong table key"
end
ok,err = _check(vtemp, v, tostring(k))
if not ok then
return false, "wrong table value"
end
end
return true
end
end
-- this syntax can be simplified ,like:
-- STRUCT{foo = bar} as {foo = bar}
function CfgEnv.STRUCT(template)
return function (cfg)
if type(cfg) ~= "table" then
return false, "not table"
end
for k,v in pairs(template) do
local ok,err = _check(v, cfg[k], tostring(k))
if not ok then
return false, err
end
end
return true
end
end
function CfgEnv.OR(...)
local templates = {...}
return function (value)
local path = _PATH
local curpath_sz = #path
local ok,err
for _,temp in ipairs(templates) do
for j=curpath_sz+1,#path do path[j] = nil end
ok,err = _check(temp, value)
if ok then
return true
end
end
return false,err
end
end
return CfgEnv, function (template, cfg)
_PATH = {}
local ok, err = _check(template, cfg)
return ok, err, _PATH
end
end
local mt = {}
mt.__index = mt
function mt:check(cfg, template)
template = template or self.template
local ok,err, path = self.begin_check(template, cfg)
if not ok then
err = sformat("%s: %s", tconcat(path, "."), err)
end
return ok, err
end
function mt:get_template()
return self.template
end
local M = {}
function M.new(template_func, userenv)
local self = {}
assert(type(template_func) == "function")
self.env, self.begin_check = _new_env(userenv or {})
self.template = template_func(self.env)
return setmetatable(self, mt)
end
function M.load_chunk(chunk)
local reader = coroutine.wrap(function ()
coroutine.yield("return function (_ENV)\n")
coroutine.yield(chunk)
coroutine.yield("\nend\n")
end)
return load(reader,path)()
end
return M