From 23022dc88c5a2df0cb3415720a8f3a8c479d6b92 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 28 Apr 2018 15:20:48 -0700 Subject: [PATCH] Re-added (..), which is necessary for long expressions as first arg to an action. --- nomsu.lua | 64 ++++++++++++++++++++++++----------------------- nomsu.peg | 2 +- nomsu_tree.lua | 68 +++++++++++++++++++++++++++----------------------- 3 files changed, 71 insertions(+), 63 deletions(-) diff --git a/nomsu.lua b/nomsu.lua index 0730724..242d7a6 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -1,4 +1,3 @@ -local lfs = require('lfs') local re = require('re') local lpeg = require('lpeg') lpeg.setmaxstack(10000) @@ -41,7 +40,7 @@ FILE_CACHE = setmetatable({ }, { if not (file) then return nil end - local contents = file:read("a"):sub(1, -2) + local contents = file:read("a"):sub(1, -1) file:close() self[filename] = contents return contents @@ -362,41 +361,44 @@ do end return self:run_lua(lua) end, - run_file = function(self, filename, compile_fn) + run_file = function(self, path, compile_fn) if compile_fn == nil then compile_fn = nil end - local file_attributes = assert(lfs.attributes(filename), "File not found: " .. tostring(filename)) - if file_attributes.mode == "directory" then - for short_filename in lfs.dir(filename) do - local full_filename = filename .. '/' .. short_filename - local attr = lfs.attributes(full_filename) - if attr.mode ~= "directory" and short_filename:match(".*%.nom") then - self:run_file(full_filename, compile_fn) + local ret = nil + for filename in io.popen("find " .. path .. " -type f"):lines() do + local _continue_0 = false + repeat + if filename:match("%.lua$") then + local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename)) + ret = self:run_lua(Lua(Source(filename), file)) + elseif filename:match("%.nom$") then + if not self.skip_precompiled then + local lua_filename = filename:gsub("%.nom$", ".lua") + local file = FILE_CACHE[lua_filename] + if file then + ret = self:run_lua(Lua(Source(filename), file)) + _continue_0 = true + break + end + end + local file = file or FILE_CACHE[filename] + if not file then + error("File does not exist: " .. tostring(filename), 0) + end + ret = self:run(Nomsu(Source(filename), file), compile_fn) + _continue_0 = true + break + else + error("Invalid filetype for " .. tostring(filename), 0) end + _continue_0 = true + until true + if not _continue_0 then + break end - return - end - if filename:match(".*%.lua") then - local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename)) - return self:run_lua(Lua(Source(filename), file)) - end - if filename:match(".*%.nom") then - if not self.skip_precompiled then - local lua_filename = filename:gsub("%.nom$", ".lua") - local file = FILE_CACHE[lua_filename] - if file then - return self:run_lua(Lua(Source(filename), file)) - end - end - local file = file or FILE_CACHE[filename] - if not file then - error("File does not exist: " .. tostring(filename), 0) - end - return self:run(Nomsu(Source(filename), file), compile_fn) - else - return error("Invalid filetype for " .. tostring(filename), 0) end + return ret end, use_file = function(self, filename) local loaded = self.environment.LOADED diff --git a/nomsu.peg b/nomsu.peg index 731a83c..7f6cbf6 100644 --- a/nomsu.peg +++ b/nomsu.peg @@ -30,7 +30,7 @@ inline_expression: index_chain / noindex_inline_expression indented_expression: indented_text / indented_nomsu / indented_list / indented_dict - / (indent + / ("(..)"? indent (action dedent / expression dedent / block (dedent / (("" -> "Error while parsing indented expression") => error))) diff --git a/nomsu_tree.lua b/nomsu_tree.lua index 3d09d5c..7c70489 100644 --- a/nomsu_tree.lua +++ b/nomsu_tree.lua @@ -83,9 +83,12 @@ Tree("File", { end local nomsu = Nomsu(self.source) for i, line in ipairs(self.value) do - 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 < #self.value then + if tostring(line):match("\n") then + nomsu:append("\n") + end nomsu:append("\n") end end @@ -141,7 +144,7 @@ Tree("Block", { end local nomsu = Nomsu(self.source) for i, line in ipairs(self.value) do - 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 < #self.value then nomsu:append("\n") @@ -284,10 +287,13 @@ Tree("Action", { end return concat(bits, " ") end, - as_nomsu = function(self, inline) + as_nomsu = function(self, inline, can_use_colon) if inline == nil then inline = false end + if can_use_colon == nil then + can_use_colon = false + end if inline then local nomsu = Nomsu(self.source) for i, bit in ipairs(self.value) do @@ -312,44 +318,47 @@ Tree("Action", { end return nomsu else - local inline_version = self:as_nomsu(true) - if inline_version and #inline_version <= MAX_LINE then - return inline_version - end local nomsu = Nomsu(self.source) - local spacer = nil + local next_space = "" + local last_colon = nil for i, bit in ipairs(self.value) do if bit.type == "Word" then - if spacer then - nomsu:append(spacer) - end - nomsu:append(bit.value) - spacer = " " + nomsu:append(next_space, bit.value) + next_space = " " else - local arg_nomsu = bit:as_nomsu(true) + local arg_nomsu = bit.type ~= "Block" and bit:as_nomsu(true) if arg_nomsu and #arg_nomsu < MAX_LINE then - if spacer then - nomsu:append(spacer) + if bit.type == "Action" then + if can_use_colon and i > 1 then + nomsu:append(next_space:match("[^ ]*"), ": ", arg_nomsu) + next_space = "\n.." + last_colon = i + else + nomsu:append(next_space, "(", arg_nomsu, ")") + next_space = " " + end + else + nomsu:append(next_space, arg_nomsu) + next_space = " " end - if bit.type == "Action" or bit.type == "Block" then - arg_nomsu:parenthesize() - end - spacer = " " else - arg_nomsu = bit:as_nomsu() + arg_nomsu = bit:as_nomsu(nil, true) if not (nomsu) then return nil end - if bit.type == "Action" or bit.type == "Block" then - nomsu:append("\n ") - else - if spacer then - nomsu:append(spacer) + if bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then + if i == 1 then + arg_nomsu = Nomsu(bit.source, "(..)\n ", arg_nomsu) + else + arg_nomsu = Nomsu(bit.source, "\n ", arg_nomsu) end end - spacer = "\n.." + if last_colon == i - 1 and (bit.type == "Action" or bit.type == "Block") then + next_space = "" + end + nomsu:append(next_space, arg_nomsu) + next_space = "\n.." end - nomsu:append(arg_nomsu) end end return nomsu @@ -416,9 +425,6 @@ Tree("Text", { for _index_0 = 1, #_list_0 do local bit = _list_0[_index_0] if type(bit) == 'string' then - if bit:find("\n") then - return nil - end nomsu:append((bit:gsub("\\", "\\\\"):gsub("\n", "\\n"))) else local interp_nomsu = bit:as_nomsu(true)