diff options
author | Aki <please@ignore.pl> | 2024-08-20 22:11:19 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2024-08-20 22:11:19 +0200 |
commit | 580835fb52cfe2ba46f3cd5980ee388d2408f214 (patch) | |
tree | b7b1d5d3e14c0551b75c1fa6b50370c8de0a91bb | |
parent | 82fffc923163efd50342a96a4556821e2a50446d (diff) | |
download | headers-580835fb52cfe2ba46f3cd5980ee388d2408f214.zip headers-580835fb52cfe2ba46f3cd5980ee388d2408f214.tar.gz headers-580835fb52cfe2ba46f3cd5980ee388d2408f214.tar.bz2 |
Command implementation will now output the query results
While this no longer depends on side-effects to output the results, it
still depends on side-effects when parsing in a typical OOP fashion.
This works for now but is not really safe or maintainable. Luckily,
this tool is not intended for long term maintenance so I'll be leaving
it at that. I already procrastinated enough and the main goal - a
showcase of particular use of Lua as DSL - has been accomplished.
-rwxr-xr-x | headers.lua | 15 | ||||
-rw-r--r-- | headers/parser.lua | 41 |
2 files changed, 37 insertions, 19 deletions
diff --git a/headers.lua b/headers.lua index 8726615..680142d 100755 --- a/headers.lua +++ b/headers.lua @@ -11,7 +11,18 @@ for _, filename in pairs(definitions) do local handle = io.open(filename) local data = handle:read("a") handle:close() - if parser:parse(data, args.selection) then - return -- interface is yet to be properly defined, this allows only for first-result queries + parser:parse(data) +end +if args.selection then + local headers = parser:get_headers(args.selection) + if not headers then + os.exit(1) + end + for _, header in pairs(headers) do + print(header) end + return +end +for _, tag in pairs(parser:get_tags()) do + print(tag) end diff --git a/headers/parser.lua b/headers/parser.lua index f5aa6a1..45dc322 100644 --- a/headers/parser.lua +++ b/headers/parser.lua @@ -1,3 +1,4 @@ +local tablex = require "pl.tablex" local definition = require "headers.definition" local multiset = require "headers.multiset" local parser = {} @@ -98,24 +99,30 @@ function parser:reset () end -function parser:parse (input, selection) - self:reset() -- interface yet to be defined, this is a naive workaround to avoid retriggering prints - local db = load(input, nil, nil, definition.new(self)) -- Publishes the entire parser interface into definition - db() - if selection then - local found = self:get_tag(selection) - if not found then - return false - end - for header in found.headers:all() do - print(header) - end - else - for _, tag in pairs(self.tag_lookup) do - print(tag) - end +function parser:parse (input) + load(input, nil, nil, definition.new(self))() -- Publishes the entire parser interface into definition +end + + +function parser:get_tags () + local names = {} + for _, tag in tablex.sortv(self.tags, function (lhs, rhs) return tostring(lhs) < tostring(rhs) end) do + table.insert(names, tostring(tag)) + end + return names +end + + +function parser:get_headers (tag) + local found = self:get_tag(tag) + if not found then + return nil + end + local names = {} + for header in found.headers:all() do + table.insert(names, header) end - return true + return names end |