Lua tutorial (II): syntax conventions


The name (also known as identifier) used in Lua can be any string composed of letters, numbers and underscores that do not start with numbers. This fits almost allprograming languageDefinition of name in. (the definition of letters depends on the current environment: the letters in the alphabet defined in the system environment can be used as identifiers.) Identifiers are used to name variables or as domain names for tables.

The following keywords are reserved and cannot be used as names:


Copy codeThe code is as follows:

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 are two different legal names. As a general convention, names that begin with an underscore and connect a string of uppercase letters (such as _version) are reserved for Lua’s internal global variables.

Here are other Tokens:

Copy codeThe code is as follows:

+     –     *     /     %     ^     #


==    ~=    <=    >=    <     >     =

(     )     {     }     [     ]

;     :     ,     .     ..    …


The string can be caused by a pair of single quotation marks or double quotation marks. It can also contain escape characters similar to C: ‘\ a’ (ring), ‘\ B’ (backspace), ‘\ f’ (form), ‘\ n’ (line feed), ‘\ R’ (carriage return), ‘\ t’ (horizontal tab), ‘\ V’ (vertical tab), ‘\ \’ (backslash), ‘\’ (double quotation marks), and ‘\’ (single quotation marks). Moreover, if a backslash is followed by a true line break, the result is a line break in the string. We can also use the form of backslash plus number \ DDD to describe a character. Here, DDD is a string of up to three decimal digits. (note that if this description method needs to be followed by a character with a number, three numbers must be written after the backslash.) Strings in Lua can contain any 8-bit value. Include zeros represented by ‘\ 0’.

You must use escape characters only if you need to put different characters such as quotation marks, line breaks, backslashes, or zero terminators into a string. Any other character can be written directly in the text. (some controls can affect the file system and cause some problems, but they won’t cause any problems for Lua.)

Strings can also be defined in a way that is enclosed in long parentheses. We define N equal signs inserted between two positive square brackets as level n positive long brackets. That is to say, level 0 positive long bracket writing [[, level 1 positive long bracket writing [= [, and so on. Reverse long extension has a similar definition; for example, level 4 negative long bracket writing] = = = = = =. A long string can start with a positive brace of any level and end with the first negative brace of the same level. The whole lexical analysis process will not be limited by branches, will not process any conversion symbols, and will ignore any different levels of long brackets. The string described in this way can contain anything, except for a specific level of anti long brackets.

Another convention is that when the positive long bracket is immediately followed by a newline character, the newline character is not included in the string. For example, suppose a system uses ASCII code (at this time, ‘a’ code is 97, newline code is 10, and ‘1’ code is 49). The following five ways describe exactly the same string:

Copy codeThe code is as follows:

a = ‘alo\n123″‘


a = “alo\n123\””

a = ‘\97lo\10\04923″‘

a = [[alo


a = [==[



Digital constants can be written in two parts, the decimal base part and the decimal index part. The index section is optional. Lua also supports hexadecimal integer constants, which only need to be prefixed with 0x. Here are some examples of legal numeric constants:

Copy codeThe code is as follows:

3   3.0   3.1416   314.16e-2   0.31416E1   0xff   0×56

Comments can start with two lines (–) anywhere except in the string. If there is not a long bracket following the two horizontal lines, it is a short comment, and its scope is up to the end of the line; Otherwise, it is a long comment whose scope is until it encounters an inverted long bracket. Long comments are often used to temporarily mask code blocks.