/[pcre]/code/branches/pcre16/pcre_printint.src
ViewVC logotype

Contents of /code/branches/pcre16/pcre_printint.src

Parent Directory Parent Directory | Revision Log Revision Log


Revision 767 - (show annotations)
Sat Nov 26 12:48:56 2011 UTC (7 years, 8 months ago) by zherczeg
File MIME type: application/x-wais-source
File size: 17609 byte(s)
Make simple patterns work in PCRE16
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Copyright (c) 1997-2010 University of Cambridge
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains a PCRE private debugging function for printing out the
42 internal form of a compiled regular expression, along with some supporting
43 local functions. This source file is used in two places:
44
45 (1) It is #included by pcre_compile.c when it is compiled in debugging mode
46 (PCRE_DEBUG defined in pcre_internal.h). It is not included in production
47 compiles.
48
49 (2) It is always #included by pcretest.c, which can be asked to print out a
50 compiled regex for debugging purposes. */
51
52
53 /* Macro that decides whether a character should be output as a literal or in
54 hexadecimal. We don't use isprint() because that can vary from system to system
55 (even without the use of locales) and we want the output always to be the same,
56 for testing purposes. This macro is used in pcretest as well as in this file. */
57
58 #ifdef EBCDIC
59 #define PRINTABLE(c) ((c) >= 64 && (c) < 255)
60 #else
61 #define PRINTABLE(c) ((c) >= 32 && (c) < 127)
62 #endif
63
64 /* The table of operator names. */
65
66 static const char *OP_names[] = { OP_NAME_LIST };
67
68
69
70 /*************************************************
71 * Print single- or multi-byte character *
72 *************************************************/
73
74 static int
75 print_char(FILE *f, pcre_uchar *ptr, BOOL utf8)
76 {
77 int c = *ptr;
78
79 #ifndef SUPPORT_UTF8
80 (void)utf8; /* Avoid compiler warning */
81 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
82 return 0;
83
84 #else
85 if (!utf8 || (c & 0xc0) != 0xc0)
86 {
87 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
88 return 0;
89 }
90 else
91 {
92 int i;
93 int a = PRIV(utf8_table4)[c & 0x3f]; /* Number of additional bytes */
94 int s = 6*a;
95 c = (c & PRIV(utf8_table3)[a]) << s;
96 for (i = 1; i <= a; i++)
97 {
98 /* This is a check for malformed UTF-8; it should only occur if the sanity
99 check has been turned off. Rather than swallow random bytes, just stop if
100 we hit a bad one. Print it with \X instead of \x as an indication. */
101
102 if ((ptr[i] & 0xc0) != 0x80)
103 {
104 fprintf(f, "\\X{%x}", c);
105 return i - 1;
106 }
107
108 /* The byte is OK */
109
110 s -= 6;
111 c |= (ptr[i] & 0x3f) << s;
112 }
113 if (c < 128) fprintf(f, "\\x%02x", c); else fprintf(f, "\\x{%x}", c);
114 return a;
115 }
116 #endif
117 }
118
119 /*************************************************
120 * Print uchar string (regardless of utf8) *
121 *************************************************/
122
123 static void
124 print_puchar(FILE *f, PCRE_PUCHAR ptr)
125 {
126 while (*ptr != '\0')
127 {
128 register int c = *ptr++;
129 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x{%x}", c);
130 }
131 }
132
133 /*************************************************
134 * Find Unicode property name *
135 *************************************************/
136
137 static const char *
138 get_ucpname(int ptype, int pvalue)
139 {
140 #ifdef SUPPORT_UCP
141 int i;
142 for (i = PRIV(utt_size) - 1; i >= 0; i--)
143 {
144 if (ptype == PRIV(utt)[i].type && pvalue == PRIV(utt)[i].value) break;
145 }
146 return (i >= 0)? PRIV(utt_names) + PRIV(utt)[i].name_offset : "??";
147 #else
148 /* It gets harder and harder to shut off unwanted compiler warnings. */
149 ptype = ptype * pvalue;
150 return (ptype == pvalue)? "??" : "??";
151 #endif
152 }
153
154
155
156 /*************************************************
157 * Print compiled regex *
158 *************************************************/
159
160 /* Make this function work for a regex with integers either byte order.
161 However, we assume that what we are passed is a compiled regex. The
162 print_lengths flag controls whether offsets and lengths of items are printed.
163 They can be turned off from pcretest so that automatic tests on bytecode can be
164 written that do not depend on the value of LINK_SIZE. */
165
166 static void
167 pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths)
168 {
169 real_pcre *re = (real_pcre *)external_re;
170 pcre_uchar *codestart, *code;
171 BOOL utf8;
172
173 unsigned int options = re->options;
174 int offset = re->name_table_offset;
175 int count = re->name_count;
176 int size = re->name_entry_size;
177
178 if (re->magic_number != MAGIC_NUMBER)
179 {
180 offset = ((offset << 8) & 0xff00) | ((offset >> 8) & 0xff);
181 count = ((count << 8) & 0xff00) | ((count >> 8) & 0xff);
182 size = ((size << 8) & 0xff00) | ((size >> 8) & 0xff);
183 options = ((options << 24) & 0xff000000) |
184 ((options << 8) & 0x00ff0000) |
185 ((options >> 8) & 0x0000ff00) |
186 ((options >> 24) & 0x000000ff);
187 }
188
189 code = codestart = (pcre_uchar *)re + offset + count * size;
190 utf8 = (options & PCRE_UTF8) != 0;
191
192 for(;;)
193 {
194 pcre_uchar *ccode;
195 const char *flag = " ";
196 int c;
197 int extra = 0;
198
199 if (print_lengths)
200 fprintf(f, "%3d ", (int)(code - codestart));
201 else
202 fprintf(f, " ");
203
204 switch(*code)
205 {
206 /* ========================================================================== */
207 /* These cases are never obeyed. This is a fudge that causes a compile-
208 time error if the vectors OP_names or PRIV(OP_lengths), which are indexed
209 by opcode, are not the correct length. It seems to be the only way to do
210 such a check at compile time, as the sizeof() operator does not work in
211 the C preprocessor. We do this while compiling pcretest, because that
212 #includes pcre_tables.c, which holds PRIV(OP_lengths). We can't do this
213 when building pcre_compile.c with PCRE_DEBUG set, because it doesn't then
214 know the size of PRIV(OP_lengths). */
215
216 #ifdef COMPILING_PCRETEST
217 case OP_TABLE_LENGTH:
218 case OP_TABLE_LENGTH +
219 ((sizeof(OP_names)/sizeof(const char *) == OP_TABLE_LENGTH) &&
220 (sizeof(PRIV(OP_lengths)) == OP_TABLE_LENGTH)):
221 break;
222 #endif
223 /* ========================================================================== */
224
225 case OP_END:
226 fprintf(f, " %s\n", OP_names[*code]);
227 fprintf(f, "------------------------------------------------------------------\n");
228 return;
229
230 case OP_CHAR:
231 fprintf(f, " ");
232 do
233 {
234 code++;
235 code += 1 + print_char(f, code, utf8);
236 }
237 while (*code == OP_CHAR);
238 fprintf(f, "\n");
239 continue;
240
241 case OP_CHARI:
242 fprintf(f, " /i ");
243 do
244 {
245 code++;
246 code += 1 + print_char(f, code, utf8);
247 }
248 while (*code == OP_CHARI);
249 fprintf(f, "\n");
250 continue;
251
252 case OP_CBRA:
253 case OP_CBRAPOS:
254 case OP_SCBRA:
255 case OP_SCBRAPOS:
256 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
257 else fprintf(f, " ");
258 fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
259 break;
260
261 case OP_BRA:
262 case OP_BRAPOS:
263 case OP_SBRA:
264 case OP_SBRAPOS:
265 case OP_KETRMAX:
266 case OP_KETRMIN:
267 case OP_KETRPOS:
268 case OP_ALT:
269 case OP_KET:
270 case OP_ASSERT:
271 case OP_ASSERT_NOT:
272 case OP_ASSERTBACK:
273 case OP_ASSERTBACK_NOT:
274 case OP_ONCE:
275 case OP_ONCE_NC:
276 case OP_COND:
277 case OP_SCOND:
278 case OP_REVERSE:
279 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
280 else fprintf(f, " ");
281 fprintf(f, "%s", OP_names[*code]);
282 break;
283
284 case OP_CLOSE:
285 fprintf(f, " %s %d", OP_names[*code], GET2(code, 1));
286 break;
287
288 case OP_CREF:
289 case OP_NCREF:
290 fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
291 break;
292
293 case OP_RREF:
294 c = GET2(code, 1);
295 if (c == RREF_ANY)
296 fprintf(f, " Cond recurse any");
297 else
298 fprintf(f, " Cond recurse %d", c);
299 break;
300
301 case OP_NRREF:
302 c = GET2(code, 1);
303 if (c == RREF_ANY)
304 fprintf(f, " Cond nrecurse any");
305 else
306 fprintf(f, " Cond nrecurse %d", c);
307 break;
308
309 case OP_DEF:
310 fprintf(f, " Cond def");
311 break;
312
313 case OP_STARI:
314 case OP_MINSTARI:
315 case OP_POSSTARI:
316 case OP_PLUSI:
317 case OP_MINPLUSI:
318 case OP_POSPLUSI:
319 case OP_QUERYI:
320 case OP_MINQUERYI:
321 case OP_POSQUERYI:
322 flag = "/i";
323 /* Fall through */
324 case OP_STAR:
325 case OP_MINSTAR:
326 case OP_POSSTAR:
327 case OP_PLUS:
328 case OP_MINPLUS:
329 case OP_POSPLUS:
330 case OP_QUERY:
331 case OP_MINQUERY:
332 case OP_POSQUERY:
333 case OP_TYPESTAR:
334 case OP_TYPEMINSTAR:
335 case OP_TYPEPOSSTAR:
336 case OP_TYPEPLUS:
337 case OP_TYPEMINPLUS:
338 case OP_TYPEPOSPLUS:
339 case OP_TYPEQUERY:
340 case OP_TYPEMINQUERY:
341 case OP_TYPEPOSQUERY:
342 fprintf(f, " %s ", flag);
343 if (*code >= OP_TYPESTAR)
344 {
345 fprintf(f, "%s", OP_names[code[1]]);
346 if (code[1] == OP_PROP || code[1] == OP_NOTPROP)
347 {
348 fprintf(f, " %s ", get_ucpname(code[2], code[3]));
349 extra = 2;
350 }
351 }
352 else extra = print_char(f, code+1, utf8);
353 fprintf(f, "%s", OP_names[*code]);
354 break;
355
356 case OP_EXACTI:
357 case OP_UPTOI:
358 case OP_MINUPTOI:
359 case OP_POSUPTOI:
360 flag = "/i";
361 /* Fall through */
362 case OP_EXACT:
363 case OP_UPTO:
364 case OP_MINUPTO:
365 case OP_POSUPTO:
366 fprintf(f, " %s ", flag);
367 extra = print_char(f, code+3, utf8);
368 fprintf(f, "{");
369 if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,");
370 fprintf(f, "%d}", GET2(code,1));
371 if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?");
372 else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+");
373 break;
374
375 case OP_TYPEEXACT:
376 case OP_TYPEUPTO:
377 case OP_TYPEMINUPTO:
378 case OP_TYPEPOSUPTO:
379 fprintf(f, " %s", OP_names[code[3]]);
380 if (code[3] == OP_PROP || code[3] == OP_NOTPROP)
381 {
382 fprintf(f, " %s ", get_ucpname(code[4], code[5]));
383 extra = 2;
384 }
385 fprintf(f, "{");
386 if (*code != OP_TYPEEXACT) fprintf(f, "0,");
387 fprintf(f, "%d}", GET2(code,1));
388 if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
389 else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
390 break;
391
392 case OP_NOTI:
393 flag = "/i";
394 /* Fall through */
395 case OP_NOT:
396 c = code[1];
397 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
398 else fprintf(f, " %s [^\\x%02x]", flag, c);
399 break;
400
401 case OP_NOTSTARI:
402 case OP_NOTMINSTARI:
403 case OP_NOTPOSSTARI:
404 case OP_NOTPLUSI:
405 case OP_NOTMINPLUSI:
406 case OP_NOTPOSPLUSI:
407 case OP_NOTQUERYI:
408 case OP_NOTMINQUERYI:
409 case OP_NOTPOSQUERYI:
410 flag = "/i";
411 /* Fall through */
412
413 case OP_NOTSTAR:
414 case OP_NOTMINSTAR:
415 case OP_NOTPOSSTAR:
416 case OP_NOTPLUS:
417 case OP_NOTMINPLUS:
418 case OP_NOTPOSPLUS:
419 case OP_NOTQUERY:
420 case OP_NOTMINQUERY:
421 case OP_NOTPOSQUERY:
422 c = code[1];
423 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
424 else fprintf(f, " %s [^\\x%02x]", flag, c);
425 fprintf(f, "%s", OP_names[*code]);
426 break;
427
428 case OP_NOTEXACTI:
429 case OP_NOTUPTOI:
430 case OP_NOTMINUPTOI:
431 case OP_NOTPOSUPTOI:
432 flag = "/i";
433 /* Fall through */
434
435 case OP_NOTEXACT:
436 case OP_NOTUPTO:
437 case OP_NOTMINUPTO:
438 case OP_NOTPOSUPTO:
439 c = code[3];
440 if (PRINTABLE(c)) fprintf(f, " %s [^%c]{", flag, c);
441 else fprintf(f, " %s [^\\x%02x]{", flag, c);
442 if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
443 fprintf(f, "%d}", GET2(code,1));
444 if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
445 else
446 if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
447 break;
448
449 case OP_RECURSE:
450 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
451 else fprintf(f, " ");
452 fprintf(f, "%s", OP_names[*code]);
453 break;
454
455 case OP_REFI:
456 flag = "/i";
457 /* Fall through */
458 case OP_REF:
459 fprintf(f, " %s \\%d", flag, GET2(code,1));
460 ccode = code + PRIV(OP_lengths)[*code];
461 goto CLASS_REF_REPEAT;
462
463 case OP_CALLOUT:
464 fprintf(f, " %s %d %d %d", OP_names[*code], code[1], GET(code,2),
465 GET(code, 2 + LINK_SIZE));
466 break;
467
468 case OP_PROP:
469 case OP_NOTPROP:
470 fprintf(f, " %s %s", OP_names[*code], get_ucpname(code[1], code[2]));
471 break;
472
473 /* OP_XCLASS can only occur in UTF-8 mode. However, there's no harm in
474 having this code always here, and it makes it less messy without all those
475 #ifdefs. */
476
477 case OP_CLASS:
478 case OP_NCLASS:
479 case OP_XCLASS:
480 {
481 int i, min, max;
482 BOOL printmap;
483
484 fprintf(f, " [");
485
486 if (*code == OP_XCLASS)
487 {
488 extra = GET(code, 1);
489 ccode = code + LINK_SIZE + 1;
490 printmap = (*ccode & XCL_MAP) != 0;
491 if ((*ccode++ & XCL_NOT) != 0) fprintf(f, "^");
492 }
493 else
494 {
495 printmap = TRUE;
496 ccode = code + 1;
497 }
498
499 /* Print a bit map */
500
501 if (printmap)
502 {
503 for (i = 0; i < 256; i++)
504 {
505 if ((ccode[i/8] & (1 << (i&7))) != 0)
506 {
507 int j;
508 for (j = i+1; j < 256; j++)
509 if ((ccode[j/8] & (1 << (j&7))) == 0) break;
510 if (i == '-' || i == ']') fprintf(f, "\\");
511 if (PRINTABLE(i)) fprintf(f, "%c", i);
512 else fprintf(f, "\\x%02x", i);
513 if (--j > i)
514 {
515 if (j != i + 1) fprintf(f, "-");
516 if (j == '-' || j == ']') fprintf(f, "\\");
517 if (PRINTABLE(j)) fprintf(f, "%c", j);
518 else fprintf(f, "\\x%02x", j);
519 }
520 i = j;
521 }
522 }
523 ccode += 32;
524 }
525
526 /* For an XCLASS there is always some additional data */
527
528 if (*code == OP_XCLASS)
529 {
530 int ch;
531 while ((ch = *ccode++) != XCL_END)
532 {
533 if (ch == XCL_PROP)
534 {
535 int ptype = *ccode++;
536 int pvalue = *ccode++;
537 fprintf(f, "\\p{%s}", get_ucpname(ptype, pvalue));
538 }
539 else if (ch == XCL_NOTPROP)
540 {
541 int ptype = *ccode++;
542 int pvalue = *ccode++;
543 fprintf(f, "\\P{%s}", get_ucpname(ptype, pvalue));
544 }
545 else
546 {
547 ccode += 1 + print_char(f, ccode, TRUE);
548 if (ch == XCL_RANGE)
549 {
550 fprintf(f, "-");
551 ccode += 1 + print_char(f, ccode, TRUE);
552 }
553 }
554 }
555 }
556
557 /* Indicate a non-UTF8 class which was created by negation */
558
559 fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
560
561 /* Handle repeats after a class or a back reference */
562
563 CLASS_REF_REPEAT:
564 switch(*ccode)
565 {
566 case OP_CRSTAR:
567 case OP_CRMINSTAR:
568 case OP_CRPLUS:
569 case OP_CRMINPLUS:
570 case OP_CRQUERY:
571 case OP_CRMINQUERY:
572 fprintf(f, "%s", OP_names[*ccode]);
573 extra += PRIV(OP_lengths)[*ccode];
574 break;
575
576 case OP_CRRANGE:
577 case OP_CRMINRANGE:
578 min = GET2(ccode,1);
579 max = GET2(ccode,3);
580 if (max == 0) fprintf(f, "{%d,}", min);
581 else fprintf(f, "{%d,%d}", min, max);
582 if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
583 extra += PRIV(OP_lengths)[*ccode];
584 break;
585
586 /* Do nothing if it's not a repeat; this code stops picky compilers
587 warning about the lack of a default code path. */
588
589 default:
590 break;
591 }
592 }
593 break;
594
595 case OP_MARK:
596 case OP_PRUNE_ARG:
597 case OP_SKIP_ARG:
598 case OP_THEN_ARG:
599 fprintf(f, " %s ", OP_names[*code]);
600 print_puchar(f, code + 2);
601 extra += code[1];
602 break;
603
604 case OP_THEN:
605 fprintf(f, " %s", OP_names[*code]);
606 break;
607
608 case OP_CIRCM:
609 case OP_DOLLM:
610 flag = "/m";
611 /* Fall through */
612
613 /* Anything else is just an item with no data, but possibly a flag. */
614
615 default:
616 fprintf(f, " %s %s", flag, OP_names[*code]);
617 break;
618 }
619
620 code += PRIV(OP_lengths)[*code] + extra;
621 fprintf(f, "\n");
622 }
623 }
624
625 /* End of pcre_printint.src */

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5