2.1 – Lexical Conventions
Identifiers in Lua can be any string of letters, digits, and underscores, not beginning with a digit. This coincides with the definition of identifiers in most languages. (The definition of letter depends on the current locale: any character considered alphabetic by the current locale can be used in an identifier.)
The following keywords are reserved and cannot be used as identifiers:
- and break do else elseif
- end false for function if
- in local nil not or
- repeat return then true until while
Lua is a case-sensitive language: and
is a reserved word, but And
and AND
are two different, valid identifiers. As a convention, identifiers starting with an underscore followed by uppercase letters (such as _VERSION
) are reserved for internal variables used by Lua.
The following strings denote other tokens:
- + - * / ^ =
- ~= <= >= < > ==
- ( ) { } [ ]
- ; : , . .. ...
Literal strings can be delimited by matching single or double quotes, and can contain the following C-like escape sequences:
\a
—- bell\b
—- backspace\f
—- form feed\n
—- newline\r
—- carriage return\t
—- horizontal tab\v
—- vertical tab\\
—- backslash\"
—- quotation mark\'
—- apostrophe\[
—- left square bracket\]
—- right square bracket
Moreover, a `\
newline´ (that is, a backslash followed by a real newline) results in a newline in the string. A character in a string may also be specified by its numerical value using the escape sequence `\
ddd´, where ddd is a sequence of up to three decimal digits. Strings in Lua may contain any 8-bit value, including embedded zeros, which can be specified as `\0
´.
Literal strings can also be delimited by matching double square brackets [[
· · · ]]
. Literals in this bracketed form may run for several lines, may contain nested [[
· · · ]]
pairs, and do not interpret any escape sequences. For convenience, when the opening `[[
´ is immediately followed by a newline, the newline is not included in the string. As an example, in a system using ASCII (in which `a
´ is coded as 97, newline is coded as 10, and `1
´ is coded as 49), the four literals below denote the same string:
- (1) "alo\n123\""
- (2) '\97lo\10\04923"'
- (3) [[alo
- 123"]]
- (4) [[
- alo
- 123"]]
Numerical constants may be written with an optional decimal part and an optional decimal exponent. Examples of valid numerical constants are
- 3 3.0 3.1416 314.16e-2 0.31416E1
Comments start anywhere outside a string with a double hyphen (--
). If the text immediately after --
is different from [[
, the comment is a short comment, which runs until the end of the line. Otherwise, it is a long comment, which runs until the corresponding ]]
. Long comments may run for several lines and may contain nested [[
· · · ]]
pairs.
For convenience, the first line of a chunk is skipped if it starts with #
. This facility allows the use of Lua as a script interpreter in Unix systems (see 6).