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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
local unpack = table.unpack or unpack -- luacheck: compat
 
local utils = {}
 
utils.dir_sep = package.config:sub(1,1)
utils.is_windows = utils.dir_sep == "\\"
 
local bom = "\239\187\191"
 
-- Returns all contents of file (path or file handler) or nil + error message.
function utils.read_file(file)
   local handler
 
   if type(file) == "string" then
      local open_err
      handler, open_err = io.open(file, "rb")
 
      if not handler then
         open_err = utils.unprefix(open_err, file .. ": ")
         return nil, "couldn't read: " .. open_err
      end
   else
      handler = file
   end
 
   local res, read_err = handler:read("*a")
   handler:close()
 
   if not res then
      return nil, "couldn't read: " .. read_err
   end
 
   -- Use :len() instead of # operator because in some environments
   -- string library is patched to handle UTF.
   if res:sub(1, bom:len()) == bom then
      res = res:sub(bom:len() + 1)
   end
 
   return res
end
 
-- luacheck: push
-- luacheck: compat
if _VERSION:find "5.1" then
   -- Loads Lua source string in an environment, returns function or nil, error.
   function utils.load(src, env, chunkname)
      local func, err = loadstring(src, chunkname)
 
      if func then
         if env then
            setfenv(func, env)
         end
 
         return func
      else
         return nil, err
      end
   end
else
   -- Loads Lua source string in an environment, returns function or nil, error.
   function utils.load(src, env, chunkname)
      return load(src, chunkname, "t", env or _ENV)
   end
end
-- luacheck: pop
 
-- Loads config containing assignments to global variables from path.
-- Returns config table and return value of config or nil and error type
-- ("I/O" or "syntax" or "runtime") and error message.
function utils.load_config(path, env)
   env = env or {}
   local src, read_err = utils.read_file(path)
 
   if not src then
      return nil, "I/O", read_err
   end
 
   local func, load_err = utils.load(src, env, "chunk")
 
   if not func then
      return nil, "syntax", "line " .. utils.unprefix(load_err, "[string \"chunk\"]:")
   end
 
   local ok, res = pcall(func)
 
   if not ok then
      return nil, "runtime", "line " .. utils.unprefix(res, "[string \"chunk\"]:")
   end
 
   return env, res
end
 
function utils.array_to_set(array)
   local set = {}
 
   for index, value in ipairs(array) do
      set[value] = index
   end
 
   return set
end
 
function utils.concat_arrays(array)
   local res = {}
 
   for _, subarray in ipairs(array) do
      for _, item in ipairs(subarray) do
         table.insert(res, item)
      end
   end
 
   return res
end
 
function utils.update(t1, t2)
   for k, v in pairs(t2) do
      t1[k] = v
   end
 
   return t1
end
 
local class_metatable = {}
 
function class_metatable.__call(class, ...)
   local obj = setmetatable({}, class)
 
   if class.__init then
      class.__init(obj, ...)
   end
 
   return obj
end
 
function utils.class()
   local class = setmetatable({}, class_metatable)
   class.__index = class
   return class
end
 
function utils.is_instance(object, class)
   return rawequal(debug.getmetatable(object), class)
end
 
utils.Stack = utils.class()
 
function utils.Stack:__init()
   self.size = 0
end
 
function utils.Stack:push(value)
   self.size = self.size + 1
   self[self.size] = value
   self.top = value
end
 
function utils.Stack:pop()
   local value = self[self.size]
   self[self.size] = nil
   self.size = self.size - 1
   self.top = self[self.size]
   return value
end
 
local ErrorWrapper = utils.class()
 
function ErrorWrapper:__init(err, traceback)
   self.err = err
   self.traceback = traceback
end
 
function ErrorWrapper:__tostring()
   return tostring(self.err) .. "\n" .. self.traceback
end
 
local function error_handler(err)
   if utils.is_instance(err, ErrorWrapper) then
      return err
   else
      return ErrorWrapper(err, debug.traceback())
   end
end
 
-- Like pcall, but wraps errors in {err = err, traceback = traceback}
-- tables unless already wrapped.
function utils.try(f, ...)
   local args = {...}
   local num_args = select("#", ...)
 
   local function task()
      return f(unpack(args, 1, num_args))
   end
 
   return xpcall(task, error_handler)
end
 
local function ripairs_iterator(array, i)
   if i == 1 then
      return nil
   else
      i = i - 1
      return i, array[i]
   end
end
 
function utils.ripairs(array)
   return ripairs_iterator, array, #array + 1
end
 
function utils.sorted_pairs(t)
   local keys = {}
 
   for key in pairs(t) do
      table.insert(keys, key)
   end
 
   table.sort(keys)
 
   local index = 1
 
   return function()
      local key = keys[index]
 
      if key == nil then
         return
      end
 
      index = index + 1
 
      return key, t[key]
   end
end
 
function utils.unprefix(str, prefix)
   if str:sub(1, #prefix) == prefix then
      return str:sub(#prefix + 1)
   else
      return str
   end
end
 
function utils.after(str, pattern)
   local _, last_matched_index = str:find(pattern)
 
   if last_matched_index then
      return str:sub(last_matched_index + 1)
   end
end
 
function utils.strip(str)
   local _, last_start_space = str:find("^%s*")
   local first_end_space = str:find("%s*$")
   return str:sub(last_start_space + 1, first_end_space - 1)
end
 
-- `sep` must be nil or a single character. Behaves like python's `str.split`.
function utils.split(str, sep)
   local parts = {}
   local pattern
 
   if sep then
      pattern = sep .. "([^" .. sep .. "]*)"
      str = sep .. str
   else
      pattern = "%S+"
   end
 
   for part in str:gmatch(pattern) do
      table.insert(parts, part)
   end
 
   return parts
end
 
utils.InvalidPatternError = utils.class()
 
function utils.InvalidPatternError:__init(err, pattern)
   self.err = err
   self.pattern = pattern
end
 
function utils.InvalidPatternError:__tostring()
   return self.err
end
 
-- Behaves like string.match, except it normally returns boolean and
-- throws an instance of utils.InvalidPatternError on invalid pattern.
-- The error object turns into original error when tostring is used on it,
-- to ensure behaviour is predictable when luacheck is used as a module.
function utils.pmatch(str, pattern)
   assert(type(str) == "string")
   assert(type(pattern) == "string")
 
   local ok, res = pcall(string.match, str, pattern)
 
   if not ok then
      error(utils.InvalidPatternError(res, pattern), 0)
   else
      return not not res
   end
end
 
-- Maps func over array.
function utils.map(func, array)
   local res = {}
 
   for i, item in ipairs(array) do
      res[i] = func(item)
   end
 
   return res
end
 
-- Returns validator checking type.
function utils.has_type(type_)
   return function(x)
      if type(x) == type_ then
         return true
      else
         return false, ("%s expected, got %s"):format(type_, type(x))
      end
   end
end
 
-- Returns validator checking type and allowing false.
function utils.has_type_or_false(type_)
   return function(x)
      if type(x) == type_ then
         return true
      elseif type(x) == "boolean" then
         if x then
            return false, ("%s or false expected, got true"):format(type_)
         else
            return true
         end
      else
         return false, ("%s or false expected, got %s"):format(type_, type(x))
      end
   end
end
 
-- Returns validator checking two type possibilities.
function utils.has_either_type(type1, type2)
   return function(x)
      if type(x) == type1 or type(x) == type2 then
         return true
      else
         return false, ("%s or %s expected, got %s"):format(type1, type2, type(x))
      end
   end
end
 
-- Returns validator checking that value is an array with elements of type.
function utils.array_of(type_)
   return function(x)
      if type(x) ~= "table" then
         return false, ("array of %ss expected, got %s"):format(type_, type(x))
      end
 
      for index, item in ipairs(x) do
         if type(item) ~= type_ then
            return false, ("array of %ss expected, got %s at index [%d]"):format(type_, type(item), index)
         end
      end
 
      return true
   end
end
 
return utils