Improvements to nomsu codegen.
This commit is contained in:
parent
0442c8dd21
commit
39a0121856
11
code_obj.lua
11
code_obj.lua
@ -103,9 +103,18 @@ do
|
||||
if trailing_text then
|
||||
self.current_indent = #spaces
|
||||
self.trailing_line_len = #trailing_text
|
||||
else
|
||||
self.trailing_line_len = self.trailing_line_len + #b
|
||||
end
|
||||
else
|
||||
self.trailing_line_len = math.min(self.trailing_line_len + #tostring(b), b.trailing_line_len)
|
||||
do
|
||||
local trailing_text = match(tostring(b), "\n([^\n]*)$")
|
||||
if trailing_text then
|
||||
self.trailing_line_len = #trailing_text + self.current_indent
|
||||
else
|
||||
self.trailing_line_len = self.trailing_line_len + #tostring(b)
|
||||
end
|
||||
end
|
||||
if self.current_indent ~= 0 then
|
||||
indents[#bits] = self.current_indent
|
||||
end
|
||||
|
@ -72,8 +72,11 @@ class Code
|
||||
if trailing_text
|
||||
@current_indent = #spaces
|
||||
@trailing_line_len = #trailing_text
|
||||
else @trailing_line_len += #b
|
||||
else
|
||||
@trailing_line_len = math.min(@trailing_line_len + #tostring(b), b.trailing_line_len)
|
||||
if trailing_text = match(tostring(b), "\n([^\n]*)$")
|
||||
@trailing_line_len = #trailing_text + @current_indent
|
||||
else @trailing_line_len += #tostring(b)
|
||||
if @current_indent != 0
|
||||
indents[#bits] = @current_indent
|
||||
@__str = nil
|
||||
|
@ -775,11 +775,9 @@ do
|
||||
opts.consumed_comments = options.consumed_comments
|
||||
return self:tree_to_nomsu(t, opts)
|
||||
end
|
||||
local inline
|
||||
inline = options.inline
|
||||
local _exp_0 = tree.type
|
||||
if "FileChunks" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
error("Cannot inline a FileChunks")
|
||||
end
|
||||
local nomsu = NomsuCode(tree.source, pop_comments(tree.source.start))
|
||||
@ -795,11 +793,11 @@ do
|
||||
nomsu:append(pop_comments(tree.source.stop, '\n'))
|
||||
return nomsu
|
||||
elseif "Action" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
local nomsu = NomsuCode(tree.source)
|
||||
for i, bit in ipairs(tree) do
|
||||
if type(bit) == "string" then
|
||||
if i > 1 then
|
||||
if i > 1 and not (type(tree[i - 1]) == 'string' and Parser.is_operator(bit) ~= Parser.is_operator(tree[i - 1])) then
|
||||
nomsu:append(" ")
|
||||
end
|
||||
nomsu:append(bit)
|
||||
@ -822,13 +820,16 @@ do
|
||||
return nomsu
|
||||
else
|
||||
local pos = tree.source.start
|
||||
local nomsu = NomsuCode(tree.source, pop_comments(pos))
|
||||
local nomsu = NomsuCode(tree.source, pop_comments(pos, '\n'))
|
||||
local next_space = ""
|
||||
for i, bit in ipairs(tree) do
|
||||
if match(next_space, '\n') then
|
||||
nomsu:append(pop_comments(pos, '\n'))
|
||||
end
|
||||
if type(bit) == "string" then
|
||||
if next_space == ' ' and (type(tree[i - 1]) == 'string' and Parser.is_operator(bit) ~= Parser.is_operator(tree[i - 1])) then
|
||||
next_space = ''
|
||||
end
|
||||
nomsu:append(next_space, bit)
|
||||
next_space = " "
|
||||
else
|
||||
@ -877,7 +878,7 @@ do
|
||||
local nomsu = NomsuCode(tree.source, "\\(", assert(recurse(tree[1], {
|
||||
inline = true
|
||||
})), ")")
|
||||
if inline or #tostring(nomsu) <= MAX_LINE then
|
||||
if options.inline or #tostring(nomsu) <= MAX_LINE then
|
||||
return nomsu
|
||||
end
|
||||
nomsu = assert(recurse(tree[1]))
|
||||
@ -888,7 +889,7 @@ do
|
||||
return NomsuCode(tree.source, "\\(..)\n ", pop_comments(tree.source.start), nomsu)
|
||||
end
|
||||
elseif "Block" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
local nomsu = NomsuCode(tree.source, ":")
|
||||
for i, line in ipairs(tree) do
|
||||
nomsu:append(i == 1 and " " or "; ")
|
||||
@ -900,7 +901,7 @@ do
|
||||
end
|
||||
local nomsu = NomsuCode(tree.source, pop_comments(tree.source.start))
|
||||
for i, line in ipairs(tree) do
|
||||
nomsu:append(pop_comments(line.source.start))
|
||||
nomsu:append(pop_comments(line.source.start, '\n'))
|
||||
line = assert(recurse(line), "Could not convert line to nomsu")
|
||||
nomsu:append(line)
|
||||
if i < #tree then
|
||||
@ -913,7 +914,7 @@ do
|
||||
nomsu:append(pop_comments(tree.source.stop, '\n'))
|
||||
return options.top and nomsu or NomsuCode(tree.source, ":\n ", nomsu)
|
||||
elseif "Text" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
local make_text
|
||||
make_text = function(tree)
|
||||
local nomsu = NomsuCode(tree.source)
|
||||
@ -940,7 +941,7 @@ do
|
||||
local inline_version = recurse(tree, {
|
||||
inline = true
|
||||
})
|
||||
if inline_version and #inline_version <= MAX_LINE then
|
||||
if inline_version and #tostring(inline_version) <= MAX_LINE then
|
||||
return inline_version
|
||||
end
|
||||
local make_text
|
||||
@ -1006,7 +1007,7 @@ do
|
||||
return NomsuCode(tree.source, '".."\n ', make_text(tree))
|
||||
end
|
||||
elseif "List" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
local nomsu = NomsuCode(tree.source, "[")
|
||||
for i, item in ipairs(tree) do
|
||||
if i > 1 then
|
||||
@ -1063,7 +1064,7 @@ do
|
||||
return NomsuCode(tree.source, "[..]\n ", nomsu)
|
||||
end
|
||||
elseif "Dict" == _exp_0 then
|
||||
if inline then
|
||||
if options.inline then
|
||||
local nomsu = NomsuCode(tree.source, "{")
|
||||
for i, entry in ipairs(tree) do
|
||||
if i > 1 then
|
||||
@ -1139,7 +1140,7 @@ do
|
||||
if value.type == "Block" then
|
||||
value_nomsu:parenthesize()
|
||||
end
|
||||
if inline or #tostring(key_nomsu) + 2 + #tostring(value_nomsu) <= MAX_LINE then
|
||||
if options.inline or #tostring(key_nomsu) + 2 + #tostring(value_nomsu) <= MAX_LINE then
|
||||
return NomsuCode(tree.source, key_nomsu, ": ", value_nomsu)
|
||||
end
|
||||
value_nomsu = recurse(value)
|
||||
|
@ -505,10 +505,9 @@ with NomsuCompiler
|
||||
opts.consumed_comments = options.consumed_comments
|
||||
return @tree_to_nomsu(t, opts)
|
||||
|
||||
{:inline} = options
|
||||
switch tree.type
|
||||
when "FileChunks"
|
||||
error("Cannot inline a FileChunks") if inline
|
||||
error("Cannot inline a FileChunks") if options.inline
|
||||
nomsu = NomsuCode(tree.source, pop_comments(tree.source.start))
|
||||
for i, chunk in ipairs tree
|
||||
nomsu\append "\n\n#{("~")\rep(80)}\n\n" if i > 1
|
||||
@ -518,11 +517,11 @@ with NomsuCompiler
|
||||
return nomsu
|
||||
|
||||
when "Action"
|
||||
if inline
|
||||
if options.inline
|
||||
nomsu = NomsuCode(tree.source)
|
||||
for i,bit in ipairs tree
|
||||
if type(bit) == "string"
|
||||
nomsu\append " " if i > 1
|
||||
nomsu\append " " if i > 1 and not (type(tree[i-1]) == 'string' and Parser.is_operator(bit) != Parser.is_operator(tree[i-1]))
|
||||
nomsu\append bit
|
||||
else
|
||||
arg_nomsu = recurse(bit,inline:true)
|
||||
@ -535,12 +534,14 @@ with NomsuCompiler
|
||||
return nomsu
|
||||
else
|
||||
pos = tree.source.start
|
||||
nomsu = NomsuCode(tree.source, pop_comments(pos))
|
||||
nomsu = NomsuCode(tree.source, pop_comments(pos, '\n'))
|
||||
next_space = ""
|
||||
for i,bit in ipairs tree
|
||||
if match(next_space, '\n')
|
||||
nomsu\append pop_comments(pos, '\n')
|
||||
if type(bit) == "string"
|
||||
if next_space == ' ' and (type(tree[i-1]) == 'string' and Parser.is_operator(bit) != Parser.is_operator(tree[i-1]))
|
||||
next_space = ''
|
||||
nomsu\append next_space, bit
|
||||
next_space = " "
|
||||
else
|
||||
@ -573,7 +574,7 @@ with NomsuCompiler
|
||||
|
||||
when "EscapedNomsu"
|
||||
nomsu = NomsuCode(tree.source, "\\(", assert(recurse(tree[1], inline:true)), ")")
|
||||
if inline or #tostring(nomsu) <= MAX_LINE
|
||||
if options.inline or #tostring(nomsu) <= MAX_LINE
|
||||
return nomsu
|
||||
nomsu = assert recurse(tree[1])
|
||||
switch tree[1].type
|
||||
@ -583,7 +584,7 @@ with NomsuCompiler
|
||||
return NomsuCode tree.source, "\\(..)\n ", pop_comments(tree.source.start), nomsu
|
||||
|
||||
when "Block"
|
||||
if inline
|
||||
if options.inline
|
||||
nomsu = NomsuCode(tree.source, ":")
|
||||
for i,line in ipairs tree
|
||||
nomsu\append(i == 1 and " " or "; ")
|
||||
@ -591,7 +592,7 @@ with NomsuCompiler
|
||||
return nomsu
|
||||
nomsu = NomsuCode(tree.source, pop_comments(tree.source.start))
|
||||
for i, line in ipairs tree
|
||||
nomsu\append pop_comments(line.source.start)
|
||||
nomsu\append pop_comments(line.source.start, '\n')
|
||||
line = assert(recurse(line), "Could not convert line to nomsu")
|
||||
nomsu\append line
|
||||
if i < #tree
|
||||
@ -602,7 +603,7 @@ with NomsuCompiler
|
||||
return options.top and nomsu or NomsuCode(tree.source, ":\n ", nomsu)
|
||||
|
||||
when "Text"
|
||||
if inline
|
||||
if options.inline
|
||||
make_text = (tree)->
|
||||
nomsu = NomsuCode(tree.source)
|
||||
for bit in *tree
|
||||
@ -620,7 +621,7 @@ with NomsuCompiler
|
||||
return NomsuCode(tree.source, '"', make_text(tree), '"')
|
||||
else
|
||||
inline_version = recurse(tree, inline:true)
|
||||
if inline_version and #inline_version <= MAX_LINE
|
||||
if inline_version and #tostring(inline_version) <= MAX_LINE
|
||||
return inline_version
|
||||
make_text = (tree)->
|
||||
nomsu = NomsuCode(tree.source)
|
||||
@ -664,7 +665,7 @@ with NomsuCompiler
|
||||
return NomsuCode(tree.source, '".."\n ', make_text(tree))
|
||||
|
||||
when "List"
|
||||
if inline
|
||||
if options.inline
|
||||
nomsu = NomsuCode(tree.source, "[")
|
||||
for i, item in ipairs tree
|
||||
nomsu\append ", " if i > 1
|
||||
@ -699,7 +700,7 @@ with NomsuCompiler
|
||||
return NomsuCode(tree.source, "[..]\n ", nomsu)
|
||||
|
||||
when "Dict"
|
||||
if inline
|
||||
if options.inline
|
||||
nomsu = NomsuCode(tree.source, "{")
|
||||
for i, entry in ipairs tree
|
||||
nomsu\append ", " if i > 1
|
||||
@ -742,7 +743,7 @@ with NomsuCompiler
|
||||
else NomsuCode(tree.source, "")
|
||||
assert(value.type != "Block", "Didn't expect to find a Block as a value in a dict")
|
||||
value_nomsu\parenthesize! if value.type == "Block"
|
||||
if inline or #tostring(key_nomsu) + 2 + #tostring(value_nomsu) <= MAX_LINE
|
||||
if options.inline or #tostring(key_nomsu) + 2 + #tostring(value_nomsu) <= MAX_LINE
|
||||
return NomsuCode tree.source, key_nomsu, ": ", value_nomsu
|
||||
value_nomsu = recurse(value)
|
||||
if value.type == "List" or value.type == "Dict" or value.type == "Text"
|
||||
|
@ -229,4 +229,7 @@ Parser.parse = function(nomsu_code, source, version)
|
||||
tree.version = userdata.version
|
||||
return tree
|
||||
end
|
||||
Parser.is_operator = function(s)
|
||||
return not not (NOMSU_DEFS.operator_char ^ 1):match(s)
|
||||
end
|
||||
return Parser
|
||||
|
@ -139,4 +139,7 @@ Parser.parse = (nomsu_code, source=nil, version=nil)->
|
||||
tree.version = userdata.version
|
||||
return tree
|
||||
|
||||
Parser.is_operator = (s)->
|
||||
return not not (NOMSU_DEFS.operator_char^1)\match(s)
|
||||
|
||||
return Parser
|
||||
|
Loading…
Reference in New Issue
Block a user