Skip to content
ReFreezed edited this page Jul 12, 2021 · 27 revisions

Note: The documentation has moved to the LuaPreprocess website. Information here may be out of date!

There are two general contexts where code run:

  • Build system: The place you call processFile()/processString() from.
  • Metaprogram: The file/Lua string being processed. (All examples on this page are in this context, unless noted otherwise.)

All global functions that are available to metaprograms are also available to the build system through the library:

-- Build system context:
local pp = require("preprocess") -- Examples on this page assume the library has been required into `pp`.
local luaString = pp.toLua("foo")

-- Metaprogram context:
!local luaString = toLua("foo")

Global functions in metaprograms:

Exported stuff from the library:


Global Functions in Metaprograms

concatTokens()

luaString = concatTokens( tokens )

[v1.3] Concatinate tokens by their representations.

local tokens = {}

table.insert(tokens, newToken("identifier",  "foo"))
table.insert(tokens, newToken("punctuation", "="))
table.insert(tokens, newToken("string",      "bar"))

local luaString = concatTokens(tokens) -- foo="bar"

copyTable()

copy = copyTable( table [, deep=false ] )

[v1.8] Copy a table, optionally recursively (deep copy). Multiple references to the same table and self-references are preserved during deep copying.

eachToken()

for index, token in eachToken( tokens [, ignoreUselessTokens=false ] ) do

[v1.6] Loop through tokens.

escapePattern()

escapedString = escapePattern( string )

Escape a string so it can be used in a pattern as plain text.

local s = "2*5=10"
print(s:match("2*5"))                -- Matches "5"
print(s:match(escapePattern("2*5"))) -- Matches "2*5"

getFileContents()

contents = getFileContents( path [, isTextFile=false ] )

Get the entire contents of a binary file or text file. Returns nil and a message on error.

fileExists()

bool = fileExists( path )

Check if a file exists.

getCurrentPathIn()

path = getCurrentPathIn( )

[v1.8] Get what file is currently being processed, if any.

getCurrentPathOut()

path = getCurrentPathOut( )

[v1.8] Get what file the currently processed file will be written to, if any.

getNextUsefulToken()

token, index = getNextUsefulToken( tokens, startIndex [, steps=1 ] )

[v1.7] Get the next token that isn't a whitespace or comment. Returns nil if no more tokens are found. Specify a negative steps value to get an earlier token.

isToken()

bool = isToken( token, tokenType [, tokenValue=any ] )

[v1.6] Check if a token is of a specific type, optionally also check it's value.

local tokens = tokenize("local foo = 123")
local token1 = tokens[1]

if token1 and isToken(token1, "keyword", "if") then
	print("We got an if statement.")
end

newToken()

token = newToken( tokenType, ... )

[v1.1] Create a new token. Different token types take different arguments.

commentToken     = newToken( "comment",     contents [, forceLongForm=false ] )
identifierToken  = newToken( "identifier",  identifier )
keywordToken     = newToken( "keyword",     keyword )
numberToken      = newToken( "number",      number [, numberFormat="auto" ] )
punctuationToken = newToken( "punctuation", symbol )
stringToken      = newToken( "string",      contents [, longForm=false ] )
whitespaceToken  = newToken( "whitespace",  contents )
ppEntryToken     = newToken( "pp_entry",    isDouble )
ppKeywordToken   = newToken( "pp_keyword",  keyword )
commentToken     = { type="comment",     representation=string, value=string, long=isLongForm }
identifierToken  = { type="identifier",  representation=string, value=string }
keywordToken     = { type="keyword",     representation=string, value=string }
numberToken      = { type="number",      representation=string, value=number }
punctuationToken = { type="punctuation", representation=string, value=string }
stringToken      = { type="string",      representation=string, value=string, long=isLongForm }
whitespaceToken  = { type="whitespace",  representation=string, value=string }
ppEntryToken     = { type="pp_entry",    representation=string, value=string, double=isDouble }
ppKeywordToken   = { type="pp_keyword",  representation=string, value=string }

Number Formats

  • "integer" (e.g. 42)
  • "int" (same as integer, e.g. 42)
  • "float" (e.g. 3.14)
  • "scientific" (e.g. 0.7e+12)
  • "SCIENTIFIC" (e.g. 0.7E+12 (upper case))
  • "e" (same as scientific, e.g. 0.7e+12)
  • "E" (same as SCIENTIFIC, e.g. 0.7E+12 (upper case))
  • "hexadecimal" (e.g. 0x19af)
  • "HEXADECIMAL" (e.g. 0x19AF (upper case))
  • "hex" (same as hexadecimal, e.g. 0x19af)
  • "HEX" (same as HEXADECIMAL, e.g. 0x19AF (upper case))
  • "auto"

Note: Infinite numbers and NaN always get automatic format:

  • +Infinity: (1/0)
  • -Infinity: (-1/0)
  • NaN: (0/0)

outputLua()

outputLua( luaString1, ... )

Output one or more strings as raw Lua code. Raises an error if no file or string is being processed.

local funcName = "doNothing"
outputLua("local function ", funcName, "()\n")
outputLua("end\n")

Note: Generated metaprograms will contain calls to __LUA() which is an alias of outputLua().

outputLuaTemplate()

outputLuaTemplate( luaStringTemplate, value1, ... )

[v1.10] Use a string as a template for outputting Lua code with values. Question marks (?) are replaced with the values. Raises an error if no file or string is being processed.

outputLuaTemplate("local name, age = ?, ?", "Harry", 48)
outputLuaTemplate("dogs[?] = ?", "greyhound", {italian=false, count=5})

-- Output:
local name, age = "Harry", 48
dogs["greyhound"] = {count=5,italian=false}

outputValue()

outputValue( value1, ... )

Output one or more values, like strings or tables, as literals. Cannot output functions or userdata. Raises an error if no file or string is being processed.

outputLua("local person = ")
outputValue({name="Barry", age=49})

Note: Generated metaprograms will contain calls to __VAL() which is an alias of outputValue().

pack()

values = pack( value1, ... )

[v1.8] Put values in a new table. values.n is the amount of values (which can be zero) including any nil values. Alias for table.pack() in Lua 5.2+.

local function getValues()
	return 99, nil, "hello"
end

local values = pack(getValues())

print(#values)  -- Either 3 or 1 depending on interpreter implementation details. Unreliable!
print(values.n) -- 3

print(unpack(values, 1, values.n)) -- 99, nil, "hello"

printf()

printf( formatString, value1, ... )

Print a formatted string. Same as print(formatString:format(value1, ...)).

removeUselessTokens()

removeUselessTokens( tokens )

[v1.6] Remove whitespace and comment tokens.

run()

returnValue1, ... = run( path [, arg1, ... ] )

Execute a Lua file, optionally sending it extra arguments. Similar to dofile().

local docs = run("scripts/downloadDocumentation.lua", "perl")

serialize()

success, error = serialize( buffer, value )

Same as toLua() except adds the result to an array instead of returning the Lua code as a string.

local buffer = {}

table.insert(buffer, "local person = ")
serialize(buffer, {name="Barry", age=49})

local luaString = table.concat(buffer)
outputLua(luaString)
-- Output: local person = {age=49,name="Barry"}

tokenize()

tokens = tokenize( luaString [, allowPreprocessorCode=false ] )

[v1.1] Convert Lua code to tokens. Returns nil and a message on error. See newToken() for token fields and types. Additional token fields:

  • line: Token start line number.
  • lineEnd: Token end line number.
  • position: Starting position in bytes.
  • file: What file path the token came from.

toLua()

luaString = toLua( value )

Convert a value to a Lua literal. Does not work with certain types, like functions or userdata. Returns nil and a message on error.

local person    = {name="Barry", age=49}
local luaString = toLua(person)
outputLua("local person = ", luaString)
-- Output: local person = {age=49,name="Barry"}

unpack()

value1, ... = unpack( table [, fromIndex=1, toIndex=#table ] )

Is the normal unpack() in Lua 5.1 and alias for table.unpack() in Lua 5.2+.

Exported Stuff from the Library

All global functions that are available to metaprograms are also here (like getFileContents() etc.).

VERSION

pp.VERSION

The version of LuaPreprocess, e.g. "1.8.0".

metaEnvironment

pp.metaEnvironment

The environment used for metaprograms (and the message handler in the command line program).

-- Build system context:
pp.metaEnvironment.theValue = "Hello"

-- Metaprogram context:
!print(theValue) -- Hello

processFile()

processedFileInfo = pp.processFile( params )

Process a Lua file. Returns nil and a message on error.

  • processedFileInfo: Table with various information (see processedFileInfo).
  • params: Table with the following fields:
pathIn          = pathToInputFile       -- [Required]
pathOut         = pathToOutputFile      -- [Required]
pathMeta        = pathForMetaprogram    -- [Optional] You can inspect this temporary output file if an error occurs in the metaprogram.

debug           = boolean               -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
addLineNumbers  = boolean               -- [Optional] Add comments with line numbers to the output.

backtickStrings = boolean               -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks. (Default: false)
jitSyntax       = boolean               -- [Optional] Allow LuaJIT-specific syntax. (Default: false)
canOutputNil    = boolean               -- [Optional] Allow !() and outputValue() to output nil. (Default: true)
fastStrings     = boolean               -- [Optional] Force fast serialization of string values. (Non-ASCII characters will look ugly.) (Default: false)
validate        = boolean               -- [Optional] Validate output. (Default: true)

onInsert        = function( name )      -- [Optional] Called for each @insert"name" statement. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
onBeforeMeta    = function( )           -- [Optional] Called before the metaprogram runs.
onAfterMeta     = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
onError         = function( error )     -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().

processString()

luaString, processedFileInfo = pp.processString( params )

[v1.2] Process Lua code. Returns nil and a message on error.

  • luaString: The processed Lua code.
  • processedFileInfo: Table with various information (see processedFileInfo).
  • params: Table with the following fields:
code            = luaString             -- [Required]
pathMeta        = pathForMetaprogram    -- [Optional] You can inspect this temporary output file if an error occurs in the metaprogram.

debug           = boolean               -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
addLineNumbers  = boolean               -- [Optional] Add comments with line numbers to the output.

backtickStrings = boolean               -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks. (Default: false)
jitSyntax       = boolean               -- [Optional] Allow LuaJIT-specific syntax. (Default: false)
canOutputNil    = boolean               -- [Optional] Allow !() and outputValue() to output nil. (Default: true)
fastStrings     = boolean               -- [Optional] Force fast serialization of string values. (Non-ASCII characters will look ugly.) (Default: false)
validate        = boolean               -- [Optional] Validate output. (Default: true)

onInsert        = function( name )      -- [Optional] Called for each @insert"name" statement. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
onBeforeMeta    = function( )           -- [Optional] Called before the metaprogram runs.
onError         = function( error )     -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().

Other Info

processedFileInfo

Table returned from processFile() and processString() (and sent to the "filedone" message handler in the command line program). Contains these fields:

  • path: Path to the processed file. (Empty if processString() was used.)
  • outputPath: [v1.9] Path to the outputted file. (Empty if processString() was used.)
  • processedByteCount: The length of the processed data in bytes.
  • lineCount: Total line count.
  • linesOfCode: [v1.3] Amount of lines with code.
  • tokenCount: Total token count.
  • hasPreprocessorCode: Whether any preprocessor code was encountered. If this is false then the file is 100% pure Lua.
  • hasMetaprogram: [v1.13] Whether any preprocessor code that triggered code execution was encountered.
  • insertedFiles: [v1.10] Array of names to resources inserted by @insert "name" statements.