From 580835fb52cfe2ba46f3cd5980ee388d2408f214 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 20 Aug 2024 22:11:19 +0200 Subject: 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. --- headers.lua | 15 +++++++++++++-- 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 -- cgit v1.1