|
|
|
@ -18,18 +18,19 @@ local strings = {} -- not to be returned. Used for split() function.
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
/*
|
|
|
|
|
* CONSTANTS
|
|
|
|
|
* CLASS `ParseTable`
|
|
|
|
|
*/
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
-- The number corresponding to <CR> in vim.
|
|
|
|
|
ParseTable.CR = 13
|
|
|
|
|
|
|
|
|
|
------------------------------------
|
|
|
|
|
--[[ SUMMARY:
|
|
|
|
|
* Split some `str` using a regex `pattern`.
|
|
|
|
|
]]
|
|
|
|
|
--[[
|
|
|
|
|
/*
|
|
|
|
|
* f(x)
|
|
|
|
|
*/
|
|
|
|
|
--]]
|
|
|
|
|
* `str` => the string to split.
|
|
|
|
|
* `pattern` => the regex pattern to split `str` with.
|
|
|
|
|
]]
|
|
|
|
|
------------------------------------
|
|
|
|
|
function strings.split(str, pattern)
|
|
|
|
|
local split = {}
|
|
|
|
|
for char in string.gmatch(str, pattern) do
|
|
|
|
@ -38,18 +39,77 @@ function strings.split(str, pattern)
|
|
|
|
|
return split
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-------------------------
|
|
|
|
|
--[[
|
|
|
|
|
/*
|
|
|
|
|
* CLASS `ParseTable`
|
|
|
|
|
*/
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
-- The number corresponding to <CR> in vim.
|
|
|
|
|
ParseTable.CR = 13
|
|
|
|
|
|
|
|
|
|
----------------------------------
|
|
|
|
|
--[[ SUMMARY:
|
|
|
|
|
* Create a new parse table from a user-defined table.
|
|
|
|
|
]]
|
|
|
|
|
--[[ PARAMS:
|
|
|
|
|
* `userTable` => the table of combos defined by the user.
|
|
|
|
|
]]
|
|
|
|
|
-------------------------
|
|
|
|
|
function ParseTable:new(userTable)
|
|
|
|
|
----------------------------------
|
|
|
|
|
function ParseTable.new(userTable)
|
|
|
|
|
local parseTable = {}
|
|
|
|
|
|
|
|
|
|
-------------------------------
|
|
|
|
|
----------------------------
|
|
|
|
|
--[[ SUMMARY:
|
|
|
|
|
* Get a value from this `ParseTable`.
|
|
|
|
|
]]
|
|
|
|
|
--[[ PARAMS:
|
|
|
|
|
* `key` => the PARSED key to get.
|
|
|
|
|
]]
|
|
|
|
|
--[[
|
|
|
|
|
* `function` => when `key` is a full match.
|
|
|
|
|
* `table` => when the `key` partially mathes.
|
|
|
|
|
* `false` => when `key` is not ANYWHERE.
|
|
|
|
|
]]
|
|
|
|
|
----------------------------
|
|
|
|
|
function parseTable:get(key)
|
|
|
|
|
local function parseGet(dict, splitKey)
|
|
|
|
|
--[[ Get the next character in the combo string. ]]
|
|
|
|
|
|
|
|
|
|
local k = ''
|
|
|
|
|
if #splitKey > 0 then -- There is more input to parse
|
|
|
|
|
k = api.nvim_eval("char2nr('" .. table.remove(splitKey) .. "')")
|
|
|
|
|
else -- The user input has run out, but there is more in the dictionary.
|
|
|
|
|
return dict
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--[[ Parse the `k`. ]]
|
|
|
|
|
|
|
|
|
|
-- Make sure the dicitonary has a key for that value.
|
|
|
|
|
if dict[k] then
|
|
|
|
|
val = dict[k]
|
|
|
|
|
local valType = type(val)
|
|
|
|
|
|
|
|
|
|
if valType == globals.TYPE_TBL then
|
|
|
|
|
if val[ParseTable.CR] and #splitKey < 1 then
|
|
|
|
|
return val
|
|
|
|
|
else
|
|
|
|
|
return parseGet(val, splitKey)
|
|
|
|
|
end
|
|
|
|
|
elseif valType == globals.TYPE_STR and #splitKey < 1 then
|
|
|
|
|
return val
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- run the inner recursive function in order to return the desired result
|
|
|
|
|
return parseGet(self, strings.split(
|
|
|
|
|
string.reverse(key), '.'
|
|
|
|
|
))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
|
--[[ SUMMARY:
|
|
|
|
|
* Put `value` into the parse tree as `key`.
|
|
|
|
|
]]
|
|
|
|
@ -57,7 +117,7 @@ function ParseTable:new(userTable)
|
|
|
|
|
* `key` => the key that `value` is reffered to by.
|
|
|
|
|
* `value` => the value to store as `key`.
|
|
|
|
|
]]
|
|
|
|
|
-------------------------------
|
|
|
|
|
----------------------------------------
|
|
|
|
|
function parseTable:parsePut(key, value)
|
|
|
|
|
-- Internal recursion function.
|
|
|
|
|
local function update(dict, splitKey) -- †
|
|
|
|
@ -91,14 +151,14 @@ function ParseTable:new(userTable)
|
|
|
|
|
))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-------------------------------
|
|
|
|
|
---------------------------------------------
|
|
|
|
|
--[[ SUMMARY:
|
|
|
|
|
* Create the union of `self` and `tableToUnite`
|
|
|
|
|
]]
|
|
|
|
|
--[[ PARAMS:
|
|
|
|
|
* `tableToUnite` => the table to unite with `self.`
|
|
|
|
|
]]
|
|
|
|
|
-------------------------------
|
|
|
|
|
---------------------------------------------
|
|
|
|
|
function parseTable:parsePutAll(tableToUnite)
|
|
|
|
|
for k, v in pairs(tableToUnite) do
|
|
|
|
|
self:parsePut(k, v)
|
|
|
|
@ -116,4 +176,5 @@ end
|
|
|
|
|
* PUBLICIZE MODULE
|
|
|
|
|
*/
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
return ParseTable
|
|
|
|
|