aboutsummaryrefslogtreecommitdiff
path: root/nomsu_compiler.lua
diff options
context:
space:
mode:
Diffstat (limited to 'nomsu_compiler.lua')
-rw-r--r--nomsu_compiler.lua101
1 files changed, 60 insertions, 41 deletions
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index ada6c12..6dbe4c4 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -607,7 +607,7 @@ do
end
local bit_lua = self:compile(bit)
if not (bit_lua.is_value) then
- local src = ' ' .. gsub(tostring(self:tree_to_nomsu(bit)), '\n', '\n ')
+ local src = ' ' .. gsub(tostring(recurse(bit)), '\n', '\n ')
local line = tostring(bit.source.filename) .. ":" .. tostring(files.get_line_number(files.read(bit.source.filename), bit.source.start))
self:compile_error(bit, "Cannot use:\n%s\nas a string interpolation value, since it's not an expression.")
end
@@ -719,27 +719,45 @@ do
end
NomsuCompiler.tree_to_nomsu = function(self, tree, options)
options = options or { }
- local comment_i = 0
- local pop_comments
- pop_comments = function(pos)
- if not (tree.comments) then
- return ''
- end
- local nomsu = NomsuCode(tree.source)
- for i = comment_i + 1, #tree.comments do
- comment_i = i
- local comment = tree.comments[i]
- if comment.source.start > pos then
- break
+ if not (options.pop_comments) then
+ local comments
+ do
+ local _accum_0 = { }
+ local _len_0 = 1
+ for p, c in pairs(tree.comments or { }) do
+ _accum_0[_len_0] = {
+ comment = c,
+ pos = p
+ }
+ _len_0 = _len_0 + 1
end
- nomsu:append("#" .. (gsub(comment[1], "\n", "\n ")) .. "\n")
+ comments = _accum_0
end
- if #nomsu.bits == 0 then
- return ''
+ table.sort(comments, function(a, b)
+ return a.pos < b.pos
+ end)
+ local comment_i = 1
+ options.pop_comments = function(pos)
+ local nomsu = NomsuCode(tree.source)
+ while comments[comment_i] and comments[comment_i].pos <= pos do
+ local comment = comments[comment_i].comment
+ nomsu:append("#" .. (gsub(comment, "\n", "\n ")) .. "\n")
+ if comment:match("^\n.") then
+ nomsu:append("\n")
+ end
+ comment_i = comment_i + 1
+ end
+ return #nomsu.bits == 0 and '' or nomsu
end
- return nomsu
end
- local inline, can_use_colon = options.inline, options.can_use_colon
+ local recurse
+ recurse = function(t, opts)
+ opts = opts or { }
+ opts.pop_comments = options.pop_comments
+ return self:tree_to_nomsu(t, opts)
+ end
+ local inline, can_use_colon, pop_comments
+ inline, can_use_colon, pop_comments = options.inline, options.can_use_colon, options.pop_comments
local _exp_0 = tree.type
if "File" == _exp_0 then
if inline then
@@ -751,8 +769,9 @@ do
nomsu:append("\n\n" .. tostring(("~"):rep(80)) .. "\n\n")
end
nomsu:append(pop_comments(chunk.source.start))
- nomsu:append(self:tree_to_nomsu(chunk))
+ nomsu:append(recurse(chunk))
end
+ nomsu:append(pop_comments(tree.source.stop))
return nomsu
elseif "Action" == _exp_0 then
if inline then
@@ -764,7 +783,7 @@ do
end
nomsu:append(bit)
else
- local arg_nomsu = self:tree_to_nomsu(bit, {
+ local arg_nomsu = recurse(bit, {
inline = true
})
if not (arg_nomsu) then
@@ -800,7 +819,7 @@ do
elseif bit.type == "Block" then
arg_nomsu = nil
else
- arg_nomsu = self:tree_to_nomsu(bit, {
+ arg_nomsu = recurse(bit, {
inline = true
})
end
@@ -822,7 +841,7 @@ do
next_space = " "
end
else
- arg_nomsu = self:tree_to_nomsu(bit, {
+ arg_nomsu = recurse(bit, {
can_use_colon = true
})
if not (arg_nomsu) then
@@ -850,11 +869,11 @@ do
return nomsu
end
elseif "EscapedNomsu" == _exp_0 then
- local nomsu = self:tree_to_nomsu(tree[1], {
+ local nomsu = recurse(tree[1], {
inline = true
})
if nomsu == nil and not inline then
- nomsu = self:tree_to_nomsu(tree[1])
+ nomsu = recurse(tree[1])
return nomsu and NomsuCode(tree.source, "\\:\n ", pop_comments(tree.source.start), nomsu)
end
return nomsu and NomsuCode(tree.source, "\\(", nomsu, ")")
@@ -865,7 +884,7 @@ do
if i > 1 then
nomsu:append("; ")
end
- local line_nomsu = self:tree_to_nomsu(line, {
+ local line_nomsu = recurse(line, {
inline = true
})
if not (line_nomsu) then
@@ -878,7 +897,7 @@ do
local nomsu = NomsuCode(tree.source)
for i, line in ipairs(tree) do
nomsu:append(pop_comments(line.source.start))
- line = assert(self:tree_to_nomsu(line, {
+ line = assert(recurse(line, {
can_use_colon = true
}), "Could not convert line to nomsu")
nomsu:append(line)
@@ -902,7 +921,7 @@ do
elseif bit.type == "Text" then
nomsu:append(make_text(bit))
else
- local interp_nomsu = assert(self:tree_to_nomsu(bit, {
+ local interp_nomsu = assert(recurse(bit, {
inline = true
}))
if bit.type ~= "Var" and bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then
@@ -915,7 +934,7 @@ do
end
return NomsuCode(tree.source, '"', make_text(tree), '"')
else
- local inline_version = self:tree_to_nomsu(tree, {
+ local inline_version = recurse(tree, {
inline = true
})
if inline_version and #inline_version <= MAX_LINE then
@@ -959,7 +978,7 @@ do
elseif bit.type == "Text" then
nomsu:append(make_text(bit))
else
- local interp_nomsu = self:tree_to_nomsu(bit, {
+ local interp_nomsu = recurse(bit, {
inline = true
})
if interp_nomsu then
@@ -968,7 +987,7 @@ do
end
nomsu:append("\\", interp_nomsu)
else
- interp_nomsu = assert(self:tree_to_nomsu(bit))
+ interp_nomsu = assert(recurse(bit))
if not (interp_nomsu) then
return nil
end
@@ -987,7 +1006,7 @@ do
if inline then
local nomsu = NomsuCode(tree.source, "[")
for i, item in ipairs(tree) do
- local item_nomsu = self:tree_to_nomsu(item, {
+ local item_nomsu = recurse(item, {
inline = true
})
if not (item_nomsu) then
@@ -1001,7 +1020,7 @@ do
nomsu:append("]")
return nomsu
else
- local inline_version = self:tree_to_nomsu(tree, {
+ local inline_version = recurse(tree, {
inline = true
})
if inline_version and #inline_version <= MAX_LINE then
@@ -1016,7 +1035,7 @@ do
line_comments = ''
end
for i, item in ipairs(tree) do
- local item_nomsu = self:tree_to_nomsu(item, {
+ local item_nomsu = recurse(item, {
inline = true
})
if item_nomsu and #tostring(line) + #", " + #item_nomsu <= MAX_LINE then
@@ -1026,7 +1045,7 @@ do
line:append(item_nomsu)
else
if not (item_nomsu) then
- item_nomsu = self:tree_to_nomsu(item)
+ item_nomsu = recurse(item)
if not (item_nomsu) then
return nil
end
@@ -1055,7 +1074,7 @@ do
if inline then
local nomsu = NomsuCode(tree.source, "{")
for i, entry in ipairs(tree) do
- local entry_nomsu = self:tree_to_nomsu(entry, {
+ local entry_nomsu = recurse(entry, {
inline = true
})
if not (entry_nomsu) then
@@ -1069,7 +1088,7 @@ do
nomsu:append("}")
return nomsu
else
- local inline_version = self:tree_to_nomsu(tree, {
+ local inline_version = recurse(tree, {
inline = true
})
if inline_version then
@@ -1084,7 +1103,7 @@ do
line_comments = ''
end
for i, entry in ipairs(tree) do
- local entry_nomsu = self:tree_to_nomsu(entry)
+ local entry_nomsu = recurse(entry)
if not (entry_nomsu) then
return nil
end
@@ -1116,7 +1135,7 @@ do
end
elseif "DictEntry" == _exp_0 then
local key, value = tree[1], tree[2]
- local key_nomsu = self:tree_to_nomsu(key, {
+ local key_nomsu = recurse(key, {
inline = true
})
if not (key_nomsu) then
@@ -1127,7 +1146,7 @@ do
end
local value_nomsu
if value then
- value_nomsu = self:tree_to_nomsu(value, {
+ value_nomsu = recurse(value, {
inline = true
})
else
@@ -1140,7 +1159,7 @@ do
if inline then
return nil
end
- value_nomsu = self:tree_to_nomsu(value)
+ value_nomsu = recurse(value)
if not (value_nomsu) then
return nil
end
@@ -1159,7 +1178,7 @@ do
end
end
if not (bit_nomsu) then
- bit_nomsu = self:tree_to_nomsu(bit, {
+ bit_nomsu = recurse(bit, {
inline = true
})
end