/[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 770 - (show annotations)
Mon Nov 28 20:39:30 2011 UTC (7 years, 8 months ago) by zherczeg
File MIME type: application/x-wais-source
File size: 17807 byte(s)
Make character ranges 16 bit friendly
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 + 1 + IMM2_SIZE, 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[1 + IMM2_SIZE]]);
380 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
381 {
382 fprintf(f, " %s ", get_ucpname(code[1 + IMM2_SIZE + 1],
383 code[1 + IMM2_SIZE + 2]));
384 extra = 2;
385 }
386 fprintf(f, "{");
387 if (*code != OP_TYPEEXACT) fprintf(f, "0,");
388 fprintf(f, "%d}", GET2(code,1));
389 if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
390 else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
391 break;
392
393 case OP_NOTI:
394 flag = "/i";
395 /* Fall through */
396 case OP_NOT:
397 c = code[1];
398 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
399 else fprintf(f, " %s [^\\x%02x]", flag, c);
400 break;
401
402 case OP_NOTSTARI:
403 case OP_NOTMINSTARI:
404 case OP_NOTPOSSTARI:
405 case OP_NOTPLUSI:
406 case OP_NOTMINPLUSI:
407 case OP_NOTPOSPLUSI:
408 case OP_NOTQUERYI:
409 case OP_NOTMINQUERYI:
410 case OP_NOTPOSQUERYI:
411 flag = "/i";
412 /* Fall through */
413
414 case OP_NOTSTAR:
415 case OP_NOTMINSTAR:
416 case OP_NOTPOSSTAR:
417 case OP_NOTPLUS:
418 case OP_NOTMINPLUS:
419 case OP_NOTPOSPLUS:
420 case OP_NOTQUERY:
421 case OP_NOTMINQUERY:
422 case OP_NOTPOSQUERY:
423 c = code[1];
424 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
425 else fprintf(f, " %s [^\\x%02x]", flag, c);
426 fprintf(f, "%s", OP_names[*code]);
427 break;
428
429 case OP_NOTEXACTI:
430 case OP_NOTUPTOI:
431 case OP_NOTMINUPTOI:
432 case OP_NOTPOSUPTOI:
433 flag = "/i";
434 /* Fall through */
435
436 case OP_NOTEXACT:
437 case OP_NOTUPTO:
438 case OP_NOTMINUPTO:
439 case OP_NOTPOSUPTO:
440 c = code[1 + IMM2_SIZE];
441 if (PRINTABLE(c)) fprintf(f, " %s [^%c]{", flag, c);
442 else fprintf(f, " %s [^\\x%02x]{", flag, c);
443 if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
444 fprintf(f, "%d}", GET2(code,1));
445 if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
446 else
447 if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
448 break;
449
450 case OP_RECURSE:
451 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
452 else fprintf(f, " ");
453 fprintf(f, "%s", OP_names[*code]);
454 break;
455
456 case OP_REFI:
457 flag = "/i";
458 /* Fall through */
459 case OP_REF:
460 fprintf(f, " %s \\%d", flag, GET2(code,1));
461 ccode = code + PRIV(OP_lengths)[*code];
462 goto CLASS_REF_REPEAT;
463
464 case OP_CALLOUT:
465 fprintf(f, " %s %d %d %d", OP_names[*code], code[1], GET(code,2),
466 GET(code, 2 + LINK_SIZE));
467 break;
468
469 case OP_PROP:
470 case OP_NOTPROP:
471 fprintf(f, " %s %s", OP_names[*code], get_ucpname(code[1], code[2]));
472 break;
473
474 /* OP_XCLASS can only occur in UTF or PCRE16 modes. However, there's no
475 harm in having this code always here, and it makes it less messy without
476 all those #ifdefs. */
477
478 case OP_CLASS:
479 case OP_NCLASS:
480 case OP_XCLASS:
481 {
482 int i, min, max;
483 BOOL printmap;
484 pcre_uint8 *map;
485
486 fprintf(f, " [");
487
488 if (*code == OP_XCLASS)
489 {
490 extra = GET(code, 1);
491 ccode = code + LINK_SIZE + 1;
492 printmap = (*ccode & XCL_MAP) != 0;
493 if ((*ccode++ & XCL_NOT) != 0) fprintf(f, "^");
494 }
495 else
496 {
497 printmap = TRUE;
498 ccode = code + 1;
499 }
500
501 /* Print a bit map */
502
503 if (printmap)
504 {
505 map = (pcre_uint8 *)ccode;
506 for (i = 0; i < 256; i++)
507 {
508 if ((map[i/8] & (1 << (i&7))) != 0)
509 {
510 int j;
511 for (j = i+1; j < 256; j++)
512 if ((map[j/8] & (1 << (j&7))) == 0) break;
513 if (i == '-' || i == ']') fprintf(f, "\\");
514 if (PRINTABLE(i)) fprintf(f, "%c", i);
515 else fprintf(f, "\\x%02x", i);
516 if (--j > i)
517 {
518 if (j != i + 1) fprintf(f, "-");
519 if (j == '-' || j == ']') fprintf(f, "\\");
520 if (PRINTABLE(j)) fprintf(f, "%c", j);
521 else fprintf(f, "\\x%02x", j);
522 }
523 i = j;
524 }
525 }
526 ccode += 32 / sizeof(pcre_uchar);
527 }
528
529 /* For an XCLASS there is always some additional data */
530
531 if (*code == OP_XCLASS)
532 {
533 int ch;
534 while ((ch = *ccode++) != XCL_END)
535 {
536 if (ch == XCL_PROP)
537 {
538 int ptype = *ccode++;
539 int pvalue = *ccode++;
540 fprintf(f, "\\p{%s}", get_ucpname(ptype, pvalue));
541 }
542 else if (ch == XCL_NOTPROP)
543 {
544 int ptype = *ccode++;
545 int pvalue = *ccode++;
546 fprintf(f, "\\P{%s}", get_ucpname(ptype, pvalue));
547 }
548 else
549 {
550 ccode += 1 + print_char(f, ccode, TRUE);
551 if (ch == XCL_RANGE)
552 {
553 fprintf(f, "-");
554 ccode += 1 + print_char(f, ccode, TRUE);
555 }
556 }
557 }
558 }
559
560 /* Indicate a non-UTF8 class which was created by negation */
561
562 fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
563
564 /* Handle repeats after a class or a back reference */
565
566 CLASS_REF_REPEAT:
567 switch(*ccode)
568 {
569 case OP_CRSTAR:
570 case OP_CRMINSTAR:
571 case OP_CRPLUS:
572 case OP_CRMINPLUS:
573 case OP_CRQUERY:
574 case OP_CRMINQUERY:
575 fprintf(f, "%s", OP_names[*ccode]);
576 extra += PRIV(OP_lengths)[*ccode];
577 break;
578
579 case OP_CRRANGE:
580 case OP_CRMINRANGE:
581 min = GET2(ccode,1);
582 max = GET2(ccode,1 + IMM2_SIZE);
583 if (max == 0) fprintf(f, "{%d,}", min);
584 else fprintf(f, "{%d,%d}", min, max);
585 if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
586 extra += PRIV(OP_lengths)[*ccode];
587 break;
588
589 /* Do nothing if it's not a repeat; this code stops picky compilers
590 warning about the lack of a default code path. */
591
592 default:
593 break;
594 }
595 }
596 break;
597
598 case OP_MARK:
599 case OP_PRUNE_ARG:
600 case OP_SKIP_ARG:
601 case OP_THEN_ARG:
602 fprintf(f, " %s ", OP_names[*code]);
603 print_puchar(f, code + 2);
604 extra += code[1];
605 break;
606
607 case OP_THEN:
608 fprintf(f, " %s", OP_names[*code]);
609 break;
610
611 case OP_CIRCM:
612 case OP_DOLLM:
613 flag = "/m";
614 /* Fall through */
615
616 /* Anything else is just an item with no data, but possibly a flag. */
617
618 default:
619 fprintf(f, " %s %s", flag, OP_names[*code]);
620 break;
621 }
622
623 code += PRIV(OP_lengths)[*code] + extra;
624 fprintf(f, "\n");
625 }
626 }
627
628 /* 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