@@ -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
3575mark = ( 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