From 3e3855bc8a5831199c81e0fb6d3997cdf9e274a6 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 15 Feb 2024 14:21:10 +0100 Subject: [PATCH] arg tokenizer --- tokenize_args.lua | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tokenize_args.lua diff --git a/tokenize_args.lua b/tokenize_args.lua new file mode 100644 index 0000000..16b3b76 --- /dev/null +++ b/tokenize_args.lua @@ -0,0 +1,71 @@ +function tokenize_args(raw) + -- NOTE: string.gmatch is does not use regex, but a smaller pattern matcher! + -- A complete regex parser would be larger than lua itself. See + -- [Programming in Lua 20.2](https://www.lua.org/pil/20.2.html). + -- + -- Notable differences: + -- '-' is ungreedy wildcard + -- '*?' does not work + -- '|' is not or + -- + -- This means we're better of implementing a programatic lexer. + local t = {} + local current = "" + local in_str = false + local str_seek + for c in string.gmatch(raw, ".") do -- iterate through all chars + if c == ' ' and not in_str then + if string.len(current) > 0 then + table.insert(t, current) + current = "" + end + elseif c == '"' and not in_str then + in_str = true + str_seek = '"' + elseif c == "'" and not in_str then + in_str = true + str_seek = "'" + elseif c == str_seek and in_str then + in_str = false + table.insert(t, current) + current = "" + else + current = current .. c + end + end + table.insert(t, current) + return t +end + +--- dumps a variable into a string, so it can be printed. This is meant for +--- debug prints +--- @param any t any variable +--- @return string t_dumped t dumped to string +function dump(t) + if type(t) == 'table' then + local s = '{ ' + for k, v in pairs(t) do + if type(k) ~= 'number' then k = '"' .. k .. '"' end + if k ~= 1 then + s = s .. ', ' + end + s = s .. '[' .. k .. '] = \'' .. dump(v) .. '\'' + end + return s .. ' }' + else + return tostring(t) + end +end + +function main() + local mockargs = [[-iab --foo '{"QUX": "BAR"}' --ala=boa]] + print(mockargs) + local split = tokenize_args(mockargs) + print(dump(split)) +end + +if pcall(getfenv, 4) then + print("Library") +else + main() +end