/usr/share/lua/5.1/luacheck/utils.lua is in lua-check 0.17.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | 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
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 function error_handler(err)
return {
err = err,
traceback = debug.traceback()
}
end
-- Calls f with arg, returns what it does.
-- If f throws a table, returns nil, the table.
-- If f throws not a table, rethrows.
function utils.pcall(f, arg)
local function task()
return f(arg)
end
local ok, res = xpcall(task, error_handler)
if ok then
return res
elseif type(res.err) == "table" then
return nil, res.err
else
error(tostring(res.err) .. "\n" .. res.traceback, 0)
end
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.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
-- Splits a string into an array of lines.
-- "\n", "\r", "\r\n", and "\n\r" are considered
-- line endings to be consistent with Lua lexer.
function utils.split_lines(str)
local lines = {}
local pos = 1
while true do
local line_end_pos, _, line_end = str:find("([\n\r])", pos)
if not line_end_pos then
break
end
local line = str:sub(pos, line_end_pos - 1)
table.insert(lines, line)
pos = line_end_pos + 1
local next_char = str:sub(pos, pos)
if next_char:match("[\n\r]") and next_char ~= line_end then
pos = pos + 1
end
end
if pos <= #str then
local last_line = str:sub(pos)
table.insert(lines, last_line)
end
return lines
end
-- Behaves like string.match, except it normally returns boolean and
-- throws a table {pattern = pattern} on invalid pattern.
-- The error message 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(setmetatable({pattern = pattern}, {__tostring = function() return res end}))
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 predicate checking type.
function utils.has_type(type_)
return function(x)
return type(x) == type_
end
end
-- Returns predicate 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
end
for _, item in ipairs(x) do
if type(item) ~= type_ then
return false
end
end
return true
end
end
-- Returns predicate chacking if value satisfies on of predicates.
function utils.either(pred1, pred2)
return function(x)
return pred1(x) or pred2(x)
end
end
return utils
|