mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-28 00:59:31 +00:00
Add scoped function injections
This commit is contained in:
parent
c4fb2649c2
commit
428042c5d9
2 changed files with 39 additions and 10 deletions
19
anselme.lua
19
anselme.lua
|
|
@ -368,7 +368,7 @@ local vm_mt = {
|
||||||
self.state.builtin_aliases["🏁"] = reached
|
self.state.builtin_aliases["🏁"] = reached
|
||||||
return self
|
return self
|
||||||
end,
|
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
|
-- nil to disable
|
||||||
-- can typically be used to define variables for every function like 👁️
|
-- can typically be used to define variables for every function like 👁️
|
||||||
-- return self
|
-- return self
|
||||||
|
|
@ -376,6 +376,13 @@ local vm_mt = {
|
||||||
self.state.inject.function_start = code
|
self.state.inject.function_start = code
|
||||||
return self
|
return self
|
||||||
end,
|
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
|
--- same as injectfunctionstart, but inject code at the start of every checkpoint
|
||||||
-- nil to disable
|
-- nil to disable
|
||||||
-- return self
|
-- return self
|
||||||
|
|
@ -383,13 +390,20 @@ local vm_mt = {
|
||||||
self.state.inject.checkpoint_start = code
|
self.state.inject.checkpoint_start = code
|
||||||
return self
|
return self
|
||||||
end,
|
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
|
-- nil to disable
|
||||||
-- return self
|
-- return self
|
||||||
injectfunctionend = function(self, code)
|
injectfunctionend = function(self, code)
|
||||||
self.state.inject.function_end = code
|
self.state.inject.function_end = code
|
||||||
return self
|
return self
|
||||||
end,
|
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
|
--- same as injectfunctionend, but inject code at the end of every checkpoint
|
||||||
-- nil to disable
|
-- nil to disable
|
||||||
-- return self
|
-- return self
|
||||||
|
|
@ -593,6 +607,7 @@ return setmetatable(anselme, {
|
||||||
local state = {
|
local state = {
|
||||||
inject = {
|
inject = {
|
||||||
function_start = nil, function_end = nil,
|
function_start = nil, function_end = nil,
|
||||||
|
scoped_function_start = nil, scoped_function_end = nil,
|
||||||
checkpoint_start = nil, checkpoint_end = nil
|
checkpoint_start = nil, checkpoint_end = nil
|
||||||
},
|
},
|
||||||
feature_flags = {
|
feature_flags = {
|
||||||
|
|
|
||||||
|
|
@ -199,14 +199,27 @@ local function parse_line(line, state, namespace)
|
||||||
table.insert(line.children, 1, { content = ":🔖=()", source = line.source })
|
table.insert(line.children, 1, { content = ":🔖=()", source = line.source })
|
||||||
end
|
end
|
||||||
-- custom code injection
|
-- custom code injection
|
||||||
if state.inject.function_start then
|
if r.scoped then
|
||||||
for i, ll in ipairs(state.inject.function_start) do
|
if state.inject.scoped_function_start then
|
||||||
table.insert(line.children, 1+i, ll)
|
for i, ll in ipairs(state.inject.scoped_function_start) do
|
||||||
|
table.insert(line.children, 1+i, ll)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
if state.inject.scoped_function_end then
|
||||||
if state.inject.function_end then
|
for _, ll in ipairs(state.inject.scoped_function_end) do
|
||||||
for _, ll in ipairs(state.inject.function_end) do
|
table.insert(line.children, ll)
|
||||||
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
|
||||||
end
|
end
|
||||||
elseif r.type == "checkpoint" then
|
elseif r.type == "checkpoint" then
|
||||||
|
|
@ -459,6 +472,7 @@ local function parse(state, s, name, source)
|
||||||
local state_proxy = {
|
local state_proxy = {
|
||||||
inject = {
|
inject = {
|
||||||
function_start = nil, function_end = nil,
|
function_start = nil, function_end = nil,
|
||||||
|
scoped_function_start = nil, scoped_function_end = nil,
|
||||||
checkpoint_start = nil, checkpoint_end = nil
|
checkpoint_start = nil, checkpoint_end = nil
|
||||||
},
|
},
|
||||||
aliases = setmetatable({}, { __index = state.aliases }),
|
aliases = setmetatable({}, { __index = state.aliases }),
|
||||||
|
|
@ -480,7 +494,7 @@ local function parse(state, s, name, source)
|
||||||
global_state = state
|
global_state = state
|
||||||
}
|
}
|
||||||
-- parse injects
|
-- 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
|
if state.inject[inject] then
|
||||||
local inject_indented, err = parse_indented(state.inject[inject], nil, "injected "..inject:gsub("_", " "))
|
local inject_indented, err = parse_indented(state.inject[inject], nil, "injected "..inject:gsub("_", " "))
|
||||||
if not inject_indented then return nil, err end
|
if not inject_indented then return nil, err end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue