From ec5d730fa0ac26ace7fdd6a1de5fed9e05c133c0 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 24 Apr 2018 20:38:25 -0700 Subject: Switched repr() to only return ""-style strings, and not [[]]-style, which helps with indenting generated lua code correctly. Also made a few ergonomic fixes/changes for line breaking. --- utils.lua | 40 ++-------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) (limited to 'utils.lua') diff --git a/utils.lua b/utils.lua index 3374e4a..0d0e034 100644 --- a/utils.lua +++ b/utils.lua @@ -25,25 +25,6 @@ local function size(t) return n end -local _quote_state = {} -local max = math.max -local _quote_patt = re.compile("(({'\n' / '\"' / \"'\" / '\\'}->mark_char) / (']' ({'='*}->mark_eq) (']' / !.)) / .)*", - {mark_char=function(q) - if q == "\n" or q == "\\" then - _quote_state["'"] = false - _quote_state['"'] = false - if _quote_state.min_eq == nil then - _quote_state.min_eq = 0 - end - elseif q == "'" then - _quote_state["'"] = false - elseif q == '"' then - _quote_state['"'] = false - end - end, - mark_eq=function(eq) - _quote_state.min_eq = max(_quote_state.min_eq or 0, #eq+1) - end}) local function repr(x, depth) -- Create a string representation of the object that is close to the lua code that will -- reproduce the object (similar to Python's "repr" function) @@ -74,25 +55,8 @@ local function repr(x, depth) if x == "\n" then return "'\\n'" end - _quote_state = {} - _quote_patt:match(x) - if _quote_state["'"] ~= false then - return "\'" .. x .. "\'" - elseif _quote_state['"'] ~= false then - return "\"" .. x .. "\"" - else - local eq = ("="):rep(_quote_state.min_eq or 0) - -- BEWARE!!! - -- Lua's parser and syntax are dumb, so Lua interprets x[[=[asdf]=]] as - -- a function call to x (i.e. x("=[asdf]=")), instead of indexing x - -- (i.e. x["asdf"]), which it obviously should be. This can be fixed by - -- slapping spaces or parens around the [=[asdf]=]. - if x:sub(1, 1) == "\n" then - return "["..eq.."[\n"..x.."]"..eq.."]" - else - return "["..eq.."["..x.."]"..eq.."]" - end - end + local escaped = x:gsub("\\", "\\\\"):gsub("\n","\\n"):gsub('"', '\\"') + return '"'..escaped..'"' else return tostring(x) end -- cgit v1.2.3