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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

code/trunk/pcre_printint.src revision 117 by ph10, Fri Mar 9 15:59:06 2007 UTC code/branches/pcre16/pcre_printint.src revision 767 by zherczeg, Sat Nov 26 12:48:56 2011 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2007 University of Cambridge             Copyright (c) 1997-2010 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 43  internal form of a compiled regular expr Line 43  internal form of a compiled regular expr
43  local functions. This source file is used in two places:  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  (1) It is #included by pcre_compile.c when it is compiled in debugging mode
46  (DEBUG defined in pcre_internal.h). It is not included in production compiles.  (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  (2) It is always #included by pcretest.c, which can be asked to print out a
50  compiled regex for debugging purposes. */  compiled regex for debugging purposes. */
# Line 54  hexadecimal. We don't use isprint() beca Line 55  hexadecimal. We don't use isprint() beca
55  (even without the use of locales) and we want the output always to be the same,  (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. */  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)  #define PRINTABLE(c) ((c) >= 32 && (c) < 127)
62    #endif
63    
64  /* The table of operator names. */  /* The table of operator names. */
65    
# Line 67  static const char *OP_names[] = { OP_NAM Line 72  static const char *OP_names[] = { OP_NAM
72  *************************************************/  *************************************************/
73    
74  static int  static int
75  print_char(FILE *f, uschar *ptr, BOOL utf8)  print_char(FILE *f, pcre_uchar *ptr, BOOL utf8)
76  {  {
77  int c = *ptr;  int c = *ptr;
78    
79  #ifndef SUPPORT_UTF8  #ifndef SUPPORT_UTF8
80  utf8 = utf8;  /* Avoid compiler warning */  (void)utf8;  /* Avoid compiler warning */
81  if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);  if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
82  return 0;  return 0;
83    
# Line 85  if (!utf8 || (c & 0xc0) != 0xc0) Line 90  if (!utf8 || (c & 0xc0) != 0xc0)
90  else  else
91    {    {
92    int i;    int i;
93    int a = _pcre_utf8_table4[c & 0x3f];  /* Number of additional bytes */    int a = PRIV(utf8_table4)[c & 0x3f];  /* Number of additional bytes */
94    int s = 6*a;    int s = 6*a;
95    c = (c & _pcre_utf8_table3[a]) << s;    c = (c & PRIV(utf8_table3)[a]) << s;
96    for (i = 1; i <= a; i++)    for (i = 1; i <= a; i++)
97      {      {
98      /* This is a check for malformed UTF-8; it should only occur if the sanity      /* This is a check for malformed UTF-8; it should only occur if the sanity
# Line 111  else Line 116  else
116  #endif  #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            *  *          Find Unicode property name            *
# Line 122  get_ucpname(int ptype, int pvalue) Line 139  get_ucpname(int ptype, int pvalue)
139  {  {
140  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
141  int i;  int i;
142  for (i = _pcre_utt_size; i >= 0; i--)  for (i = PRIV(utt_size) - 1; i >= 0; i--)
143    {    {
144    if (ptype == _pcre_utt[i].type && pvalue == _pcre_utt[i].value) break;    if (ptype == PRIV(utt)[i].type && pvalue == PRIV(utt)[i].value) break;
145    }    }
146  return (i >= 0)? _pcre_utt[i].name : "??";  return (i >= 0)? PRIV(utt_names) + PRIV(utt)[i].name_offset : "??";
147  #else  #else
148  /* It gets harder and harder to shut off unwanted compiler warnings. */  /* It gets harder and harder to shut off unwanted compiler warnings. */
149  ptype = ptype * pvalue;  ptype = ptype * pvalue;
# Line 142  return (ptype == pvalue)? "??" : "??"; Line 159  return (ptype == pvalue)? "??" : "??";
159    
160  /* Make this function work for a regex with integers either byte order.  /* 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  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.  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  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. */  written that do not depend on the value of LINK_SIZE. */
165    
# Line 150  static void Line 167  static void
167  pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths)  pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths)
168  {  {
169  real_pcre *re = (real_pcre *)external_re;  real_pcre *re = (real_pcre *)external_re;
170  uschar *codestart, *code;  pcre_uchar *codestart, *code;
171  BOOL utf8;  BOOL utf8;
172    
173  unsigned int options = re->options;  unsigned int options = re->options;
# Line 169  if (re->magic_number != MAGIC_NUMBER) Line 186  if (re->magic_number != MAGIC_NUMBER)
186              ((options >> 24) & 0x000000ff);              ((options >> 24) & 0x000000ff);
187    }    }
188    
189  code = codestart = (uschar *)re + offset + count * size;  code = codestart = (pcre_uchar *)re + offset + count * size;
190  utf8 = (options & PCRE_UTF8) != 0;  utf8 = (options & PCRE_UTF8) != 0;
191    
192  for(;;)  for(;;)
193    {    {
194    uschar *ccode;    pcre_uchar *ccode;
195      const char *flag = "  ";
196    int c;    int c;
197    int extra = 0;    int extra = 0;
198    
199    if (print_lengths)    if (print_lengths)
200      fprintf(f, "%3d ", (int)(code - codestart));      fprintf(f, "%3d ", (int)(code - codestart));
201    else    else
202      fprintf(f, "    ");      fprintf(f, "    ");
203    
204    switch(*code)    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:      case OP_END:
226      fprintf(f, "    %s\n", OP_names[*code]);      fprintf(f, "    %s\n", OP_names[*code]);
227      fprintf(f, "------------------------------------------------------------------\n");      fprintf(f, "------------------------------------------------------------------\n");
228      return;      return;
229    
     case OP_OPT:  
     fprintf(f, " %.2x %s", code[1], OP_names[*code]);  
     break;  
   
230      case OP_CHAR:      case OP_CHAR:
231      fprintf(f, "    ");      fprintf(f, "    ");
232      do      do
# Line 205  for(;;) Line 238  for(;;)
238      fprintf(f, "\n");      fprintf(f, "\n");
239      continue;      continue;
240    
241      case OP_CHARNC:      case OP_CHARI:
242      fprintf(f, " NC ");      fprintf(f, " /i ");
243      do      do
244        {        {
245        code++;        code++;
246        code += 1 + print_char(f, code, utf8);        code += 1 + print_char(f, code, utf8);
247        }        }
248      while (*code == OP_CHARNC);      while (*code == OP_CHARI);
249      fprintf(f, "\n");      fprintf(f, "\n");
250      continue;      continue;
251    
252      case OP_CBRA:      case OP_CBRA:
253        case OP_CBRAPOS:
254      case OP_SCBRA:      case OP_SCBRA:
255        case OP_SCBRAPOS:
256      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
257        else fprintf(f, "    ");        else fprintf(f, "    ");
258      fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));      fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
259      break;      break;
260    
261      case OP_BRA:      case OP_BRA:
262        case OP_BRAPOS:
263      case OP_SBRA:      case OP_SBRA:
264        case OP_SBRAPOS:
265      case OP_KETRMAX:      case OP_KETRMAX:
266      case OP_KETRMIN:      case OP_KETRMIN:
267        case OP_KETRPOS:
268      case OP_ALT:      case OP_ALT:
269      case OP_KET:      case OP_KET:
270      case OP_ASSERT:      case OP_ASSERT:
# Line 234  for(;;) Line 272  for(;;)
272      case OP_ASSERTBACK:      case OP_ASSERTBACK:
273      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
274      case OP_ONCE:      case OP_ONCE:
275        case OP_ONCE_NC:
276      case OP_COND:      case OP_COND:
277      case OP_SCOND:      case OP_SCOND:
278      case OP_REVERSE:      case OP_REVERSE:
279      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
280        else fprintf(f, "    ");        else fprintf(f, "    ");
281      fprintf(f, "%s", OP_names[*code]);      fprintf(f, "%s", OP_names[*code]);
282      break;      break;
283    
284        case OP_CLOSE:
285        fprintf(f, "    %s %d", OP_names[*code], GET2(code, 1));
286        break;
287    
288      case OP_CREF:      case OP_CREF:
289        case OP_NCREF:
290      fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);      fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
291      break;      break;
292    
# Line 254  for(;;) Line 298  for(;;)
298        fprintf(f, "    Cond recurse %d", c);        fprintf(f, "    Cond recurse %d", c);
299      break;      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:      case OP_DEF:
310      fprintf(f, "    Cond def");      fprintf(f, "    Cond def");
311      break;      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:      case OP_STAR:
325      case OP_MINSTAR:      case OP_MINSTAR:
326      case OP_POSSTAR:      case OP_POSSTAR:
# Line 276  for(;;) Line 339  for(;;)
339      case OP_TYPEQUERY:      case OP_TYPEQUERY:
340      case OP_TYPEMINQUERY:      case OP_TYPEMINQUERY:
341      case OP_TYPEPOSQUERY:      case OP_TYPEPOSQUERY:
342      fprintf(f, "    ");      fprintf(f, " %s ", flag);
343      if (*code >= OP_TYPESTAR)      if (*code >= OP_TYPESTAR)
344        {        {
345        fprintf(f, "%s", OP_names[code[1]]);        fprintf(f, "%s", OP_names[code[1]]);
# Line 290  for(;;) Line 353  for(;;)
353      fprintf(f, "%s", OP_names[*code]);      fprintf(f, "%s", OP_names[*code]);
354      break;      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:      case OP_EXACT:
363      case OP_UPTO:      case OP_UPTO:
364      case OP_MINUPTO:      case OP_MINUPTO:
365      case OP_POSUPTO:      case OP_POSUPTO:
366      fprintf(f, "    ");      fprintf(f, " %s ", flag);
367      extra = print_char(f, code+3, utf8);      extra = print_char(f, code+3, utf8);
368      fprintf(f, "{");      fprintf(f, "{");
369      if (*code != OP_EXACT) fprintf(f, "0,");      if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,");
370      fprintf(f, "%d}", GET2(code,1));      fprintf(f, "%d}", GET2(code,1));
371      if (*code == OP_MINUPTO) fprintf(f, "?");      if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?");
372        else if (*code == OP_POSUPTO) fprintf(f, "+");        else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+");
373      break;      break;
374    
375      case OP_TYPEEXACT:      case OP_TYPEEXACT:
# Line 320  for(;;) Line 389  for(;;)
389        else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");        else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
390      break;      break;
391    
392        case OP_NOTI:
393        flag = "/i";
394        /* Fall through */
395      case OP_NOT:      case OP_NOT:
396      c = code[1];      c = code[1];
397      if (PRINTABLE(c)) fprintf(f, "    [^%c]", c);      if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
398        else fprintf(f, "    [^\\x%02x]", c);        else fprintf(f, " %s [^\\x%02x]", flag, c);
399      break;      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:      case OP_NOTSTAR:
414      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
415      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
# Line 336  for(;;) Line 420  for(;;)
420      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
421      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
422      c = code[1];      c = code[1];
423      if (PRINTABLE(c)) fprintf(f, "    [^%c]", c);      if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
424        else fprintf(f, "    [^\\x%02x]", c);        else fprintf(f, " %s [^\\x%02x]", flag, c);
425      fprintf(f, "%s", OP_names[*code]);      fprintf(f, "%s", OP_names[*code]);
426      break;      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:      case OP_NOTEXACT:
436      case OP_NOTUPTO:      case OP_NOTUPTO:
437      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
438      case OP_NOTPOSUPTO:      case OP_NOTPOSUPTO:
439      c = code[3];      c = code[3];
440      if (PRINTABLE(c)) fprintf(f, "    [^%c]{", c);      if (PRINTABLE(c)) fprintf(f, " %s [^%c]{", flag, c);
441        else fprintf(f, "    [^\\x%02x]{", c);        else fprintf(f, " %s [^\\x%02x]{", flag, c);
442      if (*code != OP_NOTEXACT) fprintf(f, "0,");      if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
443      fprintf(f, "%d}", GET2(code,1));      fprintf(f, "%d}", GET2(code,1));
444      if (*code == OP_NOTMINUPTO) fprintf(f, "?");      if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
445        else if (*code == OP_NOTPOSUPTO) fprintf(f, "+");        else
446        if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
447      break;      break;
448    
449      case OP_RECURSE:      case OP_RECURSE:
450      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));      if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
451        else fprintf(f, "    ");        else fprintf(f, "    ");
452      fprintf(f, "%s", OP_names[*code]);      fprintf(f, "%s", OP_names[*code]);
453      break;      break;
454    
455        case OP_REFI:
456        flag = "/i";
457        /* Fall through */
458      case OP_REF:      case OP_REF:
459      fprintf(f, "    \\%d", GET2(code,1));      fprintf(f, " %s \\%d", flag, GET2(code,1));
460      ccode = code + _pcre_OP_lengths[*code];      ccode = code + PRIV(OP_lengths)[*code];
461      goto CLASS_REF_REPEAT;      goto CLASS_REF_REPEAT;
462    
463      case OP_CALLOUT:      case OP_CALLOUT:
# Line 475  for(;;) Line 570  for(;;)
570          case OP_CRQUERY:          case OP_CRQUERY:
571          case OP_CRMINQUERY:          case OP_CRMINQUERY:
572          fprintf(f, "%s", OP_names[*ccode]);          fprintf(f, "%s", OP_names[*ccode]);
573          extra += _pcre_OP_lengths[*ccode];          extra += PRIV(OP_lengths)[*ccode];
574          break;          break;
575    
576          case OP_CRRANGE:          case OP_CRRANGE:
# Line 485  for(;;) Line 580  for(;;)
580          if (max == 0) fprintf(f, "{%d,}", min);          if (max == 0) fprintf(f, "{%d,}", min);
581          else fprintf(f, "{%d,%d}", min, max);          else fprintf(f, "{%d,%d}", min, max);
582          if (*ccode == OP_CRMINRANGE) fprintf(f, "?");          if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
583          extra += _pcre_OP_lengths[*ccode];          extra += PRIV(OP_lengths)[*ccode];
584          break;          break;
585    
586          /* Do nothing if it's not a repeat; this code stops picky compilers          /* Do nothing if it's not a repeat; this code stops picky compilers
# Line 497  for(;;) Line 592  for(;;)
592        }        }
593      break;      break;
594    
595      /* Anything else is just an item with no data*/      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      default:      case OP_THEN:
605      fprintf(f, "    %s", OP_names[*code]);      fprintf(f, "    %s", OP_names[*code]);
606      break;      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 += _pcre_OP_lengths[*code] + extra;    code += PRIV(OP_lengths)[*code] + extra;
621    fprintf(f, "\n");    fprintf(f, "\n");
622    }    }
623  }  }

Legend:
Removed from v.117  
changed lines
  Added in v.767

  ViewVC Help
Powered by ViewVC 1.1.5