My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday, 7AM PST due to brief network maintenance
                
Code license: MIT License
Labels: lua, checker, lint
People details
Project owners:
  russ.ls

Introduction

Lua 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.

Please post any comments about Lua Checker to the General Discussion group.

Background

Languages 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.

Usage

To help Lua Checker to do its job, Lua source code must be written in a slightly more restricted way.

but don't do this, even though it is functionally the same:
  function FooSetter()
    foo = 123           -- Lua Checker doesn't know that foo is allowed to be a global
  end
  FooSetter()
  print(foo)
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 Arguments

The 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

Implementation

The interesting parts of Lua Checker are implemented within bison parsers. Two separate parsers are used, that are run by two separate programs:

  1. lua_simplifier takes the original Lua source code and rewrites it into a simpler form. The simplified source has most syntactic sugar expanded out and has fewer syntactic ambiguities (mostly achieved by putting semicolon at the end of every line). See the comments at the start of lua.y for a detailed description. The point of simplification is to allow the actual Lua checking code to live within a much simpler parser that does not have to also deal with complex Lua syntax issues.
  2. lua_checker takes the simplified Lua source and actually performs the checks.

Future

Features that are in the works:









Hosted by Google Code