From d13a945b5f8924c1416ea2bd72212efdb3bebc20 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 28 Apr 2018 15:25:12 -0700 Subject: Removed LFS dependency in favor of just using `find` --- nomsu.lua | 2 +- nomsu.moon | 49 +++++++++++++++++++++------------------------- nomsu_tree.moon | 60 +++++++++++++++++++++++++++++++++------------------------ 3 files changed, 58 insertions(+), 53 deletions(-) diff --git a/nomsu.lua b/nomsu.lua index 242d7a6..a8a68e3 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -40,7 +40,7 @@ FILE_CACHE = setmetatable({ }, { if not (file) then return nil end - local contents = file:read("a"):sub(1, -1) + local contents = file:read("a") file:close() self[filename] = contents return contents diff --git a/nomsu.moon b/nomsu.moon index 7e94ecd..cf89666 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -10,7 +10,6 @@ -- nomsu:run(your_nomsu_code) -- Or from the command line: -- lua nomsu.lua [input_file [output_file or -]] -lfs = require 'lfs' re = require 're' lpeg = require 'lpeg' lpeg.setmaxstack 10000 @@ -44,7 +43,7 @@ FILE_CACHE = setmetatable {}, { __index: (filename)=> file = io.open(filename) return nil unless file - contents = file\read("a")\sub(1,-2) -- Lua appends trailing newline for no apparent reason. + contents = file\read("a") file\close! self[filename] = contents return contents @@ -322,31 +321,27 @@ class NomsuCompiler compile_fn(lua) return @run_lua(lua) - run_file: (filename, compile_fn=nil)=> - file_attributes = assert(lfs.attributes(filename), "File not found: #{filename}") - if file_attributes.mode == "directory" - for short_filename in lfs.dir(filename) - full_filename = filename..'/'..short_filename - attr = lfs.attributes(full_filename) - if attr.mode ~= "directory" and short_filename\match(".*%.nom") - @run_file full_filename, compile_fn - return - - if filename\match(".*%.lua") - file = assert(FILE_CACHE[filename], "Could not find file: #{filename}") - return @run_lua(Lua(Source(filename), file)) - if filename\match(".*%.nom") - if not @skip_precompiled -- Look for precompiled version - lua_filename = filename\gsub("%.nom$", ".lua") - file = FILE_CACHE[lua_filename] - if file - return @run_lua(Lua(Source(filename), file)) - file = file or FILE_CACHE[filename] - if not file - error("File does not exist: #{filename}", 0) - return @run(Nomsu(Source(filename), file), compile_fn) - else - error("Invalid filetype for #{filename}", 0) + run_file: (path, compile_fn=nil)=> + ret = nil + for filename in io.popen("find "..path.." -type f")\lines! + if filename\match("%.lua$") + file = assert(FILE_CACHE[filename], "Could not find file: #{filename}") + ret = @run_lua(Lua(Source(filename), file)) + elseif filename\match("%.nom$") + if not @skip_precompiled -- Look for precompiled version + lua_filename = filename\gsub("%.nom$", ".lua") + file = FILE_CACHE[lua_filename] + if file + ret = @run_lua(Lua(Source(filename), file)) + continue + file = file or FILE_CACHE[filename] + if not file + error("File does not exist: #{filename}", 0) + ret = @run(Nomsu(Source(filename), file), compile_fn) + continue + else + error("Invalid filetype for #{filename}", 0) + return ret use_file: (filename)=> loaded = @environment.LOADED diff --git a/nomsu_tree.moon b/nomsu_tree.moon index e2b2c74..5a505d0 100644 --- a/nomsu_tree.moon +++ b/nomsu_tree.moon @@ -55,9 +55,11 @@ Tree "File", return nil if inline nomsu = Nomsu(@source) for i, line in ipairs @value - line = assert(line\as_nomsu!, "Could not convert line to nomsu") + line = assert(line\as_nomsu(nil,true), "Could not convert line to nomsu") nomsu\append line if i < #@value + if tostring(line)\match("\n") + nomsu\append "\n" nomsu\append "\n" return nomsu @@ -95,7 +97,7 @@ Tree "Block", return nomsu nomsu = Nomsu(@source) for i, line in ipairs @value - line = assert(line\as_nomsu!, "Could not convert line to nomsu") + line = assert(line\as_nomsu(nil, true), "Could not convert line to nomsu") nomsu\append line if i < #@value nomsu\append "\n" @@ -163,7 +165,7 @@ Tree "Action", else [(t.type == "Word" and t.value or "%") for t in *@value] return concat(bits, " ") - as_nomsu: (inline=false)=> + as_nomsu: (inline=false, can_use_colon=false)=> if inline nomsu = Nomsu(@source) for i,bit in ipairs @value @@ -181,32 +183,42 @@ Tree "Action", nomsu\append arg_nomsu return nomsu else - inline_version = @as_nomsu(true) - if inline_version and #inline_version <= MAX_LINE - return inline_version nomsu = Nomsu(@source) - spacer = nil + next_space = "" + -- TODO: track line length as we go and use 80-that instead of 80 for wrapping + last_colon = nil for i,bit in ipairs @value if bit.type == "Word" - if spacer then nomsu\append spacer - nomsu\append bit.value - spacer = " " + nomsu\append next_space, bit.value + next_space = " " else - arg_nomsu = bit\as_nomsu(true) + arg_nomsu = bit.type != "Block" and bit\as_nomsu(true) if arg_nomsu and #arg_nomsu < MAX_LINE - if spacer then nomsu\append spacer - if bit.type == "Action" or bit.type == "Block" - arg_nomsu\parenthesize! - spacer = " " + if bit.type == "Action" + if can_use_colon and i > 1 + nomsu\append next_space\match("[^ ]*"), ": ", arg_nomsu + next_space = "\n.." + last_colon = i + else + nomsu\append next_space, "(", arg_nomsu, ")" + next_space = " " + else + nomsu\append next_space, arg_nomsu + next_space = " " else - arg_nomsu = bit\as_nomsu! + arg_nomsu = bit\as_nomsu(nil, true) return nil unless nomsu - if bit.type == "Action" or bit.type == "Block" - nomsu\append "\n " - else - if spacer then nomsu\append spacer - spacer = "\n.." - nomsu\append arg_nomsu + -- These types carry their own indentation + if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" + if i == 1 + arg_nomsu = Nomsu(bit.source, "(..)\n ", arg_nomsu) + else + arg_nomsu = Nomsu(bit.source, "\n ", arg_nomsu) + + if last_colon == i-1 and (bit.type == "Action" or bit.type == "Block") + next_space = "" + nomsu\append next_space, arg_nomsu + next_space = "\n.." return nomsu @@ -244,9 +256,7 @@ Tree "Text", nomsu = Nomsu(@source, '"') for bit in *@value if type(bit) == 'string' - -- Force indented text - return nil if bit\find("\n") - -- TODO: unescape better + -- TODO: unescape better? nomsu\append (bit\gsub("\\","\\\\")\gsub("\n","\\n")) else interp_nomsu = bit\as_nomsu(true) -- cgit v1.2.3