diff options
author | Aki <please@ignore.pl> | 2024-08-20 18:41:13 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2024-08-20 18:41:13 +0200 |
commit | 82fffc923163efd50342a96a4556821e2a50446d (patch) | |
tree | cefbc72d331b72fd757b78d75834c0265454e41d | |
parent | 71ae0ff1f713e45430ea02a1ff1e46ab2d28b63c (diff) | |
download | headers-82fffc923163efd50342a96a4556821e2a50446d.zip headers-82fffc923163efd50342a96a4556821e2a50446d.tar.gz headers-82fffc923163efd50342a96a4556821e2a50446d.tar.bz2 |
Parser is no longer a singleton-like entity
-rw-r--r-- | headers/definition.lua | 2 | ||||
-rw-r--r-- | headers/parser.lua | 51 |
2 files changed, 30 insertions, 23 deletions
diff --git a/headers/definition.lua b/headers/definition.lua index 33c5143..0266f97 100644 --- a/headers/definition.lua +++ b/headers/definition.lua @@ -1,7 +1,7 @@ local definition = {} ---- Builds a definition parser environment that binds to a *private* object that implements +3,+41/private:/ callbacks. +--- Builds a definition parser environment that binds to a *private* object that implements +4,+48/private:/ callbacks. function definition.new (private) local public = {} diff --git a/headers/parser.lua b/headers/parser.lua index 95312ab..f5aa6a1 100644 --- a/headers/parser.lua +++ b/headers/parser.lua @@ -1,5 +1,14 @@ local definition = require "headers.definition" local multiset = require "headers.multiset" +local parser = {} + + +local function init (obj) + obj = obj or {} + obj.tags = {} + obj.tag_lookup = {} + return obj +end local tag do @@ -37,12 +46,7 @@ local alias do end -local private = {} -- FIXME: Shared state at module level when a constructor function is provided -private.tags = {} -private.tag_lookup = {} - - -function private:get_or_add_tag (name) +function parser:get_or_add_tag (name) local found = self:get_tag(name) if not found then found = tag(name) @@ -53,18 +57,18 @@ function private:get_or_add_tag (name) end -function private:get_tag (name) +function parser:get_tag (name) return self.tag_lookup[name] end -function private:add_alias (tag, name) +function parser:add_alias (tag, name) self.tag_lookup[name] = alias(name, tag) return self.tag_lookup[name] end -function private:add_entry (tag, entry) +function parser:add_entry (tag, entry) if type(entry) == "function" then return entry(tag) end @@ -72,7 +76,7 @@ function private:add_entry (tag, entry) end -function private:new_include (name) +function parser:new_include (name) return function (target) local source = self:get_tag(name) or error("tag not found: " .. name) for header in source.headers:all() do @@ -82,26 +86,24 @@ function private:new_include (name) end -function private:new_remove (header) +function parser:new_remove (header) return function (target) target:remove(header) end end -function private:reset () - self.tags = {} - self.tag_lookup = {} +function parser:reset () + init(self) end -local -function parse (self, input, selection) - private:reset() -- interface yet to be defined, this is a naive workaround to avoid retriggering prints - local db = load(input, nil, nil, definition.new(private)) +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 = private:get_tag(selection) + local found = self:get_tag(selection) if not found then return false end @@ -109,7 +111,7 @@ function parse (self, input, selection) print(header) end else - for _, tag in pairs(private.tag_lookup) do + for _, tag in pairs(self.tag_lookup) do print(tag) end end @@ -117,10 +119,15 @@ function parse (self, input, selection) end +local mt = { + __index=parser, +} + + local function new () - return {parse=parse} + return setmetatable(init(), mt) end -return {new=new} +return setmetatable({new=new}, mt) |