aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-07-19 21:09:44 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-07-19 21:10:19 -0700
commit9d6627aee52d764f14bab55fee202af84baf0065 (patch)
tree4f26aa76447e0223235436de1635e5d30e3a7f27
parent47db74229deaf281ad02c8305c4e4d4c8dbd3d7d (diff)
Hopefully last correctness fix for trailing_line_len(), now it's just
dumb and slow, but correct. Also simplified recursion options a bit for tree_to_nomsu()
-rw-r--r--code_obj.lua30
-rw-r--r--code_obj.moon20
-rw-r--r--nomsu_compiler.lua46
-rw-r--r--nomsu_compiler.moon33
4 files changed, 68 insertions, 61 deletions
diff --git a/code_obj.lua b/code_obj.lua
index 470cd11..51d9b00 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -113,28 +113,7 @@ do
end,
trailing_line_len = function(self)
if self._trailing_line_len == nil then
- local bits, match = self.bits, string.match
- local len = 0
- for i = #bits, 1, -1 do
- local b = bits[i]
- if type(b) == 'string' then
- do
- local line = match(b, "\n([^\n]*)$")
- if line then
- len = len + #line
- break
- else
- len = len + #b
- end
- end
- else
- len = len + b:trailing_line_len()
- if b:is_multiline() then
- break
- end
- end
- end
- self._trailing_line_len = len
+ self._trailing_line_len = #tostring(self):match("[^\n]*$")
end
return self._trailing_line_len
end,
@@ -145,7 +124,12 @@ do
local _list_0 = self.bits
for _index_0 = 1, #_list_0 do
local b = _list_0[_index_0]
- if type(b) ~= 'string' or match(b, "\n") then
+ if type(b) == 'string' then
+ if match(b, '\n') then
+ self._is_multiline = true
+ break
+ end
+ elseif b:is_multiline() then
self._is_multiline = true
break
end
diff --git a/code_obj.moon b/code_obj.moon
index 35d1027..3d376bb 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -73,19 +73,7 @@ class Code
trailing_line_len: =>
if @_trailing_line_len == nil
- bits, match = @bits, string.match
- len = 0
- for i=#bits,1,-1
- b = bits[i]
- if type(b) == 'string'
- if line = match(b, "\n([^\n]*)$")
- len += #line
- break
- else len += #b
- else
- len += b\trailing_line_len!
- break if b\is_multiline!
- @_trailing_line_len = len
+ @_trailing_line_len = #tostring(@)\match("[^\n]*$")
return @_trailing_line_len
is_multiline: =>
@@ -93,7 +81,11 @@ class Code
match = string.match
@_is_multiline = false
for b in *@bits
- if type(b) != 'string' or match(b, "\n")
+ if type(b) == 'string'
+ if match(b, '\n')
+ @_is_multiline = true
+ break
+ elseif b\is_multiline!
@_is_multiline = true
break
return @_is_multiline
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index bdaa726..2ab57fe 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -874,9 +874,11 @@ do
return error("Unknown type: " .. tostring(tree.type))
end
end
- NomsuCompiler.tree_to_nomsu = function(self, tree, options)
- options = options or { }
- options.consumed_comments = options.consumed_comments or { }
+ NomsuCompiler.tree_to_nomsu = function(self, tree, consumed_comments)
+ if consumed_comments == nil then
+ consumed_comments = nil
+ end
+ consumed_comments = consumed_comments or { }
local pop_comments
pop_comments = function(pos, prefix, suffix)
if prefix == nil then
@@ -891,7 +893,7 @@ do
local _list_0 = t.comments
for _index_0 = 1, #_list_0 do
local c = _list_0[_index_0]
- if not (options.consumed_comments[c]) then
+ if not (consumed_comments[c]) then
coroutine.yield(c)
end
end
@@ -910,7 +912,7 @@ do
if comment.pos > pos then
break
end
- options.consumed_comments[comment] = true
+ consumed_comments[comment] = true
nomsu:append("#" .. (gsub(comment.comment, "\n", "\n ")) .. "\n")
if comment.comment:match("^\n.") then
nomsu:append("\n")
@@ -924,10 +926,8 @@ do
return nomsu
end
local recurse
- recurse = function(t, opts)
- opts = opts or { }
- opts.consumed_comments = options.consumed_comments
- return self:tree_to_nomsu(t, opts)
+ recurse = function(t)
+ return self:tree_to_nomsu(t, consumed_comments)
end
local _exp_0 = tree.type
if "FileChunks" == _exp_0 then
@@ -937,9 +937,19 @@ do
nomsu:append("\n\n" .. tostring(("~"):rep(80)) .. "\n\n")
end
nomsu:append(pop_comments(chunk.source.start))
- nomsu:append(recurse(chunk, {
- top = true
- }))
+ if chunk.type == "Block" then
+ for j, line in ipairs(chunk) do
+ nomsu:append(pop_comments(line.source.start, '\n'))
+ local line_nomsu = recurse(line)
+ nomsu:append(line_nomsu)
+ if j < #chunk then
+ nomsu:append(line_nomsu:is_multiline() and "\n\n" or "\n")
+ end
+ end
+ nomsu:append(pop_comments(tree.source.stop, '\n'))
+ else
+ nomsu:append(recurse(chunk))
+ end
end
nomsu:append(pop_comments(tree.source.stop, '\n'))
return nomsu
@@ -1023,7 +1033,7 @@ do
end
end
nomsu:append(pop_comments(tree.source.stop, '\n'))
- return options.top and nomsu or NomsuCode(tree.source, ":\n ", nomsu)
+ return NomsuCode(tree.source, ":\n ", nomsu)
elseif "Text" == _exp_0 then
local inline_version = self:tree_to_inline_nomsu(tree)
if inline_version and #tostring(inline_version) <= MAX_LINE then
@@ -1114,6 +1124,11 @@ do
nomsu:append(", ")
end
nomsu:append(item_nomsu)
+ elseif #tostring(item_nomsu) <= MAX_LINE then
+ if nomsu:trailing_line_len() > 0 then
+ nomsu:append("\n")
+ end
+ nomsu:append(item_nomsu)
else
item_nomsu = recurse(item)
local _exp_1 = item.type
@@ -1146,6 +1161,11 @@ do
nomsu:append(", ")
end
nomsu:append(item_nomsu)
+ elseif #tostring(item_nomsu) <= MAX_LINE then
+ if nomsu:trailing_line_len() > 0 then
+ nomsu:append("\n")
+ end
+ nomsu:append(item_nomsu)
else
item_nomsu = recurse(item)
local _exp_1 = item.type
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 1c9fbca..6f9c9d5 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -592,20 +592,19 @@ with NomsuCompiler
else
error("Unknown type: #{tree.type}")
- .tree_to_nomsu = (tree, options)=>
- options or= {}
- options.consumed_comments or= {}
+ .tree_to_nomsu = (tree, consumed_comments=nil)=>
+ consumed_comments or= {}
pop_comments = (pos, prefix='', suffix='')->
find_comments = (t)->
if t.comments and t.source.filename == tree.source.filename
for c in *t.comments
- coroutine.yield(c) unless options.consumed_comments[c]
+ coroutine.yield(c) unless consumed_comments[c]
for x in *t
find_comments(x) if AST.is_syntax_tree x
nomsu = NomsuCode(tree.source)
for comment in coroutine.wrap(-> find_comments(tree))
break if comment.pos > pos
- options.consumed_comments[comment] = true
+ consumed_comments[comment] = true
nomsu\append("#"..(gsub(comment.comment, "\n", "\n ")).."\n")
if comment.comment\match("^\n.") then nomsu\append("\n") -- for aesthetics
return '' if #nomsu.bits == 0
@@ -613,10 +612,8 @@ with NomsuCompiler
nomsu\append suffix
return nomsu
- recurse = (t, opts)->
- opts or= {}
- opts.consumed_comments = options.consumed_comments
- return @tree_to_nomsu(t, opts)
+ -- For concision:
+ recurse = (t)-> @tree_to_nomsu(t, consumed_comments)
switch tree.type
when "FileChunks"
@@ -624,7 +621,15 @@ with NomsuCompiler
for i, chunk in ipairs tree
nomsu\append "\n\n#{("~")\rep(80)}\n\n" if i > 1
nomsu\append pop_comments(chunk.source.start)
- nomsu\append recurse(chunk, top:true)
+ if chunk.type == "Block"
+ for j, line in ipairs chunk
+ nomsu\append pop_comments(line.source.start, '\n')
+ line_nomsu = recurse(line)
+ nomsu\append line_nomsu
+ nomsu\append(line_nomsu\is_multiline! and "\n\n" or "\n") if j < #chunk
+ nomsu\append pop_comments(tree.source.stop, '\n')
+ else
+ nomsu\append recurse(chunk)
nomsu\append pop_comments(tree.source.stop, '\n')
return nomsu
@@ -691,7 +696,7 @@ with NomsuCompiler
if i < #tree
nomsu\append(line_nomsu\is_multiline! and "\n\n" or "\n")
nomsu\append pop_comments(tree.source.stop, '\n')
- return options.top and nomsu or NomsuCode(tree.source, ":\n ", nomsu)
+ return NomsuCode(tree.source, ":\n ", nomsu)
when "Text"
inline_version = @tree_to_inline_nomsu(tree)
@@ -759,6 +764,9 @@ with NomsuCompiler
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
nomsu\append ", " if nomsu\trailing_line_len! > 0
nomsu\append item_nomsu
+ elseif #tostring(item_nomsu) <= MAX_LINE
+ nomsu\append "\n" if nomsu\trailing_line_len! > 0
+ nomsu\append item_nomsu
else
item_nomsu = recurse(item)
switch item.type
@@ -782,6 +790,9 @@ with NomsuCompiler
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
nomsu\append ", " if nomsu\trailing_line_len! > 0
nomsu\append item_nomsu
+ elseif #tostring(item_nomsu) <= MAX_LINE
+ nomsu\append "\n" if nomsu\trailing_line_len! > 0
+ nomsu\append item_nomsu
else
item_nomsu = recurse(item)
switch item.type