diff --git a/anselme.lua b/anselme.lua index 47001b7..5a24fb1 100644 --- a/anselme.lua +++ b/anselme.lua @@ -368,7 +368,7 @@ local vm_mt = { self.state.builtin_aliases["🏁"] = reached return self end, - --- set some code that will be added at the start of every function defined after this is called + --- set some code that will be added at the start of every non-scoped function defined after this is called -- nil to disable -- can typically be used to define variables for every function like 👁️ -- return self @@ -376,6 +376,13 @@ local vm_mt = { self.state.inject.function_start = code return self end, + --- same as injectfunctionstart, but inject code at the start of every scoped function + -- nil to disable + -- return self + injectscopedfunctionstart = function(self, code) + self.state.inject.scoped_function_start = code + return self + end, --- same as injectfunctionstart, but inject code at the start of every checkpoint -- nil to disable -- return self @@ -383,13 +390,20 @@ local vm_mt = { self.state.inject.checkpoint_start = code return self end, - --- same as injectfunctionstart, but inject code at the end of every function + --- same as injectfunctionstart, but inject code at the end of every non-scoped function -- nil to disable -- return self injectfunctionend = function(self, code) self.state.inject.function_end = code return self end, + --- same as injectfunctionstart, but inject code at the end of every scoped function + -- nil to disable + -- return self + injectscopedfunctionend = function(self, code) + self.state.inject.scoped_function_end = code + return self + end, --- same as injectfunctionend, but inject code at the end of every checkpoint -- nil to disable -- return self @@ -593,6 +607,7 @@ return setmetatable(anselme, { local state = { inject = { function_start = nil, function_end = nil, + scoped_function_start = nil, scoped_function_end = nil, checkpoint_start = nil, checkpoint_end = nil }, feature_flags = { diff --git a/parser/preparser.lua b/parser/preparser.lua index 5ced080..17e81a2 100644 --- a/parser/preparser.lua +++ b/parser/preparser.lua @@ -199,14 +199,27 @@ local function parse_line(line, state, namespace) table.insert(line.children, 1, { content = ":🔖=()", source = line.source }) end -- custom code injection - if state.inject.function_start then - for i, ll in ipairs(state.inject.function_start) do - table.insert(line.children, 1+i, ll) + if r.scoped then + if state.inject.scoped_function_start then + for i, ll in ipairs(state.inject.scoped_function_start) do + table.insert(line.children, 1+i, ll) + end end - end - if state.inject.function_end then - for _, ll in ipairs(state.inject.function_end) do - table.insert(line.children, ll) + if state.inject.scoped_function_end then + for _, ll in ipairs(state.inject.scoped_function_end) do + table.insert(line.children, ll) + end + end + else + if state.inject.function_start then + for i, ll in ipairs(state.inject.function_start) do + table.insert(line.children, 1+i, ll) + end + end + if state.inject.function_end then + for _, ll in ipairs(state.inject.function_end) do + table.insert(line.children, ll) + end end end elseif r.type == "checkpoint" then @@ -459,6 +472,7 @@ local function parse(state, s, name, source) local state_proxy = { inject = { function_start = nil, function_end = nil, + scoped_function_start = nil, scoped_function_end = nil, checkpoint_start = nil, checkpoint_end = nil }, aliases = setmetatable({}, { __index = state.aliases }), @@ -480,7 +494,7 @@ local function parse(state, s, name, source) global_state = state } -- parse injects - for _, inject in ipairs{"function_start", "function_end", "checkpoint_start", "checkpoint_end"} do + for _, inject in ipairs{"function_start", "function_end", "scoped_function_start", "scoped_function_end", "checkpoint_start", "checkpoint_end"} do if state.inject[inject] then local inject_indented, err = parse_indented(state.inject[inject], nil, "injected "..inject:gsub("_", " ")) if not inject_indented then return nil, err end