lua-users home
lua-l archive

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


Jason Murdick wrote:
> I've found lots of resources for syntax checking and highlighting,
> but I haven't seen anything that described my particular issue.  I
> have a project I've integrated lua into and scripts are run in
> response to triggered events.   
> 
> The problem I'm having is syntax checking the scripts.  The syntax
> checkers I've seen all do a fine job with the basic lua syntax, but I
> have numerous C++ functions and constants that I have exposed through
> my application.  I have not been able to find a way of syntax
> checking the scripts with those functions referenced without running
> the app, shutting it down, reading the log files, repeat.  This is a
> very time-consuming process and so I was hoping for something a bit
> more effective.       
> 
> Is there a way that others do this?  Some sort of xml header file?
> Any assistance would be greatly appreciated.

Functions and constants are anonymous in Lua, that means that the name
you gave them is in fact just the name of one of the reference to that
function. Script author can assign the content of that reference (that
is the anonymous function) to another variable, with a new name (the
name can even be dynamic). Consider the following example:

-- You expose C++ function foo
foo()
-- You can create a new reference to that function
local bar = foo
-- You can then call it like it the original one
bar()
-- In fact the same is true with any complex expression that returns a
function
local baz = function(val)
    return { [val-4] = foo }
end
baz(37)[33]() -- That is equivalent to foo() and is perfectly valid
baz(37)[34]() -- That is a runtime error, trying to call nil

That's why it's hard to do static checks in Lua, unless you severely
restrict syntax to prevent creating new reference to functions, but I
think that's not an easy job.