Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
11 KiB

  1. /* regcomp.h
  2. */
  3. typedef OP OP_4tree; /* Will be redefined later. */
  4. /*
  5. * The "internal use only" fields in regexp.h are present to pass info from
  6. * compile to execute that permits the execute phase to run lots faster on
  7. * simple cases. They are:
  8. *
  9. * regstart sv that must begin a match; Nullch if none obvious
  10. * reganch is the match anchored (at beginning-of-line only)?
  11. * regmust string (pointer into program) that match must include, or NULL
  12. * [regmust changed to SV* for bminstr()--law]
  13. * regmlen length of regmust string
  14. * [regmlen not used currently]
  15. *
  16. * Regstart and reganch permit very fast decisions on suitable starting points
  17. * for a match, cutting down the work a lot. Regmust permits fast rejection
  18. * of lines that cannot possibly match. The regmust tests are costly enough
  19. * that pregcomp() supplies a regmust only if the r.e. contains something
  20. * potentially expensive (at present, the only such thing detected is * or +
  21. * at the start of the r.e., which can involve a lot of backup). Regmlen is
  22. * supplied because the test in pregexec() needs it and pregcomp() is computing
  23. * it anyway.
  24. * [regmust is now supplied always. The tests that use regmust have a
  25. * heuristic that disables the test if it usually matches.]
  26. *
  27. * [In fact, we now use regmust in many cases to locate where the search
  28. * starts in the string, so if regback is >= 0, the regmust search is never
  29. * wasted effort. The regback variable says how many characters back from
  30. * where regmust matched is the earliest possible start of the match.
  31. * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.]
  32. */
  33. /*
  34. * Structure for regexp "program". This is essentially a linear encoding
  35. * of a nondeterministic finite-state machine (aka syntax charts or
  36. * "railroad normal form" in parsing technology). Each node is an opcode
  37. * plus a "next" pointer, possibly plus an operand. "Next" pointers of
  38. * all nodes except BRANCH implement concatenation; a "next" pointer with
  39. * a BRANCH on both ends of it is connecting two alternatives. (Here we
  40. * have one of the subtle syntax dependencies: an individual BRANCH (as
  41. * opposed to a collection of them) is never concatenated with anything
  42. * because of operator precedence.) The operand of some types of node is
  43. * a literal string; for others, it is a node leading into a sub-FSM. In
  44. * particular, the operand of a BRANCH node is the first node of the branch.
  45. * (NB this is *not* a tree structure: the tail of the branch connects
  46. * to the thing following the set of BRANCHes.) The opcodes are:
  47. */
  48. /*
  49. * A node is one char of opcode followed by two chars of "next" pointer.
  50. * "Next" pointers are stored as two 8-bit pieces, high order first. The
  51. * value is a positive offset from the opcode of the node containing it.
  52. * An operand, if any, simply follows the node. (Note that much of the
  53. * code generation knows about this implicit relationship.)
  54. *
  55. * Using two bytes for the "next" pointer is vast overkill for most things,
  56. * but allows patterns to get big without disasters.
  57. *
  58. * [The "next" pointer is always aligned on an even
  59. * boundary, and reads the offset directly as a short. Also, there is no
  60. * special test to reverse the sign of BACK pointers since the offset is
  61. * stored negative.]
  62. */
  63. struct regnode_string {
  64. U8 str_len;
  65. U8 type;
  66. U16 next_off;
  67. char string[1];
  68. };
  69. struct regnode_1 {
  70. U8 flags;
  71. U8 type;
  72. U16 next_off;
  73. U32 arg1;
  74. };
  75. struct regnode_2 {
  76. U8 flags;
  77. U8 type;
  78. U16 next_off;
  79. U16 arg1;
  80. U16 arg2;
  81. };
  82. #define ANYOF_BITMAP_SIZE 32 /* 256 b/(8 b/B) */
  83. #define ANYOF_CLASSBITMAP_SIZE 4
  84. struct regnode_charclass {
  85. U8 flags;
  86. U8 type;
  87. U16 next_off;
  88. char bitmap[ANYOF_BITMAP_SIZE];
  89. };
  90. struct regnode_charclass_class {
  91. U8 flags;
  92. U8 type;
  93. U16 next_off;
  94. char bitmap[ANYOF_BITMAP_SIZE];
  95. char classflags[ANYOF_CLASSBITMAP_SIZE];
  96. };
  97. /* XXX fix this description.
  98. Impose a limit of REG_INFTY on various pattern matching operations
  99. to limit stack growth and to avoid "infinite" recursions.
  100. */
  101. /* The default size for REG_INFTY is I16_MAX, which is the same as
  102. SHORT_MAX (see perl.h). Unfortunately I16 isn't necessarily 16 bits
  103. (see handy.h). On the Cray C90, sizeof(short)==4 and hence I16_MAX is
  104. ((1<<31)-1), while on the Cray T90, sizeof(short)==8 and I16_MAX is
  105. ((1<<63)-1). To limit stack growth to reasonable sizes, supply a
  106. smaller default.
  107. --Andy Dougherty 11 June 1998
  108. */
  109. #if SHORTSIZE > 2
  110. # ifndef REG_INFTY
  111. # define REG_INFTY ((1<<15)-1)
  112. # endif
  113. #endif
  114. #ifndef REG_INFTY
  115. # define REG_INFTY I16_MAX
  116. #endif
  117. #define ARG_VALUE(arg) (arg)
  118. #define ARG__SET(arg,val) ((arg) = (val))
  119. #define ARG(p) ARG_VALUE(ARG_LOC(p))
  120. #define ARG1(p) ARG_VALUE(ARG1_LOC(p))
  121. #define ARG2(p) ARG_VALUE(ARG2_LOC(p))
  122. #define ARG_SET(p, val) ARG__SET(ARG_LOC(p), (val))
  123. #define ARG1_SET(p, val) ARG__SET(ARG1_LOC(p), (val))
  124. #define ARG2_SET(p, val) ARG__SET(ARG2_LOC(p), (val))
  125. #ifndef lint
  126. # define NEXT_OFF(p) ((p)->next_off)
  127. # define NODE_ALIGN(node)
  128. # define NODE_ALIGN_FILL(node) ((node)->flags = 0xde) /* deadbeef */
  129. #else /* lint */
  130. # define NEXT_OFF(p) 0
  131. # define NODE_ALIGN(node)
  132. # define NODE_ALIGN_FILL(node)
  133. #endif /* lint */
  134. #define SIZE_ALIGN NODE_ALIGN
  135. #define OP(p) ((p)->type)
  136. #define OPERAND(p) (((struct regnode_string *)p)->string)
  137. #define MASK(p) ((char*)OPERAND(p))
  138. #define STR_LEN(p) (((struct regnode_string *)p)->str_len)
  139. #define STRING(p) (((struct regnode_string *)p)->string)
  140. #define STR_SZ(l) ((l + sizeof(regnode) - 1) / sizeof(regnode))
  141. #define NODE_SZ_STR(p) (STR_SZ(STR_LEN(p))+1)
  142. #define NODE_ALIGN(node)
  143. #define ARG_LOC(p) (((struct regnode_1 *)p)->arg1)
  144. #define ARG1_LOC(p) (((struct regnode_2 *)p)->arg1)
  145. #define ARG2_LOC(p) (((struct regnode_2 *)p)->arg2)
  146. #define NODE_STEP_REGNODE 1 /* sizeof(regnode)/sizeof(regnode) */
  147. #define EXTRA_STEP_2ARGS EXTRA_SIZE(struct regnode_2)
  148. #define NODE_STEP_B 4
  149. #define NEXTOPER(p) ((p) + NODE_STEP_REGNODE)
  150. #define PREVOPER(p) ((p) - NODE_STEP_REGNODE)
  151. #define FILL_ADVANCE_NODE(ptr, op) STMT_START { \
  152. (ptr)->type = op; (ptr)->next_off = 0; (ptr)++; } STMT_END
  153. #define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \
  154. ARG_SET(ptr, arg); FILL_ADVANCE_NODE(ptr, op); (ptr) += 1; } STMT_END
  155. #define REG_MAGIC 0234
  156. #define SIZE_ONLY (PL_regcode == &PL_regdummy)
  157. /* Flags for node->flags of ANYOF */
  158. #define ANYOF_CLASS 0x08
  159. #define ANYOF_INVERT 0x04
  160. #define ANYOF_FOLD 0x02
  161. #define ANYOF_LOCALE 0x01
  162. /* Used for regstclass only */
  163. #define ANYOF_EOS 0x10 /* Can match an empty string too */
  164. /* Character classes for node->classflags of ANYOF */
  165. /* Should be synchronized with a table in regprop() */
  166. /* 2n should pair with 2n+1 */
  167. #define ANYOF_ALNUM 0 /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
  168. #define ANYOF_NALNUM 1
  169. #define ANYOF_SPACE 2 /* \s */
  170. #define ANYOF_NSPACE 3
  171. #define ANYOF_DIGIT 4
  172. #define ANYOF_NDIGIT 5
  173. #define ANYOF_ALNUMC 6 /* isalnum(3), utf8::IsAlnum, ALNUMC */
  174. #define ANYOF_NALNUMC 7
  175. #define ANYOF_ALPHA 8
  176. #define ANYOF_NALPHA 9
  177. #define ANYOF_ASCII 10
  178. #define ANYOF_NASCII 11
  179. #define ANYOF_CNTRL 12
  180. #define ANYOF_NCNTRL 13
  181. #define ANYOF_GRAPH 14
  182. #define ANYOF_NGRAPH 15
  183. #define ANYOF_LOWER 16
  184. #define ANYOF_NLOWER 17
  185. #define ANYOF_PRINT 18
  186. #define ANYOF_NPRINT 19
  187. #define ANYOF_PUNCT 20
  188. #define ANYOF_NPUNCT 21
  189. #define ANYOF_UPPER 22
  190. #define ANYOF_NUPPER 23
  191. #define ANYOF_XDIGIT 24
  192. #define ANYOF_NXDIGIT 25
  193. #define ANYOF_PSXSPC 26 /* POSIX space: \s plus the vertical tab */
  194. #define ANYOF_NPSXSPC 27
  195. #define ANYOF_BLANK 28 /* GNU extension: space and tab */
  196. #define ANYOF_NBLANK 29
  197. #define ANYOF_MAX 32
  198. /* Backward source code compatibility. */
  199. #define ANYOF_ALNUML ANYOF_ALNUM
  200. #define ANYOF_NALNUML ANYOF_NALNUM
  201. #define ANYOF_SPACEL ANYOF_SPACE
  202. #define ANYOF_NSPACEL ANYOF_NSPACE
  203. /* Utility macros for the bitmap and classes of ANYOF */
  204. #define ANYOF_SIZE (sizeof(struct regnode_charclass))
  205. #define ANYOF_CLASS_SIZE (sizeof(struct regnode_charclass_class))
  206. #define ANYOF_FLAGS(p) ((p)->flags)
  207. #define ANYOF_FLAGS_ALL 0xff
  208. #define ANYOF_BIT(c) (1 << ((c) & 7))
  209. #define ANYOF_CLASS_BYTE(p, c) (((struct regnode_charclass_class*)(p))->classflags[((c) >> 3) & 3])
  210. #define ANYOF_CLASS_SET(p, c) (ANYOF_CLASS_BYTE(p, c) |= ANYOF_BIT(c))
  211. #define ANYOF_CLASS_CLEAR(p, c) (ANYOF_CLASS_BYTE(p, c) &= ~ANYOF_BIT(c))
  212. #define ANYOF_CLASS_TEST(p, c) (ANYOF_CLASS_BYTE(p, c) & ANYOF_BIT(c))
  213. #define ANYOF_CLASS_ZERO(ret) Zero(((struct regnode_charclass_class*)(ret))->classflags, ANYOF_CLASSBITMAP_SIZE, char)
  214. #define ANYOF_BITMAP_ZERO(ret) Zero(((struct regnode_charclass*)(ret))->bitmap, ANYOF_BITMAP_SIZE, char)
  215. #define ANYOF_BITMAP(p) (((struct regnode_charclass*)(p))->bitmap)
  216. #define ANYOF_BITMAP_BYTE(p, c) (ANYOF_BITMAP(p)[((c) >> 3) & 31])
  217. #define ANYOF_BITMAP_SET(p, c) (ANYOF_BITMAP_BYTE(p, c) |= ANYOF_BIT(c))
  218. #define ANYOF_BITMAP_CLEAR(p,c) (ANYOF_BITMAP_BYTE(p, c) &= ~ANYOF_BIT(c))
  219. #define ANYOF_BITMAP_TEST(p, c) (ANYOF_BITMAP_BYTE(p, c) & ANYOF_BIT(c))
  220. #define ANYOF_SKIP ((ANYOF_SIZE - 1)/sizeof(regnode))
  221. #define ANYOF_CLASS_SKIP ((ANYOF_CLASS_SIZE - 1)/sizeof(regnode))
  222. #define ANYOF_CLASS_ADD_SKIP (ANYOF_CLASS_SKIP - ANYOF_SKIP)
  223. /*
  224. * Utility definitions.
  225. */
  226. #ifndef lint
  227. #ifndef CHARMASK
  228. #define UCHARAT(p) ((int)*(U8*)(p))
  229. #else
  230. #define UCHARAT(p) ((int)*(p)&CHARMASK)
  231. #endif
  232. #else /* lint */
  233. #define UCHARAT(p) PL_regdummy
  234. #endif /* lint */
  235. #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
  236. #define REG_SEEN_ZERO_LEN 1
  237. #define REG_SEEN_LOOKBEHIND 2
  238. #define REG_SEEN_GPOS 4
  239. #define REG_SEEN_EVAL 8
  240. START_EXTERN_C
  241. #include "regnodes.h"
  242. /* The following have no fixed length. U8 so we can do strchr() on it. */
  243. #ifndef DOINIT
  244. EXTCONST U8 PL_varies[];
  245. #else
  246. EXTCONST U8 PL_varies[] = {
  247. BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, REFF, REFFL,
  248. WHILEM, CURLYM, CURLYN, BRANCHJ, IFTHEN, SUSPEND, CLUMP, 0
  249. };
  250. #endif
  251. /* The following always have a length of 1. U8 we can do strchr() on it. */
  252. /* (Note that length 1 means "one character" under UTF8, not "one octet".) */
  253. #ifndef DOINIT
  254. EXTCONST U8 PL_simple[];
  255. #else
  256. EXTCONST U8 PL_simple[] = {
  257. REG_ANY, ANYUTF8, SANY, SANYUTF8, ANYOF, ANYOFUTF8,
  258. ALNUM, ALNUMUTF8, ALNUML, ALNUMLUTF8,
  259. NALNUM, NALNUMUTF8, NALNUML, NALNUMLUTF8,
  260. SPACE, SPACEUTF8, SPACEL, SPACELUTF8,
  261. NSPACE, NSPACEUTF8, NSPACEL, NSPACELUTF8,
  262. DIGIT, DIGITUTF8, NDIGIT, NDIGITUTF8, 0
  263. };
  264. #endif
  265. END_EXTERN_C
  266. typedef struct re_scream_pos_data_s
  267. {
  268. char **scream_olds; /* match pos */
  269. I32 *scream_pos; /* Internal iterator of scream. */
  270. } re_scream_pos_data;
  271. struct reg_data {
  272. U32 count;
  273. U8 *what;
  274. void* data[1];
  275. };
  276. struct reg_substr_datum {
  277. I32 min_offset;
  278. I32 max_offset;
  279. SV *substr;
  280. };
  281. struct reg_substr_data {
  282. struct reg_substr_datum data[3]; /* Actual array */
  283. };
  284. #define anchored_substr substrs->data[0].substr
  285. #define anchored_offset substrs->data[0].min_offset
  286. #define float_substr substrs->data[1].substr
  287. #define float_min_offset substrs->data[1].min_offset
  288. #define float_max_offset substrs->data[1].max_offset
  289. #define check_substr substrs->data[2].substr
  290. #define check_offset_min substrs->data[2].min_offset
  291. #define check_offset_max substrs->data[2].max_offset