Thu, 23 Mar 2023 15:30:32 +0000
Remove dependency on util.iterators
I mistakenly thought it was bundled with verse.
171
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | local function filter_expression(script, s) |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local expr = s:match("^%$%{(.+)%}$"); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | if not expr then return s end |
173
14ed4cb241f4
scansion: Support for per-script captures
Matthew Wild <mwild1@gmail.com>
parents:
171
diff
changeset
|
4 | if script.captures and script.captures[expr] then |
14ed4cb241f4
scansion: Support for per-script captures
Matthew Wild <mwild1@gmail.com>
parents:
171
diff
changeset
|
5 | return script.captures[expr]; |
14ed4cb241f4
scansion: Support for per-script captures
Matthew Wild <mwild1@gmail.com>
parents:
171
diff
changeset
|
6 | end |
171
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | local name, value_name = expr:match("^(.+)'s (.+)$"); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | assert(name, "Unable to parse expression: "..expr); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | local key = value_name:lower():gsub(" ", "_"); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | assert(script.objects[name], "Unknown object called "..name); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local value = script.objects[name][key]; |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | return value; |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | end |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | local function fill_vars(script, stanza) |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | for k, v in pairs(stanza.attr) do |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | stanza.attr[k] = filter_expression(script, v); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | end |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | for i, child in ipairs(stanza) do |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | if type(child) == "string" then |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | stanza[i] = filter_expression(script, child); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | elseif type(child) == "table" then |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | fill_vars(script, child); |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | end |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | end |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | return stanza; |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | end |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
178
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
30 | local function sorted_pairs(t, sort_func) |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
31 | local keys = {}; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
32 | for k in pairs(t) do table.insert(keys, k); end |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
33 | table.sort(keys, sort_func); |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
34 | local i = 0; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
35 | return function () |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
36 | i = i + 1; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
37 | local key = keys[i]; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
38 | if key ~= nil then |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
39 | return key, t[key]; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
40 | end |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
41 | end; |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
42 | end |
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
43 | |
171
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | return { |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | filter_expression = filter_expression; |
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | fill_vars = fill_vars; |
178
e547ddf8b64d
Remove dependency on util.iterators
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
47 | sorted_pairs = sorted_pairs; |
171
433a1f36d0d3
client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | }; |