summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-12-17 19:52:11 +0100
committerAki <please@ignore.pl>2023-12-17 19:52:11 +0100
commitf318cd649290e40feaa08ac6c926f1d35e96615e (patch)
tree3cf61b2a3a5badaa252426521b33d7854e44ae30
parent02cb3c8b3cbd150522aa855cfc5cfaf0ec2269f0 (diff)
downloadlua-srcinfo-f318cd649290e40feaa08ac6c926f1d35e96615e.zip
lua-srcinfo-f318cd649290e40feaa08ac6c926f1d35e96615e.tar.gz
lua-srcinfo-f318cd649290e40feaa08ac6c926f1d35e96615e.tar.bz2
Output packages or bases and merge multi-values together
-rw-r--r--srcinfo.lua73
1 files changed, 65 insertions, 8 deletions
diff --git a/srcinfo.lua b/srcinfo.lua
index 9982a98..40ae7d1 100644
--- a/srcinfo.lua
+++ b/srcinfo.lua
@@ -1,20 +1,77 @@
local lpeg = require "lpeg"
+
+local PKGBASE = "pkgbase"
+local PKGNAME = "pkgname"
+local modes = {
+ [PKGBASE] = PKGBASE,
+ [PKGNAME] = PKGNAME,
+}
+
+
+function apply (state, method, ...)
+ if state[method] then
+ return state[method](state, ...)
+ end
+ return nil
+end
+
+
local identifier = lpeg.R"az" + lpeg.R"09" + lpeg.S"@+_"
identifier = identifier * (identifier + lpeg.S".-")^0
local whitespace = lpeg.S" \t"^0
local newline = lpeg.P"\r\n" + "\n"
local rest = (1 - newline)^0
-local definition = whitespace * lpeg.C(identifier) * whitespace * "=" * whitespace * lpeg.C(rest) * newline
-local emptylines = (whitespace * newline)^1
-local comment = lpeg.P"#" * rest * newline
-local block = lpeg.Ct((lpeg.Ct(definition) + comment)^1)
-local thing = emptylines^0 * block * (emptylines * block)^0 * emptylines^0 * -lpeg.P(1)
-local capture = lpeg.Ct(thing)
+local definition = lpeg.Cc"definition" * whitespace * lpeg.C(identifier) * whitespace * "=" * whitespace * lpeg.C(rest)
+local comment = lpeg.Cc"comment" * lpeg.P"#" * whitespace * lpeg.C(rest)
+local empty = lpeg.Cc"empty" * whitespace
+local invalid = lpeg.Cc"invalid" * lpeg.C(rest)
+local line = lpeg.Carg(1) * (definition + comment + empty + invalid) / apply * newline
+local capture = lpeg.Ct(line^0) * -lpeg.P(1)
local
-function srcinfo (handle)
- return capture:match(handle:read"a")
+function srcinfo (text, options)
+ options = options or {}
+ mode = modes[options.mode] or PKGNAME
+ return capture:match(
+ text,
+ nil,
+ {
+ mode = mode,
+ base = nil,
+ current = nil,
+ definition = function (self, name, value)
+ if name == PKGBASE then
+ self.base = {name=value}
+ self.current = self.base
+ if self.mode == PKGBASE then
+ self.base.packages = {}
+ return self.current
+ end
+ elseif name == PKGNAME then
+ self.current = {name=value}
+ if self.mode == PKGNAME then
+ self.current.base = self.base
+ return self.current
+ else
+ table.insert(self.base.packages, self.current)
+ end
+ else
+ local current_value = self.current[name]
+ if type(current_value) == "table" then
+ table.insert(current_value, value)
+ elseif current_value ~= nil then
+ self.current[name] = {current_value, value}
+ else
+ self.current[name] = value
+ end
+ end
+ end,
+ comment = function (self, text) end,
+ empty = function (self) end,
+ invalid = function (self, text) return text end,
+ }
+ )
end