From 149af030cd02ebb439e2e279d58560730b93f09f Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Mon, 26 Jul 2021 13:58:34 +0200 Subject: [PATCH] Make parse return a list of parse errors Which basically just means unmatched lines. There are no descriptive errors, at least for now, but at least this gives you the option to display warnings or errors. --- inifile.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/inifile.lua b/inifile.lua index cd772cc..e6949ba 100644 --- a/inifile.lua +++ b/inifile.lua @@ -59,8 +59,12 @@ function inifile.parse(name, backend) local comments = {} local sectionorder = {} local cursectionorder + local lineNumber = 0 + local errors = {} for line in backends[backend].lines(name) do + lineNumber = lineNumber + 1 + local validLine = false -- Section headers local s = line:match("^%[([^%]]+)%]$") @@ -69,6 +73,7 @@ function inifile.parse(name, backend) t[section] = t[section] or {} cursectionorder = {name = section} table.insert(sectionorder, cursectionorder) + validLine = true end -- Comments @@ -77,6 +82,7 @@ function inifile.parse(name, backend) local commentsection = section or comments comments[commentsection] = comments[commentsection] or {} table.insert(comments[commentsection], s) + validLine = true end -- Key-value pairs @@ -87,6 +93,11 @@ function inifile.parse(name, backend) if key and value ~= nil then t[section][key] = value table.insert(cursectionorder, key) + validLine = true + end + + if not validLine then + table.insert(errors, ("Line %d: Invalid data found '%s'"):format(lineNumber, line)) end end @@ -96,7 +107,7 @@ function inifile.parse(name, backend) comments = comments, sectionorder = sectionorder, } - }) + }), errors end function inifile.save(name, t, backend)