Implement printing from lua

This commit is contained in:
2025-03-26 13:37:27 +01:00
parent 10c39b02a0
commit a9b6f7f984
3 changed files with 45 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package processor
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -239,6 +240,8 @@ modified = false
if err := L.DoString(helperScript); err != nil { if err := L.DoString(helperScript); err != nil {
return fmt.Errorf("error loading helper functions: %v", err) return fmt.Errorf("error loading helper functions: %v", err)
} }
L.SetGlobal("print", L.NewFunction(printToGo))
return nil return nil
} }
@@ -295,6 +298,20 @@ func BuildLuaScript(luaExpr string) string {
return fullScript return fullScript
} }
func printToGo(L *lua.LState) int {
// Get the number of arguments passed to the Lua print function
n := L.GetTop()
// Create a slice to hold the arguments
args := make([]interface{}, n)
for i := 1; i <= n; i++ {
args[i-1] = L.Get(i) // Get the argument from Lua stack
}
// Print the arguments to Go's stdout
log.Print("Lua: ")
log.Println(args...)
return 0 // No return values
}
// Max returns the maximum of two integers // Max returns the maximum of two integers
func Max(a, b int) int { func Max(a, b int) int {
if a > b { if a > b {

View File

@@ -107,14 +107,6 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
luaExpr = BuildLuaScript(luaExpr) luaExpr = BuildLuaScript(luaExpr)
log.Printf("Changing Lua expression from: %s to: %s", previous, luaExpr) log.Printf("Changing Lua expression from: %s to: %s", previous, luaExpr)
L, err := NewLuaState()
if err != nil {
log.Printf("Error creating Lua state: %v", err)
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
}
defer L.Close()
log.Printf("Lua state created successfully")
// Initialize Lua environment // Initialize Lua environment
modificationCount := 0 modificationCount := 0
@@ -128,6 +120,17 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
// By going backwards we fuck up all the indices to the end of the file that we don't care about // By going backwards we fuck up all the indices to the end of the file that we don't care about
// Because there either aren't any (last match) or they're already modified (subsequent matches) // Because there either aren't any (last match) or they're already modified (subsequent matches)
for i := len(indices) - 1; i >= 0; i-- { for i := len(indices) - 1; i >= 0; i-- {
L, err := NewLuaState()
if err != nil {
log.Printf("Error creating Lua state: %v", err)
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
}
// Hmm... Maybe we don't want to defer this..
// Maybe we want to close them every iteration
// We'll leave it as is for now
defer L.Close()
log.Printf("Lua state created successfully")
matchIndices := indices[i] matchIndices := indices[i]
log.Printf("Processing match indices: %v", matchIndices) log.Printf("Processing match indices: %v", matchIndices)
@@ -150,9 +153,19 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
log.Println("Odd number of indices of groups, what the fuck?") log.Println("Odd number of indices of groups, what the fuck?")
continue continue
} }
for _, index := range groups {
if index == -1 {
// return "", 0, 0, fmt.Errorf("negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. Please check the regex pattern and input content.", matchIndices)
log.Printf("Negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. This is not an error but it's possibly not what you want.", matchIndices)
continue
}
}
captures := make([]string, 0, len(groups)/2) captures := make([]string, 0, len(groups)/2)
for j := 0; j < len(groups); j += 2 { for j := 0; j < len(groups); j += 2 {
if groups[j] == -1 || groups[j+1] == -1 {
continue
}
captures = append(captures, content[groups[j]:groups[j+1]]) captures = append(captures, content[groups[j]:groups[j+1]])
} }
log.Printf("Captured groups: %v", captures) log.Printf("Captured groups: %v", captures)
@@ -167,6 +180,9 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
if name == "" { if name == "" {
continue continue
} }
if groups[i*2] == -1 || groups[i*2+1] == -1 {
continue
}
namedCaptures = append(namedCaptures, NamedCapture{ namedCaptures = append(namedCaptures, NamedCapture{
Name: name, Name: name,
Value: captures[i], Value: captures[i],

View File

@@ -1233,9 +1233,12 @@ func TestVariousNamedCaptureFormats(t *testing.T) {
p := &RegexProcessor{} p := &RegexProcessor{}
result, mods, matches, err := p.ProcessContent( result, mods, matches, err := p.ProcessContent(
content, content,
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")?`, `<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")? />`,
`-- Prefix the ID with "ID-" `-- Prefix the ID with "ID-"
id_num = "ID-" .. id_num id_num = "ID-" .. id_num
print(id_num)
print(val)
print(status)
-- Double the value except for inactive status -- Double the value except for inactive status
if not status or status ~= "inactive" then if not status or status ~= "inactive" then