regex2.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*-
  2. * This code is derived from OpenBSD's libc/regex, original license follows:
  3. *
  4. * Copyright (c) 1992, 1993, 1994 Henry Spencer.
  5. * Copyright (c) 1992, 1993, 1994
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Henry Spencer.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)regex2.h 8.4 (Berkeley) 3/20/94
  36. */
  37. #ifndef LLVM_SUPPORT_REGEX2_H
  38. #define LLVM_SUPPORT_REGEX2_H
  39. #include "regutils.h"
  40. #include <stddef.h>
  41. /*
  42. * internals of regex_t
  43. */
  44. #define MAGIC1 ((('r'^0200)<<8) | 'e')
  45. /*
  46. * The internal representation is a *strip*, a sequence of
  47. * operators ending with an endmarker. (Some terminology etc. is a
  48. * historical relic of earlier versions which used multiple strips.)
  49. * Certain oddities in the representation are there to permit running
  50. * the machinery backwards; in particular, any deviation from sequential
  51. * flow must be marked at both its source and its destination. Some
  52. * fine points:
  53. *
  54. * - OPLUS_ and O_PLUS are *inside* the loop they create.
  55. * - OQUEST_ and O_QUEST are *outside* the bypass they create.
  56. * - OCH_ and O_CH are *outside* the multi-way branch they create, while
  57. * OOR1 and OOR2 are respectively the end and the beginning of one of
  58. * the branches. Note that there is an implicit OOR2 following OCH_
  59. * and an implicit OOR1 preceding O_CH.
  60. *
  61. * In state representations, an operator's bit is on to signify a state
  62. * immediately *preceding* "execution" of that operator.
  63. */
  64. typedef unsigned long sop; /* strip operator */
  65. typedef long sopno;
  66. #define OPRMASK 0xf8000000LU
  67. #define OPDMASK 0x07ffffffLU
  68. #define OPSHIFT ((unsigned)27)
  69. #define OP(n) ((n)&OPRMASK)
  70. #define OPND(n) ((n)&OPDMASK)
  71. #define SOP(op, opnd) ((op)|(opnd))
  72. /* operators meaning operand */
  73. /* (back, fwd are offsets) */
  74. #define OEND (1LU<<OPSHIFT) /* endmarker - */
  75. #define OCHAR (2LU<<OPSHIFT) /* character unsigned char */
  76. #define OBOL (3LU<<OPSHIFT) /* left anchor - */
  77. #define OEOL (4LU<<OPSHIFT) /* right anchor - */
  78. #define OANY (5LU<<OPSHIFT) /* . - */
  79. #define OANYOF (6LU<<OPSHIFT) /* [...] set number */
  80. #define OBACK_ (7LU<<OPSHIFT) /* begin \d paren number */
  81. #define O_BACK (8LU<<OPSHIFT) /* end \d paren number */
  82. #define OPLUS_ (9LU<<OPSHIFT) /* + prefix fwd to suffix */
  83. #define O_PLUS (10LU<<OPSHIFT) /* + suffix back to prefix */
  84. #define OQUEST_ (11LU<<OPSHIFT) /* ? prefix fwd to suffix */
  85. #define O_QUEST (12LU<<OPSHIFT) /* ? suffix back to prefix */
  86. #define OLPAREN (13LU<<OPSHIFT) /* ( fwd to ) */
  87. #define ORPAREN (14LU<<OPSHIFT) /* ) back to ( */
  88. #define OCH_ (15LU<<OPSHIFT) /* begin choice fwd to OOR2 */
  89. #define OOR1 (16LU<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
  90. #define OOR2 (17LU<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
  91. #define O_CH (18LU<<OPSHIFT) /* end choice back to OOR1 */
  92. #define OBOW (19LU<<OPSHIFT) /* begin word - */
  93. #define OEOW (20LU<<OPSHIFT) /* end word - */
  94. /*
  95. * Structure for [] character-set representation. Character sets are
  96. * done as bit vectors, grouped 8 to a byte vector for compactness.
  97. * The individual set therefore has both a pointer to the byte vector
  98. * and a mask to pick out the relevant bit of each byte. A hash code
  99. * simplifies testing whether two sets could be identical.
  100. *
  101. * This will get trickier for multicharacter collating elements. As
  102. * preliminary hooks for dealing with such things, we also carry along
  103. * a string of multi-character elements, and decide the size of the
  104. * vectors at run time.
  105. */
  106. typedef struct {
  107. uch *ptr; /* -> uch [csetsize] */
  108. uch mask; /* bit within array */
  109. uch hash; /* hash code */
  110. size_t smultis;
  111. char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */
  112. } cset;
  113. /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
  114. #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
  115. #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
  116. #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask)
  117. #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* llvm_regcomp() internal fns */
  118. #define MCsub(p, cs, cp) mcsub(p, cs, cp)
  119. #define MCin(p, cs, cp) mcin(p, cs, cp)
  120. /* stuff for character categories */
  121. typedef unsigned char cat_t;
  122. /*
  123. * main compiled-expression structure
  124. */
  125. struct re_guts {
  126. int magic;
  127. # define MAGIC2 ((('R'^0200)<<8)|'E')
  128. sop *strip; /* malloced area for strip */
  129. int csetsize; /* number of bits in a cset vector */
  130. int ncsets; /* number of csets in use */
  131. cset *sets; /* -> cset [ncsets] */
  132. uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
  133. int cflags; /* copy of llvm_regcomp() cflags argument */
  134. sopno nstates; /* = number of sops */
  135. sopno firststate; /* the initial OEND (normally 0) */
  136. sopno laststate; /* the final OEND */
  137. int iflags; /* internal flags */
  138. # define USEBOL 01 /* used ^ */
  139. # define USEEOL 02 /* used $ */
  140. # define REGEX_BAD 04 /* something wrong */
  141. int nbol; /* number of ^ used */
  142. int neol; /* number of $ used */
  143. int ncategories; /* how many character categories */
  144. cat_t *categories; /* ->catspace[-CHAR_MIN] */
  145. char *must; /* match must contain this string */
  146. int mlen; /* length of must */
  147. size_t nsub; /* copy of re_nsub */
  148. int backrefs; /* does it use back references? */
  149. sopno nplus; /* how deep does it nest +s? */
  150. /* catspace must be last */
  151. cat_t catspace[1]; /* actually [NC] */
  152. };
  153. /* misc utilities */
  154. #define OUT (CHAR_MAX+1) /* a non-character value */
  155. #define ISWORD(c) (isalnum(c&0xff) || (c) == '_')
  156. #endif