|
Project Information
|
IntroductionLua Checker is a program that analyzes Lua source for common programming errors, much as the "lint" program does for C. The following problems are currently identified:
Lua Checker was developed within the Street View team at Google to validate Lua scripts. Russ Smith is the primary author. Lua Checker is licensed under the standard Lua 5.0 license (an MIT-style license). Lua Checker contains a bison compatible LALR(1) parser for the Lua language (see lua.y) which may be useful to Lua developers in its own right. BackgroundLanguages such as C/C++ and Java are strongly typed. This means that each variable has only one type (such as a number, string or object), and the compiler will give an error when the variable is used in an incorrect way. This means that many problems are caught early. In contrast, the script language Lua is dynamically typed, with a simple type model. Variables can be assigned values of any type. This makes development of small scripts somewhat easier, but it means that in larger programs many common programming errors are not detected until run time. For example:
In practice, Lua programs over (say) 1000 lines tend to accumulate these kinds of problems, making debugging difficult. A standard Lua practice to deal with undefined global variables is to install a special 'get-value' handler on the global variable table that warns when undefined globals are accessed. This is of limited benefit because problems are still only detected at run-time. Lua Checker is designed to solve these problems. It performs a static analysis of Lua source code prior to it being run, and prints out warnings about any problems that it finds. UsageTo help Lua Checker to do its job, Lua source code must be written in a slightly more restricted way.
foo = 123 -- Foo declared print(foo) -- Foo used but don't do this, even though it is functionally the same:
dofile 'foo.lua' -- foo.lua will also be scanned
dofile('bar.lua') -- bar.lua will be ignored
if true then
dofile 'baz.lua' -- Inner scope: baz.lua will be ignored
endfoo = 123 --@ const the --@ marker indicates that special Lua Checker keywords will follow on the same line. The const keyword means that the previous variable declared is to be a constant. Any further attempt to set that variable will be an error. Note that special keywords can be followed by regular Lua comments as follows:foo = 123 --@ const -- A comment Command Line ArgumentsThe CHECK_LUA.sh script is currently used to invoke lua_checker. It is clunky and will be replaced with something better. The lua_checker program is invoked as: lua_checker ...flags... filename.lua The available flags are
function Foo() ... end -- Foo is now a constant function variable Foo = 123 -- Error, attempt to assign to a constant ImplementationThe interesting parts of Lua Checker are implemented within bison parsers. Two separate parsers are used, that are run by two separate programs:
FutureFeatures that are in the works:
|