aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-04-14 17:16:19 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-04-14 17:16:19 -0300
commiteb8b906d5eb5113e7377f06afbfd641c1c5e6a1e (patch)
tree7854589b2605df9f3d97b56d64b72eac3a579b3f
parent0ac14cf58c5b1c954e979dc35e50e610e8dd5115 (diff)
downloadlpeg-eb8b906d5eb5113e7377f06afbfd641c1c5e6a1e.tar.gz
lpeg-eb8b906d5eb5113e7377f06afbfd641c1c5e6a1e.tar.bz2
lpeg-eb8b906d5eb5113e7377f06afbfd641c1c5e6a1e.zip
Some refactoring in debug code
Functions lp_printtree and lp_printcode moved to lpprint.c, to concentrate there debug/testing code.
-rw-r--r--lpprint.c67
-rw-r--r--lpprint.h16
-rw-r--r--lptree.c29
-rw-r--r--lptree.h4
-rw-r--r--makefile2
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 @@
3#include <limits.h> 3#include <limits.h>
4#include <stdio.h> 4#include <stdio.h>
5 5
6#include "lauxlib.h"
6 7
7#include "lptypes.h" 8#include "lptypes.h"
8#include "lpprint.h" 9#include "lpprint.h"
9#include "lpcode.h" 10#include "lpcode.h"
11#include "lptree.h"
10 12
11 13
12#if defined(LPEG_DEBUG) 14#if defined(LPEG_DEBUG)
@@ -18,7 +20,7 @@
18*/ 20*/
19 21
20 22
21void printcharset (const byte *st) { 23static void printcharset (const byte *st) {
22 int i; 24 int i;
23 printf("["); 25 printf("[");
24 for (i = 0; i <= UCHAR_MAX; i++) { 26 for (i = 0; i <= UCHAR_MAX; i++) {
@@ -137,7 +139,7 @@ void printinst (const Instruction *op, const Instruction *p) {
137} 139}
138 140
139 141
140void printpatt (Instruction *p) { 142static void printpatt (Instruction *p) {
141 Instruction *op = p; 143 Instruction *op = p;
142 uint n = op[-1].codesize - 1; 144 uint n = op[-1].codesize - 1;
143 while (p < op + n) { 145 while (p < op + n) {
@@ -154,30 +156,10 @@ static void printcap (Capture *cap, int ident) {
154} 156}
155 157
156 158
157/* 159void printcaplist (Capture *cap, Capture *fin) {
158** Print a capture and its nested captures
159*/
160static Capture *printcap2close (Capture *cap, int ident) {
161 Capture *head = cap++;
162 printcap(head, ident); /* print head capture */
163 while (capinside(head, cap))
164 cap = printcap2close(cap, ident + 2); /* print nested captures */
165 if (isopencap(head)) {
166 assert(isclosecap(cap));
167 printcap(cap++, ident); /* print and skip close capture */
168 }
169 return cap;
170}
171
172
173void printcaplist (Capture *cap) {
174 { /* for debugging, print first a raw list of captures */
175 Capture *c = cap;
176 while (c->index != MAXINDT) { printcap(c, 0); c++; }
177 }
178 printf(">======\n"); 160 printf(">======\n");
179 while (!isclosecap(cap)) 161 while (cap < fin)
180 cap = printcap2close(cap, 0); 162 printcap(cap++, 0);
181 printf("=======\n"); 163 printf("=======\n");
182} 164}
183 165
@@ -202,7 +184,7 @@ static const char *tagnames[] = {
202}; 184};
203 185
204 186
205void printtree (TTree *tree, int ident) { 187static void printtree (TTree *tree, int ident) {
206 int i; 188 int i;
207 int sibs = numsiblings[tree->tag]; 189 int sibs = numsiblings[tree->tag];
208 for (i = 0; i < ident; i++) printf(" "); 190 for (i = 0; i < ident; i++) printf(" ");
@@ -273,7 +255,7 @@ void printtree (TTree *tree, int ident) {
273} 255}
274 256
275 257
276void printktable (lua_State *L, int idx) { 258static void printktable (lua_State *L, int idx) {
277 int n, i; 259 int n, i;
278 lua_getuservalue(L, idx); 260 lua_getuservalue(L, idx);
279 if (lua_isnil(L, -1)) /* no ktable? */ 261 if (lua_isnil(L, -1)) /* no ktable? */
@@ -295,4 +277,35 @@ void printktable (lua_State *L, int idx) {
295 277
296/* }====================================================== */ 278/* }====================================================== */
297 279
280
281static int lp_printtree (lua_State *L) {
282 Pattern *p = (Pattern *)luaL_checkudata(L, 1, PATTERN_T);
283 if (lua_toboolean(L, 2))
284 prepcompile(L, p, 1);
285 printktable(L, 1);
286 printtree(p->tree, 0);
287 return 0;
288}
289
290
291static int lp_printcode (lua_State *L) {
292 Pattern *p = (Pattern *)luaL_checkudata(L, 1, PATTERN_T);
293 printktable(L, 1);
294 if (p->code == NULL) /* not compiled yet? */
295 prepcompile(L, p, 1);
296 printpatt(p->code);
297 return 0;
298}
299
300static struct luaL_Reg debugreg[] = {
301 {"ptree", lp_printtree},
302 {"pcode", lp_printcode},
303 {NULL, NULL}
304};
305
306
307void opendebug (lua_State *L) {
308 luaL_setfuncs(L, debugreg, 0);
309}
310
298#endif 311#endif
diff --git a/lpprint.h b/lpprint.h
index e8e04e8..e693081 100644
--- a/lpprint.h
+++ b/lpprint.h
@@ -9,24 +9,16 @@
9 9
10#if defined(LPEG_DEBUG) 10#if defined(LPEG_DEBUG)
11 11
12void printpatt (Instruction *p); 12void printcaplist (Capture *cap, Capture *fin);
13void printtree (TTree *tree, int ident);
14void printktable (lua_State *L, int idx);
15void printcharset (const byte *st);
16void printcaplist (Capture *cap);
17void printinst (const Instruction *op, const Instruction *p); 13void printinst (const Instruction *op, const Instruction *p);
18 14
15void opendebug (lua_State *L);
16
19#else 17#else
20 18
21#define printktable(L,idx) \ 19#define opendebug(L) { /* no op */ }
22 luaL_error(L, "function only implemented in debug mode")
23#define printtree(tree,i) \
24 luaL_error(L, "function only implemented in debug mode")
25#define printpatt(p) \
26 luaL_error(L, "function only implemented in debug mode")
27 20
28#endif 21#endif
29 22
30
31#endif 23#endif
32 24
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) {
1198/* }====================================================== */ 1198/* }====================================================== */
1199 1199
1200 1200
1201static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { 1201Instruction *prepcompile (lua_State *L, Pattern *p, int idx) {
1202 lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */ 1202 lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */
1203 finalfix(L, 0, NULL, p->tree); 1203 finalfix(L, 0, NULL, p->tree);
1204 lua_pop(L, 1); /* remove 'ktable' */ 1204 lua_pop(L, 1); /* remove 'ktable' */
@@ -1206,30 +1206,6 @@ static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) {
1206} 1206}
1207 1207
1208 1208
1209static int lp_printtree (lua_State *L) {
1210 TTree *tree = getpatt(L, 1, NULL);
1211 int c = lua_toboolean(L, 2);
1212 if (c) {
1213 lua_getuservalue(L, 1); /* push 'ktable' (may be used by 'finalfix') */
1214 finalfix(L, 0, NULL, tree);
1215 lua_pop(L, 1); /* remove 'ktable' */
1216 }
1217 printktable(L, 1);
1218 printtree(tree, 0);
1219 return 0;
1220}
1221
1222
1223static int lp_printcode (lua_State *L) {
1224 Pattern *p = getpattern(L, 1);
1225 printktable(L, 1);
1226 if (p->code == NULL) /* not compiled yet? */
1227 prepcompile(L, p, 1);
1228 printpatt(p->code);
1229 return 0;
1230}
1231
1232
1233/* 1209/*
1234** Get the initial position for the match, interpreting negative 1210** Get the initial position for the match, interpreting negative
1235** values from the end of the subject 1211** values from the end of the subject
@@ -1349,8 +1325,6 @@ static int lp_locale (lua_State *L) {
1349 1325
1350 1326
1351static struct luaL_Reg pattreg[] = { 1327static struct luaL_Reg pattreg[] = {
1352 {"ptree", lp_printtree},
1353 {"pcode", lp_printcode},
1354 {"match", lp_match}, 1328 {"match", lp_match},
1355 {"B", lp_behind}, 1329 {"B", lp_behind},
1356 {"V", lp_V}, 1330 {"V", lp_V},
@@ -1397,6 +1371,7 @@ int luaopen_lpeg (lua_State *L) {
1397 lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); 1371 lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX);
1398 luaL_setfuncs(L, metareg, 0); 1372 luaL_setfuncs(L, metareg, 0);
1399 luaL_newlib(L, pattreg); 1373 luaL_newlib(L, pattreg);
1374 opendebug(L);
1400 lua_pushvalue(L, -1); 1375 lua_pushvalue(L, -1);
1401 lua_setfield(L, -3, "__index"); 1376 lua_setfield(L, -3, "__index");
1402 lua_pushliteral(L, "LPeg " VERSION); 1377 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[];
85#define sib2(t) ((t) + (t)->u.ps) 85#define sib2(t) ((t) + (t)->u.ps)
86 86
87 87
88 88union Instruction *prepcompile (lua_State *L, Pattern *p, int idx);
89
90
91 89
92#endif 90#endif
93 91
diff --git a/makefile b/makefile
index 6e49307..6562e2a 100644
--- a/makefile
+++ b/makefile
@@ -51,7 +51,7 @@ clean:
51 rm -f $(FILES) lpeg.so 51 rm -f $(FILES) lpeg.so
52 52
53 53
54lpcap.o: lpcap.c lpcap.h lptypes.h 54lpcap.o: lpcap.c lpcap.h lptypes.h lpprint.h lptree.h lpvm.h
55lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h lpcset.h 55lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h lpcset.h
56lpcset.o: lpcset.c lptypes.h lpcset.h lpcode.h lptree.h lpvm.h lpcap.h 56lpcset.o: lpcset.c lptypes.h lpcset.h lpcode.h lptree.h lpvm.h lpcap.h
57lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h lpcode.h 57lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h lpcode.h