aboutsummaryrefslogtreecommitdiff
path: root/lpprint.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-06-15 11:28:33 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-06-15 11:28:33 -0300
commita561630f17e61548193666abf9a8b20f20462558 (patch)
treeb1ffda7472f2c801bb7677239b42b0e09b1ceec9 /lpprint.c
parentcf1705c1d96b549ef5887a2bc3038dbc31912e50 (diff)
downloadlpeg-a561630f17e61548193666abf9a8b20f20462558.tar.gz
lpeg-a561630f17e61548193666abf9a8b20f20462558.tar.bz2
lpeg-a561630f17e61548193666abf9a8b20f20462558.zip
Full captures can contain nested captures
Nested captures can be recognized because they start (and end) inside the character range of the full capture. This optimization can remove a lot of 'close' captures from the capture logs.
Diffstat (limited to 'lpprint.c')
-rw-r--r--lpprint.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/lpprint.c b/lpprint.c
index bdb85b8..6349ac2 100644
--- a/lpprint.c
+++ b/lpprint.c
@@ -149,27 +149,35 @@ void printpatt (Instruction *p) {
149 149
150static void printcap (Capture *cap, int ident) { 150static void printcap (Capture *cap, int ident) {
151 while (ident--) printf(" "); 151 while (ident--) printf(" ");
152 printf("%s (idx: %d - size: %d) -> %ld\n", 152 printf("%s (idx: %d - size: %d) -> %lu\n",
153 capkind(cap->kind), cap->idx, cap->siz, (long)cap->index); 153 capkind(cap->kind), cap->idx, cap->siz, (long)cap->index);
154} 154}
155 155
156 156
157/*
158** Print a capture and its nested captures
159*/
157static Capture *printcap2close (Capture *cap, int ident) { 160static Capture *printcap2close (Capture *cap, int ident) {
158 while (cap->kind != Cclose) { 161 Capture *head = cap++;
159 printcap(cap, ident); 162 printcap(head, ident); /* print head capture */
160 if (cap->siz == 0) { 163 while (capinside(head, cap))
161 cap = printcap2close(cap + 1, ident + 2); 164 cap = printcap2close(cap, ident + 2); /* print nested captures */
162 printcap(cap, ident); /* print 'close' capture */ 165 if (isopencap(head)) {
163 } 166 assert(isclosecap(cap));
164 cap++; 167 printcap(cap++, ident); /* print and skip close capture */
165 } 168 }
166 return cap; 169 return cap;
167} 170}
168 171
169 172
170void printcaplist (Capture *cap) { 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 }
171 printf(">======\n"); 178 printf(">======\n");
172 printcap2close(cap, 0); 179 while (!isclosecap(cap))
180 cap = printcap2close(cap, 0);
173 printf("=======\n"); 181 printf("=======\n");
174} 182}
175 183