From aa9140406fa284483aedab3b96fb17087a8c932a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 15 May 2021 22:48:09 +0200 Subject: [PATCH] feat: add a parser system --- sonic-radiance.love/birb/classes/parser.lua | 63 +++++++++++++++++++++ sonic-radiance.love/birb/core/datas.lua | 20 +++++++ sonic-radiance.love/birb/utils/table.lua | 6 ++ 3 files changed, 89 insertions(+) create mode 100644 sonic-radiance.love/birb/classes/parser.lua diff --git a/sonic-radiance.love/birb/classes/parser.lua b/sonic-radiance.love/birb/classes/parser.lua new file mode 100644 index 0000000..8800ae0 --- /dev/null +++ b/sonic-radiance.love/birb/classes/parser.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/core/datas.lua b/sonic-radiance.love/birb/core/datas.lua index 8d12c55..b1b008e 100644 --- a/sonic-radiance.love/birb/core/datas.lua +++ b/sonic-radiance.love/birb/core/datas.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/utils/table.lua b/sonic-radiance.love/birb/utils/table.lua index ddf1441..3e859cb 100644 --- a/sonic-radiance.love/birb/utils/table.lua +++ b/sonic-radiance.love/birb/utils/table.lua @@ -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