Debian Codegraph @

texk/web2c/luatexdir/luaffi/pretty.lua

raw symbols SCIP
1 --[[
2 Author: Julio Manuel Fernandez-Diaz
3 Date: January 12, 2007
4 (For Lua 5.1)
5
6 Modified slightly by RiciLake to avoid the unnecessary table traversal in tablecount()
7
8 Formats tables with cycles recursively to any depth.
9 The output is returned as a string.
10 References to other tables are shown as values.
11 Self references are indicated.
12
13 The string returned is "Lua code", which can be procesed
14 (in the case in which indent is composed by spaces or "--").
15 Userdata and function keys and values are shown as strings,
16 which logically are exactly not equivalent to the original code.
17
18 This routine can serve for pretty formating tables with
19 proper indentations, apart from printing them:
20
21 print(table.show(t, "t")) -- a typical use
22
23 Heavily based on "Saving tables with cycles", PIL2, p. 113.
24
25 Arguments:
26 t is the table.
27 name is the name of the table (optional)
28 indent is a first indentation (optional).
29 --]]
30 local debug = require('debug')
31 local dbg_getfenv = debug.getfenv or debug.getuservalue
32
33 function table.show(t, name, indent)
34 local cart -- a container
35 local autoref -- for self references
36
37 --[[ counts the number of elements in a table
38 local function tablecount(t)
39 local n = 0
40 for _, _ in pairs(t) do n = n+1 end
41 return n
42 end
43 ]]
44 -- (RiciLake) returns true if the table is empty
45 local function isemptytable(t) return type(t) == "table" and next(t) == nil end
46
47 local function basicSerialize (o)
48 local so = tostring(o)
49 if type(o) == "function" then
50 local info = debug.getinfo(o, "S")
51 -- info.name is nil because o is not a calling level
52 if info.what == "C" then
53 return string.format("%q", so .. ", C function")
54 else
55 -- the information is defined through lines
56 return string.format("%q", so .. ", defined in (" ..
57 info.linedefined .. "-" .. info.lastlinedefined ..
58 ")" .. info.source)
59 end
60 elseif type(o) == "number" or type(o) == "boolean" then
61 return so
62 else
63 return string.format("%q", so)
64 end
65 end
66
67 local function addtocart (value, name, indent, saved, field)
68 indent = indent or ""
69 saved = saved or {}
70 field = field or name
71
72 cart = cart .. indent .. field
73
74 if type(value) == "table" then
75 if saved[value] then
76 cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n"
77 autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
78 else
79 saved[value] = name
80 --if tablecount(value) == 0 then
81 if isemptytable(value) then
82 cart = cart .. " = {};\n"
83 else
84 cart = cart .. " = {\n"
85 for k, v in pairs(value) do
86 k = basicSerialize(k)
87 local fname = string.format("%s[%s]", name, k)
88 field = string.format("[%s]", k)
89 -- three spaces between levels
90 addtocart(v, fname, indent .. " ", saved, field)
91 end
92 for k, v in pairs{env = dbg_getfenv(value), mt = debug.getmetatable(value)} do
93 k = basicSerialize(k)
94 local fname = string.format("%s[%s]", name, k)
95 field = string.format("[%s]", k)
96 -- three spaces between levels
97 addtocart(v, fname, indent .. " ", saved, field)
98 end
99 cart = cart .. indent .. "};\n"
100 end
101 end
102 elseif type(value) == "userdata" then
103 if saved[value] then
104 cart = cart .. " = " .. basicSerialize(value) .. "; -- " .. saved[value] .. " (self reference)\n"
105 autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
106 else
107 saved[value] = name
108 cart = cart .. " = " .. basicSerialize(value) .. " {\n"
109 for k, v in pairs{env = dbg_getfenv(value), mt = debug.getmetatable(value)} do
110 k = basicSerialize(k)
111 local fname = string.format("%s[%s]", name, k)
112 field = string.format("[%s]", k)
113 -- three spaces between levels
114 addtocart(v, fname, indent .. " ", saved, field)
115 end
116 cart = cart .. indent .. "};\n"
117 end
118 elseif type(value) == "function" then
119 cart = cart .. " = " .. basicSerialize(value)
120 if debug.getupvalue(value, 1) == nil then
121 cart = cart .. ";\n"
122 else
123 cart = cart .. " {\n"
124 local i = 1
125 while true do
126 local k, v = debug.getupvalue(value, i)
127 if k == nil and v == nil then break end
128 k = basicSerialize(i)
129 local fname = string.format("%s[%s]", name, k)
130 field = string.format("[%s]", k)
131 -- three spaces between levels
132 addtocart(v, fname, indent .. " ", saved, field)
133 i = i + 1
134 end
135 cart = cart .. indent .. "};\n"
136 end
137 else
138 cart = cart .. " = " .. basicSerialize(value) .. ";\n"
139 end
140 end
141
142 name = name or "__unnamed__"
143 if type(t) ~= "table" and type(t) ~= 'userdata' then
144 return name .. " = " .. basicSerialize(t)
145 end
146 cart, autoref = "", ""
147 addtocart(t, name, indent)
148 return cart .. autoref
149 end
150