Skip to content

Commit bd93b32

Browse files
committed
add -X flag to moonc to dump posmap
1 parent db1081f commit bd93b32

File tree

5 files changed

+65
-64
lines changed

5 files changed

+65
-64
lines changed

bin/moonc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require "moonscript.util"
99
require "alt_getopt"
1010
require "lfs"
1111

12-
local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTb", {
12+
local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTXb", {
1313
print = "p", tree = "T", version = "v", help = "h"
1414
})
1515

@@ -24,6 +24,7 @@ local help = [[Usage: %s [options] files...
2424
-t path Specify where to place compiled files
2525
-p Write output to standard out
2626
-T Write parse tree instead of code (to stdout)
27+
-X Write line rewrite map instead of code (to stdout)
2728
-b Dump parse and compile time (doesn't write output)
2829
-v Print version
2930
@@ -122,10 +123,18 @@ function compile_file(text, fname)
122123
return ""
123124
else
124125
local compile_time = gettime()
125-
local code, err, pos = compile.tree(tree)
126+
local code, posmap_or_err, err_pos = compile.tree(tree)
126127
compile_time = gettime() - compile_time
128+
127129
if not code then
128-
return nil, compile.format_error(err, pos, text)
130+
return nil, compile.format_error(posmap_or_err, err_pos, text)
131+
end
132+
133+
if opts.X then
134+
opts.p = true
135+
print("Pos", "Lua", ">>", "Moon")
136+
print(util.debug_posmap(posmap_or_err, text, code))
137+
return ""
129138
end
130139

131140
if opts.b then

moonscript/compile.lua

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -594,47 +594,6 @@ Block = (function()
594594
end
595595
return _class_0
596596
end)()
597-
local debug_posmap
598-
debug_posmap = function(posmap, fname, lua_code)
599-
if fname == nil then
600-
fname = error("pass in input file")
601-
end
602-
local moon_code = io.open(fname):read("*a")
603-
local tuples = (function()
604-
local _accum_0 = { }
605-
local _len_0 = 0
606-
for k, v in pairs(posmap) do
607-
_len_0 = _len_0 + 1
608-
_accum_0[_len_0] = {
609-
k,
610-
v
611-
}
612-
end
613-
return _accum_0
614-
end)()
615-
table.sort(tuples, function(a, b)
616-
return a[1] < b[1]
617-
end)
618-
local lines = (function()
619-
local _accum_0 = { }
620-
local _len_0 = 0
621-
local _list_0 = tuples
622-
for _index_0 = 1, #_list_0 do
623-
local pair = _list_0[_index_0]
624-
local lua_line, pos = unpack(pair)
625-
local moon_line = pos_to_line(moon_code, pos)
626-
local lua_text = get_line(lua_code, lua_line)
627-
local moon_text = get_closest_line(moon_code, moon_line)
628-
local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
629-
if _value_0 ~= nil then
630-
_len_0 = _len_0 + 1
631-
_accum_0[_len_0] = _value_0
632-
end
633-
end
634-
return _accum_0
635-
end)()
636-
return concat(lines, "\n") .. "\n"
637-
end
638597
RootBlock = (function()
639598
local _parent_0 = Block
640599
local _base_0 = {

moonscript/compile.moon

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,24 +351,6 @@ class Block
351351
@_lines = Lines!
352352
@stms fn lines
353353

354-
debug_posmap = (posmap, fname=error"pass in input file", lua_code) ->
355-
moon_code = io.open(fname)\read "*a"
356-
357-
tuples = [{k, v} for k, v in pairs posmap]
358-
359-
table.sort tuples, (a, b) -> a[1] < b[1]
360-
361-
lines = for pair in *tuples
362-
lua_line, pos = unpack pair
363-
moon_line = pos_to_line moon_code, pos
364-
365-
lua_text = get_line lua_code, lua_line
366-
moon_text = get_closest_line moon_code, moon_line
367-
368-
"#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]"
369-
370-
concat(lines, "\n") .. "\n"
371-
372354
class RootBlock extends Block
373355
new: (...) =>
374356
@root = self
@@ -419,6 +401,5 @@ tree = (tree, scope=RootBlock!) ->
419401
else
420402
lua_code = scope\render!
421403
posmap = scope._lines\flatten_posmap!
422-
-- print debug_posmap posmap, "scrap.moon", lua_code
423404
lua_code, posmap
424405

moonscript/util.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,39 @@ dump = function(what)
9999
end
100100
return _dump(what)
101101
end
102+
debug_posmap = function(posmap, moon_code, lua_code)
103+
local tuples = (function()
104+
local _accum_0 = { }
105+
local _len_0 = 0
106+
for k, v in pairs(posmap) do
107+
_len_0 = _len_0 + 1
108+
_accum_0[_len_0] = {
109+
k,
110+
v
111+
}
112+
end
113+
return _accum_0
114+
end)()
115+
table.sort(tuples, function(a, b)
116+
return a[1] < b[1]
117+
end)
118+
local lines = (function()
119+
local _accum_0 = { }
120+
local _len_0 = 0
121+
local _list_0 = tuples
122+
for _index_0 = 1, #_list_0 do
123+
local pair = _list_0[_index_0]
124+
local lua_line, pos = unpack(pair)
125+
local moon_line = pos_to_line(moon_code, pos)
126+
local lua_text = get_line(lua_code, lua_line)
127+
local moon_text = get_closest_line(moon_code, moon_line)
128+
local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
129+
if _value_0 ~= nil then
130+
_len_0 = _len_0 + 1
131+
_accum_0[_len_0] = _value_0
132+
end
133+
end
134+
return _accum_0
135+
end)()
136+
return concat(lines, "\n")
137+
end

moonscript/util.moon

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module "moonscript.util", package.seeall
44
export moon
55
export pos_to_line, get_closest_line, get_line
66
export reversed, trim, split
7-
export dump
7+
export dump, debug_posmap
88

99
import concat from table
1010

@@ -75,3 +75,19 @@ dump = (what) ->
7575
_dump what
7676

7777

78+
debug_posmap = (posmap, moon_code, lua_code) ->
79+
tuples = [{k, v} for k, v in pairs posmap]
80+
table.sort tuples, (a, b) -> a[1] < b[1]
81+
82+
lines = for pair in *tuples
83+
lua_line, pos = unpack pair
84+
moon_line = pos_to_line moon_code, pos
85+
86+
lua_text = get_line lua_code, lua_line
87+
moon_text = get_closest_line moon_code, moon_line
88+
89+
"#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]"
90+
91+
concat(lines, "\n")
92+
93+

0 commit comments

Comments
 (0)