From cbd143775295cca490b09eae37c1766389da5edf Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 17 Jul 2018 16:13:35 -0700 Subject: Optimization/simplification pass. --- files.lua | 186 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 93 insertions(+), 93 deletions(-) (limited to 'files.lua') diff --git a/files.lua b/files.lua index fe1263e..65a8e1b 100644 --- a/files.lua +++ b/files.lua @@ -34,25 +34,52 @@ files.read = function(filename) _FILE_CACHE[filename] = contents return contents end -local iterate_single -iterate_single = function(item, prev) - if item == prev then - return nil - else - return item - end -end local match, gsub do local _obj_0 = string match, gsub = _obj_0.match, _obj_0.gsub end -iterate_single = function(item, prev) - if item == prev then - return nil - else - return item +local sanitize +sanitize = function(path) + path = gsub(path, "\\", "\\\\") + path = gsub(path, "`", "") + path = gsub(path, '"', '\\"') + path = gsub(path, "$", "") + return path +end +files.exists = function(path) + if not (io.popen("ls " .. tostring(sanitize(path))):close()) then + return true + end + if package.nomsupath then + for nomsupath in package.nomsupath:gmatch("[^;]+") do + if not (io.popen("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))):close()) then + return true + end + end end + return false +end +local _browse_cache = { } +local browse +browse = function(path) + if not (_browse_cache[path]) then + local f = io.popen('find -L "' .. package.nomsupath .. '/' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') + local _files + do + local _tbl_0 = { } + for line in f:lines() do + local _key_0, _val_0 = line + _tbl_0[_key_0] = _val_0 + end + _files = _tbl_0 + end + if not (f:close()) then + _files = false + end + _browse_cache[path] = _files + end + return _browse_cache[path] end local ok, lfs = pcall(require, "lfs") if ok then @@ -78,109 +105,76 @@ if ok then end return false end - local browse browse = function(filename) - local file_type, err = lfs.attributes(filename, 'mode') - if file_type == 'file' then - if match(filename, "%.nom$") or match(filename, "%.lua$") then - coroutine.yield(filename) - return true - end - elseif file_type == 'directory' or file_type == 'link' then - for subfile in lfs.dir(filename) do - if not (subfile == "." or subfile == "..") then - browse(filename .. "/" .. subfile) + if not (_browse_cache[filename]) then + _browse_cache[filename] = false + local file_type, err = lfs.attributes(filename, 'mode') + if file_type == 'file' then + if match(filename, "%.nom$") or match(filename, "%.lua$") then + _browse_cache[filename] = { + filename + } end - end - return true - elseif file_type == 'char device' then - coroutine.yield(filename) - return true - end - return false - end - files.walk = function(path) - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' then - return iterate_single, path - end - return coroutine.wrap(function() - if not browse(path) and package.nomsupath then - for nomsupath in package.nomsupath:gmatch("[^;]+") do - if browse(nomsupath .. "/" .. path) then - break + elseif file_type == 'char device' then + _browse_cache[filename] = { + filename + } + elseif file_type == 'directory' or file_type == 'link' then + local _files = { } + for subfile in lfs.dir(filename) do + if not (subfile == "." or subfile == "..") then + local _list_0 = (browse(filename .. "/" .. subfile) or { }) + for _index_0 = 1, #_list_0 do + local f = _list_0[_index_0] + _files[#_files + 1] = f + end end end + _browse_cache[filename] = _files end - return nil - end) + end + return _browse_cache[filename] end else if io.popen('find . -maxdepth 0'):close() then error("Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: http://keplerproject.github.io/luafilesystem/ or `luarocks install luafilesystem`)", 0) end - local sanitize - sanitize = function(path) - path = gsub(path, "\\", "\\\\") - path = gsub(path, "`", "") - path = gsub(path, '"', '\\"') - path = gsub(path, "$", "") - return path +end +files.walk = function(path, flush_cache) + if flush_cache == nil then + flush_cache = false + end + if flush_cache then + _browse_cache = { } end - files.exists = function(path) - if not (io.popen("ls " .. tostring(sanitize(path))):close()) then - return true - end - if package.nomsupath then - for nomsupath in package.nomsupath:gmatch("[^;]+") do - if not (io.popen("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))):close()) then - return true + local _files = browse(path) + if package.nomsupath and not _files then + for nomsupath in package.nomsupath:gmatch("[^;]+") do + do + _files = browse(nomsupath .. "/" .. path) + if _files then + break end end end - return false end - files.walk = function(path) - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' then - return iterate_single, path - end - path = sanitize(path) - return coroutine.wrap(function() - local f = io.popen('find -L "' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') - local found = false - for line in f:lines() do - found = true - coroutine.yield(line) + local iter + iter = function(_files, i) + i = i + 1 + do + local f = _files[i] + if f then + return i, f end - if not found and package.nomsupath then - f:close() - for nomsupath in package.nomsupath:gmatch("[^;]+") do - f = io.popen('find -L "' .. package.nomsupath .. '/' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') - for line in f:lines() do - found = true - coroutine.yield(line) - end - f:close() - if found then - break - end - end - end - if not (found) then - return error("Invalid file path: " .. tostring(path)) - end - end) + end end + return iter, _files, 0 end local line_counter = re.compile([[ lines <- {| line (%nl line)* |} line <- {} (!%nl .)* ]], { nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") }) -local get_lines = re.compile([[ lines <- {| line (%nl line)* |} - line <- {[^%nl]*} -]], { - nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") -}) local _LINE_STARTS = { } files.get_line_starts = function(str) if type(str) ~= 'string' then @@ -196,6 +190,7 @@ files.get_line_starts = function(str) _LINE_STARTS[str] = line_starts return line_starts end +local log = { } files.get_line_number = function(str, pos) local line_starts = files.get_line_starts(str) local lo, hi = 1, #line_starts @@ -213,6 +208,11 @@ files.get_line = function(str, line_no) local line_starts = files.get_line_starts(str) return str:sub(line_starts[line_no] or 1, (line_starts[line_no + 1] or 1) - 2) end +local get_lines = re.compile([[ lines <- {| line (%nl line)* |} + line <- {[^%nl]*} +]], { + nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") +}) files.get_lines = function(str) return get_lines:match(str) end -- cgit v1.2.3