Fix the god damn shorthands
This commit is contained in:
18
main.go
18
main.go
@@ -55,16 +55,21 @@ func main() {
|
||||
luaExpr := args[1]
|
||||
files := args[2:]
|
||||
|
||||
log.Printf("Parsed arguments: regexPattern=%s, luaExpr=%s, files=%v", regexPattern, luaExpr, files)
|
||||
|
||||
// Prepare the Lua expression
|
||||
luaExpr = buildLuaScript(luaExpr)
|
||||
log.Printf("Built Lua expression: %s", luaExpr)
|
||||
|
||||
if strings.Contains(regexPattern, "!num") {
|
||||
regexPattern = strings.ReplaceAll(regexPattern, "!num", "(\\d*\\.?\\d+)")
|
||||
log.Printf("Updated regexPattern after replacing !num: %s", regexPattern)
|
||||
}
|
||||
|
||||
// Make sure the regex can match across multiple lines by adding (?s) flag
|
||||
if !strings.HasPrefix(regexPattern, "(?s)") {
|
||||
regexPattern = "(?s)" + regexPattern
|
||||
log.Printf("Updated regexPattern to match across multiple lines: %s", regexPattern)
|
||||
}
|
||||
|
||||
// Compile the pattern for file processing
|
||||
@@ -76,6 +81,7 @@ func main() {
|
||||
|
||||
// Process each file
|
||||
for _, file := range files {
|
||||
log.Printf("Processing file: %s", file)
|
||||
err := processFile(file, pattern, luaExpr)
|
||||
if err != nil {
|
||||
Error.Printf("Error processing file %s: %v", file, err)
|
||||
@@ -85,7 +91,6 @@ func main() {
|
||||
|
||||
// buildLuaScript creates a complete Lua script from the expression
|
||||
func buildLuaScript(luaExpr string) string {
|
||||
// Check if the expression starts with an operator that needs special handling
|
||||
if strings.HasPrefix(luaExpr, "*") {
|
||||
luaExpr = "v1 = v1" + luaExpr
|
||||
} else if strings.HasPrefix(luaExpr, "/") {
|
||||
@@ -102,11 +107,16 @@ func buildLuaScript(luaExpr string) string {
|
||||
// Handle direct assignment with = operator
|
||||
luaExpr = "v1 " + luaExpr
|
||||
}
|
||||
// Check if the expression starts with an operator that needs special handling
|
||||
if !strings.Contains(luaExpr, "=") {
|
||||
luaExpr = "v1 = " + luaExpr
|
||||
}
|
||||
|
||||
// Replace shorthand v1, v2, etc. with their direct variable names
|
||||
shorthandRegex := regexp.MustCompile(`\bv\[(\d+)\]\b`)
|
||||
luaExpr = shorthandRegex.ReplaceAllString(luaExpr, "v$1")
|
||||
|
||||
log.Printf("Final Lua expression after processing: %s", luaExpr)
|
||||
return luaExpr
|
||||
}
|
||||
|
||||
@@ -170,6 +180,7 @@ function ceil(x) return math.ceil(x) end
|
||||
captures := pattern.FindStringSubmatch(match)
|
||||
if len(captures) <= 1 {
|
||||
// No capture groups, return unchanged
|
||||
log.Printf("No capture groups found for match: %s", match)
|
||||
return match
|
||||
}
|
||||
|
||||
@@ -179,8 +190,10 @@ function ceil(x) return math.ceil(x) end
|
||||
floatVal, err := strconv.ParseFloat(capture, 64)
|
||||
if err == nil {
|
||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LNumber(floatVal))
|
||||
log.Printf("Set global v%d to float value: %f", i+1, floatVal)
|
||||
} else {
|
||||
L.SetGlobal(fmt.Sprintf("v%d", i+1), lua.LString(capture))
|
||||
log.Printf("Set global v%d to string value: %s", i+1, capture)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,12 +208,14 @@ function ceil(x) return math.ceil(x) end
|
||||
for i := 0; i < 12; i++ {
|
||||
varName := fmt.Sprintf("v%d", i+1)
|
||||
returnValues[i] = L.GetGlobal(varName)
|
||||
log.Printf("Retrieved value for %s: %v", varName, returnValues[i])
|
||||
}
|
||||
|
||||
// Replace each capture group with its new value (if available)
|
||||
result := match
|
||||
for i := 0; i < len(captures)-1 && i < 12; i++ {
|
||||
if returnValues[i] == lua.LNil {
|
||||
log.Printf("Skipping replacement for %s as it is nil", captures[i+1])
|
||||
continue // Skip if nil
|
||||
}
|
||||
|
||||
@@ -218,6 +233,7 @@ function ceil(x) return math.ceil(x) end
|
||||
|
||||
// If we have a value, replace it
|
||||
if newVal != "" {
|
||||
log.Printf("Replacing %s with %s in match: %s", oldVal, newVal, match)
|
||||
result = strings.Replace(result, oldVal, newVal, 1)
|
||||
}
|
||||
}
|
||||
|
@@ -65,7 +65,7 @@ func TestShorthandNotation(t *testing.T) {
|
||||
`
|
||||
|
||||
regex := regexp.MustCompile(`(?s)<value>(\d+)</value>`)
|
||||
luaExpr := `v1 = v1 * 1.5` // Use direct assignment syntax
|
||||
luaExpr := `v1 * 1.5` // Use direct assignment syntax
|
||||
luaScript := buildLuaScript(luaExpr)
|
||||
|
||||
modifiedContent, err := process(fileContents, regex, luaScript)
|
||||
|
Reference in New Issue
Block a user