diff options
-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 |