diff options
Diffstat (limited to 'lpprint.c')
| -rw-r--r-- | lpprint.c | 67 |
1 files changed, 40 insertions, 27 deletions
| @@ -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 | ||
| 21 | void printcharset (const byte *st) { | 23 | static 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 | ||
| 140 | void printpatt (Instruction *p) { | 142 | static 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 | /* | 159 | void printcaplist (Capture *cap, Capture *fin) { |
| 158 | ** Print a capture and its nested captures | ||
| 159 | */ | ||
| 160 | static 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 | |||
| 173 | void 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 | ||
| 205 | void printtree (TTree *tree, int ident) { | 187 | static 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 | ||
| 276 | void printktable (lua_State *L, int idx) { | 258 | static 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 | |||
| 281 | static 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 | |||
| 291 | static 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 | |||
| 300 | static struct luaL_Reg debugreg[] = { | ||
| 301 | {"ptree", lp_printtree}, | ||
| 302 | {"pcode", lp_printcode}, | ||
| 303 | {NULL, NULL} | ||
| 304 | }; | ||
| 305 | |||
| 306 | |||
| 307 | void opendebug (lua_State *L) { | ||
| 308 | luaL_setfuncs(L, debugreg, 0); | ||
| 309 | } | ||
| 310 | |||
| 298 | #endif | 311 | #endif |
