/[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 295 by ph10, Mon Dec 31 17:00:24 2007 UTC revision 333 by ph10, Thu Apr 10 19:55:57 2008 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-2008 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 158  static const char verbnames[] = Line 158  static const char verbnames[] =
158    "SKIP\0"    "SKIP\0"
159    "THEN";    "THEN";
160    
161  static verbitem verbs[] = {  static const verbitem verbs[] = {
162    { 6, OP_ACCEPT },    { 6, OP_ACCEPT },
163    { 6, OP_COMMIT },    { 6, OP_COMMIT },
164    { 1, OP_FAIL },    { 1, OP_FAIL },
# Line 168  static verbitem verbs[] = { Line 168  static verbitem verbs[] = {
168    { 4, OP_THEN  }    { 4, OP_THEN  }
169  };  };
170    
171  static int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
172    
173    
174  /* Tables of names of POSIX character classes and their lengths. The names are  /* Tables of names of POSIX character classes and their lengths. The names are
# Line 295  static const char error_texts[] = Line 295  static const char error_texts[] =
295    /* 55 */    /* 55 */
296    "repeating a DEFINE group is not allowed\0"    "repeating a DEFINE group is not allowed\0"
297    "inconsistent NEWLINE options\0"    "inconsistent NEWLINE options\0"
298    "\\g is not followed by a braced name or an optionally braced non-zero number\0"    "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
299    "(?+ or (?- or (?(+ or (?(- must be followed by a non-zero number\0"    "a numbered reference must not be zero\0"
300    "(*VERB) with an argument is not supported\0"    "(*VERB) with an argument is not supported\0"
301    /* 60 */    /* 60 */
302    "(*VERB) not recognized\0"    "(*VERB) not recognized\0"
# Line 531  else Line 531  else
531      *errorcodeptr = ERR37;      *errorcodeptr = ERR37;
532      break;      break;
533    
534      /* \g must be followed by a number, either plain or braced. If positive, it      /* \g must be followed by one of a number of specific things:
535      is an absolute backreference. If negative, it is a relative backreference.  
536      This is a Perl 5.10 feature. Perl 5.10 also supports \g{name} as a      (1) A number, either plain or braced. If positive, it is an absolute
537      reference to a named group. This is part of Perl's movement towards a      backreference. If negative, it is a relative backreference. This is a Perl
538      unified syntax for back references. As this is synonymous with \k{name}, we      5.10 feature.
539      fudge it up by pretending it really was \k. */  
540        (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
541        is part of Perl's movement towards a unified syntax for back references. As
542        this is synonymous with \k{name}, we fudge it up by pretending it really
543        was \k.
544    
545        (3) For Oniguruma compatibility we also support \g followed by a name or a
546        number either in angle brackets or in single quotes. However, these are
547        (possibly recursive) subroutine calls, _not_ backreferences. Just return
548        the -ESC_g code (cf \k). */
549    
550      case 'g':      case 'g':
551        if (ptr[1] == '<' || ptr[1] == '\'')
552          {
553          c = -ESC_g;
554          break;
555          }
556    
557        /* Handle the Perl-compatible cases */
558    
559      if (ptr[1] == '{')      if (ptr[1] == '{')
560        {        {
561        const uschar *p;        const uschar *p;
# Line 565  else Line 582  else
582      while ((digitab[ptr[1]] & ctype_digit) != 0)      while ((digitab[ptr[1]] & ctype_digit) != 0)
583        c = c * 10 + *(++ptr) - '0';        c = c * 10 + *(++ptr) - '0';
584    
585      if (c < 0)      if (c < 0)   /* Integer overflow */
586        {        {
587        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
588        break;        break;
589        }        }
590    
591      if (c == 0 || (braced && *(++ptr) != '}'))      if (braced && *(++ptr) != '}')
592        {        {
593        *errorcodeptr = ERR57;        *errorcodeptr = ERR57;
594        break;        break;
595        }        }
596    
597        if (c == 0)
598          {
599          *errorcodeptr = ERR58;
600          break;
601          }
602    
603      if (negated)      if (negated)
604        {        {
# Line 611  else Line 634  else
634        c -= '0';        c -= '0';
635        while ((digitab[ptr[1]] & ctype_digit) != 0)        while ((digitab[ptr[1]] & ctype_digit) != 0)
636          c = c * 10 + *(++ptr) - '0';          c = c * 10 + *(++ptr) - '0';
637        if (c < 0)        if (c < 0)    /* Integer overflow */
638          {          {
639          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
640          break;          break;
# Line 1738  return TRUE; Line 1761  return TRUE;
1761    
1762  /* This function is called when the sequence "[:" or "[." or "[=" is  /* This function is called when the sequence "[:" or "[." or "[=" is
1763  encountered in a character class. It checks whether this is followed by a  encountered in a character class. It checks whether this is followed by a
1764  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
1765  reach an unescaped ']' without the special preceding character, return FALSE.  reach an unescaped ']' without the special preceding character, return FALSE.
1766    
1767  Originally, this function only recognized a sequence of letters between the  Originally, this function only recognized a sequence of letters between the
1768  terminators, but it seems that Perl recognizes any sequence of characters,  terminators, but it seems that Perl recognizes any sequence of characters,
1769  though of course unknown POSIX names are subsequently rejected. Perl gives an  though of course unknown POSIX names are subsequently rejected. Perl gives an
1770  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
1771  didn't consider this to be a POSIX class. Likewise for [:1234:].  didn't consider this to be a POSIX class. Likewise for [:1234:].
1772    
1773  The problem in trying to be exactly like Perl is in the handling of escapes. We  The problem in trying to be exactly like Perl is in the handling of escapes. We
1774  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
1775  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
1776  below handles the special case of \], but does not try to do any other escape  below handles the special case of \], but does not try to do any other escape
1777  processing. This makes it different from Perl for cases such as [:l\ower:]  processing. This makes it different from Perl for cases such as [:l\ower:]
1778  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
1779  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
1780  I think.  I think.
1781    
1782  Arguments:  Arguments:
# Line 1771  terminator = *(++ptr);   /* compiler war Line 1794  terminator = *(++ptr);   /* compiler war
1794  for (++ptr; *ptr != 0; ptr++)  for (++ptr; *ptr != 0; ptr++)
1795    {    {
1796    if (*ptr == '\\' && ptr[1] == ']') ptr++; else    if (*ptr == '\\' && ptr[1] == ']') ptr++; else
1797      {      {
1798      if (*ptr == ']') return FALSE;      if (*ptr == ']') return FALSE;
1799      if (*ptr == terminator && ptr[1] == ']')      if (*ptr == terminator && ptr[1] == ']')
1800        {        {
1801        *endptr = ptr;        *endptr = ptr;
1802        return TRUE;        return TRUE;
1803        }        }
1804      }      }
1805    }    }
1806  return FALSE;  return FALSE;
1807  }  }
1808    
# Line 2113  if (next >= 0) switch(op_code) Line 2136  if (next >= 0) switch(op_code)
2136    /* For OP_NOT, "item" must be a single-byte character. */    /* For OP_NOT, "item" must be a single-byte character. */
2137    
2138    case OP_NOT:    case OP_NOT:
   if (next < 0) return FALSE;  /* Not a character */  
2139    if (item == next) return TRUE;    if (item == next) return TRUE;
2140    if ((options & PCRE_CASELESS) == 0) return FALSE;    if ((options & PCRE_CASELESS) == 0) return FALSE;
2141  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 2376  uschar classbits[32]; Line 2398  uschar classbits[32];
2398  BOOL class_utf8;  BOOL class_utf8;
2399  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
2400  uschar *class_utf8data;  uschar *class_utf8data;
2401    uschar *class_utf8data_base;
2402  uschar utf8_char[6];  uschar utf8_char[6];
2403  #else  #else
2404  BOOL utf8 = FALSE;  BOOL utf8 = FALSE;
# Line 2687  for (;; ptr++) Line 2710  for (;; ptr++)
2710  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
2711      class_utf8 = FALSE;                       /* No chars >= 256 */      class_utf8 = FALSE;                       /* No chars >= 256 */
2712      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */
2713        class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */
2714  #endif  #endif
2715    
2716      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 2702  for (;; ptr++) Line 2726  for (;; ptr++)
2726          {                           /* Braces are required because the */          {                           /* Braces are required because the */
2727          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
2728          }          }
2729    
2730          /* In the pre-compile phase, accumulate the length of any UTF-8 extra
2731          data and reset the pointer. This is so that very large classes that
2732          contain a zillion UTF-8 characters no longer overwrite the work space
2733          (which is on the stack). */
2734    
2735          if (lengthptr != NULL)
2736            {
2737            *lengthptr += class_utf8data - class_utf8data_base;
2738            class_utf8data = class_utf8data_base;
2739            }
2740    
2741  #endif  #endif
2742    
2743        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 4554  we set the flag only if there is a liter Line 4590  we set the flag only if there is a liter
4590          references (?P=name) and recursion (?P>name), as well as falling          references (?P=name) and recursion (?P>name), as well as falling
4591          through from the Perl recursion syntax (?&name). We also come here from          through from the Perl recursion syntax (?&name). We also come here from
4592          the Perl \k<name> or \k'name' back reference syntax and the \k{name}          the Perl \k<name> or \k'name' back reference syntax and the \k{name}
4593          .NET syntax. */          .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
4594    
4595          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
4596          name = ++ptr;          name = ++ptr;
# Line 4632  we set the flag only if there is a liter Line 4668  we set the flag only if there is a liter
4668          case '5': case '6': case '7': case '8': case '9':   /* subroutine */          case '5': case '6': case '7': case '8': case '9':   /* subroutine */
4669            {            {
4670            const uschar *called;            const uschar *called;
4671              terminator = ')';
4672    
4673              /* Come here from the \g<...> and \g'...' code (Oniguruma
4674              compatibility). However, the syntax has been checked to ensure that
4675              the ... are a (signed) number, so that neither ERR63 nor ERR29 will
4676              be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
4677              ever be taken. */
4678    
4679              HANDLE_NUMERICAL_RECURSION:
4680    
4681            if ((refsign = *ptr) == '+')            if ((refsign = *ptr) == '+')
4682              {              {
# Line 4653  we set the flag only if there is a liter Line 4698  we set the flag only if there is a liter
4698            while((digitab[*ptr] & ctype_digit) != 0)            while((digitab[*ptr] & ctype_digit) != 0)
4699              recno = recno * 10 + *ptr++ - '0';              recno = recno * 10 + *ptr++ - '0';
4700    
4701            if (*ptr != ')')            if (*ptr != terminator)
4702              {              {
4703              *errorcodeptr = ERR29;              *errorcodeptr = ERR29;
4704              goto FAILED;              goto FAILED;
# Line 5049  we set the flag only if there is a liter Line 5094  we set the flag only if there is a liter
5094      back references and those types that consume a character may be repeated.      back references and those types that consume a character may be repeated.
5095      We can test for values between ESC_b and ESC_Z for the latter; this may      We can test for values between ESC_b and ESC_Z for the latter; this may
5096      have to change if any new ones are ever created. */      have to change if any new ones are ever created. */
5097    
5098      case '\\':      case '\\':
5099      tempptr = ptr;      tempptr = ptr;
5100      c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);      c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);
# Line 5076  we set the flag only if there is a liter Line 5121  we set the flag only if there is a liter
5121    
5122        zerofirstbyte = firstbyte;        zerofirstbyte = firstbyte;
5123        zeroreqbyte = reqbyte;        zeroreqbyte = reqbyte;
5124    
5125          /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
5126          is a subroutine call by number (Oniguruma syntax). In fact, the value
5127          -ESC_g is returned only for these cases. So we don't need to check for <
5128          or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is
5129          -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as
5130          that is a synonym). */
5131    
5132          if (-c == ESC_g)
5133            {
5134            const uschar *p;
5135            terminator = (*(++ptr) == '<')? '>' : '\'';
5136    
5137            /* These two statements stop the compiler for warning about possibly
5138            unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
5139            fact, because we actually check for a number below, the paths that
5140            would actually be in error are never taken. */
5141    
5142            skipbytes = 0;
5143            reset_bracount = FALSE;
5144    
5145            /* Test for a name */
5146    
5147            if (ptr[1] != '+' && ptr[1] != '-')
5148              {
5149              BOOL isnumber = TRUE;
5150              for (p = ptr + 1; *p != 0 && *p != terminator; p++)
5151                {
5152                if ((cd->ctypes[*p] & ctype_digit) == 0) isnumber = FALSE;
5153                if ((cd->ctypes[*p] & ctype_word) == 0) break;
5154                }
5155              if (*p != terminator)
5156                {
5157                *errorcodeptr = ERR57;
5158                break;
5159                }
5160              if (isnumber)
5161                {
5162                ptr++;
5163                goto HANDLE_NUMERICAL_RECURSION;
5164                }
5165              is_recurse = TRUE;
5166              goto NAMED_REF_OR_RECURSE;
5167              }
5168    
5169            /* Test a signed number in angle brackets or quotes. */
5170    
5171            p = ptr + 2;
5172            while ((digitab[*p] & ctype_digit) != 0) p++;
5173            if (*p != terminator)
5174              {
5175              *errorcodeptr = ERR57;
5176              break;
5177              }
5178            ptr++;
5179            goto HANDLE_NUMERICAL_RECURSION;
5180            }
5181    
5182        /* \k<name> or \k'name' is a back reference by name (Perl syntax).        /* \k<name> or \k'name' is a back reference by name (Perl syntax).
5183        We also support \k{name} (.NET syntax) */        We also support \k{name} (.NET syntax) */
# Line 5807  to fill in forward references to subpatt Line 5909  to fill in forward references to subpatt
5909    
5910  uschar cworkspace[COMPILE_WORK_SIZE];  uschar cworkspace[COMPILE_WORK_SIZE];
5911    
   
5912  /* Set this early so that early errors get offset 0. */  /* Set this early so that early errors get offset 0. */
5913    
5914  ptr = (const uschar *)pattern;  ptr = (const uschar *)pattern;

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

  ViewVC Help
Powered by ViewVC 1.1.5