scansion/helpers.lua

Thu, 23 Mar 2023 11:51:31 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 11:51:31 +0000
changeset 171
433a1f36d0d3
child 173
14ed4cb241f4
permissions
-rw-r--r--

client: Move some generic utility functions to a helpers module

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
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 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
5 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
6 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
7 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
8 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
9 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
10 return value;
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 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
16 end
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 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
18 if type(child) == "string" then
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 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
20 elseif type(child) == "table" then
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 fill_vars(script, child);
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return stanza;
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
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return {
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 filter_expression = filter_expression;
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 fill_vars = fill_vars;
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 };

mercurial