diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-17 14:12:11 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-17 14:12:51 -0700 |
| commit | 0442c8dd216f16c7371873a8e8fd8bf83d30dad0 (patch) | |
| tree | f3ca5f178b9851d27d95bf060f966e2398ef9a6a /parser.lua | |
| parent | be06fc096aa28358914bd6a76b4ea380a04b8873 (diff) | |
Overhaul of comment handling, plus a few fixes (e.g. a fix for indented
text that begins with a nomsu comment)
Diffstat (limited to 'parser.lua')
| -rw-r--r-- | parser.lua | 38 |
1 files changed, 37 insertions, 1 deletions
@@ -100,7 +100,6 @@ setmetatable(NOMSU_DEFS, { end end setmetatable(value, AST[key]) - value.comments = userdata.comments if value.__init then value:__init() end @@ -190,6 +189,43 @@ Parser.parse = function(nomsu_code, source, version) end error("Errors occurred while parsing (v" .. tostring(version) .. "):\n\n" .. table.concat(errors, "\n\n"), 0) end + local comments + do + local _accum_0 = { } + local _len_0 = 1 + for p, c in pairs(userdata.comments) do + _accum_0[_len_0] = { + comment = c, + pos = p + } + _len_0 = _len_0 + 1 + end + comments = _accum_0 + end + table.sort(comments, function(a, b) + return a.pos > b.pos + end) + local comment_i = 1 + local walk_tree + walk_tree = function(t) + local comment_buff = { } + while comments[#comments] and comments[#comments].pos <= t.source.start do + table.insert(comment_buff, table.remove(comments)) + end + for _index_0 = 1, #t do + local x = t[_index_0] + if AST.is_syntax_tree(x) then + walk_tree(x) + end + end + while comments[#comments] and comments[#comments].pos <= t.source.stop do + table.insert(comment_buff, table.remove(comments)) + end + if #comment_buff > 0 then + t.comments = comment_buff + end + end + walk_tree(tree) tree.version = userdata.version return tree end |
