scansion/helpers.lua

Thu, 23 Mar 2023 15:09:10 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 15:09:10 +0000
changeset 173
14ed4cb241f4
parent 171
433a1f36d0d3
child 178
e547ddf8b64d
permissions
-rw-r--r--

scansion: Support for per-script captures

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
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return {
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 filter_expression = filter_expression;
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 fill_vars = fill_vars;
433a1f36d0d3 client: Move some generic utility functions to a helpers module
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 };

mercurial