mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
add -X flag to moonc to dump posmap
This commit is contained in:
parent
db1081f8e5
commit
bd93b322d0
15
bin/moonc
15
bin/moonc
@ -9,7 +9,7 @@ require "moonscript.util"
|
|||||||
require "alt_getopt"
|
require "alt_getopt"
|
||||||
require "lfs"
|
require "lfs"
|
||||||
|
|
||||||
local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTb", {
|
local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTXb", {
|
||||||
print = "p", tree = "T", version = "v", help = "h"
|
print = "p", tree = "T", version = "v", help = "h"
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ local help = [[Usage: %s [options] files...
|
|||||||
-t path Specify where to place compiled files
|
-t path Specify where to place compiled files
|
||||||
-p Write output to standard out
|
-p Write output to standard out
|
||||||
-T Write parse tree instead of code (to stdout)
|
-T Write parse tree instead of code (to stdout)
|
||||||
|
-X Write line rewrite map instead of code (to stdout)
|
||||||
-b Dump parse and compile time (doesn't write output)
|
-b Dump parse and compile time (doesn't write output)
|
||||||
-v Print version
|
-v Print version
|
||||||
|
|
||||||
@ -122,10 +123,18 @@ function compile_file(text, fname)
|
|||||||
return ""
|
return ""
|
||||||
else
|
else
|
||||||
local compile_time = gettime()
|
local compile_time = gettime()
|
||||||
local code, err, pos = compile.tree(tree)
|
local code, posmap_or_err, err_pos = compile.tree(tree)
|
||||||
compile_time = gettime() - compile_time
|
compile_time = gettime() - compile_time
|
||||||
|
|
||||||
if not code then
|
if not code then
|
||||||
return nil, compile.format_error(err, pos, text)
|
return nil, compile.format_error(posmap_or_err, err_pos, text)
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts.X then
|
||||||
|
opts.p = true
|
||||||
|
print("Pos", "Lua", ">>", "Moon")
|
||||||
|
print(util.debug_posmap(posmap_or_err, text, code))
|
||||||
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.b then
|
if opts.b then
|
||||||
|
@ -594,47 +594,6 @@ Block = (function()
|
|||||||
end
|
end
|
||||||
return _class_0
|
return _class_0
|
||||||
end)()
|
end)()
|
||||||
local debug_posmap
|
|
||||||
debug_posmap = function(posmap, fname, lua_code)
|
|
||||||
if fname == nil then
|
|
||||||
fname = error("pass in input file")
|
|
||||||
end
|
|
||||||
local moon_code = io.open(fname):read("*a")
|
|
||||||
local tuples = (function()
|
|
||||||
local _accum_0 = { }
|
|
||||||
local _len_0 = 0
|
|
||||||
for k, v in pairs(posmap) do
|
|
||||||
_len_0 = _len_0 + 1
|
|
||||||
_accum_0[_len_0] = {
|
|
||||||
k,
|
|
||||||
v
|
|
||||||
}
|
|
||||||
end
|
|
||||||
return _accum_0
|
|
||||||
end)()
|
|
||||||
table.sort(tuples, function(a, b)
|
|
||||||
return a[1] < b[1]
|
|
||||||
end)
|
|
||||||
local lines = (function()
|
|
||||||
local _accum_0 = { }
|
|
||||||
local _len_0 = 0
|
|
||||||
local _list_0 = tuples
|
|
||||||
for _index_0 = 1, #_list_0 do
|
|
||||||
local pair = _list_0[_index_0]
|
|
||||||
local lua_line, pos = unpack(pair)
|
|
||||||
local moon_line = pos_to_line(moon_code, pos)
|
|
||||||
local lua_text = get_line(lua_code, lua_line)
|
|
||||||
local moon_text = get_closest_line(moon_code, moon_line)
|
|
||||||
local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
|
|
||||||
if _value_0 ~= nil then
|
|
||||||
_len_0 = _len_0 + 1
|
|
||||||
_accum_0[_len_0] = _value_0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return _accum_0
|
|
||||||
end)()
|
|
||||||
return concat(lines, "\n") .. "\n"
|
|
||||||
end
|
|
||||||
RootBlock = (function()
|
RootBlock = (function()
|
||||||
local _parent_0 = Block
|
local _parent_0 = Block
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
|
@ -351,24 +351,6 @@ class Block
|
|||||||
@_lines = Lines!
|
@_lines = Lines!
|
||||||
@stms fn lines
|
@stms fn lines
|
||||||
|
|
||||||
debug_posmap = (posmap, fname=error"pass in input file", lua_code) ->
|
|
||||||
moon_code = io.open(fname)\read "*a"
|
|
||||||
|
|
||||||
tuples = [{k, v} for k, v in pairs posmap]
|
|
||||||
|
|
||||||
table.sort tuples, (a, b) -> a[1] < b[1]
|
|
||||||
|
|
||||||
lines = for pair in *tuples
|
|
||||||
lua_line, pos = unpack pair
|
|
||||||
moon_line = pos_to_line moon_code, pos
|
|
||||||
|
|
||||||
lua_text = get_line lua_code, lua_line
|
|
||||||
moon_text = get_closest_line moon_code, moon_line
|
|
||||||
|
|
||||||
"#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]"
|
|
||||||
|
|
||||||
concat(lines, "\n") .. "\n"
|
|
||||||
|
|
||||||
class RootBlock extends Block
|
class RootBlock extends Block
|
||||||
new: (...) =>
|
new: (...) =>
|
||||||
@root = self
|
@root = self
|
||||||
@ -419,6 +401,5 @@ tree = (tree, scope=RootBlock!) ->
|
|||||||
else
|
else
|
||||||
lua_code = scope\render!
|
lua_code = scope\render!
|
||||||
posmap = scope._lines\flatten_posmap!
|
posmap = scope._lines\flatten_posmap!
|
||||||
-- print debug_posmap posmap, "scrap.moon", lua_code
|
|
||||||
lua_code, posmap
|
lua_code, posmap
|
||||||
|
|
||||||
|
@ -99,3 +99,39 @@ dump = function(what)
|
|||||||
end
|
end
|
||||||
return _dump(what)
|
return _dump(what)
|
||||||
end
|
end
|
||||||
|
debug_posmap = function(posmap, moon_code, lua_code)
|
||||||
|
local tuples = (function()
|
||||||
|
local _accum_0 = { }
|
||||||
|
local _len_0 = 0
|
||||||
|
for k, v in pairs(posmap) do
|
||||||
|
_len_0 = _len_0 + 1
|
||||||
|
_accum_0[_len_0] = {
|
||||||
|
k,
|
||||||
|
v
|
||||||
|
}
|
||||||
|
end
|
||||||
|
return _accum_0
|
||||||
|
end)()
|
||||||
|
table.sort(tuples, function(a, b)
|
||||||
|
return a[1] < b[1]
|
||||||
|
end)
|
||||||
|
local lines = (function()
|
||||||
|
local _accum_0 = { }
|
||||||
|
local _len_0 = 0
|
||||||
|
local _list_0 = tuples
|
||||||
|
for _index_0 = 1, #_list_0 do
|
||||||
|
local pair = _list_0[_index_0]
|
||||||
|
local lua_line, pos = unpack(pair)
|
||||||
|
local moon_line = pos_to_line(moon_code, pos)
|
||||||
|
local lua_text = get_line(lua_code, lua_line)
|
||||||
|
local moon_text = get_closest_line(moon_code, moon_line)
|
||||||
|
local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
|
||||||
|
if _value_0 ~= nil then
|
||||||
|
_len_0 = _len_0 + 1
|
||||||
|
_accum_0[_len_0] = _value_0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return _accum_0
|
||||||
|
end)()
|
||||||
|
return concat(lines, "\n")
|
||||||
|
end
|
||||||
|
@ -4,7 +4,7 @@ module "moonscript.util", package.seeall
|
|||||||
export moon
|
export moon
|
||||||
export pos_to_line, get_closest_line, get_line
|
export pos_to_line, get_closest_line, get_line
|
||||||
export reversed, trim, split
|
export reversed, trim, split
|
||||||
export dump
|
export dump, debug_posmap
|
||||||
|
|
||||||
import concat from table
|
import concat from table
|
||||||
|
|
||||||
@ -75,3 +75,19 @@ dump = (what) ->
|
|||||||
_dump what
|
_dump what
|
||||||
|
|
||||||
|
|
||||||
|
debug_posmap = (posmap, moon_code, lua_code) ->
|
||||||
|
tuples = [{k, v} for k, v in pairs posmap]
|
||||||
|
table.sort tuples, (a, b) -> a[1] < b[1]
|
||||||
|
|
||||||
|
lines = for pair in *tuples
|
||||||
|
lua_line, pos = unpack pair
|
||||||
|
moon_line = pos_to_line moon_code, pos
|
||||||
|
|
||||||
|
lua_text = get_line lua_code, lua_line
|
||||||
|
moon_text = get_closest_line moon_code, moon_line
|
||||||
|
|
||||||
|
"#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]"
|
||||||
|
|
||||||
|
concat(lines, "\n")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user