/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Diff of /code/trunk/pcre_compile.c

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

revision 333 by ph10, Thu Apr 10 19:55:57 2008 UTC revision 341 by ph10, Sat Apr 19 16:41:04 2008 UTC
# Line 302  static const char error_texts[] = Line 302  static const char error_texts[] =
302    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
303    "number is too big\0"    "number is too big\0"
304    "subpattern name expected\0"    "subpattern name expected\0"
305    "digit expected after (?+";    "digit expected after (?+\0"
306      "] is an invalid data character in JavaScript compatibility mode";
307    
308    
309  /* 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 975  be terminated by '>' because that is che Line 976  be terminated by '>' because that is che
976    
977  Arguments:  Arguments:
978    ptr          current position in the pattern    ptr          current position in the pattern
979    count        current count of capturing parens so far encountered    cd           compile background data
980    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
981    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
982    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
# Line 984  Returns:       the number of the named s Line 985  Returns:       the number of the named s
985  */  */
986    
987  static int  static int
988  find_parens(const uschar *ptr, int count, const uschar *name, int lorn,  find_parens(const uschar *ptr, compile_data *cd, const uschar *name, int lorn,
989    BOOL xmode)    BOOL xmode)
990  {  {
991  const uschar *thisname;  const uschar *thisname;
992    int count = cd->bracount;
993    
994  for (; *ptr != 0; ptr++)  for (; *ptr != 0; ptr++)
995    {    {
# Line 1007  for (; *ptr != 0; ptr++) Line 1009  for (; *ptr != 0; ptr++)
1009      continue;      continue;
1010      }      }
1011    
1012    /* Skip over character classes */    /* Skip over character classes; this logic must be similar to the way they
1013      are handled for real. If the first character is '^', skip it. Also, if the
1014      first few characters (either before or after ^) are \Q\E or \E we skip them
1015      too. This makes for compatibility with Perl. */
1016    
1017    if (*ptr == '[')    if (*ptr == '[')
1018      {      {
1019        BOOL negate_class = FALSE;
1020        for (;;)
1021          {
1022          int c = *(++ptr);
1023          if (c == '\\')
1024            {
1025            if (ptr[1] == 'E') ptr++;
1026              else if (strncmp((const char *)ptr+1, "Q\\E", 3) == 0) ptr += 3;
1027                else break;
1028            }
1029          else if (!negate_class && c == '^')
1030            negate_class = TRUE;
1031          else break;
1032          }
1033    
1034        /* If the next character is ']', it is a data character that must be
1035        skipped, except in JavaScript compatibility mode. */
1036    
1037        if (ptr[1] == ']' && (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)
1038          ptr++;
1039    
1040      while (*(++ptr) != ']')      while (*(++ptr) != ']')
1041        {        {
1042        if (*ptr == 0) return -1;        if (*ptr == 0) return -1;
# Line 1567  for (code = first_significant_code(code Line 1593  for (code = first_significant_code(code
1593    
1594    /* Groups with zero repeats can of course be empty; skip them. */    /* Groups with zero repeats can of course be empty; skip them. */
1595    
1596    if (c == OP_BRAZERO || c == OP_BRAMINZERO)    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO)
1597      {      {
1598      code += _pcre_OP_lengths[c];      code += _pcre_OP_lengths[c];
1599      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
# Line 1847  return -1; Line 1873  return -1;
1873  that is referenced. This means that groups can be replicated for fixed  that is referenced. This means that groups can be replicated for fixed
1874  repetition simply by copying (because the recursion is allowed to refer to  repetition simply by copying (because the recursion is allowed to refer to
1875  earlier groups that are outside the current group). However, when a group is  earlier groups that are outside the current group). However, when a group is
1876  optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before  optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
1877  it, after it has been compiled. This means that any OP_RECURSE items within it  inserted before it, after it has been compiled. This means that any OP_RECURSE
1878  that refer to the group itself or any contained groups have to have their  items within it that refer to the group itself or any contained groups have to
1879  offsets adjusted. That one of the jobs of this function. Before it is called,  have their offsets adjusted. That one of the jobs of this function. Before it
1880  the partially compiled regex must be temporarily terminated with OP_END.  is called, the partially compiled regex must be temporarily terminated with
1881    OP_END.
1882    
1883  This function has been extended with the possibility of forward references for  This function has been extended with the possibility of forward references for
1884  recursions and subroutine calls. It must also check the list of such references  recursions and subroutine calls. It must also check the list of such references
# Line 1882  while ((ptr = (uschar *)find_recurse(ptr Line 1909  while ((ptr = (uschar *)find_recurse(ptr
1909    
1910    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
1911    reference. */    reference. */
1912    
1913    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
1914      {      {
1915      offset = GET(hc, 0);      offset = GET(hc, 0);
# Line 2458  for (;; ptr++) Line 2485  for (;; ptr++)
2485    /* Get next byte in the pattern */    /* Get next byte in the pattern */
2486    
2487    c = *ptr;    c = *ptr;
2488    
2489    /* 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
2490    previous cycle of this loop. */    previous cycle of this loop. */
2491    
# Line 2653  for (;; ptr++) Line 2680  for (;; ptr++)
2680      opcode is compiled. It may optionally have a bit map for characters < 256,      opcode is compiled. It may optionally have a bit map for characters < 256,
2681      but those above are are explicitly listed afterwards. A flag byte tells      but those above are are explicitly listed afterwards. A flag byte tells
2682      whether the bitmap is present, and whether this is a negated class or not.      whether the bitmap is present, and whether this is a negated class or not.
2683      */  
2684        In JavaScript compatibility mode, an isolated ']' causes an error. In
2685        default (Perl) mode, it is treated as a data character. */
2686    
2687        case ']':
2688        if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
2689          {
2690          *errorcodeptr = ERR64;
2691          goto FAILED;
2692          }
2693        goto NORMAL_CHAR;
2694    
2695      case '[':      case '[':
2696      previous = code;      previous = code;
# Line 2686  for (;; ptr++) Line 2723  for (;; ptr++)
2723          negate_class = TRUE;          negate_class = TRUE;
2724        else break;        else break;
2725        }        }
2726    
2727        /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
2728        an initial ']' is taken as a data character -- the code below handles
2729        that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
2730        [^] must match any character, so generate OP_ALLANY. */
2731    
2732        if (c ==']' && (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
2733          {
2734          *code++ = negate_class? OP_ALLANY : OP_FAIL;
2735          if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
2736          zerofirstbyte = firstbyte;
2737          break;
2738          }
2739    
2740      /* If a class contains a negative special such as \S, we need to flip the      /* If a class contains a negative special such as \S, we need to flip the
2741      negation flag at the end, so that support for characters > 255 works      negation flag at the end, so that support for characters > 255 works
# Line 3842  we set the flag only if there is a liter Line 3892  we set the flag only if there is a liter
3892    
3893        if (repeat_min == 0)        if (repeat_min == 0)
3894          {          {
3895          /* If the maximum is also zero, we just omit the group from the output          /* If the maximum is also zero, we used to just omit the group from the
3896          altogether. */          output altogether, like this:
3897    
3898          if (repeat_max == 0)          ** if (repeat_max == 0)
3899            {          **   {
3900            code = previous;          **   code = previous;
3901            goto END_REPEAT;          **   goto END_REPEAT;
3902            }          **   }
3903    
3904          /* If the maximum is 1 or unlimited, we just have to stick in the          However, that fails when a group is referenced as a subroutine from
3905          BRAZERO and do no more at this point. However, we do need to adjust          elsewhere in the pattern, so now we stick in OP_SKIPZERO in front of it
3906          any OP_RECURSE calls inside the group that refer to the group itself or          so that it is skipped on execution. As we don't have a list of which
3907          any internal or forward referenced group, because the offset is from          groups are referenced, we cannot do this selectively.
3908          the start of the whole regex. Temporarily terminate the pattern while  
3909          doing this. */          If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
3910            and do no more at this point. However, we do need to adjust any
3911            OP_RECURSE calls inside the group that refer to the group itself or any
3912            internal or forward referenced group, because the offset is from the
3913            start of the whole regex. Temporarily terminate the pattern while doing
3914            this. */
3915    
3916          if (repeat_max <= 1)          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
3917            {            {
3918            *code = OP_END;            *code = OP_END;
3919            adjust_recurse(previous, 1, utf8, cd, save_hwm);            adjust_recurse(previous, 1, utf8, cd, save_hwm);
3920            memmove(previous+1, previous, len);            memmove(previous+1, previous, len);
3921            code++;            code++;
3922              if (repeat_max == 0)
3923                {
3924                *previous++ = OP_SKIPZERO;
3925                goto END_REPEAT;
3926                }
3927            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
3928            }            }
3929    
# Line 4057  we set the flag only if there is a liter Line 4117  we set the flag only if there is a liter
4117            }            }
4118          }          }
4119        }        }
4120    
4121        /* If previous is OP_FAIL, it was generated by an empty class [] in
4122        JavaScript mode. The other ways in which OP_FAIL can be generated, that is
4123        by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
4124        error above. We can just ignore the repeat in JS case. */
4125    
4126        else if (*previous == OP_FAIL) goto END_REPEAT;
4127    
4128      /* Else there's some kind of shambles */      /* Else there's some kind of shambles */
4129    
# Line 4138  we set the flag only if there is a liter Line 4205  we set the flag only if there is a liter
4205      bravalue = OP_CBRA;      bravalue = OP_CBRA;
4206      save_hwm = cd->hwm;      save_hwm = cd->hwm;
4207      reset_bracount = FALSE;      reset_bracount = FALSE;
4208    
4209      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
4210    
4211      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)      if (*(++ptr) == '*' && (cd->ctypes[ptr[1]] & ctype_letter) != 0)
# Line 4344  we set the flag only if there is a liter Line 4411  we set the flag only if there is a liter
4411    
4412          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
4413    
4414          else if ((i = find_parens(ptr, cd->bracount, name, namelen,          else if ((i = find_parens(ptr, cd, name, namelen,
4415                          (options & PCRE_EXTENDED) != 0)) > 0)                          (options & PCRE_EXTENDED) != 0)) > 0)
4416            {            {
4417            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
# Line 4641  we set the flag only if there is a liter Line 4708  we set the flag only if there is a liter
4708              recno = GET2(slot, 0);              recno = GET2(slot, 0);
4709              }              }
4710            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
4711                      find_parens(ptr, cd->bracount, name, namelen,                      find_parens(ptr, cd, name, namelen,
4712                        (options & PCRE_EXTENDED) != 0)) <= 0)                        (options & PCRE_EXTENDED) != 0)) <= 0)
4713              {              {
4714              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
# Line 4751  we set the flag only if there is a liter Line 4818  we set the flag only if there is a liter
4818    
4819              if (called == NULL)              if (called == NULL)
4820                {                {
4821                if (find_parens(ptr, cd->bracount, NULL, recno,                if (find_parens(ptr, cd, NULL, recno,
4822                     (options & PCRE_EXTENDED) != 0) < 0)                      (options & PCRE_EXTENDED) != 0) < 0)
4823                  {                  {
4824                  *errorcodeptr = ERR15;                  *errorcodeptr = ERR15;
4825                  goto FAILED;                  goto FAILED;
# Line 5127  we set the flag only if there is a liter Line 5194  we set the flag only if there is a liter
5194        -ESC_g is returned only for these cases. So we don't need to check for <        -ESC_g is returned only for these cases. So we don't need to check for <
5195        or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is        or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is
5196        -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as        -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as
5197        that is a synonym). */        that is a synonym for a named back reference). */
5198    
5199        if (-c == ESC_g)        if (-c == ESC_g)
5200          {          {
5201          const uschar *p;          const uschar *p;
5202            save_hwm = cd->hwm;   /* Normally this is set when '(' is read */
5203          terminator = (*(++ptr) == '<')? '>' : '\'';          terminator = (*(++ptr) == '<')? '>' : '\'';
5204    
5205          /* These two statements stop the compiler for warning about possibly          /* These two statements stop the compiler for warning about possibly
# Line 6197  while (errorcode == 0 && cd->hwm > cwork Line 6265  while (errorcode == 0 && cd->hwm > cwork
6265    if (groupptr == NULL) errorcode = ERR53;    if (groupptr == NULL) errorcode = ERR53;
6266      else PUT(((uschar *)codestart), offset, groupptr - codestart);      else PUT(((uschar *)codestart), offset, groupptr - codestart);
6267    }    }
6268    
6269  /* Give an error if there's back reference to a non-existent capturing  /* Give an error if there's back reference to a non-existent capturing
6270  subpattern. */  subpattern. */
6271    

Legend:
Removed from v.333  
changed lines
  Added in v.341

  ViewVC Help
Powered by ViewVC 1.1.5