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()
This commit is contained in:
parent
47db74229d
commit
9d6627aee5
30
code_obj.lua
30
code_obj.lua
@ -113,28 +113,7 @@ do
|
|||||||
end,
|
end,
|
||||||
trailing_line_len = function(self)
|
trailing_line_len = function(self)
|
||||||
if self._trailing_line_len == nil then
|
if self._trailing_line_len == nil then
|
||||||
local bits, match = self.bits, string.match
|
self._trailing_line_len = #tostring(self):match("[^\n]*$")
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return self._trailing_line_len
|
return self._trailing_line_len
|
||||||
end,
|
end,
|
||||||
@ -145,7 +124,12 @@ do
|
|||||||
local _list_0 = self.bits
|
local _list_0 = self.bits
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
local b = _list_0[_index_0]
|
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
|
self._is_multiline = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -73,19 +73,7 @@ class Code
|
|||||||
|
|
||||||
trailing_line_len: =>
|
trailing_line_len: =>
|
||||||
if @_trailing_line_len == nil
|
if @_trailing_line_len == nil
|
||||||
bits, match = @bits, string.match
|
@_trailing_line_len = #tostring(@)\match("[^\n]*$")
|
||||||
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
|
|
||||||
return @_trailing_line_len
|
return @_trailing_line_len
|
||||||
|
|
||||||
is_multiline: =>
|
is_multiline: =>
|
||||||
@ -93,7 +81,11 @@ class Code
|
|||||||
match = string.match
|
match = string.match
|
||||||
@_is_multiline = false
|
@_is_multiline = false
|
||||||
for b in *@bits
|
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
|
@_is_multiline = true
|
||||||
break
|
break
|
||||||
return @_is_multiline
|
return @_is_multiline
|
||||||
|
@ -874,9 +874,11 @@ do
|
|||||||
return error("Unknown type: " .. tostring(tree.type))
|
return error("Unknown type: " .. tostring(tree.type))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
NomsuCompiler.tree_to_nomsu = function(self, tree, options)
|
NomsuCompiler.tree_to_nomsu = function(self, tree, consumed_comments)
|
||||||
options = options or { }
|
if consumed_comments == nil then
|
||||||
options.consumed_comments = options.consumed_comments or { }
|
consumed_comments = nil
|
||||||
|
end
|
||||||
|
consumed_comments = consumed_comments or { }
|
||||||
local pop_comments
|
local pop_comments
|
||||||
pop_comments = function(pos, prefix, suffix)
|
pop_comments = function(pos, prefix, suffix)
|
||||||
if prefix == nil then
|
if prefix == nil then
|
||||||
@ -891,7 +893,7 @@ do
|
|||||||
local _list_0 = t.comments
|
local _list_0 = t.comments
|
||||||
for _index_0 = 1, #_list_0 do
|
for _index_0 = 1, #_list_0 do
|
||||||
local c = _list_0[_index_0]
|
local c = _list_0[_index_0]
|
||||||
if not (options.consumed_comments[c]) then
|
if not (consumed_comments[c]) then
|
||||||
coroutine.yield(c)
|
coroutine.yield(c)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -910,7 +912,7 @@ do
|
|||||||
if comment.pos > pos then
|
if comment.pos > pos then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
options.consumed_comments[comment] = true
|
consumed_comments[comment] = true
|
||||||
nomsu:append("#" .. (gsub(comment.comment, "\n", "\n ")) .. "\n")
|
nomsu:append("#" .. (gsub(comment.comment, "\n", "\n ")) .. "\n")
|
||||||
if comment.comment:match("^\n.") then
|
if comment.comment:match("^\n.") then
|
||||||
nomsu:append("\n")
|
nomsu:append("\n")
|
||||||
@ -924,10 +926,8 @@ do
|
|||||||
return nomsu
|
return nomsu
|
||||||
end
|
end
|
||||||
local recurse
|
local recurse
|
||||||
recurse = function(t, opts)
|
recurse = function(t)
|
||||||
opts = opts or { }
|
return self:tree_to_nomsu(t, consumed_comments)
|
||||||
opts.consumed_comments = options.consumed_comments
|
|
||||||
return self:tree_to_nomsu(t, opts)
|
|
||||||
end
|
end
|
||||||
local _exp_0 = tree.type
|
local _exp_0 = tree.type
|
||||||
if "FileChunks" == _exp_0 then
|
if "FileChunks" == _exp_0 then
|
||||||
@ -937,9 +937,19 @@ do
|
|||||||
nomsu:append("\n\n" .. tostring(("~"):rep(80)) .. "\n\n")
|
nomsu:append("\n\n" .. tostring(("~"):rep(80)) .. "\n\n")
|
||||||
end
|
end
|
||||||
nomsu:append(pop_comments(chunk.source.start))
|
nomsu:append(pop_comments(chunk.source.start))
|
||||||
nomsu:append(recurse(chunk, {
|
if chunk.type == "Block" then
|
||||||
top = true
|
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
|
end
|
||||||
nomsu:append(pop_comments(tree.source.stop, '\n'))
|
nomsu:append(pop_comments(tree.source.stop, '\n'))
|
||||||
return nomsu
|
return nomsu
|
||||||
@ -1023,7 +1033,7 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
nomsu:append(pop_comments(tree.source.stop, '\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)
|
||||||
elseif "Text" == _exp_0 then
|
elseif "Text" == _exp_0 then
|
||||||
local inline_version = self:tree_to_inline_nomsu(tree)
|
local inline_version = self:tree_to_inline_nomsu(tree)
|
||||||
if inline_version and #tostring(inline_version) <= MAX_LINE then
|
if inline_version and #tostring(inline_version) <= MAX_LINE then
|
||||||
@ -1114,6 +1124,11 @@ do
|
|||||||
nomsu:append(", ")
|
nomsu:append(", ")
|
||||||
end
|
end
|
||||||
nomsu:append(item_nomsu)
|
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
|
else
|
||||||
item_nomsu = recurse(item)
|
item_nomsu = recurse(item)
|
||||||
local _exp_1 = item.type
|
local _exp_1 = item.type
|
||||||
@ -1146,6 +1161,11 @@ do
|
|||||||
nomsu:append(", ")
|
nomsu:append(", ")
|
||||||
end
|
end
|
||||||
nomsu:append(item_nomsu)
|
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
|
else
|
||||||
item_nomsu = recurse(item)
|
item_nomsu = recurse(item)
|
||||||
local _exp_1 = item.type
|
local _exp_1 = item.type
|
||||||
|
@ -592,20 +592,19 @@ with NomsuCompiler
|
|||||||
else
|
else
|
||||||
error("Unknown type: #{tree.type}")
|
error("Unknown type: #{tree.type}")
|
||||||
|
|
||||||
.tree_to_nomsu = (tree, options)=>
|
.tree_to_nomsu = (tree, consumed_comments=nil)=>
|
||||||
options or= {}
|
consumed_comments or= {}
|
||||||
options.consumed_comments or= {}
|
|
||||||
pop_comments = (pos, prefix='', suffix='')->
|
pop_comments = (pos, prefix='', suffix='')->
|
||||||
find_comments = (t)->
|
find_comments = (t)->
|
||||||
if t.comments and t.source.filename == tree.source.filename
|
if t.comments and t.source.filename == tree.source.filename
|
||||||
for c in *t.comments
|
for c in *t.comments
|
||||||
coroutine.yield(c) unless options.consumed_comments[c]
|
coroutine.yield(c) unless consumed_comments[c]
|
||||||
for x in *t
|
for x in *t
|
||||||
find_comments(x) if AST.is_syntax_tree x
|
find_comments(x) if AST.is_syntax_tree x
|
||||||
nomsu = NomsuCode(tree.source)
|
nomsu = NomsuCode(tree.source)
|
||||||
for comment in coroutine.wrap(-> find_comments(tree))
|
for comment in coroutine.wrap(-> find_comments(tree))
|
||||||
break if comment.pos > pos
|
break if comment.pos > pos
|
||||||
options.consumed_comments[comment] = true
|
consumed_comments[comment] = true
|
||||||
nomsu\append("#"..(gsub(comment.comment, "\n", "\n ")).."\n")
|
nomsu\append("#"..(gsub(comment.comment, "\n", "\n ")).."\n")
|
||||||
if comment.comment\match("^\n.") then nomsu\append("\n") -- for aesthetics
|
if comment.comment\match("^\n.") then nomsu\append("\n") -- for aesthetics
|
||||||
return '' if #nomsu.bits == 0
|
return '' if #nomsu.bits == 0
|
||||||
@ -613,10 +612,8 @@ with NomsuCompiler
|
|||||||
nomsu\append suffix
|
nomsu\append suffix
|
||||||
return nomsu
|
return nomsu
|
||||||
|
|
||||||
recurse = (t, opts)->
|
-- For concision:
|
||||||
opts or= {}
|
recurse = (t)-> @tree_to_nomsu(t, consumed_comments)
|
||||||
opts.consumed_comments = options.consumed_comments
|
|
||||||
return @tree_to_nomsu(t, opts)
|
|
||||||
|
|
||||||
switch tree.type
|
switch tree.type
|
||||||
when "FileChunks"
|
when "FileChunks"
|
||||||
@ -624,7 +621,15 @@ with NomsuCompiler
|
|||||||
for i, chunk in ipairs tree
|
for i, chunk in ipairs tree
|
||||||
nomsu\append "\n\n#{("~")\rep(80)}\n\n" if i > 1
|
nomsu\append "\n\n#{("~")\rep(80)}\n\n" if i > 1
|
||||||
nomsu\append pop_comments(chunk.source.start)
|
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')
|
nomsu\append pop_comments(tree.source.stop, '\n')
|
||||||
return nomsu
|
return nomsu
|
||||||
|
|
||||||
@ -691,7 +696,7 @@ with NomsuCompiler
|
|||||||
if i < #tree
|
if i < #tree
|
||||||
nomsu\append(line_nomsu\is_multiline! and "\n\n" or "\n")
|
nomsu\append(line_nomsu\is_multiline! and "\n\n" or "\n")
|
||||||
nomsu\append pop_comments(tree.source.stop, '\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"
|
when "Text"
|
||||||
inline_version = @tree_to_inline_nomsu(tree)
|
inline_version = @tree_to_inline_nomsu(tree)
|
||||||
@ -759,6 +764,9 @@ with NomsuCompiler
|
|||||||
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
|
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
|
||||||
nomsu\append ", " if nomsu\trailing_line_len! > 0
|
nomsu\append ", " if nomsu\trailing_line_len! > 0
|
||||||
nomsu\append item_nomsu
|
nomsu\append item_nomsu
|
||||||
|
elseif #tostring(item_nomsu) <= MAX_LINE
|
||||||
|
nomsu\append "\n" if nomsu\trailing_line_len! > 0
|
||||||
|
nomsu\append item_nomsu
|
||||||
else
|
else
|
||||||
item_nomsu = recurse(item)
|
item_nomsu = recurse(item)
|
||||||
switch item.type
|
switch item.type
|
||||||
@ -782,6 +790,9 @@ with NomsuCompiler
|
|||||||
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
|
if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE
|
||||||
nomsu\append ", " if nomsu\trailing_line_len! > 0
|
nomsu\append ", " if nomsu\trailing_line_len! > 0
|
||||||
nomsu\append item_nomsu
|
nomsu\append item_nomsu
|
||||||
|
elseif #tostring(item_nomsu) <= MAX_LINE
|
||||||
|
nomsu\append "\n" if nomsu\trailing_line_len! > 0
|
||||||
|
nomsu\append item_nomsu
|
||||||
else
|
else
|
||||||
item_nomsu = recurse(item)
|
item_nomsu = recurse(item)
|
||||||
switch item.type
|
switch item.type
|
||||||
|
Loading…
Reference in New Issue
Block a user