diff options
Diffstat (limited to 'nomsu_compiler.moon')
| -rw-r--r-- | nomsu_compiler.moon | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon index 283fa52..0b330c8 100644 --- a/nomsu_compiler.moon +++ b/nomsu_compiler.moon @@ -383,7 +383,7 @@ with NomsuCompiler string_buffer = "" bit_lua = @compile(bit) unless bit_lua.is_value - src = ' '..gsub(tostring(@tree_to_nomsu(bit)), '\n','\n ') + src = ' '..gsub(tostring(recurse(bit)), '\n','\n ') line = "#{bit.source.filename}:#{files.get_line_number(files.read(bit.source.filename), bit.source.start)}" @compile_error bit, "Cannot use:\n%s\nas a string interpolation value, since it's not an expression." @@ -483,19 +483,25 @@ with NomsuCompiler .tree_to_nomsu = (tree, options)=> options or= {} - comment_i = 0 - pop_comments = (pos)-> - return '' unless tree.comments - nomsu = NomsuCode(tree.source) - for i=comment_i+1,#tree.comments - comment_i = i - comment = tree.comments[i] - break if comment.source.start > pos - nomsu\append "#"..(gsub(comment[1], "\n", "\n ")).."\n" - if #nomsu.bits == 0 then return '' - return nomsu - - inline, can_use_colon = options.inline, options.can_use_colon + unless options.pop_comments + comments = [{comment:c, pos:p} for p,c in pairs(tree.comments or {})] + table.sort comments, (a,b)-> a.pos < b.pos + comment_i = 1 + options.pop_comments = (pos)-> + nomsu = NomsuCode(tree.source) + while comments[comment_i] and comments[comment_i].pos <= pos + comment = comments[comment_i].comment + nomsu\append("#"..(gsub(comment, "\n", "\n ")).."\n") + if comment\match("^\n.") then nomsu\append("\n") -- for aesthetics + comment_i += 1 + return #nomsu.bits == 0 and '' or nomsu + + recurse = (t, opts)-> + opts or= {} + opts.pop_comments = options.pop_comments + return @tree_to_nomsu(t, opts) + + {:inline, :can_use_colon, :pop_comments} = options switch tree.type when "File" return nil if inline @@ -504,7 +510,8 @@ with NomsuCompiler if i > 1 nomsu\append "\n\n#{("~")\rep(80)}\n\n" nomsu\append pop_comments(chunk.source.start) - nomsu\append @tree_to_nomsu(chunk) + nomsu\append recurse(chunk) + nomsu\append pop_comments(tree.source.stop) return nomsu when "Action" @@ -516,7 +523,7 @@ with NomsuCompiler nomsu\append " " nomsu\append bit else - arg_nomsu = @tree_to_nomsu(bit,inline:true) + arg_nomsu = recurse(bit,inline:true) return nil unless arg_nomsu if bit.type == "Action" or bit.type == "Block" if bit.type == "Action" and i == #tree @@ -539,7 +546,7 @@ with NomsuCompiler else arg_nomsu = if last_colon == i-1 and bit.type == "Action" then nil elseif bit.type == "Block" then nil - else @tree_to_nomsu(bit,inline:true) + else recurse(bit,inline:true) if arg_nomsu and line_len + #tostring(arg_nomsu) < MAX_LINE if bit.type == "Action" @@ -557,7 +564,7 @@ with NomsuCompiler line_len += #next_space + #tostring(arg_nomsu) next_space = " " else - arg_nomsu = @tree_to_nomsu(bit, can_use_colon:true) + arg_nomsu = recurse(bit, can_use_colon:true) return nil unless arg_nomsu -- These types carry their own indentation if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" @@ -577,9 +584,9 @@ with NomsuCompiler return nomsu when "EscapedNomsu" - nomsu = @tree_to_nomsu(tree[1], inline:true) + nomsu = recurse(tree[1], inline:true) if nomsu == nil and not inline - nomsu = @tree_to_nomsu(tree[1]) + nomsu = recurse(tree[1]) return nomsu and NomsuCode tree.source, "\\:\n ", pop_comments(tree.source.start), nomsu return nomsu and NomsuCode tree.source, "\\(", nomsu, ")" @@ -589,14 +596,14 @@ with NomsuCompiler for i,line in ipairs tree if i > 1 nomsu\append "; " - line_nomsu = @tree_to_nomsu(line,inline:true) + line_nomsu = recurse(line,inline:true) return nil unless line_nomsu nomsu\append line_nomsu return nomsu nomsu = NomsuCode(tree.source) for i, line in ipairs tree nomsu\append pop_comments(line.source.start) - line = assert(@tree_to_nomsu(line, can_use_colon:true), "Could not convert line to nomsu") + line = assert(recurse(line, can_use_colon:true), "Could not convert line to nomsu") nomsu\append line if i < #tree nomsu\append "\n" @@ -615,14 +622,14 @@ with NomsuCompiler elseif bit.type == "Text" nomsu\append(make_text(bit)) else - interp_nomsu = assert @tree_to_nomsu(bit, inline:true) + interp_nomsu = assert recurse(bit, inline:true) if bit.type != "Var" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" interp_nomsu\parenthesize! nomsu\append "\\", interp_nomsu return nomsu return NomsuCode(tree.source, '"', make_text(tree), '"') else - inline_version = @tree_to_nomsu(tree, inline:true) + inline_version = recurse(tree, inline:true) if inline_version and #inline_version <= MAX_LINE return inline_version make_text = (tree)-> @@ -652,13 +659,13 @@ with NomsuCompiler elseif bit.type == "Text" nomsu\append make_text(bit) else - interp_nomsu = @tree_to_nomsu(bit, inline:true) + interp_nomsu = recurse(bit, inline:true) if interp_nomsu if bit.type != "Var" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" interp_nomsu\parenthesize! nomsu\append "\\", interp_nomsu else - interp_nomsu = assert(@tree_to_nomsu(bit)) + interp_nomsu = assert(recurse(bit)) return nil unless interp_nomsu nomsu\append "\\\n ", interp_nomsu if i < #tree @@ -670,7 +677,7 @@ with NomsuCompiler if inline nomsu = NomsuCode(tree.source, "[") for i, item in ipairs tree - item_nomsu = @tree_to_nomsu(item, inline:true) + item_nomsu = recurse(item, inline:true) return nil unless item_nomsu if i > 1 nomsu\append ", " @@ -678,7 +685,7 @@ with NomsuCompiler nomsu\append "]" return nomsu else - inline_version = @tree_to_nomsu(tree, inline:true) + inline_version = recurse(tree, inline:true) if inline_version and #inline_version <= MAX_LINE return inline_version nomsu = NomsuCode(tree.source, "[..]") @@ -687,14 +694,14 @@ with NomsuCompiler pop_comments(tree[1].source.start) else '' for i, item in ipairs tree - item_nomsu = @tree_to_nomsu(item, inline:true) + item_nomsu = recurse(item, inline:true) if item_nomsu and #tostring(line) + #", " + #item_nomsu <= MAX_LINE if #line.bits > 1 line\append ", " line\append item_nomsu else unless item_nomsu - item_nomsu = @tree_to_nomsu(item) + item_nomsu = recurse(item) return nil unless item_nomsu if #line.bits > 1 if #tostring(line_comments) > 0 @@ -713,7 +720,7 @@ with NomsuCompiler if inline nomsu = NomsuCode(tree.source, "{") for i, entry in ipairs tree - entry_nomsu = @tree_to_nomsu(entry, inline:true) + entry_nomsu = recurse(entry, inline:true) return nil unless entry_nomsu if i > 1 nomsu\append ", " @@ -721,7 +728,7 @@ with NomsuCompiler nomsu\append "}" return nomsu else - inline_version = @tree_to_nomsu(tree, inline:true) + inline_version = recurse(tree, inline:true) if inline_version then return inline_version nomsu = NomsuCode(tree.source, "{..}") line = NomsuCode(tree.source, "\n ") @@ -729,7 +736,7 @@ with NomsuCompiler pop_comments(tree[1].source.start) else '' for i, entry in ipairs tree - entry_nomsu = @tree_to_nomsu(entry) + entry_nomsu = recurse(entry) return nil unless entry_nomsu if #line + #tostring(entry_nomsu) <= MAX_LINE if #line.bits > 1 @@ -751,17 +758,17 @@ with NomsuCompiler when "DictEntry" key, value = tree[1], tree[2] - key_nomsu = @tree_to_nomsu(key, inline:true) + key_nomsu = recurse(key, inline:true) return nil unless key_nomsu if key.type == "Action" or key.type == "Block" key_nomsu\parenthesize! value_nomsu = if value - @tree_to_nomsu(value, inline:true) + recurse(value, inline:true) else NomsuCode(tree.source, "") if inline and not value_nomsu then return nil if not value_nomsu return nil if inline - value_nomsu = @tree_to_nomsu(value) + value_nomsu = recurse(value) return nil unless value_nomsu return NomsuCode tree.source, key_nomsu, ":", value_nomsu @@ -775,7 +782,7 @@ with NomsuCompiler -- TODO: support arbitrary words here, including operators and unicode if bit[1]\match("[_a-zA-Z][_a-zA-Z0-9]*") bit_nomsu = bit[1] - unless bit_nomsu then bit_nomsu = @tree_to_nomsu(bit, inline:true) + unless bit_nomsu then bit_nomsu = recurse(bit, inline:true) return nil unless bit_nomsu switch bit.type when "Action", "Block", "IndexChain" |
