Skip to content

Commit

Permalink
show_line_position
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Sep 25, 2016
1 parent bf437b2 commit f2363f5
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
63 changes: 60 additions & 3 deletions moonscript/parse/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,58 @@ extract_line = function(str, start_pos)
end
return str:match("^.-$")
end
local show_line_position
show_line_position = function(str, pos, context)
if context == nil then
context = true
end
local lines = {
{ }
}
for c in str:gmatch(".") do
lines[#lines] = lines[#lines] or { }
table.insert(lines[#lines], c)
if c == "\n" then
lines[#lines + 1] = { }
end
end
for i, line in ipairs(lines) do
lines[i] = table.concat(line)
end
local out
local remaining = pos - 1
for k, line in ipairs(lines) do
if remaining < #line then
local left = line:sub(1, remaining)
local right = line:sub(remaining + 1)
out = {
tostring(left) .. "" .. tostring(right)
}
if context then
do
local before = lines[k - 1]
if before then
table.insert(out, 1, before)
end
end
do
local after = lines[k + 1]
if after then
table.insert(out, after)
end
end
end
break
else
remaining = remaining - #line
end
end
if not (out) then
return "-"
end
out = table.concat(out)
return (out:gsub("\n*$", ""))
end
local mark
mark = function(name)
return function(...)
Expand All @@ -60,9 +112,12 @@ pos = function(patt)
end
end
local got
got = function(what)
got = function(what, context)
if context == nil then
context = true
end
return Cmt("", function(str, pos)
print("++ got " .. tostring(what), "[" .. tostring(extract_line(str, pos)) .. "]")
print("++ got " .. tostring(what), "[" .. tostring(show_line_position(str, pos, context)) .. "]")
return true
end)
end
Expand Down Expand Up @@ -245,5 +300,7 @@ return {
join_chain = join_chain,
wrap_decorator = wrap_decorator,
check_lua_string = check_lua_string,
self_assign = self_assign
self_assign = self_assign,
got = got,
show_line_position = show_line_position
}
46 changes: 43 additions & 3 deletions moonscript/parse/util.moon
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ extract_line = (str, start_pos) ->

str\match "^.-$"

-- print the line with a token showing the position
show_line_position = (str, pos, context=true) ->
lines = { {} }
for c in str\gmatch "."
lines[#lines] or= {}
table.insert lines[#lines], c
if c == "\n"
lines[#lines + 1] = {}

for i, line in ipairs lines
lines[i] = table.concat line

local out

remaining = pos - 1
for k, line in ipairs lines
if remaining < #line
left = line\sub 1, remaining
right = line\sub remaining + 1
out = {
"#{left}◉#{right}"
}

if context
if before = lines[k - 1]
table.insert out, 1, before

if after = lines[k + 1]
table.insert out, after

break
else
remaining -= #line


return "-" unless out

out = table.concat out
(out\gsub "\n*$", "")

-- used to identify a capture with a label
mark = (name) ->
(...) -> {name, ...}
Expand All @@ -46,9 +86,9 @@ pos = (patt) ->

-- generates a debug pattern that always succeeds and prints out where we are
-- in the buffer with a label
got = (what) ->
got = (what, context=true) ->
Cmt "", (str, pos) ->
print "++ got #{what}", "[#{extract_line str, pos}]"
print "++ got #{what}", "[#{show_line_position str, pos, context}]"
true

-- converts 1 element array to its value, otherwise marks it
Expand Down Expand Up @@ -154,4 +194,4 @@ self_assign = (name, pos) ->
{ :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark,
:is_assignable, :check_assignable, :format_assign, :format_single_assign,
:sym, :symx, :simple_string, :wrap_func_arg, :join_chain, :wrap_decorator,
:check_lua_string, :self_assign }
:check_lua_string, :self_assign, :got, :show_line_position }

0 comments on commit f2363f5

Please sign in to comment.