Syntax#

Lua’s grammar is small and free-form. Whitespace is insignificant; statements run to the next reserved word or end-of-chunk and may optionally be separated by ;. A chunk is the unit of compilation; a script is one chunk, a string passed to loadstring is one chunk. Source is UTF-8 by default.

For the values that syntax produces, see Types. For the operators that combine them, see Operators.

-- chunk: one compilation unit
local function greet(name)
  print("hello, " .. name)
end

greet("operator")

Comments#

Line comments start with -- and run to end-of-line. Block comments wrap with --[[ and ]]. The conventional close is --]] so the whole block can be re-enabled by prepending a single - to the opener, turning --[[ into ---[[ (a line comment followed by an unrelated literal).

-- single line comment

--[[
block comment.
adding a leading - to the opener flips it back to live code.
--]]

Identifiers#

Identifiers are letters, digits, and underscores; they cannot start with a digit. Lua is case-sensitive. Convention is snake_case for variables and functions, CamelCase for module tables and constructors, UPPER_SNAKE for module-level constants. Names starting with _ followed by uppercase (_G, _ENV, _VERSION) are reserved for the language.

local user_count = 0          -- local variable
MAX_RETRIES      = 5          -- global by default
local function HttpClient() end

Keywords#

Reserved words the operator cannot use as identifiers.

and break do else elseif end false for function goto if in
local nil not or repeat return then true until while

Literals#

Every primitive value has a literal form write directly.

local n   = 42                -- integer (5.3+)
local big = 1000000           -- no _ separators in Lua
local x   = 0xff              -- hex
local pi  = 3.14159           -- float
local e   = 6.022e23          -- scientific

local s   = "hello"           -- string
local q   = 'hello'           -- single quote is the same
local raw = [[no escapes\n]]  -- long string; raw, no escapes
local m   = [[
multi
line]]                        -- long string spans lines
local lvl = [==[has ]] in it]==]   -- bump = on both sides to escape

local t   = true
local f   = false
local z   = nil

Statements#

A statement does something. Assignment, local declaration, return, if / for / while / repeat blocks, function calls, break and goto.

x = 1 + 2                     -- assignment statement
local y = 3                   -- local declaration statement
if x > 0 then process(x) end  -- compound statement
for i = 1, 3 do step(i) end   -- compound statement
return x                      -- return statement

Statements are separated by whitespace, newlines, or ;. Lua does not require ; anywhere; operators add it only to disambiguate (e.g. before a leading ( that could otherwise chain onto the previous expression).

Expressions#

An expression evaluates to a value. Arithmetic, function calls, table constructors, and and / or short-circuits are expressions. Anywhere Lua wants a value, the operator supplies an expression.

1 + 2                         -- arithmetic expression
#xs > 0                       -- relational expression
table.concat(xs, ",")         -- function-call expression
x > 0 and x or -x             -- ternary via and/or
{1, 2, 3}                     -- table constructor expression

Function definitions and table constructors are expressions too; local f = function() end and local t = {} both bind the result of an expression to a name.

References#