diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2013-05-23 21:46:41 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2013-05-23 21:55:41 +0100 |
commit | ba274d571b3e36b8ab0ea62981709947d848922a (patch) | |
tree | 665b7d911856c99567e9831e16d86c97782fb132 | |
parent | 022da29a4be6caadbb55a8f5e481778e566b5ffb (diff) | |
download | gitano-ba274d571b3e36b8ab0ea62981709947d848922a.tar.gz |
LACE: Update simple match with generic matchers
This updates the simple match routine to have a table of match functions.
Initially this only supports the exact and pattern matches, but we can add
more.
-rw-r--r-- | lib/gitano/lace.lua | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/lib/gitano/lace.lua b/lib/gitano/lace.lua index 4927e1c..926d49e 100644 --- a/lib/gitano/lace.lua +++ b/lib/gitano/lace.lua @@ -44,46 +44,46 @@ local function _loader(ctx, _name) return real_name, tree[blob_name].obj.content end -local matchers = {} +local match_types = { + exact = function(want, have) + return want == have + end, + pattern = function(want, have) + return (have:match(want) ~= nil) + end +} + +do + local inverted_matches = {} + for k, v in pairs(match_types) do + inverted_matches["!" .. k] = function(...) return not v(...) end + end + for k, v in pairs(inverted_matches) do + match_types[k] = v + end +end local function _do_simple_match(ctx, key, matchtype, value) value = util.process_expansion(ctx, value) - local inv = matchtype:sub(1,1) == "!" - if inv then - matchtype = matchtype:sub(2) - end - local pat = (matchtype == "pattern") and value local kk = ctx[key] or "" - local function check(v) - if pat then - if inv then - return (v:match(pat) == nil) - else - return (v:match(pat) ~= nil) - end - end - if inv then - return v ~= value - end - return v == value - end + local check = match_types[matchtype] if type(kk) == "function" then -- Realise the value first ctx[key] = kk(ctx) kk = ctx[key] end if type(kk) == "string" then - return check(kk) + return check(value, kk) else if pat and kk[1] ~= nil then for i = 1, #kk do - if check(kk[i]) then + if check(value, kk[i]) then return true end end return false else - if inv then + if matchtype:sub(1,1) == "!" then return kk[value] == nil end return kk[value] ~= nil @@ -114,6 +114,9 @@ local function _simple_match(ctx, key, matchtype, value, guard) matchtype = matchtype .. "exact" end end + if match_types[matchtype] == nil then + return lace.compiler.error("Unknown match type", {2}) + end return { fn = _do_simple_match, args = { key, matchtype, util.prep_expansion(value) } @@ -138,6 +141,8 @@ local simples = { "member/prefix", "member/suffix", } +local matchers = {} + for _, s in ipairs(simples) do matchers[s] = _simple_match end |