Skip to content

Commit

Permalink
feat: add utils split_args function
Browse files Browse the repository at this point in the history
  • Loading branch information
Diaoul committed Jul 2, 2024
1 parent 6f79b82 commit 69afab1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions lua/dap/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,68 @@ function M.pick_file(opts)
end


--- Split args string into a table of arguments.
--- Works with single and double quoted strings. Escaped quotes are supported.
--- Example:
--- split_args("--debug true --reason 'I\'m dumb'") -> {"--debug", "true", "I'm dumb"}
--- split_args('--comment "I'm \"this\" close"') -> {"--comment", "I'm \"this\" close"}
---
--- <pre>
--- require("dap.utils").split_args("runserver --debug true --reason 'I\'m dumb'")
--- {runserver, --debug, true, I'm dumb}
--- </pre>
---
--- <pre>
--- require("dap.utils").split_args('--comment "I\'m \"this\" close"')
--- {--comment, I'm "this" close}
--- </pre>
---
--- Inspired by http://lua-users.org/wiki/LpegRecipes
--- @param args string
--- @return table
function M.split_args(args)
local lpeg = vim.lpeg

local P, S, C, Cc, Ct = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct

--- @param id string
--- @param patt vim.lpeg.Capture
--- @return vim.lpeg.Pattern
local function token(id, patt)
return Ct(Cc(id) * C(patt))
end

local single_quoted = P("'") * ((1 - S("'\r\n\f\\")) + (P("\\") * 1)) ^ 0 * "'"
local double_quoted = P('"') * ((1 - S('"\r\n\f\\')) + (P("\\") * 1)) ^ 0 * '"'

local whitespace = token("whitespace", S("\r\n\f\t ") ^ 1)
local word = token("word", (1 - S("' \r\n\f\t\"")) ^ 1)
local string = token("string", single_quoted + double_quoted)

local pattern = Ct((string + whitespace + word) ^ 0)

local t = {}
local tokens = lpeg.match(pattern, args)

-- somehow, this did not work out
if tokens == nil or type(tokens) == "integer" then
return t
end

for _, tok in ipairs(tokens) do
if tok[1] ~= "whitespace" then
if tok[1] == "string" then
-- cut off quotes and replace escaped quotes
local v, _ = tok[2]:sub(2, -2):gsub("\\(['\"])", "%1")
table.insert(t, v)
else
table.insert(t, tok[2])
end
end
end

return t
end


return M

0 comments on commit 69afab1

Please sign in to comment.