1 -------------------------------------------------------------------------------
2 -- Copyright (c) 2006-2013 Fabien Fleutot and others.
4 -- All rights reserved.
6 -- This program and the accompanying materials are made available
7 -- under the terms of the Eclipse Public License v1.0 which
8 -- accompanies this distribution, and is available at
9 -- http://www.eclipse.org/legal/epl-v10.html
11 -- This program and the accompanying materials are also made available
12 -- under the terms of the MIT public license which accompanies this
13 -- distribution, and is available at http://www.lua.org/license.html
16 -- Fabien Fleutot - API and implementation
18 ----------------------------------------------------------------------
20 ----------------------------------------------------------------------
21 ----------------------------------------------------------------------
23 -- Lua objects pretty-printer
25 ----------------------------------------------------------------------
26 ----------------------------------------------------------------------
31 hide_hash = false; -- Print the non-array part of tables?
32 metalua_tag = true; -- Use Metalua's backtick syntax sugar?
33 fix_indent = nil; -- If a number, number of indentation spaces;
34 -- If false, indent to the previous brace.
35 line_max = nil; -- If a number, tries to avoid making lines with
36 -- more than this number of chars.
37 initial_indent = 0; -- If a number, starts at this level of indentation
38 keywords = { }; -- Set of keywords which must not use Lua's field
39 -- shortcuts {["foo"]=...} -> {foo=...}
42 local function valid_id(cfg, x)
43 if type(x) ~= "string" then return false end
44 if not x:match "^[a-zA-Z_][a-zA-Z0-9_]*$" then return false end
45 if cfg.keywords and cfg.keywords[x] then return false end
49 local __tostring_cache = setmetatable({ }, {__mode='k'})
51 -- Retrieve the string produced by `__tostring` metamethod if present,
52 -- return `false` otherwise. Cached in `__tostring_cache`.
53 local function __tostring(x)
54 local the_string = __tostring_cache[x]
55 if the_string~=nil then return the_string end
56 local mt = getmetatable(x)
58 local __tostring = mt.__tostring
60 the_string = __tostring(x)
61 __tostring_cache[x] = the_string
65 if x~=nil then __tostring_cache[x] = false end -- nil is an illegal key
69 local xlen -- mutually recursive with `xlen_type`
71 local xlen_cache = setmetatable({ }, {__mode='k'})
73 -- Helpers for the `xlen` function
75 ["nil"] = function ( ) return 3 end;
76 number = function (x) return #tostring(x) end;
77 boolean = function (x) return x and 4 or 5 end;
78 string = function (x) return #string.format("%q",x) end;
81 function xlen_type.table (adt, cfg, nested)
82 local custom_string = __tostring(adt)
83 if custom_string then return #custom_string end
85 -- Circular referenced objects are printed with the plain
86 -- `tostring` function in nested positions.
87 if nested [adt] then return #tostring(adt) end
90 local has_tag = cfg.metalua_tag and valid_id(cfg, adt.tag)
92 local has_arr = alen>0
93 local has_hash = false
96 if not cfg.hide_hash then
97 -- first pass: count hash-part
98 for k, v in pairs(adt) do
99 if k=="tag" and has_tag then
100 -- this is the tag -> do nothing!
101 elseif type(k)=="number" and k<=alen and math.fmod(k,1)==0 and k>0 then
102 -- array-part pair -> do nothing!
105 if valid_id(cfg, k) then x=x+#k
106 else x = x + xlen (k, cfg, nested) + 2 end -- count surrounding brackets
107 x = x + xlen (v, cfg, nested) + 5 -- count " = " and ", "
112 for i = 1, alen do x = x + xlen (adt[i], nested) + 2 end -- count ", "
114 nested[adt] = false -- No more nested calls
116 if not (has_tag or has_arr or has_hash) then return 3 end
117 if has_tag then x=x+#adt.tag+1 end
118 if not (has_arr or has_hash) then return x end
119 if not has_hash and alen==1 and type(adt[1])~="table" then
120 return x-2 -- substract extraneous ", "
122 return x+2 -- count "{ " and " }", substract extraneous ", "
126 -- Compute the number of chars it would require to display the table
127 -- on a single line. Helps to decide whether some carriage returns are
128 -- required. Since the size of each sub-table is required many times,
129 -- it's cached in [xlen_cache].
130 xlen = function (x, cfg, nested)
131 -- no need to compute length for 1-line prints
132 if not cfg.line_max then return 0 end
133 nested = nested or { }
134 if x==nil then return #"nil" end
135 local len = xlen_cache[x]
136 if len then return len end
137 local f = xlen_type[type(x)]
138 if not f then return #tostring(x) end
139 len = f (x, cfg, nested)
144 local function consider_newline(p, len)
145 if not p.cfg.line_max then return end
146 if p.current_offset + len <= p.cfg.line_max then return end
147 if p.indent < p.current_offset then
148 p:acc "\n"; p:acc ((" "):rep(p.indent))
149 p.current_offset = p.indent
156 ["nil"] = function(p) p:acc("nil") end;
157 number = function(p, adt) p:acc (tostring (adt)) end;
158 string = function(p, adt) p:acc ((string.format ("%q", adt):gsub("\\\n", "\\n"))) end;
159 boolean = function(p, adt) p:acc (adt and "true" or "false") end }
162 -- * if `cfg.fix_indent` is set to a number:
163 -- * add this number of space for each level of depth
164 -- * return to the line as soon as it flushes things further left
165 -- * if not, tabulate to one space after the opening brace.
166 -- * as a result, it never saves right-space to return before first element
168 function acc_type.table(p, adt)
169 if p.nested[adt] then p:acc(tostring(adt)); return end
172 local has_tag = p.cfg.metalua_tag and valid_id(p.cfg, adt.tag)
174 local has_arr = alen>0
175 local has_hash = false
177 local previous_indent = p.indent
179 if has_tag then p:acc("`"); p:acc(adt.tag) end
181 local function indent(p)
182 if not p.cfg.fix_indent then p.indent = p.current_offset
183 else p.indent = p.indent + p.cfg.fix_indent end
186 -- First pass: handle hash-part
187 if not p.cfg.hide_hash then
188 for k, v in pairs(adt) do
190 if has_tag and k=='tag' then -- pass the 'tag' field
191 elseif type(k)=="number" and k<=alen and k>0 and math.fmod(k,1)==0 then
192 -- pass array-part keys (consecutive ints less than `#adt`)
193 else -- hash-part keys
194 if has_hash then p:acc ", " else -- 1st hash-part pair ever found
195 p:acc "{ "; indent(p)
198 -- Determine whether a newline is required
199 local is_id, expected_len=valid_id(p.cfg, k)
200 if is_id then expected_len=#k+xlen(v, p.cfg, p.nested)+#" = , "
201 else expected_len = xlen(k, p.cfg, p.nested)+xlen(v, p.cfg, p.nested)+#"[] = , " end
202 consider_newline(p, expected_len)
205 if is_id then p:acc(k); p:acc " = " else
206 p:acc "["; acc_value (p, k); p:acc "] = "
209 acc_value (p, v) -- Print the value
215 -- Now we know whether there's a hash-part, an array-part, and a tag.
216 -- Tag and hash-part are already printed if they're present.
217 if not has_tag and not has_hash and not has_arr then p:acc "{ }";
218 elseif has_tag and not has_hash and not has_arr then -- nothing, tag already in acc
220 assert (has_hash or has_arr) -- special case { } already handled
221 local no_brace = false
222 if has_hash and has_arr then p:acc ", "
223 elseif has_tag and not has_hash and alen==1 and type(adt[1])~="table" then
224 -- No brace required; don't print "{", remember not to print "}"
225 p:acc (" "); acc_value (p, adt[1]) -- indent= indent+(cfg.fix_indent or 0))
227 elseif not has_hash then
228 -- Braces required, but not opened by hash-part handler yet
229 p:acc "{ "; indent(p)
232 -- 2nd pass: array-part
233 if not no_brace and has_arr then
234 local expected_len = xlen(adt[1], p.cfg, p.nested)
235 consider_newline(p, expected_len)
236 acc_value(p, adt[1]) -- indent+(cfg.fix_indent or 0)
239 consider_newline(p, xlen(adt[i], p.cfg, p.nested))
240 acc_value (p, adt[i]) --indent+(cfg.fix_indent or 0)
243 if not no_brace then p:acc " }" end
245 p.nested[adt] = false -- No more nested calls
246 p.indent = previous_indent
250 function acc_value(p, v)
251 local custom_string = __tostring(v)
252 if custom_string then p:acc(custom_string) else
253 local f = acc_type[type(v)]
254 if f then f(p, v) else p:acc(tostring(v)) end
259 -- FIXME: new_indent seems to be always nil?!s detection
260 -- FIXME: accumulator function should be configurable,
261 -- so that print() doesn't need to bufferize the whole string
262 -- before starting to print.
263 function M.tostring(t, cfg)
265 cfg = cfg or M.DEFAULT_CFG or { }
270 current_offset = cfg.initial_indent or 0;
273 acc = function(self, str)
274 table.insert(self.buffer, str)
275 self.current_offset = self.current_offset + #str
279 return table.concat(p.buffer)
282 function M.print(...) return print(M.tostring(...)) end
283 function M.sprintf(fmt, ...)
285 for i, v in pairs(args) do
287 if t=='table' then args[i]=M.tostring(v)
288 elseif t=='nil' then args[i]='nil' end
290 return string.format(fmt, unpack(args))
293 function M.printf(...) print(M.sprintf(...)) end