dreamworkAPI
Ctrl K
GitHub
libraryDocs / lua / lexer

lua.lexer

librarysharedmenusource

Lua 5.1 / LuaJIT lexer.

Tokens are returned as flat arrays ( types, values, lines ) instead of token tables, whitespace, newlines and comments are kept as tokens, so concatenating all values gives back the original code.

Fields

NameTypeDescription
TOKEN_SPACEintegerSpaces and tabs, without newlines. = 1
TOKEN_NEWLINEintegerA single \n. = 2
TOKEN_COMMENTinteger--, --[[ ]] comments and the shebang line. = 3
TOKEN_STRINGintegerQuoted and long bracket strings, the value includes quotes / brackets. = 4
TOKEN_NUMBERintegerNumber literals, including LuaJIT LL, ULL and i suffixes. = 5
TOKEN_NAMEintegerNames and keywords. = 6
TOKEN_SYMBOLintegerOperators and punctuation. = 7
TOKEN_DIRECTIVEintegerA #<name> line accepted by the is_directive callback of lexer.tokenize, including \ continued lines. = 8
TOKEN_NAMESstring[]Token type names, indexed by token type.
keywordstable<string, boolean>Reserved words of Lua 5.1 and LuaJIT ( goto ). continue is not here, the parser treats it as a keyword only at the start of a statement.

Functions

lua.lexer.countNewlinesCounts \n characters in the string.
lua.lexer.isIdentifierChecks if the string is a valid Lua name that is not a keyword.
lua.lexer.tokenizeSplits Lua code into tokens.

#lua.lexer.countNewlines

sharedmenunodiscardsource
function lua.lexer.countNewlines(str: string): integer

Counts \n characters in the string.

Parameters
NameTypeDescription
strstringThe string to count newlines in.
Returns
TypeNameDescription
integercountThe number of newlines.

#lua.lexer.isIdentifier

sharedmenunodiscardsource
function lua.lexer.isIdentifier(str: string): boolean

Checks if the string is a valid Lua name that is not a keyword.

continue is rejected too, so the result is safe to use as a statement start.

Parameters
NameTypeDescription
strstringThe string to check.
Returns
TypeNameDescription
booleanis_identifiertrue if the string can be used as a variable or field name.

#lua.lexer.tokenize

sharedmenusource
function lua.lexer.tokenize(code: string, is_directive?: ( fun( name: string ): boolean )?): integer[], string[], integer[], integer

Splits Lua code into tokens.

Never fails, malformed tokens ( unfinished strings, invalid numbers ) are left for the parser to report.

Parameters
NameTypeDescription
codestringThe code to tokenize.
is_directiveoptional( fun( name: string ): boolean )?If set, #<name> at the start of a line produces a TOKEN_DIRECTIVE token when this function returns true.
Returns
TypeNameDescription
integer[]typesThe token types, see lexer.TOKEN_*.
string[]valuesThe token source texts.
integer[]linesThe lines where tokens start.
integercountThe number of tokens.
Generated from the dreamwork meta branch.