Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function preprocess(script, custom_pattern)
- local pattern = custom_pattern or "%-%-%[%[(.-)%]%]"
- local function parse_arg_line(arg_line)
- local args = {}
- arg_line = arg_line:gsub("%s+", "")
- arg_line = arg_line .. ","
- for arg in arg_line:gmatch("(.-),") do
- table.insert(args, arg)
- end
- return args
- end
- local defined = {}
- script = script:gsub(pattern, function(macro)
- local func_name
- local arg_line
- local content
- local args = {macro:match("#(.-) (.-)%((.-)%)%(%((.+)%)%)")}
- if not args[1] then
- args = {macro:match("#(.-) (.-) (.+)")}
- end
- if args[1] == "define" then
- if args[4] then
- func_name = args[2]
- arg_line = args[3]
- content = args[4]
- else
- func_name = args[2]
- content = args[3]
- end
- end
- if func_name and content and not arg_line then
- table.insert(defined, {func_name = func_name, content = content})
- elseif func_name and arg_line and content then
- local args = parse_arg_line(arg_line)
- table.insert(defined, {func_name = func_name, args = args, content = content})
- end
- end)
- local function get_defined(name)
- for key, val in pairs(defined) do
- if val.func_name == name then
- return val
- end
- end
- end
- script = script:gsub(pattern, function(macro)
- local func_name, arg_line = macro:match("(.-)%((.-)%)")
- if not arg_line then
- func_name = macro:match("(.+)")
- end
- local macro = get_defined(func_name)
- if macro then
- local content = macro.content
- if arg_line and macro.args then
- local args = parse_arg_line(arg_line)
- local pos = 0
- for i, arg in ipairs(macro.args) do
- content = content:gsub(arg, "___ARG_" .. i .. "_ARG___")
- end
- for i, arg in ipairs(args) do
- content = content:gsub("___ARG_" .. i .. "_ARG___", arg)
- end
- end
- return content
- end
- end)
- return script
- end
- print(preprocess([===[
- --[[#define OKAY(a,b,c,d)((abcd))]]
- --[[#define ohhh 5]]
- print("--[[OKAY(ASDSADdddd, gggg,\"o3kasd\",/////////)]]")
- print(--[[ohhh]])
- --[[#define { then]]
- --[[#define } end]]
- -- yeah, useless.. but it's not meant for hacky stuff like this anyway
- if (1)
- --[[{]]
- print("oh");
- --[[}]]
- ]===]))
Advertisement
Add Comment
Please, Sign In to add comment