aboutsummaryrefslogtreecommitdiff
path: root/Lua
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2022-10-27 13:17:23 -0400
committerBruce Hill <bruce@bruce-hill.com>2022-10-27 13:17:23 -0400
commite258645a523c5fb4407be4a9e90df1314b225191 (patch)
tree5b760bec6541cbbafd7afde9a0d29a628134e4cf /Lua
parent54e87ff91eb4f4f65c3f6a25a7222a442d430927 (diff)
Better error handling behavior
Diffstat (limited to 'Lua')
-rw-r--r--Lua/Makefile4
-rw-r--r--Lua/lbp.c12
-rw-r--r--Lua/test.lua2
3 files changed, 10 insertions, 8 deletions
diff --git a/Lua/Makefile b/Lua/Makefile
index fc1ba14..3c9a745 100644
--- a/Lua/Makefile
+++ b/Lua/Makefile
@@ -26,9 +26,9 @@ O=-O3
ALL_FLAGS=$(CFLAGS) $(OSFLAGS) $(INCS) $(EXTRA) $(CWARN) $(G) $(O)
ifeq ($(shell uname -s),Darwin)
- MAKESO= $(CC) -bundle -undefined dynamic_lookup
+ MAKESO= $(CC) -bundle -undefined dynamic_lookup $(CFLAGS) $(OSFLAGS) $(EXTRA) $(CWARN) $(G) $(O)
else
- MAKESO= $(CC) -shared
+ MAKESO= $(CC) -shared $(CFLAGS) $(OSFLAGS) $(EXTRA) $(CWARN) $(G) $(O)
endif
all: bp.so
diff --git a/Lua/lbp.c b/Lua/lbp.c
index c1c3f61..7c5db3e 100644
--- a/Lua/lbp.c
+++ b/Lua/lbp.c
@@ -33,10 +33,8 @@ static void push_match(lua_State *L, match_t *m, const char *start);
lua_State *cur_state = NULL;
-static void match_error(pat_t *pat, const char *msg)
+static void match_error(const char *msg)
{
- (void)pat;
- recycle_all_matches();
lua_pushstring(cur_state, msg);
lua_error(cur_state);
}
@@ -170,11 +168,13 @@ static int Lmatch(lua_State *L)
match_t *m = NULL;
int ret = 0;
cur_state = L;
- if (next_match_safe(&m, text+index-1, &text[textlen], pat, builtins, NULL, false, match_error)) {
+ bp_errhand_t old = bp_set_error_handler(match_error);
+ if (next_match(&m, text+index-1, &text[textlen], pat, builtins, NULL, false)) {
push_match(L, m, text);
stop_matching(&m);
ret = 1;
}
+ bp_set_error_handler(old);
return ret;
}
@@ -209,12 +209,14 @@ static int Lreplace(lua_State *L)
const char *prev = text;
pat_t *rep_pat = maybe_replacement.value.pat;
cur_state = L;
- for (match_t *m = NULL; next_match_safe(&m, text, &text[textlen], rep_pat, builtins, NULL, false, match_error); ) {
+ bp_errhand_t old = bp_set_error_handler(match_error);
+ for (match_t *m = NULL; next_match(&m, text, &text[textlen], rep_pat, builtins, NULL, false); ) {
fwrite(prev, sizeof(char), (size_t)(m->start - prev), out);
fprint_match(out, text, m, NULL);
prev = m->end;
++replacements;
}
+ bp_set_error_handler(old);
fwrite(prev, sizeof(char), (size_t)(&text[textlen] - prev), out);
fflush(out);
lua_pushlstring(L, buf, size);
diff --git a/Lua/test.lua b/Lua/test.lua
index d812009..48a2ffa 100644
--- a/Lua/test.lua
+++ b/Lua/test.lua
@@ -38,7 +38,7 @@ print("Testing parse errors:")
local ok, msg = pcall(function()
bp.match(".;//;;; wtf", "xxx")
end)
-if not ok then print(("\x1B[41;30mParse error:\x1B[0;1;31m %s\x1B[m\n"):format(msg)) end
+if not ok then print(("Successfully got parse error: \"%s\"\n"):format(msg)) end
print("Testing builtins:")
print(bp.match("parens", "...(foo())..."))