OSDN Git Service

rebuid:
[eos/hostdependX86MAC64.git] / util / X86MAC64 / include / postgresql / server / regex / regguts.h
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
34
35 /*
36  * Environmental customization.  It should not (I hope) be necessary to
37  * alter the file you are now reading -- regcustom.h should handle it all,
38  * given care here and elsewhere.
39  */
40 #include "regcustom.h"
41
42
43
44 /*
45  * Things that regcustom.h might override.
46  */
47
48 /* assertions */
49 #ifndef assert
50 #ifndef REG_DEBUG
51 #define  NDEBUG                                 /* no assertions */
52 #endif
53 #include <assert.h>
54 #endif
55
56 /* voids */
57 #ifndef DISCARD
58 #define DISCARD void                    /* for throwing values away */
59 #endif
60 #ifndef VS
61 #define VS(x)   ((void *)(x))   /* cast something to generic ptr */
62 #endif
63
64 /* function-pointer declarator */
65 #ifndef FUNCPTR
66 #define FUNCPTR(name, args) (*(name)) args
67 #endif
68
69 /* memory allocation */
70 #ifndef MALLOC
71 #define MALLOC(n)       malloc(n)
72 #endif
73 #ifndef REALLOC
74 #define REALLOC(p, n)   realloc(VS(p), n)
75 #endif
76 #ifndef FREE
77 #define FREE(p)         free(VS(p))
78 #endif
79
80 /* want size of a char in bits, and max value in bounded quantifiers */
81 #ifndef CHAR_BIT
82 #include <limits.h>
83 #endif
84 #ifndef _POSIX2_RE_DUP_MAX
85 #define _POSIX2_RE_DUP_MAX      255 /* normally from <limits.h> */
86 #endif
87
88
89
90 /*
91  * misc
92  */
93
94 #define NOTREACHED      0
95 #define xxx             1
96
97 #define DUPMAX  _POSIX2_RE_DUP_MAX
98 #define INFINITY        (DUPMAX+1)
99
100 #define REMAGIC 0xfed7                  /* magic number for main struct */
101
102
103
104 /*
105  * debugging facilities
106  */
107 #ifdef REG_DEBUG
108 /* FDEBUG does finite-state tracing */
109 #define FDEBUG(arglist) { if (v->eflags&REG_FTRACE) printf arglist; }
110 /* MDEBUG does higher-level tracing */
111 #define MDEBUG(arglist) { if (v->eflags&REG_MTRACE) printf arglist; }
112 #else
113 #define FDEBUG(arglist) {}
114 #define MDEBUG(arglist) {}
115 #endif
116
117
118
119 /*
120  * bitmap manipulation
121  */
122 #define UBITS   (CHAR_BIT * sizeof(unsigned))
123 #define BSET(uv, sn)    ((uv)[(sn)/UBITS] |= (unsigned)1 << ((sn)%UBITS))
124 #define ISBSET(uv, sn)  ((uv)[(sn)/UBITS] & ((unsigned)1 << ((sn)%UBITS)))
125
126
127
128 /*
129  * We dissect a chr into byts for colormap table indexing.  Here we define
130  * a byt, which will be the same as a byte on most machines...  The exact
131  * size of a byt is not critical, but about 8 bits is good, and extraction
132  * of 8-bit chunks is sometimes especially fast.
133  */
134 #ifndef BYTBITS
135 #define BYTBITS 8                               /* bits in a byt */
136 #endif
137 #define BYTTAB  (1<<BYTBITS)    /* size of table with one entry per byt value */
138 #define BYTMASK (BYTTAB-1)              /* bit mask for byt */
139 #define NBYTS   ((CHRBITS+BYTBITS-1)/BYTBITS)
140 /* the definition of GETCOLOR(), below, assumes NBYTS <= 4 */
141
142
143
144 /*
145  * As soon as possible, we map chrs into equivalence classes -- "colors" --
146  * which are of much more manageable number.
147  */
148 typedef short color;                    /* colors of characters */
149 typedef int pcolor;                             /* what color promotes to */
150
151 #define MAX_COLOR       32767           /* max color (must fit in 'color' datatype) */
152 #define COLORLESS       (-1)            /* impossible color */
153 #define WHITE           0                       /* default color, parent of all others */
154
155
156
157 /*
158  * A colormap is a tree -- more precisely, a DAG -- indexed at each level
159  * by a byt of the chr, to map the chr to a color efficiently.  Because
160  * lower sections of the tree can be shared, it can exploit the usual
161  * sparseness of such a mapping table.  The tree is always NBYTS levels
162  * deep (in the past it was shallower during construction but was "filled"
163  * to full depth at the end of that); areas that are unaltered as yet point
164  * to "fill blocks" which are entirely WHITE in color.
165  */
166
167 /* the tree itself */
168 struct colors
169 {
170         color           ccolor[BYTTAB];
171 };
172 struct ptrs
173 {
174         union tree *pptr[BYTTAB];
175 };
176 union tree
177 {
178         struct colors colors;
179         struct ptrs ptrs;
180 };
181
182 #define tcolor  colors.ccolor
183 #define tptr    ptrs.pptr
184
185 /*
186  * Per-color data structure for the compile-time color machinery
187  *
188  * If "sub" is not NOSUB then it is the number of the color's current
189  * subcolor, i.e. we are in process of dividing this color (character
190  * equivalence class) into two colors.  See src/backend/regex/README for
191  * discussion of subcolors.
192  *
193  * Currently-unused colors have the FREECOL bit set and are linked into a
194  * freelist using their "sub" fields, but only if their color numbers are
195  * less than colormap.max.  Any array entries beyond "max" are just garbage.
196  */
197 struct colordesc
198 {
199         uchr            nchrs;                  /* number of chars of this color */
200         color           sub;                    /* open subcolor, if any; or free-chain ptr */
201 #define  NOSUB   COLORLESS              /* value of "sub" when no open subcolor */
202         struct arc *arcs;                       /* chain of all arcs of this color */
203         chr                     firstchr;               /* char first assigned to this color */
204         int                     flags;                  /* bit values defined next */
205 #define  FREECOL 01                             /* currently free */
206 #define  PSEUDO  02                             /* pseudocolor, no real chars */
207 #define  UNUSEDCOLOR(cd) ((cd)->flags & FREECOL)
208         union tree *block;                      /* block of solid color, if any */
209 };
210
211 /*
212  * The color map itself
213  *
214  * Much of the data in the colormap struct is only used at compile time.
215  * However, the bulk of the space usage is in the "tree" structure, so it's
216  * not clear that there's much point in converting the rest to a more compact
217  * form when compilation is finished.
218  */
219 struct colormap
220 {
221         int                     magic;
222 #define  CMMAGIC 0x876
223         struct vars *v;                         /* for compile error reporting */
224         size_t          ncds;                   /* allocated length of colordescs array */
225         size_t          max;                    /* highest color number currently in use */
226         color           free;                   /* beginning of free chain (if non-0) */
227         struct colordesc *cd;           /* pointer to array of colordescs */
228 #define  CDEND(cm)       (&(cm)->cd[(cm)->max + 1])
229         /* If we need up to NINLINECDS, we store them here to save a malloc */
230 #define  NINLINECDS  ((size_t)10)
231         struct colordesc cdspace[NINLINECDS];
232         union tree      tree[NBYTS];    /* tree top, plus lower-level fill blocks */
233 };
234
235 /* optimization magic to do fast chr->color mapping */
236 #define B0(c)   ((c) & BYTMASK)
237 #define B1(c)   (((c)>>BYTBITS) & BYTMASK)
238 #define B2(c)   (((c)>>(2*BYTBITS)) & BYTMASK)
239 #define B3(c)   (((c)>>(3*BYTBITS)) & BYTMASK)
240 #if NBYTS == 1
241 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
242 #endif
243 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
244 #if NBYTS == 2
245 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
246 #endif
247 #if NBYTS == 4
248 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
249 #endif
250
251
252 /*
253  * Interface definitions for locale-interface functions in regc_locale.c.
254  */
255
256 /*
257  * Representation of a set of characters.  chrs[] represents individual
258  * code points, ranges[] represents ranges in the form min..max inclusive.
259  *
260  * Note that in cvecs gotten from newcvec() and intended to be freed by
261  * freecvec(), both arrays of chrs are after the end of the struct, not
262  * separately malloc'd; so chrspace and rangespace are effectively immutable.
263  */
264 struct cvec
265 {
266         int                     nchrs;                  /* number of chrs */
267         int                     chrspace;               /* number of chrs allocated in chrs[] */
268         chr                *chrs;                       /* pointer to vector of chrs */
269         int                     nranges;                /* number of ranges (chr pairs) */
270         int                     rangespace;             /* number of ranges allocated in ranges[] */
271         chr                *ranges;                     /* pointer to vector of chr pairs */
272 };
273
274
275 /*
276  * definitions for NFA internal representation
277  *
278  * Having a "from" pointer within each arc may seem redundant, but it
279  * saves a lot of hassle.
280  */
281 struct state;
282
283 struct arc
284 {
285         int                     type;                   /* 0 if free, else an NFA arc type code */
286         color           co;
287         struct state *from;                     /* where it's from (and contained within) */
288         struct state *to;                       /* where it's to */
289         struct arc *outchain;           /* link in *from's outs chain or free chain */
290 #define  freechain       outchain
291         struct arc *inchain;            /* link in *to's ins chain */
292         struct arc *colorchain;         /* link in color's arc chain */
293         struct arc *colorchainRev;      /* back-link in color's arc chain */
294 };
295
296 struct arcbatch
297 {                                                               /* for bulk allocation of arcs */
298         struct arcbatch *next;
299 #define  ABSIZE  10
300         struct arc      a[ABSIZE];
301 };
302
303 struct state
304 {
305         int                     no;
306 #define  FREESTATE       (-1)
307         char            flag;                   /* marks special states */
308         int                     nins;                   /* number of inarcs */
309         struct arc *ins;                        /* chain of inarcs */
310         int                     nouts;                  /* number of outarcs */
311         struct arc *outs;                       /* chain of outarcs */
312         struct arc *free;                       /* chain of free arcs */
313         struct state *tmp;                      /* temporary for traversal algorithms */
314         struct state *next;                     /* chain for traversing all */
315         struct state *prev;                     /* back chain */
316         struct arcbatch oas;            /* first arcbatch, avoid malloc in easy case */
317         int                     noas;                   /* number of arcs used in first arcbatch */
318 };
319
320 struct nfa
321 {
322         struct state *pre;                      /* pre-initial state */
323         struct state *init;                     /* initial state */
324         struct state *final;            /* final state */
325         struct state *post;                     /* post-final state */
326         int                     nstates;                /* for numbering states */
327         struct state *states;           /* state-chain header */
328         struct state *slast;            /* tail of the chain */
329         struct state *free;                     /* free list */
330         struct colormap *cm;            /* the color map */
331         color           bos[2];                 /* colors, if any, assigned to BOS and BOL */
332         color           eos[2];                 /* colors, if any, assigned to EOS and EOL */
333         size_t          size;                   /* Current NFA size; differs from nstates as
334                                                                  * it also counts the number of states in
335                                                                  * children of this NFA. */
336         struct vars *v;                         /* simplifies compile error reporting */
337         struct nfa *parent;                     /* parent NFA, if any */
338 };
339
340
341
342 /*
343  * definitions for compacted NFA
344  *
345  * The main space savings in a compacted NFA is from making the arcs as small
346  * as possible.  We store only the transition color and next-state number for
347  * each arc.  The list of out arcs for each state is an array beginning at
348  * cnfa.states[statenumber], and terminated by a dummy carc struct with
349  * co == COLORLESS.
350  *
351  * The non-dummy carc structs are of two types: plain arcs and LACON arcs.
352  * Plain arcs just store the transition color number as "co".  LACON arcs
353  * store the lookahead constraint number plus cnfa.ncolors as "co".  LACON
354  * arcs can be distinguished from plain by testing for co >= cnfa.ncolors.
355  */
356 struct carc
357 {
358         color           co;                             /* COLORLESS is list terminator */
359         int                     to;                             /* next-state number */
360 };
361
362 struct cnfa
363 {
364         int                     nstates;                /* number of states */
365         int                     ncolors;                /* number of colors (max color in use + 1) */
366         int                     flags;
367 #define  HASLACONS      01                      /* uses lookahead constraints */
368         int                     pre;                    /* setup state number */
369         int                     post;                   /* teardown state number */
370         color           bos[2];                 /* colors, if any, assigned to BOS and BOL */
371         color           eos[2];                 /* colors, if any, assigned to EOS and EOL */
372         char       *stflags;            /* vector of per-state flags bytes */
373 #define  CNFA_NOPROGRESS        01      /* flag bit for a no-progress state */
374         struct carc **states;           /* vector of pointers to outarc lists */
375         /* states[n] are pointers into a single malloc'd array of arcs */
376         struct carc *arcs;                      /* the area for the lists */
377 };
378
379 #define ZAPCNFA(cnfa)   ((cnfa).nstates = 0)
380 #define NULLCNFA(cnfa)  ((cnfa).nstates == 0)
381
382 /*
383  * Used to limit the maximum NFA size to something sane. [Tcl Bug 1810264]
384  */
385 #ifndef REG_MAX_STATES
386 #define REG_MAX_STATES  100000
387 #endif
388
389 /*
390  * subexpression tree
391  *
392  * "op" is one of:
393  *              '='  plain regex without interesting substructure (implemented as DFA)
394  *              'b'  back-reference (has no substructure either)
395  *              '('  capture node: captures the match of its single child
396  *              '.'  concatenation: matches a match for left, then a match for right
397  *              '|'  alternation: matches a match for left or a match for right
398  *              '*'  iteration: matches some number of matches of its single child
399  *
400  * Note: the right child of an alternation must be another alternation or
401  * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
402  * might expect.  This could stand to be changed.  Actually I'd rather see
403  * a single alternation node with N children, but that will take revising
404  * the representation of struct subre.
405  *
406  * Note: when a backref is directly quantified, we stick the min/max counts
407  * into the backref rather than plastering an iteration node on top.  This is
408  * for efficiency: there is no need to search for possible division points.
409  */
410 struct subre
411 {
412         char            op;                             /* see type codes above */
413         char            flags;
414 #define  LONGER  01                             /* prefers longer match */
415 #define  SHORTER 02                             /* prefers shorter match */
416 #define  MIXED   04                             /* mixed preference below */
417 #define  CAP 010                                /* capturing parens below */
418 #define  BACKR   020                    /* back reference below */
419 #define  INUSE   0100                   /* in use in final tree */
420 #define  LOCAL   03                             /* bits which may not propagate up */
421 #define  LMIX(f) ((f)<<2)               /* LONGER -> MIXED */
422 #define  SMIX(f) ((f)<<1)               /* SHORTER -> MIXED */
423 #define  UP(f)   (((f)&~LOCAL) | (LMIX(f) & SMIX(f) & MIXED))
424 #define  MESSY(f)        ((f)&(MIXED|CAP|BACKR))
425 #define  PREF(f) ((f)&LOCAL)
426 #define  PREF2(f1, f2)   ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
427 #define  COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
428         short           id;                             /* ID of subre (1..ntree-1) */
429         int                     subno;                  /* subexpression number (for 'b' and '(') */
430         short           min;                    /* min repetitions for iteration or backref */
431         short           max;                    /* max repetitions for iteration or backref */
432         struct subre *left;                     /* left child, if any (also freelist chain) */
433         struct subre *right;            /* right child, if any */
434         struct state *begin;            /* outarcs from here... */
435         struct state *end;                      /* ...ending in inarcs here */
436         struct cnfa cnfa;                       /* compacted NFA, if any */
437         struct subre *chain;            /* for bookkeeping and error cleanup */
438 };
439
440
441
442 /*
443  * table of function pointers for generic manipulation functions
444  * A regex_t's re_fns points to one of these.
445  */
446 struct fns
447 {
448         void            FUNCPTR(free, (regex_t *));
449         int                     FUNCPTR(cancel_requested, (void));
450 };
451
452 #define CANCEL_REQUESTED(re)  \
453         ((*((struct fns *) (re)->re_fns)->cancel_requested) ())
454
455
456 /*
457  * the insides of a regex_t, hidden behind a void *
458  */
459 struct guts
460 {
461         int                     magic;
462 #define  GUTSMAGIC       0xfed9
463         int                     cflags;                 /* copy of compile flags */
464         long            info;                   /* copy of re_info */
465         size_t          nsub;                   /* copy of re_nsub */
466         struct subre *tree;
467         struct cnfa search;                     /* for fast preliminary search */
468         int                     ntree;                  /* number of subre's, less one */
469         struct colormap cmap;
470         int                     FUNCPTR(compare, (const chr *, const chr *, size_t));
471         struct subre *lacons;           /* lookahead-constraint vector */
472         int                     nlacons;                /* size of lacons */
473 };