Added builtins for lua library

This commit is contained in:
Bruce Hill 2021-09-23 21:19:39 -07:00
parent 0ec89315b4
commit 3de6de1ffa
3 changed files with 30 additions and 7 deletions

View File

@ -36,12 +36,15 @@ all: bp.so
clean:
rm -f lbp.o bp.so
lbp.o: lbp.c
lbp.o: lbp.c builtins.h
$(CC) -c $(ALL_FLAGS) -o $@ $<
bp.so: lbp.o ../pattern.o ../utils.o ../utf8.o ../match.o ../definitions.o
$(MAKESO) -o $@ $^
builtins.h: ../grammars/builtins.bp
sed 's/\\/\\\\/g;s/"/\\"/g;s/ /\\t/g;s/^/"/;s/$$/\\n"/' $< >$@
test: bp.so
$(LUA) test.lua

View File

@ -5,8 +5,11 @@
* bp.replace(str, pat, replacement, start_index) -> str with replacements
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "lua.h"
#include "lauxlib.h"
@ -20,9 +23,15 @@
#define luaL_register(L, _, R) luaL_setfuncs(L, R, 0)
#endif
static const char *builtins_source =
#include "builtins.h"
;
static int MATCH_METATABLE = 0;
static inline void push_parse_error(lua_State *L, maybe_pat_t m)
static def_t *builtins;
static inline void raise_parse_error(lua_State *L, maybe_pat_t m)
{
size_t err_len = (size_t)(m.value.error.end - m.value.error.start);
char *buf = calloc(err_len+1, sizeof(char));
@ -126,13 +135,13 @@ static int Lmatch(lua_State *L)
maybe_pat_t maybe_pat = bp_pattern(pat_text, pat_text + patlen);
if (!maybe_pat.success) {
push_parse_error(L, maybe_pat);
raise_parse_error(L, maybe_pat);
return 0;
}
match_t *m = NULL;
int ret = 0;
if (next_match(&m, NULL, text, &text[textlen], maybe_pat.value.pat, NULL, false)) {
if (next_match(&m, builtins, text, &text[textlen], maybe_pat.value.pat, NULL, false)) {
// lua_createtable(L, 0, 1);
@ -168,12 +177,12 @@ static int Lreplace(lua_State *L)
maybe_pat_t maybe_pat = bp_pattern(pat_text, pat_text + patlen);
if (!maybe_pat.success) {
push_parse_error(L, maybe_pat);
raise_parse_error(L, maybe_pat);
return 0;
}
maybe_pat = bp_replacement(maybe_pat.value.pat, rep_text, rep_text + replen);
if (!maybe_pat.success) {
push_parse_error(L, maybe_pat);
raise_parse_error(L, maybe_pat);
return 0;
}
@ -182,7 +191,7 @@ static int Lreplace(lua_State *L)
FILE *out = open_memstream(&buf, &size);
int replacements = 0;
const char *prev = text;
for (match_t *m = NULL; next_match(&m, NULL, text, &text[textlen], maybe_pat.value.pat, NULL, false); ) {
for (match_t *m = NULL; next_match(&m, builtins, text, &text[textlen], maybe_pat.value.pat, NULL, false); ) {
fwrite(prev, sizeof(char), (size_t)(m->start - prev), out);
fprint_match(out, text, m, NULL);
prev = m->end;
@ -199,6 +208,14 @@ static int Lreplace(lua_State *L)
LUALIB_API int luaopen_bp(lua_State *L)
{
maybe_pat_t maybe_pat = bp_pattern(builtins_source, builtins_source+strlen(builtins_source));
if (!maybe_pat.success) {
raise_parse_error(L, maybe_pat);
return 0;
}
for (pat_t *p = maybe_pat.value.pat; p && p->type == BP_DEFINITION; p = p->args.def.pat)
builtins = with_def(builtins, p->args.def.namelen, p->args.def.name, p->args.def.def);
lua_pushlightuserdata(L, (void*)&MATCH_METATABLE);
lua_createtable(L, 0, 4);
luaL_register(L, NULL, Rinstance_metamethods);

View File

@ -35,3 +35,6 @@ local ok, msg = pcall(function()
bp.match("xxx", ".;//;;; wtf")
end)
if not ok then print(("\x1B[41;30mParse error:\x1B[0;1;31m %s\x1B[m\n"):format(msg)) end
print("Testing builtins:")
print(bp.match("...(foo())...", "parens"))