Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.
The following keywords are reserved and cannot be used as names:
Lua is a case-sensitive language: is a reserved word, but And
and AND
are two different, valid names. As a convention, programs should avoid creating names that start with an underscore followed by one or more uppercase letters (such as ).
A short literal string 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), ‘‘ (vertical tab), ‘\\
‘ (backslash), ‘\"
‘ (quotation mark [double quote]), and ‘\'
‘ (apostrophe [single quote]). A backslash followed by a line break results in a newline in the string. The escape sequence ‘\z
‘ skips the following span of white-space characters, including line breaks; it is particularly useful to break and indent a long literal string into multiple lines without adding the newlines and spaces into the string contents. A short literal string cannot contain unescaped line breaks nor escapes not forming a valid escape sequence.
We can specify any byte in a short literal string by its numeric value (including embedded zeros). This can be done with the escape sequence \x*XX*
, where XX is a sequence of exactly two hexadecimal digits, or with the escape sequence \*ddd*
, where ddd is a sequence of up to three decimal digits. (Note that if a decimal escape sequence is to be followed by a digit, it must be expressed using exactly three digits.)
The UTF-8 encoding of a Unicode character can be inserted in a literal string with the escape sequence \u{*XXX*}
(note the mandatory enclosing brackets), where XXX is a sequence of one or more hexadecimal digits representing the character code point.
For convenience, when the opening long bracket 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 five literal strings below denote the same string:
Any byte in a literal string not explicitly affected by the previous rules represents itself. However, Lua opens files for parsing in text mode, and the system file functions may have problems with some control characters. So, it is safer to represent non-text data as a quoted literal with explicit escape sequences for the non-text characters.
A numeric constant (or numeral) can be written with an optional fractional part and an optional decimal exponent, marked by a letter ‘e
‘ or ‘E
‘. Lua also accepts hexadecimal constants, which start with 0x
or 0X
. Hexadecimal constants also accept an optional fractional part plus an optional binary exponent, marked by a letter ‘p
‘ or ‘P
‘. A numeric constant with a radix point or an exponent denotes a float; otherwise, if its value fits in an integer, it denotes an integer. Examples of valid integer constants are
A comment starts with a double hyphen (--
) anywhere outside a string. If the text immediately after is not an opening long bracket, 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 closing long bracket. Long comments are frequently used to disable code temporarily.