annotate luabuild/luarocks-2.0.12/src/luarocks/util.lua @ 1130:54653afec7cc

<GreyKnight> (cd luabuild/luarocks-2.0.12; make)
author HackBot
date Fri, 14 Dec 2012 22:22:17 +0000
parents 87f6d05d4b4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1125
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
1
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
2 --- Assorted utilities for managing tables, plus a scheduler for rollback functions.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
3 -- Does not requires modules directly (only as locals
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
4 -- inside specific functions) to avoid interdependencies,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
5 -- as this is used in the bootstrapping stage of luarocks.cfg.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
6
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
7 local global_env = _G
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
8
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
9 module("luarocks.util", package.seeall)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
10
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
11 local scheduled_functions = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
12
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
13 --- Schedule a function to be executed upon program termination.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
14 -- This is useful for actions such as deleting temporary directories
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
15 -- or failure rollbacks.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
16 -- @param f function: Function to be executed.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
17 -- @param ... arguments to be passed to function.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
18 -- @return table: A token representing the scheduled execution,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
19 -- which can be used to remove the item later from the list.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
20 function schedule_function(f, ...)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
21 assert(type(f) == "function")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
22
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
23 local item = { fn = f, args = {...} }
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
24 table.insert(scheduled_functions, item)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
25 return item
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
26 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
27
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
28 --- Unschedule a function.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
29 -- This is useful for cancelling a rollback of a completed operation.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
30 -- @param item table: The token representing the scheduled function that was
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
31 -- returned from the schedule_function call.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
32 function remove_scheduled_function(item)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
33 for k, v in pairs(scheduled_functions) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
34 if v == item then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
35 table.remove(scheduled_functions, k)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
36 return
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
37 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
38 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
39 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
40
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
41 --- Execute scheduled functions.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
42 -- Some calls create temporary files and/or directories and register
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
43 -- corresponding cleanup functions. Calling this function will run
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
44 -- these function, erasing temporaries.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
45 -- Functions are executed in the inverse order they were scheduled.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
46 function run_scheduled_functions()
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
47 local fs = require("luarocks.fs")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
48 fs.change_dir_to_root()
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
49 for i = #scheduled_functions, 1, -1 do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
50 local item = scheduled_functions[i]
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
51 item.fn(unpack(item.args))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
52 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
53 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
54
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
55 --- Extract flags from an arguments list.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
56 -- Given string arguments, extract flag arguments into a flags set.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
57 -- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz",
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
58 -- it would return the following:
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
59 -- {["bla"] = true, ["tux"] = "beep", ["baz"] = true}, "foo", "bar".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
60 function parse_flags(...)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
61 local args = {...}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
62 local flags = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
63 for i = #args, 1, -1 do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
64 local flag = args[i]:match("^%-%-(.*)")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
65 if flag then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
66 local var,val = flag:match("([a-z_%-]*)=(.*)")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
67 if val then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
68 flags[var] = val
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
69 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
70 flags[flag] = true
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
71 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
72 table.remove(args, i)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
73 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
74 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
75 return flags, unpack(args)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
76 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
77
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
78 --- Merges contents of src on top of dst's contents.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
79 -- @param dst Destination table, which will receive src's contents.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
80 -- @param src Table which provides new contents to dst.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
81 -- @see platform_overrides
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
82 function deep_merge(dst, src)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
83 for k, v in pairs(src) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
84 if type(v) == "table" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
85 if not dst[k] then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
86 dst[k] = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
87 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
88 if type(dst[k]) == "table" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
89 deep_merge(dst[k], v)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
90 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
91 dst[k] = v
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
92 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
93 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
94 dst[k] = v
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
95 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
96 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
97 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
98
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
99 --- Perform platform-specific overrides on a table.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
100 -- Overrides values of table with the contents of the appropriate
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
101 -- subset of its "platforms" field. The "platforms" field should
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
102 -- be a table containing subtables keyed with strings representing
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
103 -- platform names. Names that match the contents of the global
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
104 -- cfg.platforms setting are used. For example, if
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
105 -- cfg.platforms= {"foo"}, then the fields of
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
106 -- tbl.platforms.foo will overwrite those of tbl with the same
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
107 -- names. For table values, the operation is performed recursively
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
108 -- (tbl.platforms.foo.x.y.z overrides tbl.x.y.z; other contents of
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
109 -- tbl.x are preserved).
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
110 -- @param tbl table or nil: Table which may contain a "platforms" field;
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
111 -- if it doesn't (or if nil is passed), this function does nothing.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
112 function platform_overrides(tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
113 assert(type(tbl) == "table" or not tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
114
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
115 local cfg = require("luarocks.cfg")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
116
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
117 if not tbl then return end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
118
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
119 if tbl.platforms then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
120 for _, platform in ipairs(cfg.platforms) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
121 local platform_tbl = tbl.platforms[platform]
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
122 if platform_tbl then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
123 deep_merge(tbl, platform_tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
124 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
125 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
126 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
127 tbl.platforms = nil
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
128 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
129
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
130 local var_format_pattern = "%$%((%a[%a%d_]+)%)"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
131
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
132 --- Create a new shallow copy of a table: a new table with
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
133 -- the same keys and values. Keys point to the same objects as
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
134 -- the original table (ie, does not copy recursively).
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
135 -- @param tbl table: the input table
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
136 -- @return table: a new table with the same contents.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
137 local function make_shallow_copy(tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
138 local copy = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
139 for k,v in pairs(tbl) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
140 copy[k] = v
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
141 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
142 return copy
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
143 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
144
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
145 -- Check if a set of needed variables are referenced
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
146 -- somewhere in a list of definitions, warning the user
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
147 -- about any unused ones. Each key in needed_set should
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
148 -- appear as a $(XYZ) variable at least once as a
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
149 -- substring of some value of var_defs.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
150 -- @param var_defs: a table with string keys and string
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
151 -- values, containing variable definitions.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
152 -- @param needed_set: a set where keys are the names of
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
153 -- needed variables.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
154 -- @param msg string: the warning message to display.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
155 function warn_if_not_used(var_defs, needed_set, msg)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
156 needed_set = make_shallow_copy(needed_set)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
157 for var,val in pairs(var_defs) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
158 for used in val:gmatch(var_format_pattern) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
159 needed_set[used] = nil
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
160 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
161 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
162 for var,_ in pairs(needed_set) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
163 warning(msg:format(var))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
164 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
165 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
166
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
167 -- Output any entries that might remain in $(XYZ) format,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
168 -- warning the user that substitutions have failed.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
169 -- @param line string: the input string
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
170 local function warn_failed_matches(line)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
171 local any_failed = false
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
172 if line:match(var_format_pattern) then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
173 for unmatched in line:gmatch(var_format_pattern) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
174 warning("unmatched variable " .. unmatched)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
175 any_failed = true
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
176 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
177 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
178 return any_failed
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
179 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
180
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
181 --- Perform make-style variable substitutions on string values of a table.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
182 -- For every string value tbl.x which contains a substring of the format
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
183 -- "$(XYZ)" will have this substring replaced by vars["XYZ"], if that field
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
184 -- exists in vars. Only string values are processed; this function
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
185 -- does not scan subtables recursively.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
186 -- @param tbl table: Table to have its string values modified.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
187 -- @param vars table: Table containing string-string key-value pairs
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
188 -- representing variables to replace in the strings values of tbl.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
189 function variable_substitutions(tbl, vars)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
190 assert(type(tbl) == "table")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
191 assert(type(vars) == "table")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
192
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
193 local updated = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
194 for k, v in pairs(tbl) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
195 if type(v) == "string" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
196 updated[k] = v:gsub(var_format_pattern, vars)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
197 if warn_failed_matches(updated[k]) then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
198 updated[k] = updated[k]:gsub(var_format_pattern, "")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
199 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
200 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
201 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
202 for k, v in pairs(updated) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
203 tbl[k] = v
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
204 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
205 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
206
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
207 --- Return an array of keys of a table.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
208 -- @param tbl table: The input table.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
209 -- @return table: The array of keys.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
210 function keys(tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
211 local ks = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
212 for k,_ in pairs(tbl) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
213 table.insert(ks, k)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
214 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
215 return ks
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
216 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
217
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
218 local function default_sort(a, b)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
219 local ta = type(a)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
220 local tb = type(b)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
221 if ta == "number" and tb == "number" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
222 return a < b
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
223 elseif ta == "number" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
224 return true
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
225 elseif tb == "number" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
226 return false
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
227 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
228 return tostring(a) < tostring(b)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
229 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
230 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
231
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
232 -- The iterator function used internally by util.sortedpairs.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
233 -- @param tbl table: The table to be iterated.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
234 -- @param sort_function function or nil: An optional comparison function
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
235 -- to be used by table.sort when sorting keys.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
236 -- @see sortedpairs
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
237 local function sortedpairs_iterator(tbl, sort_function)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
238 local ks = keys(tbl)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
239 if not sort_function or type(sort_function) == "function" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
240 table.sort(ks, sort_function or default_sort)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
241 for _, k in ipairs(ks) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
242 coroutine.yield(k, tbl[k])
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
243 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
244 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
245 local order = sort_function
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
246 local done = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
247 for _, k in ipairs(order) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
248 local sub_order
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
249 if type(k) == "table" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
250 sub_order = k[2]
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
251 k = k[1]
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
252 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
253 if tbl[k] then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
254 done[k] = true
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
255 coroutine.yield(k, tbl[k], sub_order)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
256 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
257 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
258 table.sort(ks, default_sort)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
259 for _, k in ipairs(ks) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
260 if not done[k] then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
261 coroutine.yield(k, tbl[k])
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
262 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
263 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
264 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
265 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
266
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
267 --- A table iterator generator that returns elements sorted by key,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
268 -- to be used in "for" loops.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
269 -- @param tbl table: The table to be iterated.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
270 -- @param sort_function function or table or nil: An optional comparison function
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
271 -- to be used by table.sort when sorting keys, or an array listing an explicit order
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
272 -- for keys. If a value itself is an array, it is taken so that the first element
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
273 -- is a string representing the field name, and the second element is a priority table
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
274 -- for that key.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
275 -- @return function: the iterator function.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
276 function sortedpairs(tbl, sort_function)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
277 return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
278 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
279
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
280 function starts_with(s, prefix)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
281 return s:sub(1,#prefix) == prefix
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
282 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
283
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
284 --- Print a line to standard output
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
285 function printout(...)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
286 io.stdout:write(table.concat({...},"\t"))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
287 io.stdout:write("\n")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
288 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
289
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
290 --- Print a line to standard error
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
291 function printerr(...)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
292 io.stderr:write(table.concat({...},"\t"))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
293 io.stderr:write("\n")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
294 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
295
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
296 --- Display a warning message.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
297 -- @param msg string: the warning message
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
298 function warning(msg)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
299 printerr("Warning: "..msg)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
300 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
301
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
302 function title(msg, porcelain, underline)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
303 if porcelain then return end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
304 printout()
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
305 printout(msg)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
306 printout((underline or "-"):rep(#msg))
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
307 printout()
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
308 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
309
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
310 -- from http://lua-users.org/wiki/SplitJoin
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
311 -- by PhilippeLhoste
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
312 function split_string(str, delim, maxNb)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
313 -- Eliminate bad cases...
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
314 if string.find(str, delim) == nil then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
315 return { str }
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
316 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
317 if maxNb == nil or maxNb < 1 then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
318 maxNb = 0 -- No limit
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
319 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
320 local result = {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
321 local pat = "(.-)" .. delim .. "()"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
322 local nb = 0
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
323 local lastPos
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
324 for part, pos in string.gmatch(str, pat) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
325 nb = nb + 1
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
326 result[nb] = part
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
327 lastPos = pos
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
328 if nb == maxNb then break end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
329 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
330 -- Handle the last field
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
331 if nb ~= maxNb then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
332 result[nb + 1] = string.sub(str, lastPos)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
333 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
334 return result
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
335 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
336
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
337 --- Remove repeated entries from a path-style string.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
338 -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d".
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
339 -- @param list string: A path string (from $PATH or package.path)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
340 -- @param sep string: The separator
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
341 function remove_path_dupes(list, sep)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
342 assert(type(list) == "string")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
343 assert(type(sep) == "string")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
344 local parts = split_string(list, sep)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
345 local final, entries = {}, {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
346 for _, part in ipairs(parts) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
347 if not entries[part] then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
348 table.insert(final, part)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
349 entries[part] = true
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
350 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
351 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
352 return table.concat(final, sep)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
353 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
354
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
355 ---
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
356 -- Formats tables with cycles recursively to any depth.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
357 -- References to other tables are shown as values.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
358 -- Self references are indicated.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
359 -- The string returned is "Lua code", which can be procesed
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
360 -- (in the case in which indent is composed by spaces or "--").
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
361 -- Userdata and function keys and values are shown as strings,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
362 -- which logically are exactly not equivalent to the original code.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
363 -- This routine can serve for pretty formating tables with
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
364 -- proper indentations, apart from printing them:
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
365 -- io.write(table.show(t, "t")) -- a typical use
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
366 -- Written by Julio Manuel Fernandez-Diaz,
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
367 -- Heavily based on "Saving tables with cycles", PIL2, p. 113.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
368 -- @param t table: is the table.
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
369 -- @param name string: is the name of the table (optional)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
370 -- @param indent string: is a first indentation (optional).
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
371 -- @return string: the pretty-printed table
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
372 function show_table(t, name, indent)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
373 local cart -- a container
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
374 local autoref -- for self references
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
375
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
376 local function isemptytable(t) return next(t) == nil end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
377
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
378 local function basicSerialize (o)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
379 local so = tostring(o)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
380 if type(o) == "function" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
381 local info = debug.getinfo(o, "S")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
382 -- info.name is nil because o is not a calling level
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
383 if info.what == "C" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
384 return ("%q"):format(so .. ", C function")
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
385 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
386 -- the information is defined through lines
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
387 return ("%q"):format(so .. ", defined in (" .. info.linedefined .. "-" .. info.lastlinedefined .. ")" .. info.source)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
388 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
389 elseif type(o) == "number" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
390 return so
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
391 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
392 return ("%q"):format(so)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
393 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
394 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
395
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
396 local function addtocart (value, name, indent, saved, field)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
397 indent = indent or ""
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
398 saved = saved or {}
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
399 field = field or name
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
400
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
401 cart = cart .. indent .. field
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
402
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
403 if type(value) ~= "table" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
404 cart = cart .. " = " .. basicSerialize(value) .. ";\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
405 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
406 if saved[value] then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
407 cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
408 autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
409 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
410 saved[value] = name
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
411 --if tablecount(value) == 0 then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
412 if isemptytable(value) then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
413 cart = cart .. " = {};\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
414 else
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
415 cart = cart .. " = {\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
416 for k, v in pairs(value) do
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
417 k = basicSerialize(k)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
418 local fname = ("%s[%s]"):format(name, k)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
419 field = ("[%s]"):format(k)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
420 -- three spaces between levels
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
421 addtocart(v, fname, indent .. " ", saved, field)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
422 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
423 cart = cart .. indent .. "};\n"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
424 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
425 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
426 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
427 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
428
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
429 name = name or "__unnamed__"
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
430 if type(t) ~= "table" then
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
431 return name .. " = " .. basicSerialize(t)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
432 end
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
433 cart, autoref = "", ""
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
434 addtocart(t, name, indent)
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
435 return cart .. autoref
87f6d05d4b4a <GreyKnight> (cd luabuild; tar xf luarocks-2.0.12.tar.gz)
HackBot
parents:
diff changeset
436 end