/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Diff of /code/branches/pcre16/pcre_compile.c

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

revision 514 by ph10, Mon May 3 12:54:22 2010 UTC revision 518 by ph10, Tue May 18 15:47:01 2010 UTC
# Line 261  static const int posix_class_maps[] = { Line 261  static const int posix_class_maps[] = {
261    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
262  };  };
263    
264    /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
265    substitutes must be in the order of the names, defined above, and there are
266    both positive and negative cases. NULL means no substitute. */
267    
268    #ifdef SUPPORT_UCP
269    static const uschar *substitutes[] = {
270      (uschar *)"\\P{Nd}",    /* \D */
271      (uschar *)"\\p{Nd}",    /* \d */
272      (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */
273      (uschar *)"\\p{Xsp}",   /* \s */
274      (uschar *)"\\P{Xwd}",   /* \W */
275      (uschar *)"\\p{Xwd}"    /* \w */
276    };
277    
278    static const uschar *posix_substitutes[] = {
279      (uschar *)"\\p{L}",     /* alpha */
280      (uschar *)"\\p{Ll}",    /* lower */
281      (uschar *)"\\p{Lu}",    /* upper */
282      (uschar *)"\\p{Xan}",   /* alnum */
283      NULL,                   /* ascii */
284      (uschar *)"\\h",        /* blank */
285      NULL,                   /* cntrl */
286      (uschar *)"\\p{Nd}",    /* digit */
287      NULL,                   /* graph */
288      NULL,                   /* print */
289      NULL,                   /* punct */
290      (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */
291      (uschar *)"\\p{Xwd}",   /* word */
292      NULL,                   /* xdigit */
293      /* Negated cases */
294      (uschar *)"\\P{L}",     /* ^alpha */
295      (uschar *)"\\P{Ll}",    /* ^lower */
296      (uschar *)"\\P{Lu}",    /* ^upper */
297      (uschar *)"\\P{Xan}",   /* ^alnum */
298      NULL,                   /* ^ascii */
299      (uschar *)"\\H",        /* ^blank */
300      NULL,                   /* ^cntrl */
301      (uschar *)"\\P{Nd}",    /* ^digit */
302      NULL,                   /* ^graph */
303      NULL,                   /* ^print */
304      NULL,                   /* ^punct */
305      (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */
306      (uschar *)"\\P{Xwd}",   /* ^word */
307      NULL                    /* ^xdigit */
308    };
309    #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))
310    #endif
311    
312  #define STRING(a)  # a  #define STRING(a)  # a
313  #define XSTRING(s) STRING(s)  #define XSTRING(s) STRING(s)
# Line 360  static const char error_texts[] = Line 407  static const char error_texts[] =
407    /* 65 */    /* 65 */
408    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
409    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
410      "this version of PCRE is not compiled with PCRE_UCP support\0"
411    ;    ;
412    
413  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 829  else Line 877  else
877      break;      break;
878      }      }
879    }    }
880    
881  /* Perl supports \N{name} for character names, as well as plain \N for "not  /* Perl supports \N{name} for character names, as well as plain \N for "not
882  newline". PCRE does not support \N{name}. */  newline". PCRE does not support \N{name}. */
883    
884  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)  if (c == -ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET)
885    *errorcodeptr = ERR37;    *errorcodeptr = ERR37;
886    
887    /* If PCRE_UCP is set, we change the values for \d etc. */
888    
889    if ((options & PCRE_UCP) != 0 && c <= -ESC_D && c >= -ESC_w)
890      c -= (ESC_DU - ESC_D);
891    
892    /* Set the pointer to the final character before returning. */
893    
894  *ptrptr = ptr;  *ptrptr = ptr;
895  return c;  return c;
# Line 2725  BOOL inescq = FALSE; Line 2780  BOOL inescq = FALSE;
2780  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstbyte = FALSE;
2781  const uschar *ptr = *ptrptr;  const uschar *ptr = *ptrptr;
2782  const uschar *tempptr;  const uschar *tempptr;
2783    const uschar *nestptr = NULL;
2784  uschar *previous = NULL;  uschar *previous = NULL;
2785  uschar *previous_callout = NULL;  uschar *previous_callout = NULL;
2786  uschar *save_hwm = NULL;  uschar *save_hwm = NULL;
# Line 2795  for (;; ptr++) Line 2851  for (;; ptr++)
2851    
2852    c = *ptr;    c = *ptr;
2853    
2854      /* If we are at the end of a nested substitution, revert to the outer level
2855      string. Nesting only happens one level deep. */
2856    
2857      if (c == 0 && nestptr != NULL)
2858        {
2859        ptr = nestptr;
2860        nestptr = NULL;
2861        c = *ptr;
2862        }
2863    
2864    /* If we are in the pre-compile phase, accumulate the length used for the    /* If we are in the pre-compile phase, accumulate the length used for the
2865    previous cycle of this loop. */    previous cycle of this loop. */
2866    
# Line 3092  for (;; ptr++) Line 3158  for (;; ptr++)
3158          {                           /* Braces are required because the */          {                           /* Braces are required because the */
3159          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
3160          }          }
3161    
3162        /* In the pre-compile phase, accumulate the length of any UTF-8 extra        /* In the pre-compile phase, accumulate the length of any UTF-8 extra
3163        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
3164        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion UTF-8 characters no longer overwrite the work space
# Line 3161  for (;; ptr++) Line 3227  for (;; ptr++)
3227    
3228          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
3229            posix_class = 0;            posix_class = 0;
3230    
3231          /* We build the bit map for the POSIX class in a chunk of local store          /* When PCRE_UCP is set, some of the POSIX classes are converted to
3232          because we may be adding and subtracting from it, and we don't want to          different escape sequences that use Unicode properties. */
3233          subtract bits that may be in the main map already. At the end we or the  
3234          result into the bit map that is being built. */  #ifdef SUPPORT_UCP
3235            if ((options & PCRE_UCP) != 0)
3236              {
3237              int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
3238              if (posix_substitutes[pc] != NULL)
3239                {
3240                nestptr = tempptr + 1;
3241                ptr = posix_substitutes[pc] - 1;
3242                continue;
3243                }
3244              }
3245    #endif
3246            /* In the non-UCP case, we build the bit map for the POSIX class in a
3247            chunk of local store because we may be adding and subtracting from it,
3248            and we don't want to subtract bits that may be in the main map already.
3249            At the end we or the result into the bit map that is being built. */
3250    
3251          posix_class *= 3;          posix_class *= 3;
3252    
# Line 3237  for (;; ptr++) Line 3318  for (;; ptr++)
3318            register const uschar *cbits = cd->cbits;            register const uschar *cbits = cd->cbits;
3319            class_charcount += 2;     /* Greater than 1 is what matters */            class_charcount += 2;     /* Greater than 1 is what matters */
3320    
3321            /* Save time by not doing this in the pre-compile phase. */            switch (-c)
   
           if (lengthptr == NULL) switch (-c)  
3322              {              {
3323    #ifdef SUPPORT_UCP
3324                case ESC_du:     /* These are the values given for \d etc */
3325                case ESC_DU:     /* when PCRE_UCP is set. We replace the */
3326                case ESC_wu:     /* escape sequence with an appropriate \p */
3327                case ESC_WU:     /* or \P to test Unicode properties instead */
3328                case ESC_su:     /* of the default ASCII testing. */
3329                case ESC_SU:
3330                nestptr = ptr;
3331                ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
3332                class_charcount -= 2;                /* Undo! */
3333                continue;
3334    #endif
3335              case ESC_d:              case ESC_d:
3336              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
3337              continue;              continue;
# Line 3270  for (;; ptr++) Line 3361  for (;; ptr++)
3361              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
3362              continue;              continue;
3363    
3364              default:    /* Not recognized; fall through */              case ESC_h:
             break;      /* Need "default" setting to stop compiler warning. */  
             }  
   
           /* In the pre-compile phase, just do the recognition. */  
   
           else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||  
                    c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;  
   
           /* We need to deal with \H, \h, \V, and \v in both phases because  
           they use extra memory. */  
   
           if (-c == ESC_h)  
             {  
3365              SETBIT(classbits, 0x09); /* VT */              SETBIT(classbits, 0x09); /* VT */
3366              SETBIT(classbits, 0x20); /* SPACE */              SETBIT(classbits, 0x20); /* SPACE */
3367              SETBIT(classbits, 0xa0); /* NSBP */              SETBIT(classbits, 0xa0); /* NSBP */
# Line 3307  for (;; ptr++) Line 3385  for (;; ptr++)
3385                }                }
3386  #endif  #endif
3387              continue;              continue;
             }  
3388    
3389            if (-c == ESC_H)              case ESC_H:
             {  
3390              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3391                {                {
3392                int x = 0xff;                int x = 0xff;
# Line 3352  for (;; ptr++) Line 3428  for (;; ptr++)
3428                }                }
3429  #endif  #endif
3430              continue;              continue;
             }  
3431    
3432            if (-c == ESC_v)              case ESC_v:
             {  
3433              SETBIT(classbits, 0x0a); /* LF */              SETBIT(classbits, 0x0a); /* LF */
3434              SETBIT(classbits, 0x0b); /* VT */              SETBIT(classbits, 0x0b); /* VT */
3435              SETBIT(classbits, 0x0c); /* FF */              SETBIT(classbits, 0x0c); /* FF */
# Line 3371  for (;; ptr++) Line 3445  for (;; ptr++)
3445                }                }
3446  #endif  #endif
3447              continue;              continue;
             }  
3448    
3449            if (-c == ESC_V)              case ESC_V:
             {  
3450              for (c = 0; c < 32; c++)              for (c = 0; c < 32; c++)
3451                {                {
3452                int x = 0xff;                int x = 0xff;
# Line 3404  for (;; ptr++) Line 3476  for (;; ptr++)
3476                }                }
3477  #endif  #endif
3478              continue;              continue;
             }  
   
           /* We need to deal with \P and \p in both phases. */  
3479    
3480  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3481            if (-c == ESC_p || -c == ESC_P)              case ESC_p:
3482              {              case ESC_P:
3483              BOOL negated;                {
3484              int pdata;                BOOL negated;
3485              int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int pdata;
3486              if (ptype < 0) goto FAILED;                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
3487              class_utf8 = TRUE;                if (ptype < 0) goto FAILED;
3488              *class_utf8data++ = ((-c == ESC_p) != negated)?                class_utf8 = TRUE;
3489                XCL_PROP : XCL_NOTPROP;                *class_utf8data++ = ((-c == ESC_p) != negated)?
3490              *class_utf8data++ = ptype;                  XCL_PROP : XCL_NOTPROP;
3491              *class_utf8data++ = pdata;                *class_utf8data++ = ptype;
3492              class_charcount -= 2;   /* Not a < 256 character */                *class_utf8data++ = pdata;
3493              continue;                class_charcount -= 2;   /* Not a < 256 character */
3494              }                continue;
3495                  }
3496  #endif  #endif
3497            /* Unrecognized escapes are faulted if PCRE is running in its              /* Unrecognized escapes are faulted if PCRE is running in its
3498            strict mode. By default, for compatibility with Perl, they are              strict mode. By default, for compatibility with Perl, they are
3499            treated as literals. */              treated as literals. */
3500    
3501            if ((options & PCRE_EXTRA) != 0)              default:
3502              {              if ((options & PCRE_EXTRA) != 0)
3503              *errorcodeptr = ERR7;                {
3504              goto FAILED;                *errorcodeptr = ERR7;
3505                  goto FAILED;
3506                  }
3507                class_charcount -= 2;  /* Undo the default count from above */
3508                c = *ptr;              /* Get the final character and fall through */
3509                break;
3510              }              }
   
           class_charcount -= 2;  /* Undo the default count from above */  
           c = *ptr;              /* Get the final character and fall through */  
3511            }            }
3512    
3513          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if we have a single character (c >= 0). This may be
# Line 3675  for (;; ptr++) Line 3747  for (;; ptr++)
3747          }          }
3748        }        }
3749    
3750      /* Loop until ']' reached. This "while" is the end of the "do" above. */      /* Loop until ']' reached. This "while" is the end of the "do" far above.
3751        If we are at the end of an internal nested string, revert to the outer
3752        string. */
3753    
3754      while ((c = *(++ptr)) != 0 && (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));      while (((c = *(++ptr)) != 0 ||
3755               (nestptr != NULL &&
3756                 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != 0)) &&
3757               (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
3758    
3759      if (c == 0)                          /* Missing terminating ']' */      /* Check for missing terminating ']' */
3760    
3761        if (c == 0)
3762        {        {
3763        *errorcodeptr = ERR6;        *errorcodeptr = ERR6;
3764        goto FAILED;        goto FAILED;
3765        }        }
3766    
   
 /* This code has been disabled because it would mean that \s counts as  
 an explicit \r or \n reference, and that's not really what is wanted. Now  
 we set the flag only if there is a literal "\r" or "\n" in the class. */  
   
 #if 0  
     /* Remember whether \r or \n are in this class */  
   
     if (negate_class)  
       {  
       if ((classbits[1] & 0x24) != 0x24) cd->external_flags |= PCRE_HASCRORLF;  
       }  
     else  
       {  
       if ((classbits[1] & 0x24) != 0) cd->external_flags |= PCRE_HASCRORLF;  
       }  
 #endif  
   
   
3767      /* If class_charcount is 1, we saw precisely one character whose value is      /* If class_charcount is 1, we saw precisely one character whose value is
3768      less than 256. As long as there were no characters >= 128 and there was no      less than 256. As long as there were no characters >= 128 and there was no
3769      use of \p or \P, in other words, no use of any XCLASS features, we can      use of \p or \P, in other words, no use of any XCLASS features, we can
# Line 3720  we set the flag only if there is a liter Line 3780  we set the flag only if there is a liter
3780      can cause firstbyte to be set. Otherwise, there can be no first char if      can cause firstbyte to be set. Otherwise, there can be no first char if
3781      this item is first, whatever repeat count may follow. In the case of      this item is first, whatever repeat count may follow. In the case of
3782      reqbyte, save the previous value for reinstating. */      reqbyte, save the previous value for reinstating. */
3783    
3784  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3785      if (class_charcount == 1 && !class_utf8 &&      if (class_charcount == 1 && !class_utf8 &&
3786        (!utf8 || !negate_class || class_lastchar < 128))        (!utf8 || !negate_class || class_lastchar < 128))
# Line 3767  we set the flag only if there is a liter Line 3827  we set the flag only if there is a liter
3827    
3828      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
3829      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
3830      such as \S in the class, because in that case all characters > 255 are in      such as \S in the class, and PCRE_UCP is not set, because in that case all
3831      the class, so any that were explicitly given as well can be ignored. If      characters > 255 are in the class, so any that were explicitly given as
3832      (when there are explicit characters > 255 that must be listed) there are no      well can be ignored. If (when there are explicit characters > 255 that must
3833      characters < 256, we can omit the bitmap in the actual compiled code. */      be listed) there are no characters < 256, we can omit the bitmap in the
3834        actual compiled code. */
3835    
3836  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
3837      if (class_utf8 && !should_flip_negation)      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))
3838        {        {
3839        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */
3840        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
# Line 3799  we set the flag only if there is a liter Line 3860  we set the flag only if there is a liter
3860        }        }
3861  #endif  #endif
3862    
3863      /* If there are no characters > 255, set the opcode to OP_CLASS or      /* If there are no characters > 255, or they are all to be included or
3864      OP_NCLASS, depending on whether the whole class was negated and whether      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
3865      there were negative specials such as \S in the class. Then copy the 32-byte      whole class was negated and whether there were negative specials such as \S
3866      map into the code vector, negating it if necessary. */      (non-UCP) in the class. Then copy the 32-byte map into the code vector,
3867        negating it if necessary. */
3868    
3869      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
3870      if (negate_class)      if (negate_class)
# Line 5603  we set the flag only if there is a liter Line 5665  we set the flag only if there is a liter
5665    
5666      /* ===================================================================*/      /* ===================================================================*/
5667      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values      /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
5668      are arranged to be the negation of the corresponding OP_values. For the      are arranged to be the negation of the corresponding OP_values in the
5669      back references, the values are ESC_REF plus the reference number. Only      default case when PCRE_UCP is not set. For the back references, the values
5670      back references and those types that consume a character may be repeated.      are ESC_REF plus the reference number. Only back references and those types
5671      We can test for values between ESC_b and ESC_Z for the latter; this may      that consume a character may be repeated. We can test for values between
5672      have to change if any new ones are ever created. */      ESC_b and ESC_Z for the latter; this may have to change if any new ones are
5673        ever created. */
5674    
5675      case CHAR_BACKSLASH:      case CHAR_BACKSLASH:
5676      tempptr = ptr;      tempptr = ptr;
# Line 5767  we set the flag only if there is a liter Line 5830  we set the flag only if there is a liter
5830  #endif  #endif
5831    
5832        /* For the rest (including \X when Unicode properties are supported), we        /* For the rest (including \X when Unicode properties are supported), we
5833        can obtain the OP value by negating the escape value. */        can obtain the OP value by negating the escape value in the default
5834          situation when PCRE_UCP is not set. When it *is* set, we substitute
5835          Unicode property tests. */
5836    
5837        else        else
5838          {          {
5839          previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;  #ifdef SUPPORT_UCP
5840          *code++ = -c;          if (-c >= ESC_DU && -c <= ESC_wu)
5841              {
5842              nestptr = ptr + 1;                   /* Where to resume */
5843              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
5844              }
5845            else
5846    #endif
5847              {
5848              previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
5849              *code++ = -c;
5850              }
5851          }          }
5852        continue;        continue;
5853        }        }
# Line 6509  int length = 1;  /* For final END opcode Line 6584  int length = 1;  /* For final END opcode
6584  int firstbyte, reqbyte, newline;  int firstbyte, reqbyte, newline;
6585  int errorcode = 0;  int errorcode = 0;
6586  int skipatstart = 0;  int skipatstart = 0;
6587  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8;
6588  size_t size;  size_t size;
6589  uschar *code;  uschar *code;
6590  const uschar *codestart;  const uschar *codestart;
# Line 6579  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 6654  while (ptr[skipatstart] == CHAR_LEFT_PAR
6654    
6655    if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_UTF8_RIGHTPAR, 5) == 0)
6656      { skipatstart += 7; options |= PCRE_UTF8; continue; }      { skipatstart += 7; options |= PCRE_UTF8; continue; }
6657      else if (strncmp((char *)(ptr+skipatstart+2), STRING_UCP_RIGHTPAR, 4) == 0)
6658        { skipatstart += 6; options |= PCRE_UCP; continue; }
6659    
6660    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)    if (strncmp((char *)(ptr+skipatstart+2), STRING_CR_RIGHTPAR, 3) == 0)
6661      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }      { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
# Line 6602  while (ptr[skipatstart] == CHAR_LEFT_PAR Line 6679  while (ptr[skipatstart] == CHAR_LEFT_PAR
6679      options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr;      options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr;
6680    else break;    else break;
6681    }    }
6682    
6683    utf8 = (options & PCRE_UTF8) != 0;
6684    
6685  /* Can't support UTF8 unless PCRE has been compiled to include the code. */  /* Can't support UTF8 unless PCRE has been compiled to include the code. */
6686    
# Line 6620  if (utf8) Line 6699  if (utf8)
6699    }    }
6700  #endif  #endif
6701    
6702    /* Can't support UCP unless PCRE has been compiled to include the code. */
6703    
6704    #ifndef SUPPORT_UCP
6705    if ((options & PCRE_UCP) != 0)
6706      {
6707      errorcode = ERR67;
6708      goto PCRE_EARLY_ERROR_RETURN;
6709      }
6710    #endif
6711    
6712  /* Check validity of \R options. */  /* Check validity of \R options. */
6713    
6714  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))  switch (options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))

Legend:
Removed from v.514  
changed lines
  Added in v.518

  ViewVC Help
Powered by ViewVC 1.1.5