You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
4.3 KiB
Lua
194 lines
4.3 KiB
Lua
4 years ago
|
--[[
|
||
|
/*
|
||
|
* IMPORTS
|
||
|
*/
|
||
|
--]]
|
||
|
|
||
4 years ago
|
local api = vim.api
|
||
4 years ago
|
local globals = require('libmodal/src/globals')
|
||
4 years ago
|
|
||
|
--[[
|
||
|
/*
|
||
|
* MODULE
|
||
|
*/
|
||
|
--]]
|
||
|
|
||
|
local ParseTable = {}
|
||
|
|
||
|
--[[
|
||
|
/*
|
||
4 years ago
|
* Utils for `ParseTable`
|
||
4 years ago
|
*/
|
||
|
--]]
|
||
|
|
||
4 years ago
|
--------------------------------------
|
||
|
--[[ SUMMARY:
|
||
|
* Split some `str` over a `regex`.
|
||
|
]]
|
||
|
--[[ PARAMS:
|
||
|
* `str` => the string to split.
|
||
|
* `regex` => the regex to split `str` with.
|
||
|
]]
|
||
|
--------------------------------------
|
||
|
local function _stringSplit(str, regex)
|
||
|
local split = {}
|
||
|
for char in string.gmatch(str, regex) do
|
||
|
split[#split + 1] = char
|
||
|
end
|
||
|
return split
|
||
|
end
|
||
|
|
||
4 years ago
|
---------------------------------
|
||
4 years ago
|
--[[ SUMMARY:
|
||
|
* Reverse the elements of some table.
|
||
|
]]
|
||
|
--[[ PARAMS:
|
||
|
* `tbl` => the table to reverse.
|
||
|
]]
|
||
4 years ago
|
---------------------------------
|
||
|
local function _tableReverse(tbl)
|
||
|
local reversed = {}
|
||
4 years ago
|
while #reversed < #tbl do
|
||
|
reversed[#reversed + 1] = tbl[#tbl - #reversed]
|
||
4 years ago
|
end
|
||
4 years ago
|
return reversed
|
||
4 years ago
|
end
|
||
|
|
||
4 years ago
|
--[[
|
||
|
/*
|
||
|
* CLASS `ParseTable`
|
||
|
*/
|
||
|
--]]
|
||
|
|
||
|
-- The number corresponding to <CR> in vim.
|
||
|
ParseTable.CR = 13
|
||
|
|
||
|
----------------------------------
|
||
4 years ago
|
--[[ SUMMARY:
|
||
|
* Create a new parse table from a user-defined table.
|
||
|
]]
|
||
|
--[[ PARAMS:
|
||
|
* `userTable` => the table of combos defined by the user.
|
||
|
]]
|
||
4 years ago
|
----------------------------------
|
||
|
function ParseTable.new(userTable)
|
||
4 years ago
|
local parseTable = {}
|
||
|
|
||
4 years ago
|
--------------------------------
|
||
4 years ago
|
--[[ 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.
|
||
|
]]
|
||
4 years ago
|
--------------------------------
|
||
4 years ago
|
function parseTable:get(keyDict)
|
||
4 years ago
|
local function parseGet(dict, splitKey)
|
||
4 years ago
|
--[[ Get the next character in the combo string. ]]
|
||
|
|
||
|
local k = ''
|
||
|
if #splitKey > 0 then -- There is more input to parse
|
||
4 years ago
|
k = table.remove(splitKey) -- the dict should already be `char2nr()`'d
|
||
4 years ago
|
else -- the user input has run out, but there is more in the dictionary.
|
||
4 years ago
|
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
|
||
4 years ago
|
return parseGet(val, splitKey)
|
||
4 years ago
|
end
|
||
|
elseif valType == globals.TYPE_STR and #splitKey < 1 then
|
||
|
return val
|
||
|
end
|
||
|
end
|
||
|
return false
|
||
|
end
|
||
|
|
||
4 years ago
|
--[[ Reverse the dict. ]]
|
||
4 years ago
|
local reversed = _tableReverse(keyDict)
|
||
4 years ago
|
|
||
|
--[[ Get return value. ]]
|
||
4 years ago
|
-- run the inner recursive function in order to return the desired result
|
||
4 years ago
|
return parseGet(self, reversed)
|
||
4 years ago
|
end
|
||
|
|
||
|
----------------------------------------
|
||
4 years ago
|
--[[ SUMMARY:
|
||
|
* Put `value` into the parse tree as `key`.
|
||
|
]]
|
||
|
--[[ PARAMS:
|
||
|
* `key` => the key that `value` is reffered to by.
|
||
|
* `value` => the value to store as `key`.
|
||
|
]]
|
||
4 years ago
|
----------------------------------------
|
||
4 years ago
|
function parseTable:parsePut(key, value)
|
||
|
-- Internal recursion function.
|
||
4 years ago
|
local function update(dict, splitKey) -- †
|
||
4 years ago
|
--[[ Get the next character in the table. ]]
|
||
4 years ago
|
local k = string.byte(table.remove(splitKey))
|
||
4 years ago
|
|
||
4 years ago
|
if #splitKey > 0 then -- there are still kacters left in the key.
|
||
|
if not dict[k] then dict[k] = {}
|
||
4 years ago
|
-- If there is a previous command mapping in place
|
||
4 years ago
|
elseif type(dict[k]) == globals.TYPE_STR then
|
||
4 years ago
|
-- Swap the mapping to a `CR`
|
||
|
dict[k] = {[ParseTable.CR] = dict[k]}
|
||
4 years ago
|
end
|
||
|
|
||
4 years ago
|
-- run update() again
|
||
|
update(dict[k], splitKey)
|
||
4 years ago
|
-- If dict[k] is a pre-existing table, don't clobber the table— clobber the `CR` value.
|
||
4 years ago
|
elseif type(dict[k]) == globals.TYPE_TBL then
|
||
4 years ago
|
dict[k][ParseTable.CR] = value
|
||
4 years ago
|
else dict[k] = value -- dict[k] is not a table, go ahead and clobber the value.
|
||
4 years ago
|
end
|
||
4 years ago
|
end -- ‡
|
||
4 years ago
|
|
||
4 years ago
|
-- Run the recursive function.
|
||
4 years ago
|
update(self, _stringSplit(
|
||
|
string.reverse(key), '.')
|
||
|
)
|
||
4 years ago
|
end
|
||
4 years ago
|
|
||
4 years ago
|
---------------------------------------------
|
||
4 years ago
|
--[[ SUMMARY:
|
||
|
* Create the union of `self` and `tableToUnite`
|
||
|
]]
|
||
|
--[[ PARAMS:
|
||
|
* `tableToUnite` => the table to unite with `self.`
|
||
|
]]
|
||
4 years ago
|
---------------------------------------------
|
||
4 years ago
|
function parseTable:parsePutAll(tableToUnite)
|
||
|
for k, v in pairs(tableToUnite) do
|
||
|
self:parsePut(k, v)
|
||
4 years ago
|
end
|
||
4 years ago
|
end
|
||
4 years ago
|
|
||
4 years ago
|
-- Parse the passed in table.
|
||
|
parseTable:parsePutAll(userTable)
|
||
|
-- Return the new `ParseTable`.
|
||
|
return parseTable
|
||
4 years ago
|
end
|
||
|
|
||
|
--[[
|
||
|
/*
|
||
|
* PUBLICIZE MODULE
|
||
|
*/
|
||
|
--]]
|
||
4 years ago
|
|
||
4 years ago
|
return ParseTable
|