Module:Repr
This module contains functions for generating string representations of Lua objects. It is inspired by Python's repr function.
Usage
To use the module, first you have to import it.
local mRepr = require("Module:Repr")
Then you can use the functions it contains. The documentation for each function is below.
repr
This function generates a string representation of any given Lua object. The idea is that if you copy the string this function produces it, and paste it back into a Lua program, then you should be able to reproduce the original object. This doesn't work for all values, but it should hold for simple cases.
For example, mRepr.repr({bool = true, number = 6, str = "hello world"})
will output the string {bool = true, number = 6, str = "hello world"}
.
Basic syntax:
mRepr.repr(value)
Full syntax:
mRepr.repr(value, options)
Parameters:
value
: The value to convert to a string. This can be any Lua value. This parameter is optional, and defaults tonil
.options
: A table of options. This parameter is optional.
The following options can be specified in the options table:
pretty
: If true, output the string in "pretty" format (as in pretty-printing). This will add new lines and indentation between table items. If false, format everything on one line. The default is false.tabs
: If true, indent with tabs; otherwise, indent with spaces. The default is true. This only has an effect ifpretty
is true.spaces
: The number of spaces to indent with, iftabs
is false. The default is 4. This only has an effect ifpretty
is true.semicolons
: If true, table items are separated with semicolons. If false, they are separated with spaces. The default is false.sortKeys
: If true, sort table keys in lexical order, after other table key formatting has been applied (such as adding square brackets). If false, table keys are output in arbitrary order (the order they are processed by the pairs function). The default is true.depth
: The indentation depth to output the top-level object at. The default is 0. This only has an effect ifpretty
is true.
Features:
- The function handles cyclic tables gracefully; when it detects a cycle, the inner table is rendered as
{CYCLIC}
. __tostring
metamethods are automatically called if they are available.- The sequence part of a table is always rendered as a sequence. If there are also key-value pairs, they will be rendered after the sequence part.
Here is an example that shows off all the bells and whistles:
local myTable = {
hello = "repr",
usefulness = 100,
isEasyToUse = true,
sequence = {"a", "sequence", "table"},
mixed = {"a", "sequence", with = "key-value pairs"},
subTables = {
moreInfo = "Calls itself recursively on sub-tables"
},
usesToString = setmetatable({}, {__tostring = function () return "__tostring functions are called automatically" end}),
["$YMBOL$"] = "Keys that aren't Lua identifiers are quoted";
[{also = "Tables as keys work too";}] = "in case you need that",
cyclic = {note = "cyclical tables are printed as just {CYCLIC}"}
}
myTable.cyclic.cyclic = myTable.cyclic -- Create a cycle
local options = {
pretty = true, -- print with \n and indentation?
semicolons = false, -- when printing tables, use semicolons (;) instead of commas (,)?
sortKeys = true, -- when printing dictionary tables, sort keys alphabetically?
spaces = 3, -- when pretty printing, use how many spaces to indent?
tabs = false, -- when pretty printing, use tabs instead of spaces?
depth = 0, -- when pretty pretty printing, what level to start indenting at?
}
mw.log(mRepr.repr(myTable, options))
This logs the following:
{ ["$YMBOL$"] = "Keys that aren't Lua identifiers are quoted", [{ also = "Tables as keys work too" }] = "in case you need that", cyclic = { cyclic = {CYCLIC}, note = "cyclical tables are printed as just {CYCLIC}" }, hello = "repr", isEasyToUse = true, mixed = { "a", "sequence", with = "key-value pairs" }, sequence = { "a", "sequence", "table" }, subTables = { moreInfo = "Calls itself recursively on sub-tables" }, usefulness = 100, usesToString = __tostring functions are called automatically }
invocationRepr
This function generates a string representation of a function invocation.
Basic syntax:
mRepr.invocationRepr{funcName = functionName, args = functionArgs}
Full syntax:
mRepr.invocationRepr{funcName = functionName, args = functionArgs, options = options}
Parameters:
funcName
: The function name. This parameter is required, and must be a string.args
: The function arguments. This should be sequence table. The sequence items can be any Lua value, and will each be rendered using the [[#repr|]] function. This argument is optional.options
: A table of options. The options are the same as for the repr function. This argument is optional.
Examples:
mRepr.invocationRepr{funcName = "myFunc", args = {"test", 4, true, {"a", "b", "c"}}}
Result: myFunc("test", 4, true, {"a", "b", "c"})
require('Module:No globals')
local defaultOptions = {
pretty = false,
tabs = true,
semicolons = false,
spaces = 4,
sortKeys = true,
depth = 0,
}
-- Define the reprRecursive variable here so that we can call the reprRecursive
-- function from renderSequence and renderKeyValueTable without getting
-- "Tried to read nil global reprRecursive" errors.
local reprRecursive
local luaKeywords = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["for"] = true,
["function"] = true,
["if"] = true,
["in"] = true,
["local"] = true,
["nil"] = true,
["not"] = true,
["or"] = true,
["repeat"] = true,
["return"] = true,
["then"] = true,
["true"] = true,
["until"] = true,
["while"] = true,
}
--[[
-- Whether the given value is a valid Lua identifier (i.e. whether it can be
-- used as a variable name.)
--]]
local function isLuaIdentifier(str)
return type(str) == "string"
-- Must start with a-z, A-Z or underscore, and can only contain
-- a-z, A-Z, 0-9 and underscore
and str:find("^[%a_][%a%d_]*$") ~= nil
-- Cannot be a Lua keyword
and not luaKeywords[str]
end
--[[
-- Render a string representation.
--]]
local function renderString(s)
return (("%q"):format(s):gsub("\\\n", "\\n"))
end
--[[
-- Render a number representation.
--]]
local function renderNumber(n)
if n == math.huge then
return "math.huge"
elseif n == -math.huge then
return "-math.huge"
else
return tostring(n)
end
end
--[[
-- Whether a table has a __tostring metamethod.
--]]
local function hasTostringMetamethod(t)
return getmetatable(t) and type(getmetatable(t).__tostring) == "function"
end
--[[
-- Whether the given value is a positive integer.
--]]
local function isPositiveInteger(value)
return type(value) == "number"
and value >= 1
and value < math.huge
and value == math.floor(value)
end
--[[
-- Whether the given table is a sequence table.
--]]
local function isSequence(t)
for k in pairs(t) do
if not isPositiveInteger(k) then
return false
end
end
return true
end
--[[
-- Pretty print a sequence of string representations.
-- This can be made to represent different constructs depending on the values
-- of prefix, suffix, and separator. The amount of whitespace is controlled by
-- the depth and indent parameters.
--]]
local function prettyPrintItemsAtDepth(items, prefix, suffix, separator, indent, depth)
local whitespaceAtCurrentDepth = "\n" .. indent:rep(depth)
local whitespaceAtNextDepth = whitespaceAtCurrentDepth .. indent
local ret = {prefix, whitespaceAtNextDepth}
local first = items[1]
if first ~= nil then
table.insert(ret, first)
end
for i = 2, #items do
table.insert(ret, separator)
table.insert(ret, whitespaceAtNextDepth)
table.insert(ret, items[i])
end
table.insert(ret, whitespaceAtCurrentDepth)
table.insert(ret, suffix)
return table.concat(ret)
end
--[[
-- Render a sequence of string representations.
-- This can be made to represent different constructs depending on the values of
-- prefix, suffix and separator.
--]]
local function renderItems(items, prefix, suffix, separator)
return prefix .. table.concat(items, separator .. " ") .. suffix
end
--[[
-- Render a sequence of table item string representations.
-- This uses the context to decide whether to pretty-print the result or not.
--]]
local function renderTableItems(items, context, depth)
local prefix = "{"
local suffix = "}"
if context.pretty then
return prettyPrintItemsAtDepth(
items,
prefix,
suffix,
context.separator,
context.indent,
depth
)
else
return renderItems(items, prefix, suffix, context.separator)
end
end
--[[
-- Render a sequence table.
--]]
local function renderSequence(t, context, depth)
local items = {}
for i = 1, #t do
table.insert(items, reprRecursive(t[i], context, depth + 1))
end
return renderTableItems(items, context, depth)
end
--[[
-- Render a table of key-value pairs.
--]]
local function renderKeyValueTable(t, context, depth)
local keyOrder = {}
local keyValueStrings = {}
for k, v in pairs(t) do
local kStr = isLuaIdentifier(k) and k or ("[" .. reprRecursive(k, context, depth + 1) .. "]")
local vStr = reprRecursive(v, context, depth + 1)
table.insert(keyOrder, kStr)
keyValueStrings[kStr] = vStr
end
if context.sortKeys then
table.sort(keyOrder)
end
local items = {}
for _, kStr in ipairs(keyOrder) do
table.insert(items, string.format("%s = %s", kStr, keyValueStrings[kStr]))
end
return renderTableItems(items, context, depth)
end
--[[
-- Render the given table.
-- The table can be a sequence table or a table of key-value pairs. If the table
-- has a __tostring metamethod that is used to render the table, and there is
-- detection for cyclic tables.
--]]
local function renderTable(t, context, depth)
if hasTostringMetamethod(t) then
return tostring(t)
elseif context.shown[t] then
return "{CYCLIC}"
end
context.shown[t] = true
local result
if isSequence(t) then
result = renderSequence(t, context, depth)
else
result = renderKeyValueTable(t, context, depth)
end
context.shown[t] = false
return result
end
--[[
-- Recursively render a string representation of the given value.
--]]
function reprRecursive(value, context, depth)
if value == nil then
return "nil"
end
local valueType = type(value)
if valueType == "boolean" then
return tostring(value)
elseif valueType == "number" then
return renderNumber(value)
elseif valueType == "string" then
return renderString(value)
elseif valueType == "table" then
return renderTable(value, context, depth)
else
return "<" .. valueType .. ">"
end
end
--[[
-- Normalize a table of options passed by the user.
-- Any values not specified will be assigned default values.
--]]
local function normalizeOptions(options)
options = options or {}
local ret = {}
for option, defaultValue in pairs(defaultOptions) do
local value = options[option]
if value ~= nil then
ret[option] = value
else
ret[option] = defaultValue
end
end
return ret
end
--[[
-- Get the indent from the options table.
--]]
local function getIndent(options)
if options.tabs then
return "\t"
else
return string.rep(" ", options.spaces)
end
end
--[[
-- Render a string representation of the given value.
--]]
local function repr(value, options)
options = normalizeOptions(options)
local context = {}
context.pretty = options.pretty
if context.pretty then
context.indent = getIndent(options)
else
context.indent = ""
end
if options.semicolons then
context.separator = ";"
else
context.separator = ","
end
context.sortKeys = options.sortKeys
context.shown = {}
local depth = options.depth
return reprRecursive(value, context, depth)
end
--[[
-- Render a string representation of the given function invocation.
--]]
local function invocationRepr(keywordArgs)
local options = normalizeOptions(keywordArgs.options)
local depth = options.depth
options.depth = depth + 1
local items = {}
for _, arg in ipairs(keywordArgs.args) do
table.insert(items, repr(arg, options))
end
local prefix = "("
local suffix = ")"
local separator = ","
local renderedArgs
if options.pretty then
renderedArgs = prettyPrintItemsAtDepth(
items,
prefix,
suffix,
separator,
getIndent(options),
depth
)
else
renderedArgs = renderItems(items, prefix, suffix, separator)
end
return keywordArgs.funcName .. renderedArgs
end
return {
isLuaIdentifier = isLuaIdentifier,
repr = repr,
invocationRepr = invocationRepr,
}