gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

483 行
17KB

  1. /*
  2. * Internal interface definitions, etc., for the reg package
  3. *
  4. * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
  5. *
  6. * Development of this software was funded, in part, by Cray Research Inc.,
  7. * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
  8. * Corporation, none of whom are responsible for the results. The author
  9. * thanks all of them.
  10. *
  11. * Redistribution and use in source and binary forms -- with or without
  12. * modification -- are permitted for any purpose, provided that
  13. * redistributions in source form retain this entire copyright notice and
  14. * indicate the origin and nature of any modifications.
  15. *
  16. * I'd appreciate being given credit for this package in the documentation
  17. * of software which uses it, but that is not a requirement.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  21. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  22. * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * src/include/regex/regguts.h
  31. */
  32. /*
  33. * Environmental customization. It should not (I hope) be necessary to
  34. * alter the file you are now reading -- regcustom.h should handle it all,
  35. * given care here and elsewhere.
  36. */
  37. #include "regcustom.h"
  38. /*
  39. * Things that regcustom.h might override.
  40. */
  41. /* assertions */
  42. #ifndef assert
  43. #ifndef REG_DEBUG
  44. #define NDEBUG /* no assertions */
  45. #endif
  46. #include <assert.h>
  47. #endif
  48. /* voids */
  49. #ifndef DISCARD
  50. #define DISCARD void /* for throwing values away */
  51. #endif
  52. #ifndef VS
  53. #define VS(x) ((void *)(x)) /* cast something to generic ptr */
  54. #endif
  55. /* function-pointer declarator */
  56. #ifndef FUNCPTR
  57. #define FUNCPTR(name, args) (*(name)) args
  58. #endif
  59. /* memory allocation */
  60. #ifndef MALLOC
  61. #define MALLOC(n) malloc(n)
  62. #endif
  63. #ifndef REALLOC
  64. #define REALLOC(p, n) realloc(VS(p), n)
  65. #endif
  66. #ifndef FREE
  67. #define FREE(p) free(VS(p))
  68. #endif
  69. /* want size of a char in bits, and max value in bounded quantifiers */
  70. #ifndef _POSIX2_RE_DUP_MAX
  71. #define _POSIX2_RE_DUP_MAX 255 /* normally from <limits.h> */
  72. #endif
  73. /*
  74. * misc
  75. */
  76. #define NOTREACHED 0
  77. #define DUPMAX _POSIX2_RE_DUP_MAX
  78. #define DUPINF (DUPMAX+1)
  79. #define REMAGIC 0xfed7 /* magic number for main struct */
  80. /* Type codes for lookaround constraints */
  81. #define LATYPE_AHEAD_POS 03 /* positive lookahead */
  82. #define LATYPE_AHEAD_NEG 02 /* negative lookahead */
  83. #define LATYPE_BEHIND_POS 01 /* positive lookbehind */
  84. #define LATYPE_BEHIND_NEG 00 /* negative lookbehind */
  85. #define LATYPE_IS_POS(la) ((la) & 01)
  86. #define LATYPE_IS_AHEAD(la) ((la) & 02)
  87. /*
  88. * debugging facilities
  89. */
  90. #ifdef REG_DEBUG
  91. /* FDEBUG does finite-state tracing */
  92. #define FDEBUG(arglist) { if (v->eflags&REG_FTRACE) printf arglist; }
  93. /* MDEBUG does higher-level tracing */
  94. #define MDEBUG(arglist) { if (v->eflags&REG_MTRACE) printf arglist; }
  95. #else
  96. #define FDEBUG(arglist) {}
  97. #define MDEBUG(arglist) {}
  98. #endif
  99. /*
  100. * bitmap manipulation
  101. */
  102. #define UBITS (CHAR_BIT * sizeof(unsigned))
  103. #define BSET(uv, sn) ((uv)[(sn)/UBITS] |= (unsigned)1 << ((sn)%UBITS))
  104. #define ISBSET(uv, sn) ((uv)[(sn)/UBITS] & ((unsigned)1 << ((sn)%UBITS)))
  105. /*
  106. * As soon as possible, we map chrs into equivalence classes -- "colors" --
  107. * which are of much more manageable number.
  108. */
  109. typedef short color; /* colors of characters */
  110. #define MAX_COLOR 32767 /* max color (must fit in 'color' datatype) */
  111. #define COLORLESS (-1) /* impossible color */
  112. #define WHITE 0 /* default color, parent of all others */
  113. /* Note: various places in the code know that WHITE is zero */
  114. /*
  115. * Per-color data structure for the compile-time color machinery
  116. *
  117. * If "sub" is not NOSUB then it is the number of the color's current
  118. * subcolor, i.e. we are in process of dividing this color (character
  119. * equivalence class) into two colors. See src/backend/regex/README for
  120. * discussion of subcolors.
  121. *
  122. * Currently-unused colors have the FREECOL bit set and are linked into a
  123. * freelist using their "sub" fields, but only if their color numbers are
  124. * less than colormap.max. Any array entries beyond "max" are just garbage.
  125. */
  126. struct colordesc
  127. {
  128. int nschrs; /* number of simple chars of this color */
  129. int nuchrs; /* number of upper map entries of this color */
  130. color sub; /* open subcolor, if any; or free-chain ptr */
  131. #define NOSUB COLORLESS /* value of "sub" when no open subcolor */
  132. struct arc *arcs; /* chain of all arcs of this color */
  133. chr firstchr; /* simple char first assigned to this color */
  134. int flags; /* bit values defined next */
  135. #define FREECOL 01 /* currently free */
  136. #define PSEUDO 02 /* pseudocolor, no real chars */
  137. #define UNUSEDCOLOR(cd) ((cd)->flags & FREECOL)
  138. };
  139. /*
  140. * The color map itself
  141. *
  142. * This struct holds both data used only at compile time, and the chr to
  143. * color mapping information, used at both compile and run time. The latter
  144. * is the bulk of the space, so it's not really worth separating out the
  145. * compile-only portion.
  146. *
  147. * Ideally, the mapping data would just be an array of colors indexed by
  148. * chr codes; but for large character sets that's impractical. Fortunately,
  149. * common characters have smaller codes, so we can use a simple array for chr
  150. * codes up to MAX_SIMPLE_CHR, and do something more complex for codes above
  151. * that, without much loss of performance. The "something more complex" is a
  152. * 2-D array of color entries, where row indexes correspond to individual chrs
  153. * or chr ranges that have been mentioned in the regex (with row zero
  154. * representing all other chrs), and column indexes correspond to different
  155. * sets of locale-dependent character classes such as "isalpha". The
  156. * classbits[k] entry is zero if we do not care about the k'th character class
  157. * in this regex, and otherwise it is the bit to be OR'd into the column index
  158. * if the character in question is a member of that class. We find the color
  159. * of a high-valued chr by identifying which colormaprange it is in to get
  160. * the row index (use row zero if it's in none of them), identifying which of
  161. * the interesting cclasses it's in to get the column index, and then indexing
  162. * into the 2-D hicolormap array.
  163. *
  164. * The colormapranges are required to be nonempty, nonoverlapping, and to
  165. * appear in increasing chr-value order.
  166. */
  167. #define NUM_CCLASSES 13 /* must match data in regc_locale.c */
  168. typedef struct colormaprange
  169. {
  170. chr cmin; /* range represents cmin..cmax inclusive */
  171. chr cmax;
  172. int rownum; /* row index in hicolormap array (>= 1) */
  173. } colormaprange;
  174. struct colormap
  175. {
  176. int magic;
  177. #define CMMAGIC 0x876
  178. struct vars *v; /* for compile error reporting */
  179. size_t ncds; /* allocated length of colordescs array */
  180. size_t max; /* highest color number currently in use */
  181. color free; /* beginning of free chain (if non-0) */
  182. struct colordesc *cd; /* pointer to array of colordescs */
  183. #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
  184. /* mapping data for chrs <= MAX_SIMPLE_CHR: */
  185. color *locolormap; /* simple array indexed by chr code */
  186. /* mapping data for chrs > MAX_SIMPLE_CHR: */
  187. int classbits[NUM_CCLASSES]; /* see comment above */
  188. int numcmranges; /* number of colormapranges */
  189. colormaprange *cmranges; /* ranges of high chrs */
  190. color *hicolormap; /* 2-D array of color entries */
  191. int maxarrayrows; /* number of array rows allocated */
  192. int hiarrayrows; /* number of array rows in use */
  193. int hiarraycols; /* number of array columns (2^N) */
  194. /* If we need up to NINLINECDS, we store them here to save a malloc */
  195. #define NINLINECDS ((size_t) 10)
  196. struct colordesc cdspace[NINLINECDS];
  197. };
  198. /* fetch color for chr; beware of multiple evaluation of c argument */
  199. #define GETCOLOR(cm, c) \
  200. ((c) <= MAX_SIMPLE_CHR ? (cm)->locolormap[(c) - CHR_MIN] : pg_reg_getcolor(cm, c))
  201. /*
  202. * Interface definitions for locale-interface functions in regc_locale.c.
  203. */
  204. /*
  205. * Representation of a set of characters. chrs[] represents individual
  206. * code points, ranges[] represents ranges in the form min..max inclusive.
  207. *
  208. * If the cvec represents a locale-specific character class, eg [[:alpha:]],
  209. * then the chrs[] and ranges[] arrays contain only members of that class
  210. * up to MAX_SIMPLE_CHR (inclusive). cclasscode is set to regc_locale.c's
  211. * code for the class, rather than being -1 as it is in an ordinary cvec.
  212. *
  213. * Note that in cvecs gotten from newcvec() and intended to be freed by
  214. * freecvec(), both arrays of chrs are after the end of the struct, not
  215. * separately malloc'd; so chrspace and rangespace are effectively immutable.
  216. */
  217. struct cvec
  218. {
  219. int nchrs; /* number of chrs */
  220. int chrspace; /* number of chrs allocated in chrs[] */
  221. chr *chrs; /* pointer to vector of chrs */
  222. int nranges; /* number of ranges (chr pairs) */
  223. int rangespace; /* number of ranges allocated in ranges[] */
  224. chr *ranges; /* pointer to vector of chr pairs */
  225. int cclasscode; /* value of "enum classes", or -1 */
  226. };
  227. /*
  228. * definitions for NFA internal representation
  229. *
  230. * Having a "from" pointer within each arc may seem redundant, but it
  231. * saves a lot of hassle.
  232. */
  233. struct state;
  234. struct arc
  235. {
  236. int type; /* 0 if free, else an NFA arc type code */
  237. color co;
  238. struct state *from; /* where it's from (and contained within) */
  239. struct state *to; /* where it's to */
  240. struct arc *outchain; /* link in *from's outs chain or free chain */
  241. struct arc *outchainRev; /* back-link in *from's outs chain */
  242. #define freechain outchain /* we do not maintain "freechainRev" */
  243. struct arc *inchain; /* link in *to's ins chain */
  244. struct arc *inchainRev; /* back-link in *to's ins chain */
  245. struct arc *colorchain; /* link in color's arc chain */
  246. struct arc *colorchainRev; /* back-link in color's arc chain */
  247. };
  248. struct arcbatch
  249. { /* for bulk allocation of arcs */
  250. struct arcbatch *next;
  251. #define ABSIZE 10
  252. struct arc a[ABSIZE];
  253. };
  254. struct state
  255. {
  256. int no;
  257. #define FREESTATE (-1)
  258. char flag; /* marks special states */
  259. int nins; /* number of inarcs */
  260. struct arc *ins; /* chain of inarcs */
  261. int nouts; /* number of outarcs */
  262. struct arc *outs; /* chain of outarcs */
  263. struct arc *free; /* chain of free arcs */
  264. struct state *tmp; /* temporary for traversal algorithms */
  265. struct state *next; /* chain for traversing all */
  266. struct state *prev; /* back chain */
  267. struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
  268. int noas; /* number of arcs used in first arcbatch */
  269. };
  270. struct nfa
  271. {
  272. struct state *pre; /* pre-initial state */
  273. struct state *init; /* initial state */
  274. struct state *final; /* final state */
  275. struct state *post; /* post-final state */
  276. int nstates; /* for numbering states */
  277. struct state *states; /* state-chain header */
  278. struct state *slast; /* tail of the chain */
  279. struct state *free; /* free list */
  280. struct colormap *cm; /* the color map */
  281. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  282. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  283. struct vars *v; /* simplifies compile error reporting */
  284. struct nfa *parent; /* parent NFA, if any */
  285. };
  286. /*
  287. * definitions for compacted NFA
  288. *
  289. * The main space savings in a compacted NFA is from making the arcs as small
  290. * as possible. We store only the transition color and next-state number for
  291. * each arc. The list of out arcs for each state is an array beginning at
  292. * cnfa.states[statenumber], and terminated by a dummy carc struct with
  293. * co == COLORLESS.
  294. *
  295. * The non-dummy carc structs are of two types: plain arcs and LACON arcs.
  296. * Plain arcs just store the transition color number as "co". LACON arcs
  297. * store the lookaround constraint number plus cnfa.ncolors as "co". LACON
  298. * arcs can be distinguished from plain by testing for co >= cnfa.ncolors.
  299. */
  300. struct carc
  301. {
  302. color co; /* COLORLESS is list terminator */
  303. int to; /* next-state number */
  304. };
  305. struct cnfa
  306. {
  307. int nstates; /* number of states */
  308. int ncolors; /* number of colors (max color in use + 1) */
  309. int flags;
  310. #define HASLACONS 01 /* uses lookaround constraints */
  311. int pre; /* setup state number */
  312. int post; /* teardown state number */
  313. color bos[2]; /* colors, if any, assigned to BOS and BOL */
  314. color eos[2]; /* colors, if any, assigned to EOS and EOL */
  315. char *stflags; /* vector of per-state flags bytes */
  316. #define CNFA_NOPROGRESS 01 /* flag bit for a no-progress state */
  317. struct carc **states; /* vector of pointers to outarc lists */
  318. /* states[n] are pointers into a single malloc'd array of arcs */
  319. struct carc *arcs; /* the area for the lists */
  320. };
  321. #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
  322. #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
  323. /*
  324. * This symbol limits the transient heap space used by the regex compiler,
  325. * and thereby also the maximum complexity of NFAs that we'll deal with.
  326. * Currently we only count NFA states and arcs against this; the other
  327. * transient data is generally not large enough to notice compared to those.
  328. * Note that we do not charge anything for the final output data structures
  329. * (the compacted NFA and the colormap).
  330. */
  331. #ifndef REG_MAX_COMPILE_SPACE
  332. #define REG_MAX_COMPILE_SPACE \
  333. (100000 * sizeof(struct state) + 100000 * sizeof(struct arcbatch))
  334. #endif
  335. /*
  336. * subexpression tree
  337. *
  338. * "op" is one of:
  339. * '=' plain regex without interesting substructure (implemented as DFA)
  340. * 'b' back-reference (has no substructure either)
  341. * '(' capture node: captures the match of its single child
  342. * '.' concatenation: matches a match for left, then a match for right
  343. * '|' alternation: matches a match for left or a match for right
  344. * '*' iteration: matches some number of matches of its single child
  345. *
  346. * Note: the right child of an alternation must be another alternation or
  347. * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
  348. * might expect. This could stand to be changed. Actually I'd rather see
  349. * a single alternation node with N children, but that will take revising
  350. * the representation of struct subre.
  351. *
  352. * Note: when a backref is directly quantified, we stick the min/max counts
  353. * into the backref rather than plastering an iteration node on top. This is
  354. * for efficiency: there is no need to search for possible division points.
  355. */
  356. struct subre
  357. {
  358. char op; /* see type codes above */
  359. char flags;
  360. #define LONGER 01 /* prefers longer match */
  361. #define SHORTER 02 /* prefers shorter match */
  362. #define MIXED 04 /* mixed preference below */
  363. #define CAP 010 /* capturing parens below */
  364. #define BACKR 020 /* back reference below */
  365. #define INUSE 0100 /* in use in final tree */
  366. #define NOPROP 03 /* bits which may not propagate up */
  367. #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
  368. #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
  369. #define UP(f) (((f)&~NOPROP) | (LMIX(f) & SMIX(f) & MIXED))
  370. #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
  371. #define PREF(f) ((f)&NOPROP)
  372. #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
  373. #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
  374. short id; /* ID of subre (1..ntree-1) */
  375. int subno; /* subexpression number for 'b' and '(', or
  376. * LATYPE code for lookaround constraint */
  377. short min; /* min repetitions for iteration or backref */
  378. short max; /* max repetitions for iteration or backref */
  379. struct subre *left; /* left child, if any (also freelist chain) */
  380. struct subre *right; /* right child, if any */
  381. struct state *begin; /* outarcs from here... */
  382. struct state *end; /* ...ending in inarcs here */
  383. struct cnfa cnfa; /* compacted NFA, if any */
  384. struct subre *chain; /* for bookkeeping and error cleanup */
  385. };
  386. /*
  387. * table of function pointers for generic manipulation functions
  388. * A regex_t's re_fns points to one of these.
  389. */
  390. struct fns
  391. {
  392. void FUNCPTR(free, (regex_t *));
  393. int FUNCPTR(cancel_requested, (void));
  394. int FUNCPTR(stack_too_deep, (void));
  395. };
  396. #define CANCEL_REQUESTED(re) \
  397. ((*((struct fns *) (re)->re_fns)->cancel_requested) ())
  398. #define STACK_TOO_DEEP(re) \
  399. ((*((struct fns *) (re)->re_fns)->stack_too_deep) ())
  400. /*
  401. * the insides of a regex_t, hidden behind a void *
  402. */
  403. struct guts
  404. {
  405. int magic;
  406. #define GUTSMAGIC 0xfed9
  407. int cflags; /* copy of compile flags */
  408. long info; /* copy of re_info */
  409. size_t nsub; /* copy of re_nsub */
  410. struct subre *tree;
  411. struct cnfa search; /* for fast preliminary search */
  412. int ntree; /* number of subre's, plus one */
  413. struct colormap cmap;
  414. int FUNCPTR(compare, (const chr *, const chr *, size_t));
  415. struct subre *lacons; /* lookaround-constraint vector */
  416. int nlacons; /* size of lacons[]; note that only slots
  417. * numbered 1 .. nlacons-1 are used */
  418. };
  419. /* prototypes for functions that are exported from regcomp.c to regexec.c */
  420. extern void pg_set_regex_collation(Oid collation);
  421. extern color pg_reg_getcolor(struct colormap *cm, chr c);
上海开阖软件有限公司 沪ICP备12045867号-1