lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In message <20030630095616.GB14133@avorop.local>, avorop@mail.ru writes:
> Hello!
> 
> My question may sound strange but it is very important for me.
> 
> Configuration that I use for my program essentially is very simple
> 
> if(var1 == "string1") then
>    if(match("some_pattern", var2)) then return 1 end
>    if(match("some_pattern1", var2)) then return 2 end
>    if(match("some_pattern1", var2)) then return 3 end
> end
> 
> ...
> 
> BUT. I have to check ALL patterns before the execution starts. In other words
> I want to reject configuration file if there is some error in one of the
> RE patterns. Ideally there would be some hook that gets called when "match" 
> and all parameters for it are identified, giving chance to check which
> parameters would be passed at run-time.
> 
> I was thinking about additional passing  thru regular expression to find all
> 'match' and then compiling all regular expression found, but decided to ask
> first if there is some "cleaner" solution already :)

Put all your patterns in a table.  This will simplify the lookup
procedure and allow you to check the patterns.  It won't be "compile
time" but will be "ahead of time":

t = {
  "some_pattern",
  "some_pattern2",
  "some_pattern3"
}

function check(t)
  for pattern,v in t do
    -- perform check on pattern
  end
end

function config(var1, var2)
  if var1 == "string1" then
    for i,pattern in ipairs(t) do
      if match(pattern, var2) then
	return i
      end
    end
  end
end

Now call the function check just after loading the script (or include a
call to it at the end of the script).

HTH

Cheers,
 drj