Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / include / regex / regguts.h
blobf78d9132652a5e5ff35bc7e3f1eb0e1491de5842
1 /*
2 * Internal interface definitions, etc., for the reg package
4 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
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.
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.
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.
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.
30 * $PostgreSQL$
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.
40 #include "regcustom.h"
45 * Things that regcustom.h might override.
48 /* assertions */
49 #ifndef assert
50 #ifndef REG_DEBUG
51 #define NDEBUG /* no assertions */
52 #endif
53 #include <assert.h>
54 #endif
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
64 /* function-pointer declarator */
65 #ifndef FUNCPTR
66 #define FUNCPTR(name, args) (*(name)) args
67 #endif
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
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
91 * misc
94 #define NOTREACHED 0
95 #define xxx 1
97 #define DUPMAX _POSIX2_RE_DUP_MAX
98 #define INFINITY (DUPMAX+1)
100 #define REMAGIC 0xfed7 /* magic number for main struct */
105 * debugging facilities
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
120 * bitmap manipulation
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)))
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.
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 */
145 * As soon as possible, we map chrs into equivalence classes -- "colors" --
146 * which are of much more manageable number.
148 typedef short color; /* colors of characters */
149 typedef int pcolor; /* what color promotes to */
151 #define COLORLESS (-1) /* impossible color */
152 #define WHITE 0 /* default color, parent of all others */
157 * A colormap is a tree -- more precisely, a DAG -- indexed at each level
158 * by a byt of the chr, to map the chr to a color efficiently. Because
159 * lower sections of the tree can be shared, it can exploit the usual
160 * sparseness of such a mapping table. The tree is always NBYTS levels
161 * deep (in the past it was shallower during construction but was "filled"
162 * to full depth at the end of that); areas that are unaltered as yet point
163 * to "fill blocks" which are entirely WHITE in color.
166 /* the tree itself */
167 struct colors
169 color ccolor[BYTTAB];
171 struct ptrs
173 union tree *pptr[BYTTAB];
175 union tree
177 struct colors colors;
178 struct ptrs ptrs;
181 #define tcolor colors.ccolor
182 #define tptr ptrs.pptr
184 /* internal per-color descriptor structure for the color machinery */
185 struct colordesc
187 uchr nchrs; /* number of chars of this color */
188 color sub; /* open subcolor (if any); free chain ptr */
189 #define NOSUB COLORLESS
190 struct arc *arcs; /* color chain */
191 int flags;
192 #define FREECOL 01 /* currently free */
193 #define PSEUDO 02 /* pseudocolor, no real chars */
194 #define UNUSEDCOLOR(cd) ((cd)->flags&FREECOL)
195 union tree *block; /* block of solid color, if any */
198 /* the color map itself */
199 struct colormap
201 int magic;
202 #define CMMAGIC 0x876
203 struct vars *v; /* for compile error reporting */
204 size_t ncds; /* number of colordescs */
205 size_t max; /* highest in use */
206 color free; /* beginning of free chain (if non-0) */
207 struct colordesc *cd;
208 #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
209 #define NINLINECDS ((size_t)10)
210 struct colordesc cdspace[NINLINECDS];
211 union tree tree[NBYTS]; /* tree top, plus fill blocks */
214 /* optimization magic to do fast chr->color mapping */
215 #define B0(c) ((c) & BYTMASK)
216 #define B1(c) (((c)>>BYTBITS) & BYTMASK)
217 #define B2(c) (((c)>>(2*BYTBITS)) & BYTMASK)
218 #define B3(c) (((c)>>(3*BYTBITS)) & BYTMASK)
219 #if NBYTS == 1
220 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
221 #endif
222 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
223 #if NBYTS == 2
224 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
225 #endif
226 #if NBYTS == 4
227 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
228 #endif
232 * Interface definitions for locale-interface functions in locale.c.
235 /* Representation of a set of characters. */
236 struct cvec
238 int nchrs; /* number of chrs */
239 int chrspace; /* number of chrs possible */
240 chr *chrs; /* pointer to vector of chrs */
241 int nranges; /* number of ranges (chr pairs) */
242 int rangespace; /* number of chrs possible */
243 chr *ranges; /* pointer to vector of chr pairs */
244 /* both batches of chrs are on the end */
249 * definitions for NFA internal representation
251 * Having a "from" pointer within each arc may seem redundant, but it
252 * saves a lot of hassle.
254 struct state;
256 struct arc
258 int type;
259 #define ARCFREE '\0'
260 color co;
261 struct state *from; /* where it's from (and contained within) */
262 struct state *to; /* where it's to */
263 struct arc *outchain; /* *from's outs chain or free chain */
264 #define freechain outchain
265 struct arc *inchain; /* *to's ins chain */
266 struct arc *colorchain; /* color's arc chain */
267 struct arc *colorchainRev; /* back-link in color's arc chain */
270 struct arcbatch
271 { /* for bulk allocation of arcs */
272 struct arcbatch *next;
273 #define ABSIZE 10
274 struct arc a[ABSIZE];
277 struct state
279 int no;
280 #define FREESTATE (-1)
281 char flag; /* marks special states */
282 int nins; /* number of inarcs */
283 struct arc *ins; /* chain of inarcs */
284 int nouts; /* number of outarcs */
285 struct arc *outs; /* chain of outarcs */
286 struct arc *free; /* chain of free arcs */
287 struct state *tmp; /* temporary for traversal algorithms */
288 struct state *next; /* chain for traversing all */
289 struct state *prev; /* back chain */
290 struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
291 int noas; /* number of arcs used in first arcbatch */
294 struct nfa
296 struct state *pre; /* pre-initial state */
297 struct state *init; /* initial state */
298 struct state *final; /* final state */
299 struct state *post; /* post-final state */
300 int nstates; /* for numbering states */
301 struct state *states; /* state-chain header */
302 struct state *slast; /* tail of the chain */
303 struct state *free; /* free list */
304 struct colormap *cm; /* the color map */
305 color bos[2]; /* colors, if any, assigned to BOS and BOL */
306 color eos[2]; /* colors, if any, assigned to EOS and EOL */
307 size_t size; /* Current NFA size; differs from nstates as
308 * it also counts the number of states created
309 * by children of this state. */
310 struct vars *v; /* simplifies compile error reporting */
311 struct nfa *parent; /* parent NFA, if any */
317 * definitions for compacted NFA
319 struct carc
321 color co; /* COLORLESS is list terminator */
322 int to; /* state number */
325 struct cnfa
327 int nstates; /* number of states */
328 int ncolors; /* number of colors */
329 int flags;
330 #define HASLACONS 01 /* uses lookahead constraints */
331 int pre; /* setup state number */
332 int post; /* teardown state number */
333 color bos[2]; /* colors, if any, assigned to BOS and BOL */
334 color eos[2]; /* colors, if any, assigned to EOS and EOL */
335 struct carc **states; /* vector of pointers to outarc lists */
336 struct carc *arcs; /* the area for the lists */
339 #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
340 #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
343 * Used to limit the maximum NFA size to something sane. [Tcl Bug 1810264]
345 #ifndef REG_MAX_STATES
346 #define REG_MAX_STATES 100000
347 #endif
350 * subexpression tree
352 struct subre
354 char op; /* '|', '.' (concat), 'b' (backref), '(', '=' */
355 char flags;
356 #define LONGER 01 /* prefers longer match */
357 #define SHORTER 02 /* prefers shorter match */
358 #define MIXED 04 /* mixed preference below */
359 #define CAP 010 /* capturing parens below */
360 #define BACKR 020 /* back reference below */
361 #define INUSE 0100 /* in use in final tree */
362 #define LOCAL 03 /* bits which may not propagate up */
363 #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
364 #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
365 #define UP(f) (((f)&~LOCAL) | (LMIX(f) & SMIX(f) & MIXED))
366 #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
367 #define PREF(f) ((f)&LOCAL)
368 #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
369 #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
370 short retry; /* index into retry memory */
371 int subno; /* subexpression number (for 'b' and '(') */
372 short min; /* min repetitions, for backref only */
373 short max; /* max repetitions, for backref only */
374 struct subre *left; /* left child, if any (also freelist chain) */
375 struct subre *right; /* right child, if any */
376 struct state *begin; /* outarcs from here... */
377 struct state *end; /* ...ending in inarcs here */
378 struct cnfa cnfa; /* compacted NFA, if any */
379 struct subre *chain; /* for bookkeeping and error cleanup */
385 * table of function pointers for generic manipulation functions
386 * A regex_t's re_fns points to one of these.
388 struct fns
390 void FUNCPTR(free, (regex_t *));
396 * the insides of a regex_t, hidden behind a void *
398 struct guts
400 int magic;
401 #define GUTSMAGIC 0xfed9
402 int cflags; /* copy of compile flags */
403 long info; /* copy of re_info */
404 size_t nsub; /* copy of re_nsub */
405 struct subre *tree;
406 struct cnfa search; /* for fast preliminary search */
407 int ntree;
408 struct colormap cmap;
409 int FUNCPTR(compare, (const chr *, const chr *, size_t));
410 struct subre *lacons; /* lookahead-constraint vector */
411 int nlacons; /* size of lacons */