diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-24 20:38:25 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-24 20:39:18 -0700 |
| commit | ec5d730fa0ac26ace7fdd6a1de5fed9e05c133c0 (patch) | |
| tree | 0f414c109d4fceae47eb23b6c3ca167180d9c6fb /utils.lua | |
| parent | bb31a98a5674a64cb015b843be534dccb24632db (diff) | |
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.
Diffstat (limited to 'utils.lua')
| -rw-r--r-- | utils.lua | 40 |
1 files changed, 2 insertions, 38 deletions
@@ -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 |
