From 0778c66917daba1a0dc948eacdf86578ec4dd2be Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 26 Aug 2024 01:55:02 +0200 Subject: User may now ask for multiple standards/tags from the CLI utility --- headers.lua | 6 +++--- headers/parser.lua | 35 ++++++++++++++++++++++------------- spec/parser_spec.lua | 17 +++++++++++++---- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/headers.lua b/headers.lua index 680142d..290aca3 100755 --- a/headers.lua +++ b/headers.lua @@ -1,8 +1,8 @@ #!/usr/bin/env lua local args = require "pl.lapp" [[ Prints list of headers from a standard or available standards - -d (string default 'db/') Directory where definitions are placed - (optional string) Standard to display headers for + -d (string default 'db/') Directory where definitions are placed + (optional string) Standard to display the headers for ]] local dir = require "pl.dir" local parser = require "headers.parser".new() @@ -13,7 +13,7 @@ for _, filename in pairs(definitions) do handle:close() parser:parse(data) end -if args.selection then +if #args.selection > 0 then local headers = parser:get_headers(args.selection) if not headers then os.exit(1) diff --git a/headers/parser.lua b/headers/parser.lua index 45dc322..33b212b 100644 --- a/headers/parser.lua +++ b/headers/parser.lua @@ -1,3 +1,4 @@ +local seq = require "pl.seq" local tablex = require "pl.tablex" local definition = require "headers.definition" local multiset = require "headers.multiset" @@ -104,25 +105,33 @@ function parser:parse (input) end +local +function less_than (lhs, rhs) + return lhs < rhs +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 + return seq.copy(seq.sort(seq.map(tostring, self.tags), less_than)) end -function parser:get_headers (tag) - local found = self:get_tag(tag) - if not found then - return nil +function parser:get_headers (tags) + local all = {} + for _, tag in pairs(tags) do + local found = self:get_tag(tag) + if found then + local headers = {} + for header in found.headers:all() do + headers[header] = true + end + tablex.update(all, headers) + end end - local names = {} - for header in found.headers:all() do - table.insert(names, header) + if tablex.size(all) > 0 then + return seq.copy(seq.sort(seq.keys(all), less_than)) end - return names + return nil end diff --git a/spec/parser_spec.lua b/spec/parser_spec.lua index f20ae0f..b2d91fc 100644 --- a/spec/parser_spec.lua +++ b/spec/parser_spec.lua @@ -55,12 +55,12 @@ describe("Headers", function() it("are not available if never assigned", function() headers:parse [[]] - assert.is_nil(headers:get_headers("Example")) + assert.is_nil(headers:get_headers{"Example"}) end) it("can be assigned to a tag", function() headers:parse [[headers "Example" {"example.h"}]] - assert.are.same({"example.h"}, headers:get_headers("Example")) + assert.are.same({"example.h"}, headers:get_headers{"Example"}) end) it("can be included from another tag", function() @@ -68,7 +68,7 @@ describe("Headers", function() headers "Parent" {"example.h"} headers "Derived" {include "Parent"} ]] - assert.are.same({"example.h"}, headers:get_headers("Derived")) + assert.are.same({"example.h"}, headers:get_headers{"Derived"}) end) it("can be removed after including from another tag", function() @@ -76,6 +76,15 @@ describe("Headers", function() headers "Parent" {"example.h", "removed.h"} headers "Derived" {include "Parent", remove "removed.h"} ]] - assert.are.same({"example.h"}, headers:get_headers("Derived")) + assert.are.same({"example.h"}, headers:get_headers{"Derived"}) + end) + + it("can be retrieved from number of tags", function() + headers:parse [[ + headers "First" {"first.h"} + headers "Second" {"second.h"} + ]] + assert.are.same({"first.h"}, headers:get_headers{"First"}) + assert.are.same({"first.h", "second.h"}, headers:get_headers{"First", "Second"}) end) end) -- cgit v1.1