lua.parser
Lua 5.1 / LuaJIT parser, builds an AST from code or tokens.
AST nodes are plain tables with a type field, see lua.ast.*.
// is the floor division, alternative operators are normalized: != -> ~=, && -> and, || -> or, ! -> not.
Fields
| Name | Type | Description |
|---|---|---|
NODE_LOCAL | integer | Local statement node, see lua.ast.Local. = 1 |
NODE_LOCAL_FUNCTION | integer | LocalFunction statement node, see lua.ast.LocalFunction. = 2 |
NODE_FUNCTION_DECLARATION | integer | FunctionDeclaration statement node, see lua.ast.FunctionDeclaration. = 3 |
NODE_ASSIGNMENT | integer | Assignment statement node, see lua.ast.Assignment. = 4 |
NODE_CALL_STATEMENT | integer | CallStatement statement node, see lua.ast.CallStatement. = 5 |
NODE_DO | integer | Do statement node, see lua.ast.Do. = 6 |
NODE_WHILE | integer | While statement node, see lua.ast.While. = 7 |
NODE_REPEAT | integer | Repeat statement node, see lua.ast.Repeat. = 8 |
NODE_IF | integer | If statement node, see lua.ast.If. = 9 |
NODE_NUMERIC_FOR | integer | NumericFor statement node, see lua.ast.NumericFor. = 10 |
NODE_GENERIC_FOR | integer | GenericFor statement node, see lua.ast.GenericFor. = 11 |
NODE_RETURN | integer | Return statement node, see lua.ast.Return. = 12 |
NODE_BREAK | integer | Break statement node, see lua.ast.Break. = 13 |
NODE_CONTINUE | integer | Continue statement node, see lua.ast.Continue. = 14 |
NODE_GOTO | integer | Goto statement node, see lua.ast.Goto. = 15 |
NODE_LABEL | integer | Label statement node, see lua.ast.Label. = 16 |
NODE_NIL | integer | Nil expression node, see lua.ast.Nil. = 17 |
NODE_BOOLEAN | integer | Boolean expression node, see lua.ast.Boolean. = 18 |
NODE_NUMBER | integer | Number expression node, see lua.ast.Number. = 19 |
NODE_STRING | integer | String expression node, see lua.ast.String. = 20 |
NODE_VARARG | integer | Vararg expression node, see lua.ast.Vararg. = 21 |
NODE_FUNCTION | integer | Function expression node, see lua.ast.Function. = 22 |
NODE_TABLE | integer | Table expression node, see lua.ast.Table. = 23 |
NODE_BINARY | integer | Binary expression node, see lua.ast.Binary. = 24 |
NODE_UNARY | integer | Unary expression node, see lua.ast.Unary. = 25 |
NODE_IDENTIFIER | integer | Identifier expression node, see lua.ast.Identifier. = 26 |
NODE_MEMBER | integer | Member expression node, see lua.ast.Member. = 27 |
NODE_INDEX | integer | Index expression node, see lua.ast.Index. = 28 |
NODE_CALL | integer | Call expression node, see lua.ast.Call. = 29 |
NODE_METHOD_CALL | integer | MethodCall expression node, see lua.ast.MethodCall. = 30 |
NODE_PAREN | integer | Paren expression node, see lua.ast.Paren. = 31 |
NODE_TABLE_FIELD | integer | TableField node, see lua.ast.TableField. = 32 |
NODE_CHUNK | integer | Chunk node, see lua.ast.Chunk. = 33 |
NODE_NAMES | string[] | Node type names, indexed by node type. |
Functions
lua.parser.decodeNumber | Decodes a number literal token. |
lua.parser.decodeString | Decodes a string literal token ( quoted or long bracket ) into its value. |
lua.parser.parse | Parses Lua code into an AST. |
lua.parser.parseExpression | Parses a single Lua expression into an AST. |
lua.parser.parseTokens | Parses tokens returned by lexer.tokenize into an AST. |
#lua.parser.decodeNumber
function lua.parser.decodeNumber(str: string): number | nil, string | nil
Decodes a number literal token.
LuaJIT LL, ULL and i literals are valid but cannot be represented as a Lua number, so they return nil without an error.
Parameters
| Name | Type | Description |
|---|---|---|
str | string | The number literal. |
Returns
| Type | Name | Description |
|---|---|---|
number | nil | value | The number, or nil if the literal is a 64-bit / imaginary literal or malformed. |
string | nil | error_message | The reason the literal is malformed. |
#lua.parser.decodeString
function lua.parser.decodeString(str: string): string | nil, string | nil
Decodes a string literal token ( quoted or long bracket ) into its value.
Parameters
| Name | Type | Description |
|---|---|---|
str | string | The string literal with quotes or brackets. |
Returns
| Type | Name | Description |
|---|---|---|
string | nil | value | The decoded value, or nil if the literal is malformed. |
string | nil | error_message | The reason the literal is malformed. |
#lua.parser.parse
function lua.parser.parse(code: string, name?: string): lua.ast.Chunk
Parses Lua code into an AST.
Parameters
| Name | Type | Description |
|---|---|---|
code | string | The Lua code to parse. |
nameoptional | string | The chunk name used in errors, "lua" by default. |
Returns
| Type | Name | Description |
|---|---|---|
lua.ast.Chunk | chunk | The root node of the AST. |
#lua.parser.parseExpression
function lua.parser.parseExpression(code: string, name?: string): lua.ast.Expression
Parses a single Lua expression into an AST.
Parameters
| Name | Type | Description |
|---|---|---|
code | string | The Lua expression to parse. |
nameoptional | string | The chunk name used in errors, "lua" by default. |
Returns
| Type | Name | Description |
|---|---|---|
lua.ast.Expression | expression | The root node of the expression. |
#lua.parser.parseTokens
function lua.parser.parseTokens(token_types: integer[], token_values: string[], token_lines: integer[], token_count: integer, name?: string): lua.ast.Chunk
Parses tokens returned by lexer.tokenize into an AST.
Parameters
| Name | Type | Description |
|---|---|---|
token_types | integer[] | The token types. |
token_values | string[] | The token values. |
token_lines | integer[] | The token lines. |
token_count | integer | The number of tokens. |
nameoptional | string | The chunk name used in errors, "lua" by default. |
Returns
| Type | Name | Description |
|---|---|---|
lua.ast.Chunk | chunk | The root node of the AST. |