aboutsummaryrefslogtreecommitdiff
path: root/lpprint.c
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 /lpprint.c
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.
Diffstat (limited to 'lpprint.c')
-rw-r--r--lpprint.c67
1 files changed, 40 insertions, 27 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