CapsAdmin

Untitled

Dec 21st, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. local function preprocess(script, custom_pattern)
  2.    
  3.     local pattern = custom_pattern or "%-%-%[%[(.-)%]%]"
  4.    
  5.     local function parse_arg_line(arg_line)
  6.         local args = {}
  7.        
  8.         arg_line = arg_line:gsub("%s+", "")
  9.         arg_line = arg_line .. ","
  10.        
  11.         for arg in arg_line:gmatch("(.-),") do
  12.             table.insert(args, arg)
  13.         end
  14.        
  15.         return args
  16.     end
  17.    
  18.     local defined = {}
  19.    
  20.     script = script:gsub(pattern, function(macro)
  21.         local func_name
  22.         local arg_line
  23.         local content
  24.            
  25.         local args = {macro:match("#(.-) (.-)%((.-)%)%(%((.+)%)%)")}
  26.            
  27.         if not args[1] then
  28.             args = {macro:match("#(.-) (.-) (.+)")}
  29.         end
  30.            
  31.         if args[1] == "define" then
  32.             if args[4] then
  33.                 func_name = args[2]
  34.                 arg_line = args[3]
  35.                 content = args[4]
  36.             else
  37.                 func_name = args[2]
  38.                 content = args[3]
  39.             end
  40.         end
  41.                                
  42.         if func_name and content and not arg_line then
  43.             table.insert(defined, {func_name = func_name, content = content})
  44.         elseif func_name and arg_line and content then
  45.             local args = parse_arg_line(arg_line)
  46.            
  47.             table.insert(defined, {func_name = func_name, args = args, content = content})     
  48.         end
  49.     end)
  50.    
  51.     local function get_defined(name)
  52.         for key, val in pairs(defined) do
  53.             if val.func_name == name then
  54.                 return val
  55.             end
  56.         end
  57.     end
  58.    
  59.     script = script:gsub(pattern, function(macro)
  60.         local func_name, arg_line = macro:match("(.-)%((.-)%)")
  61.         if not arg_line then
  62.             func_name = macro:match("(.+)")
  63.         end
  64.            
  65.         local macro = get_defined(func_name)
  66.                        
  67.         if macro then
  68.             local content = macro.content
  69.                
  70.             if arg_line and macro.args then
  71.                 local args = parse_arg_line(arg_line)
  72.                
  73.                 local pos = 0
  74.                
  75.                 for i, arg in ipairs(macro.args) do
  76.                     content = content:gsub(arg, "___ARG_" .. i .. "_ARG___")
  77.                 end
  78.                
  79.                 for i, arg in ipairs(args) do
  80.                     content = content:gsub("___ARG_" .. i .. "_ARG___", arg)
  81.                 end
  82.             end
  83.                            
  84.             return content
  85.         end
  86.     end)
  87.        
  88.     return script
  89. end
  90.  
  91.  
  92. print(preprocess([===[
  93.  
  94. --[[#define OKAY(a,b,c,d)((abcd))]]
  95. --[[#define ohhh 5]]
  96.  
  97. print("--[[OKAY(ASDSADdddd,     gggg,\"o3kasd\",/////////)]]")
  98. print(--[[ohhh]])
  99.        
  100. --[[#define { then]]
  101. --[[#define } end]]
  102.    
  103. -- yeah, useless.. but it's not meant for hacky stuff like this anyway
  104. if (1)
  105. --[[{]]
  106.     print("oh");
  107. --[[}]]
  108.        
  109. ]===]))
Advertisement
Add Comment
Please, Sign In to add comment