Skip to content

Commit f2363f5

Browse files
committed
show_line_position
1 parent bf437b2 commit f2363f5

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

moonscript/parse/util.lua

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,58 @@ extract_line = function(str, start_pos)
4141
end
4242
return str:match("^.-$")
4343
end
44+
local show_line_position
45+
show_line_position = function(str, pos, context)
46+
if context == nil then
47+
context = true
48+
end
49+
local lines = {
50+
{ }
51+
}
52+
for c in str:gmatch(".") do
53+
lines[#lines] = lines[#lines] or { }
54+
table.insert(lines[#lines], c)
55+
if c == "\n" then
56+
lines[#lines + 1] = { }
57+
end
58+
end
59+
for i, line in ipairs(lines) do
60+
lines[i] = table.concat(line)
61+
end
62+
local out
63+
local remaining = pos - 1
64+
for k, line in ipairs(lines) do
65+
if remaining < #line then
66+
local left = line:sub(1, remaining)
67+
local right = line:sub(remaining + 1)
68+
out = {
69+
tostring(left) .. "" .. tostring(right)
70+
}
71+
if context then
72+
do
73+
local before = lines[k - 1]
74+
if before then
75+
table.insert(out, 1, before)
76+
end
77+
end
78+
do
79+
local after = lines[k + 1]
80+
if after then
81+
table.insert(out, after)
82+
end
83+
end
84+
end
85+
break
86+
else
87+
remaining = remaining - #line
88+
end
89+
end
90+
if not (out) then
91+
return "-"
92+
end
93+
out = table.concat(out)
94+
return (out:gsub("\n*$", ""))
95+
end
4496
local mark
4597
mark = function(name)
4698
return function(...)
@@ -60,9 +112,12 @@ pos = function(patt)
60112
end
61113
end
62114
local got
63-
got = function(what)
115+
got = function(what, context)
116+
if context == nil then
117+
context = true
118+
end
64119
return Cmt("", function(str, pos)
65-
print("++ got " .. tostring(what), "[" .. tostring(extract_line(str, pos)) .. "]")
120+
print("++ got " .. tostring(what), "[" .. tostring(show_line_position(str, pos, context)) .. "]")
66121
return true
67122
end)
68123
end
@@ -245,5 +300,7 @@ return {
245300
join_chain = join_chain,
246301
wrap_decorator = wrap_decorator,
247302
check_lua_string = check_lua_string,
248-
self_assign = self_assign
303+
self_assign = self_assign,
304+
got = got,
305+
show_line_position = show_line_position
249306
}

moonscript/parse/util.moon

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,46 @@ extract_line = (str, start_pos) ->
3131

3232
str\match "^.-$"
3333

34+
-- print the line with a token showing the position
35+
show_line_position = (str, pos, context=true) ->
36+
lines = { {} }
37+
for c in str\gmatch "."
38+
lines[#lines] or= {}
39+
table.insert lines[#lines], c
40+
if c == "\n"
41+
lines[#lines + 1] = {}
42+
43+
for i, line in ipairs lines
44+
lines[i] = table.concat line
45+
46+
local out
47+
48+
remaining = pos - 1
49+
for k, line in ipairs lines
50+
if remaining < #line
51+
left = line\sub 1, remaining
52+
right = line\sub remaining + 1
53+
out = {
54+
"#{left}◉#{right}"
55+
}
56+
57+
if context
58+
if before = lines[k - 1]
59+
table.insert out, 1, before
60+
61+
if after = lines[k + 1]
62+
table.insert out, after
63+
64+
break
65+
else
66+
remaining -= #line
67+
68+
69+
return "-" unless out
70+
71+
out = table.concat out
72+
(out\gsub "\n*$", "")
73+
3474
-- used to identify a capture with a label
3575
mark = (name) ->
3676
(...) -> {name, ...}
@@ -46,9 +86,9 @@ pos = (patt) ->
4686

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

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

0 commit comments

Comments
 (0)