dreamworkAPI
Ctrl K
GitHub
libraryDocs / lua / parser

lua.parser

librarysharedmenusource

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

NameTypeDescription
NODE_LOCALintegerLocal statement node, see lua.ast.Local. = 1
NODE_LOCAL_FUNCTIONintegerLocalFunction statement node, see lua.ast.LocalFunction. = 2
NODE_FUNCTION_DECLARATIONintegerFunctionDeclaration statement node, see lua.ast.FunctionDeclaration. = 3
NODE_ASSIGNMENTintegerAssignment statement node, see lua.ast.Assignment. = 4
NODE_CALL_STATEMENTintegerCallStatement statement node, see lua.ast.CallStatement. = 5
NODE_DOintegerDo statement node, see lua.ast.Do. = 6
NODE_WHILEintegerWhile statement node, see lua.ast.While. = 7
NODE_REPEATintegerRepeat statement node, see lua.ast.Repeat. = 8
NODE_IFintegerIf statement node, see lua.ast.If. = 9
NODE_NUMERIC_FORintegerNumericFor statement node, see lua.ast.NumericFor. = 10
NODE_GENERIC_FORintegerGenericFor statement node, see lua.ast.GenericFor. = 11
NODE_RETURNintegerReturn statement node, see lua.ast.Return. = 12
NODE_BREAKintegerBreak statement node, see lua.ast.Break. = 13
NODE_CONTINUEintegerContinue statement node, see lua.ast.Continue. = 14
NODE_GOTOintegerGoto statement node, see lua.ast.Goto. = 15
NODE_LABELintegerLabel statement node, see lua.ast.Label. = 16
NODE_NILintegerNil expression node, see lua.ast.Nil. = 17
NODE_BOOLEANintegerBoolean expression node, see lua.ast.Boolean. = 18
NODE_NUMBERintegerNumber expression node, see lua.ast.Number. = 19
NODE_STRINGintegerString expression node, see lua.ast.String. = 20
NODE_VARARGintegerVararg expression node, see lua.ast.Vararg. = 21
NODE_FUNCTIONintegerFunction expression node, see lua.ast.Function. = 22
NODE_TABLEintegerTable expression node, see lua.ast.Table. = 23
NODE_BINARYintegerBinary expression node, see lua.ast.Binary. = 24
NODE_UNARYintegerUnary expression node, see lua.ast.Unary. = 25
NODE_IDENTIFIERintegerIdentifier expression node, see lua.ast.Identifier. = 26
NODE_MEMBERintegerMember expression node, see lua.ast.Member. = 27
NODE_INDEXintegerIndex expression node, see lua.ast.Index. = 28
NODE_CALLintegerCall expression node, see lua.ast.Call. = 29
NODE_METHOD_CALLintegerMethodCall expression node, see lua.ast.MethodCall. = 30
NODE_PARENintegerParen expression node, see lua.ast.Paren. = 31
NODE_TABLE_FIELDintegerTableField node, see lua.ast.TableField. = 32
NODE_CHUNKintegerChunk node, see lua.ast.Chunk. = 33
NODE_NAMESstring[]Node type names, indexed by node type.

Functions

lua.parser.decodeNumberDecodes a number literal token.
lua.parser.decodeStringDecodes a string literal token ( quoted or long bracket ) into its value.
lua.parser.parseParses Lua code into an AST.
lua.parser.parseExpressionParses a single Lua expression into an AST.
lua.parser.parseTokensParses tokens returned by lexer.tokenize into an AST.

#lua.parser.decodeNumber

sharedmenusource
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
NameTypeDescription
strstringThe number literal.
Returns
TypeNameDescription
number | nilvalueThe number, or nil if the literal is a 64-bit / imaginary literal or malformed.
string | nilerror_messageThe reason the literal is malformed.

#lua.parser.decodeString

sharedmenusource
function lua.parser.decodeString(str: string): string | nil, string | nil

Decodes a string literal token ( quoted or long bracket ) into its value.

Parameters
NameTypeDescription
strstringThe string literal with quotes or brackets.
Returns
TypeNameDescription
string | nilvalueThe decoded value, or nil if the literal is malformed.
string | nilerror_messageThe reason the literal is malformed.

#lua.parser.parse

sharedmenunodiscardsource
function lua.parser.parse(code: string, name?: string): lua.ast.Chunk

Parses Lua code into an AST.

Parameters
NameTypeDescription
codestringThe Lua code to parse.
nameoptionalstringThe chunk name used in errors, "lua" by default.
Returns
TypeNameDescription
lua.ast.ChunkchunkThe root node of the AST.

#lua.parser.parseExpression

sharedmenunodiscardsource
function lua.parser.parseExpression(code: string, name?: string): lua.ast.Expression

Parses a single Lua expression into an AST.

Parameters
NameTypeDescription
codestringThe Lua expression to parse.
nameoptionalstringThe chunk name used in errors, "lua" by default.
Returns
TypeNameDescription
lua.ast.ExpressionexpressionThe root node of the expression.

#lua.parser.parseTokens

sharedmenunodiscardsource
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
NameTypeDescription
token_typesinteger[]The token types.
token_valuesstring[]The token values.
token_linesinteger[]The token lines.
token_countintegerThe number of tokens.
nameoptionalstringThe chunk name used in errors, "lua" by default.
Returns
TypeNameDescription
lua.ast.ChunkchunkThe root node of the AST.
Generated from the dreamwork meta branch.