diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-06-12 23:47:43 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-06-12 23:47:54 -0700 |
| commit | 45e0a831fedc54365af619be578982dc3d3260a1 (patch) | |
| tree | 4f403ab2f048c7ca2458a70b3cc6a25c7cce004c | |
| parent | 968b5da15050d6804404833ea258bdb531dca9ea (diff) | |
Much improved code formatting for tree_to_nomsu
| -rw-r--r-- | nomsu.lua | 55 | ||||
| -rwxr-xr-x | nomsu.moon | 44 |
2 files changed, 82 insertions, 17 deletions
@@ -30,10 +30,10 @@ do local _obj_0 = table insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat end -local match, sub, rep, gsub, format, byte +local match, sub, rep, gsub, format, byte, find do local _obj_0 = string - match, sub, rep, gsub, format, byte, match = _obj_0.match, _obj_0.sub, _obj_0.rep, _obj_0.gsub, _obj_0.format, _obj_0.byte, _obj_0.match + match, sub, rep, gsub, format, byte, match, find = _obj_0.match, _obj_0.sub, _obj_0.rep, _obj_0.gsub, _obj_0.format, _obj_0.byte, _obj_0.match, _obj_0.find end local debug_getinfo = debug.getinfo local Nomsu, Lua, Source @@ -101,6 +101,11 @@ local line_counter = re.compile([[ lines <- {| line (%nl line)* |} ]], { nl = P("\r") ^ -1 * P("\n") }) +local get_lines = re.compile([[ lines <- {| line (%nl line)* |} + line <- {[^%nl]*} +]], { + nl = P("\r") ^ -1 * P("\n") +}) LINE_STARTS = setmetatable({ }, { __mode = "k", __index = function(self, k) @@ -801,9 +806,10 @@ do else local nomsu = Nomsu(tree.source) local next_space = "" - local last_colon = nil + local line_len, last_colon = 0, nil for i, bit in ipairs(tree) do if type(bit) == "string" then + line_len = line_len + #next_space + #bit nomsu:append(next_space, bit) next_space = " " else @@ -815,18 +821,21 @@ do else arg_nomsu = self:tree_to_nomsu(bit, true) end - if arg_nomsu and #arg_nomsu < MAX_LINE then + if arg_nomsu and line_len + #tostring(arg_nomsu) < MAX_LINE then if bit.type == "Action" then if can_use_colon and i > 1 then nomsu:append(match(next_space, "[^ ]*"), ": ", arg_nomsu) next_space = "\n.." + line_len = 2 last_colon = i else nomsu:append(next_space, "(", arg_nomsu, ")") + line_len = line_len + #next_space + 2 + #tostring(arg_nomsu) next_space = " " end else nomsu:append(next_space, arg_nomsu) + line_len = line_len + #next_space + #tostring(arg_nomsu) next_space = " " end else @@ -846,6 +855,7 @@ do end nomsu:append(next_space, arg_nomsu) next_space = "\n.." + line_len = 2 end if next_space == " " and #(match(tostring(nomsu), "[^\n]*$")) > MAX_LINE then next_space = "\n.." @@ -915,9 +925,38 @@ do return inline_version end local nomsu = Nomsu(tree.source, '".."\n ') - for i, bit in ipairs(self) do + for i, bit in ipairs(tree) do if type(bit) == 'string' then - nomsu:append((gsub(gsub(bit, "\\", "\\\\"), "\n", "\\n"))) + local bit_lines = get_lines:match(bit) + for j, line in ipairs(bit_lines) do + if j > 1 then + nomsu:append("\n ") + end + if #line > 1.25 * MAX_LINE then + local remainder = line + while #remainder > 0 do + local split = find(remainder, " ", MAX_LINE, true) + if split then + local chunk + chunk, remainder = sub(remainder, 1, split), sub(remainder, split + 1, -1) + nomsu:append(chunk) + elseif #remainder > 1.75 * MAX_LINE then + split = math.floor(1.5 * MAX_LINE) + local chunk + chunk, remainder = sub(remainder, 1, split), sub(remainder, split + 1, -1) + nomsu:append(chunk) + else + nomsu:append(remainder) + break + end + if #remainder > 0 then + nomsu:append("\\\n ..") + end + end + else + nomsu:append(line) + end + end else local interp_nomsu = self:tree_to_nomsu(bit, true) if interp_nomsu then @@ -926,12 +965,12 @@ do end nomsu:append("\\", interp_nomsu) else - interp_nomsu = self:tree_to_nomsu(bit) + interp_nomsu = assert(self:tree_to_nomsu(bit)) if not (interp_nomsu) then return nil end nomsu:append("\\\n ", interp_nomsu) - if i < #self then + if i < #tree then nomsu:append("\n ..") end end @@ -30,7 +30,7 @@ colors = setmetatable({}, {__index:->""}) export colored colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..tostring(msg or '')..colors.reset)}) {:insert, :remove, :concat} = table -{:match, :sub, :rep, :gsub, :format, :byte, :match} = string +{:match, :sub, :rep, :gsub, :format, :byte, :match, :find} = string debug_getinfo = debug.getinfo {:Nomsu, :Lua, :Source} = require "code_obj" STDIN, STDOUT, STDERR = "/dev/fd/0", "/dev/fd/1", "/dev/fd/2" @@ -89,6 +89,10 @@ line_counter = re.compile([[ lines <- {| line (%nl line)* |} line <- {} (!%nl .)* ]], nl:P("\r")^-1 * P("\n")) +get_lines = re.compile([[ + lines <- {| line (%nl line)* |} + line <- {[^%nl]*} +]], nl:P("\r")^-1 * P("\n")) -- Mapping from line number -> character offset export LINE_STARTS -- LINE_STARTS is a mapping from strings to a table that maps line number to character positions @@ -668,10 +672,10 @@ class NomsuCompiler else nomsu = Nomsu(tree.source) next_space = "" - -- TODO: track line length as we go and use 80-that instead of 80 for wrapping - last_colon = nil + line_len, last_colon = 0, nil for i,bit in ipairs tree if type(bit) == "string" + line_len += #next_space + #bit nomsu\append next_space, bit next_space = " " else @@ -679,17 +683,20 @@ class NomsuCompiler elseif bit.type == "Block" then nil else @tree_to_nomsu(bit,true) - if arg_nomsu and #arg_nomsu < MAX_LINE + if arg_nomsu and line_len + #tostring(arg_nomsu) < MAX_LINE if bit.type == "Action" if can_use_colon and i > 1 nomsu\append match(next_space,"[^ ]*"), ": ", arg_nomsu next_space = "\n.." + line_len = 2 last_colon = i else nomsu\append next_space, "(", arg_nomsu, ")" + line_len += #next_space + 2 + #tostring(arg_nomsu) next_space = " " else nomsu\append next_space, arg_nomsu + line_len += #next_space + #tostring(arg_nomsu) next_space = " " else arg_nomsu = @tree_to_nomsu(bit, nil, true) @@ -705,6 +712,7 @@ class NomsuCompiler next_space = "" nomsu\append next_space, arg_nomsu next_space = "\n.." + line_len = 2 if next_space == " " and #(match(tostring(nomsu),"[^\n]*$")) > MAX_LINE next_space = "\n.." @@ -758,10 +766,28 @@ class NomsuCompiler if inline_version and #inline_version <= MAX_LINE return inline_version nomsu = Nomsu(tree.source, '".."\n ') - for i, bit in ipairs @ + for i, bit in ipairs tree if type(bit) == 'string' - -- TODO: unescape better? - nomsu\append (gsub(gsub(bit,"\\","\\\\"),"\n","\\n")) + bit_lines = get_lines\match(bit) + for j, line in ipairs bit_lines + if j > 1 then nomsu\append "\n " + if #line > 1.25*MAX_LINE + remainder = line + while #remainder > 0 + split = find(remainder, " ", MAX_LINE, true) + if split + chunk, remainder = sub(remainder, 1, split), sub(remainder, split+1, -1) + nomsu\append chunk + elseif #remainder > 1.75*MAX_LINE + split = math.floor(1.5*MAX_LINE) + chunk, remainder = sub(remainder, 1, split), sub(remainder, split+1, -1) + nomsu\append chunk + else + nomsu\append remainder + break + if #remainder > 0 then nomsu\append "\\\n .." + else + nomsu\append line else interp_nomsu = @tree_to_nomsu(bit, true) if interp_nomsu @@ -769,10 +795,10 @@ class NomsuCompiler interp_nomsu\parenthesize! nomsu\append "\\", interp_nomsu else - interp_nomsu = @tree_to_nomsu(bit) + interp_nomsu = assert(@tree_to_nomsu(bit)) return nil unless interp_nomsu nomsu\append "\\\n ", interp_nomsu - if i < #@ + if i < #tree nomsu\append "\n .." return nomsu |
