feat: add a parser system

This commit is contained in:
Kazhnuz 2021-05-15 22:48:09 +02:00
parent e3957edfa1
commit aa9140406f
3 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,63 @@
-- parser.lua :: a data parser, used to parse data from arguments
--[[
Copyright © 2021 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local Parser = Object:extend()
function Parser:new(headings, argumentsList, argumentwrapper, nullableNbr)
self.headings = headings
self.argumentsList = argumentsList
self.argumentwrapper = argumentwrapper or ""
self.nullableNbr = nullableNbr or 0
end
function Parser:getArguments(name)
if (self.argumentsList[name] ~= nil) then
local arguments = {}
utils.table.mergeList(arguments, self.headings)
utils.table.mergeList(arguments, self.argumentsList[name])
return arguments
else
error("No arguments data for argumentList " .. name)
end
end
function Parser:parse(datas)
local arguments = self:getArguments(datas[1])
print(utils.table.toString(arguments))
local parsedData = utils.table.parse(datas, arguments, self.nullableNbr)
if (utils.string.isEmpty(self.argumentwrapper)) then
-- print(utils.table.toString(parsedData))
return parsedData
else
local finalTable = {}
for i, heading in ipairs(self.headings) do
finalTable[heading] = parsedData[heading]
parsedData[heading] = nil
end
finalTable[self.argumentwrapper] = parsedData
-- print(utils.table.toString(finalTable))
return finalTable
end
end
return Parser

View file

@ -25,11 +25,13 @@
local DataManager = Object:extend()
local DataPack = require "birb.classes.datapack"
local Parser = require "birb.classes.parser"
local index = require "datas.gamedata.index"
function DataManager:new(core)
self.core = core
self:loadDatas()
self:loadParsers()
end
function DataManager:loadDatas()
@ -56,5 +58,23 @@ function DataManager:getCategories(datapack)
return self.datapacks[datapack].categories
end
-- Load parsers
function DataManager:loadParsers()
self.parsers = {}
local items = love.filesystem.getDirectoryItems("datas/parsers")
for i, item in ipairs(items) do
local filename = utils.datas.luaFileToModule(item)
local data = require("datas.parsers." .. filename)
self.core.debug:debug("datamanager", "creating parser for " .. filename)
self.parsers[filename] = Parser(data.headings, data.argumentLists, data.argumentWrapper, 0)
end
end
function DataManager:parse(parser, data)
return self.parsers[parser]:parse(data)
end
return DataManager

View file

@ -52,6 +52,12 @@ function Table.toString(table)
return string .. "}"
end
function Table.mergeList(table1, table2)
for i, value in ipairs(table2) do
table.insert(table1, value)
end
end
function Table.reduce(list, fn)
local acc
for k, v in ipairs(list) do