From eb8b906d5eb5113e7377f06afbfd641c1c5e6a1e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 14 Apr 2025 17:16:19 -0300 Subject: Some refactoring in debug code Functions lp_printtree and lp_printcode moved to lpprint.c, to concentrate there debug/testing code. --- lpprint.c | 67 ++++++++++++++++++++++++++++++++++++++------------------------- lpprint.h | 16 ++++----------- lptree.c | 29 ++------------------------- lptree.h | 4 +--- makefile | 2 +- 5 files changed, 48 insertions(+), 70 deletions(-) diff --git a/lpprint.c b/lpprint.c index da902e6..42824cc 100644 --- a/lpprint.c +++ b/lpprint.c @@ -3,10 +3,12 @@ #include #include +#include "lauxlib.h" #include "lptypes.h" #include "lpprint.h" #include "lpcode.h" +#include "lptree.h" #if defined(LPEG_DEBUG) @@ -18,7 +20,7 @@ */ -void printcharset (const byte *st) { +static void printcharset (const byte *st) { int i; printf("["); for (i = 0; i <= UCHAR_MAX; i++) { @@ -137,7 +139,7 @@ void printinst (const Instruction *op, const Instruction *p) { } -void printpatt (Instruction *p) { +static void printpatt (Instruction *p) { Instruction *op = p; uint n = op[-1].codesize - 1; while (p < op + n) { @@ -154,30 +156,10 @@ static void printcap (Capture *cap, int ident) { } -/* -** Print a capture and its nested captures -*/ -static Capture *printcap2close (Capture *cap, int ident) { - Capture *head = cap++; - printcap(head, ident); /* print head capture */ - while (capinside(head, cap)) - cap = printcap2close(cap, ident + 2); /* print nested captures */ - if (isopencap(head)) { - assert(isclosecap(cap)); - printcap(cap++, ident); /* print and skip close capture */ - } - return cap; -} - - -void printcaplist (Capture *cap) { - { /* for debugging, print first a raw list of captures */ - Capture *c = cap; - while (c->index != MAXINDT) { printcap(c, 0); c++; } - } +void printcaplist (Capture *cap, Capture *fin) { printf(">======\n"); - while (!isclosecap(cap)) - cap = printcap2close(cap, 0); + while (cap < fin) + printcap(cap++, 0); printf("=======\n"); } @@ -202,7 +184,7 @@ static const char *tagnames[] = { }; -void printtree (TTree *tree, int ident) { +static void printtree (TTree *tree, int ident) { int i; int sibs = numsiblings[tree->tag]; for (i = 0; i < ident; i++) printf(" "); @@ -273,7 +255,7 @@ void printtree (TTree *tree, int ident) { } -void printktable (lua_State *L, int idx) { +static void printktable (lua_State *L, int idx) { int n, i; lua_getuservalue(L, idx); if (lua_isnil(L, -1)) /* no ktable? */ @@ -295,4 +277,35 @@ void printktable (lua_State *L, int idx) { /* }====================================================== */ + +static int lp_printtree (lua_State *L) { + Pattern *p = (Pattern *)luaL_checkudata(L, 1, PATTERN_T); + if (lua_toboolean(L, 2)) + prepcompile(L, p, 1); + printktable(L, 1); + printtree(p->tree, 0); + return 0; +} + + +static int lp_printcode (lua_State *L) { + Pattern *p = (Pattern *)luaL_checkudata(L, 1, PATTERN_T); + printktable(L, 1); + if (p->code == NULL) /* not compiled yet? */ + prepcompile(L, p, 1); + printpatt(p->code); + return 0; +} + +static struct luaL_Reg debugreg[] = { + {"ptree", lp_printtree}, + {"pcode", lp_printcode}, + {NULL, NULL} +}; + + +void opendebug (lua_State *L) { + luaL_setfuncs(L, debugreg, 0); +} + #endif diff --git a/lpprint.h b/lpprint.h index e8e04e8..e693081 100644 --- a/lpprint.h +++ b/lpprint.h @@ -9,24 +9,16 @@ #if defined(LPEG_DEBUG) -void printpatt (Instruction *p); -void printtree (TTree *tree, int ident); -void printktable (lua_State *L, int idx); -void printcharset (const byte *st); -void printcaplist (Capture *cap); +void printcaplist (Capture *cap, Capture *fin); void printinst (const Instruction *op, const Instruction *p); +void opendebug (lua_State *L); + #else -#define printktable(L,idx) \ - luaL_error(L, "function only implemented in debug mode") -#define printtree(tree,i) \ - luaL_error(L, "function only implemented in debug mode") -#define printpatt(p) \ - luaL_error(L, "function only implemented in debug mode") +#define opendebug(L) { /* no op */ } #endif - #endif diff --git a/lptree.c b/lptree.c index 6834061..835595c 100644 --- a/lptree.c +++ b/lptree.c @@ -1198,7 +1198,7 @@ static TTree *newgrammar (lua_State *L, int arg) { /* }====================================================== */ -static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { +Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */ finalfix(L, 0, NULL, p->tree); lua_pop(L, 1); /* remove 'ktable' */ @@ -1206,30 +1206,6 @@ static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { } -static int lp_printtree (lua_State *L) { - TTree *tree = getpatt(L, 1, NULL); - int c = lua_toboolean(L, 2); - if (c) { - lua_getuservalue(L, 1); /* push 'ktable' (may be used by 'finalfix') */ - finalfix(L, 0, NULL, tree); - lua_pop(L, 1); /* remove 'ktable' */ - } - printktable(L, 1); - printtree(tree, 0); - return 0; -} - - -static int lp_printcode (lua_State *L) { - Pattern *p = getpattern(L, 1); - printktable(L, 1); - if (p->code == NULL) /* not compiled yet? */ - prepcompile(L, p, 1); - printpatt(p->code); - return 0; -} - - /* ** Get the initial position for the match, interpreting negative ** values from the end of the subject @@ -1349,8 +1325,6 @@ static int lp_locale (lua_State *L) { static struct luaL_Reg pattreg[] = { - {"ptree", lp_printtree}, - {"pcode", lp_printcode}, {"match", lp_match}, {"B", lp_behind}, {"V", lp_V}, @@ -1397,6 +1371,7 @@ int luaopen_lpeg (lua_State *L) { lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); luaL_setfuncs(L, metareg, 0); luaL_newlib(L, pattreg); + opendebug(L); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); lua_pushliteral(L, "LPeg " VERSION); diff --git a/lptree.h b/lptree.h index 051989d..4c0951d 100644 --- a/lptree.h +++ b/lptree.h @@ -85,9 +85,7 @@ extern const byte numsiblings[]; #define sib2(t) ((t) + (t)->u.ps) - - - +union Instruction *prepcompile (lua_State *L, Pattern *p, int idx); #endif diff --git a/makefile b/makefile index 6e49307..6562e2a 100644 --- a/makefile +++ b/makefile @@ -51,7 +51,7 @@ clean: rm -f $(FILES) lpeg.so -lpcap.o: lpcap.c lpcap.h lptypes.h +lpcap.o: lpcap.c lpcap.h lptypes.h lpprint.h lptree.h lpvm.h lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h lpcset.h lpcset.o: lpcset.c lptypes.h lpcset.h lpcode.h lptree.h lpvm.h lpcap.h lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h lpcode.h -- cgit v1.2.3-55-g6feb