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
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 * src/include/regex/regguts.h
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.
51 #define NDEBUG /* no assertions */
58 #define DISCARD void /* for throwing values away */
61 #define VS(x) ((void *)(x)) /* cast something to generic ptr */
64 /* function-pointer declarator */
66 #define FUNCPTR(name, args) (*(name)) args
69 /* memory allocation */
71 #define MALLOC(n) malloc(n)
74 #define REALLOC(p, n) realloc(VS(p), n)
77 #define FREE(p) free(VS(p))
80 /* want size of a char in bits, and max value in bounded quantifiers */
84 #ifndef _POSIX2_RE_DUP_MAX
85 #define _POSIX2_RE_DUP_MAX 255 /* normally from <limits.h> */
97 #define DUPMAX _POSIX2_RE_DUP_MAX
98 #define INFINITY (DUPMAX+1)
100 #define REMAGIC 0xfed7 /* magic number for main struct */
105 * debugging facilities
108 /* FDEBUG does finite-state tracing */
109 #define FDEBUG(arglist) { if (v->eflags®_FTRACE) printf arglist; }
110 /* MDEBUG does higher-level tracing */
111 #define MDEBUG(arglist) { if (v->eflags®_MTRACE) printf arglist; }
113 #define FDEBUG(arglist) {}
114 #define MDEBUG(arglist) {}
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.
135 #define BYTBITS 8 /* bits in a byt */
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 */
169 color ccolor[BYTTAB];
173 union tree *pptr[BYTTAB];
177 struct colors colors;
181 #define tcolor colors.ccolor
182 #define tptr ptrs.pptr
185 * Per-color data structure for the compile-time color machinery
187 * If "sub" is not NOSUB then it is the number of the color's current
188 * subcolor, i.e. we are in process of dividing this color (character
189 * equivalence class) into two colors. See src/backend/regex/README for
190 * discussion of subcolors.
192 * Currently-unused colors have the FREECOL bit set and are linked into a
193 * freelist using their "sub" fields, but only if their color numbers are
194 * less than colormap.max. Any array entries beyond "max" are just garbage.
198 uchr nchrs; /* number of chars of this color */
199 color sub; /* open subcolor, if any; or free-chain ptr */
200 #define NOSUB COLORLESS /* value of "sub" when no open subcolor */
201 struct arc *arcs; /* chain of all arcs of this color */
202 int flags; /* bit values defined next */
203 #define FREECOL 01 /* currently free */
204 #define PSEUDO 02 /* pseudocolor, no real chars */
205 #define UNUSEDCOLOR(cd) ((cd)->flags&FREECOL)
206 union tree *block; /* block of solid color, if any */
210 * The color map itself
212 * Only the "tree" part is used at execution time, and that only via the
213 * GETCOLOR() macro. Possibly that should be separated from the compile-time
219 #define CMMAGIC 0x876
220 struct vars *v; /* for compile error reporting */
221 size_t ncds; /* allocated length of colordescs array */
222 size_t max; /* highest color number currently in use */
223 color free; /* beginning of free chain (if non-0) */
224 struct colordesc *cd; /* pointer to array of colordescs */
225 #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
226 /* If we need up to NINLINECDS, we store them here to save a malloc */
227 #define NINLINECDS ((size_t)10)
228 struct colordesc cdspace[NINLINECDS];
229 union tree tree[NBYTS]; /* tree top, plus lower-level fill blocks */
232 /* optimization magic to do fast chr->color mapping */
233 #define B0(c) ((c) & BYTMASK)
234 #define B1(c) (((c)>>BYTBITS) & BYTMASK)
235 #define B2(c) (((c)>>(2*BYTBITS)) & BYTMASK)
236 #define B3(c) (((c)>>(3*BYTBITS)) & BYTMASK)
238 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
240 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
242 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
245 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
250 * Interface definitions for locale-interface functions in regc_locale.c.
254 * Representation of a set of characters. chrs[] represents individual
255 * code points, ranges[] represents ranges in the form min..max inclusive.
257 * Note that in cvecs gotten from newcvec() and intended to be freed by
258 * freecvec(), both arrays of chrs are after the end of the struct, not
259 * separately malloc'd; so chrspace and rangespace are effectively immutable.
263 int nchrs; /* number of chrs */
264 int chrspace; /* number of chrs allocated in chrs[] */
265 chr *chrs; /* pointer to vector of chrs */
266 int nranges; /* number of ranges (chr pairs) */
267 int rangespace; /* number of ranges allocated in ranges[] */
268 chr *ranges; /* pointer to vector of chr pairs */
273 * definitions for NFA internal representation
275 * Having a "from" pointer within each arc may seem redundant, but it
276 * saves a lot of hassle.
285 struct state *from; /* where it's from (and contained within) */
286 struct state *to; /* where it's to */
287 struct arc *outchain; /* *from's outs chain or free chain */
288 #define freechain outchain
289 struct arc *inchain; /* *to's ins chain */
290 struct arc *colorchain; /* color's arc chain */
291 struct arc *colorchainRev; /* back-link in color's arc chain */
295 { /* for bulk allocation of arcs */
296 struct arcbatch *next;
298 struct arc a[ABSIZE];
304 #define FREESTATE (-1)
305 char flag; /* marks special states */
306 int nins; /* number of inarcs */
307 struct arc *ins; /* chain of inarcs */
308 int nouts; /* number of outarcs */
309 struct arc *outs; /* chain of outarcs */
310 struct arc *free; /* chain of free arcs */
311 struct state *tmp; /* temporary for traversal algorithms */
312 struct state *next; /* chain for traversing all */
313 struct state *prev; /* back chain */
314 struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
315 int noas; /* number of arcs used in first arcbatch */
320 struct state *pre; /* pre-initial state */
321 struct state *init; /* initial state */
322 struct state *final; /* final state */
323 struct state *post; /* post-final state */
324 int nstates; /* for numbering states */
325 struct state *states; /* state-chain header */
326 struct state *slast; /* tail of the chain */
327 struct state *free; /* free list */
328 struct colormap *cm; /* the color map */
329 color bos[2]; /* colors, if any, assigned to BOS and BOL */
330 color eos[2]; /* colors, if any, assigned to EOS and EOL */
331 size_t size; /* Current NFA size; differs from nstates as
332 * it also counts the number of states created
333 * by children of this state. */
334 struct vars *v; /* simplifies compile error reporting */
335 struct nfa *parent; /* parent NFA, if any */
341 * definitions for compacted NFA
345 color co; /* COLORLESS is list terminator */
346 int to; /* state number */
351 int nstates; /* number of states */
352 int ncolors; /* number of colors */
354 #define HASLACONS 01 /* uses lookahead constraints */
355 int pre; /* setup state number */
356 int post; /* teardown state number */
357 color bos[2]; /* colors, if any, assigned to BOS and BOL */
358 color eos[2]; /* colors, if any, assigned to EOS and EOL */
359 struct carc **states; /* vector of pointers to outarc lists */
360 struct carc *arcs; /* the area for the lists */
363 #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
364 #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
367 * Used to limit the maximum NFA size to something sane. [Tcl Bug 1810264]
369 #ifndef REG_MAX_STATES
370 #define REG_MAX_STATES 100000
377 * '=' plain regex without interesting substructure (implemented as DFA)
378 * 'b' back-reference (has no substructure either)
379 * '(' capture node: captures the match of its single child
380 * '.' concatenation: matches a match for left, then a match for right
381 * '|' alternation: matches a match for left or a match for right
382 * '*' iteration: matches some number of matches of its single child
384 * Note: the right child of an alternation must be another alternation or
385 * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
386 * might expect. This could stand to be changed. Actually I'd rather see
387 * a single alternation node with N children, but that will take revising
388 * the representation of struct subre.
390 * Note: when a backref is directly quantified, we stick the min/max counts
391 * into the backref rather than plastering an iteration node on top. This is
392 * for efficiency: there is no need to search for possible division points.
396 char op; /* see type codes above */
398 #define LONGER 01 /* prefers longer match */
399 #define SHORTER 02 /* prefers shorter match */
400 #define MIXED 04 /* mixed preference below */
401 #define CAP 010 /* capturing parens below */
402 #define BACKR 020 /* back reference below */
403 #define INUSE 0100 /* in use in final tree */
404 #define LOCAL 03 /* bits which may not propagate up */
405 #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
406 #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
407 #define UP(f) (((f)&~LOCAL) | (LMIX(f) & SMIX(f) & MIXED))
408 #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
409 #define PREF(f) ((f)&LOCAL)
410 #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
411 #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
412 short id; /* ID of subre (1..ntree) */
413 int subno; /* subexpression number (for 'b' and '(') */
414 short min; /* min repetitions for iteration or backref */
415 short max; /* max repetitions for iteration or backref */
416 struct subre *left; /* left child, if any (also freelist chain) */
417 struct subre *right; /* right child, if any */
418 struct state *begin; /* outarcs from here... */
419 struct state *end; /* ...ending in inarcs here */
420 struct cnfa cnfa; /* compacted NFA, if any */
421 struct subre *chain; /* for bookkeeping and error cleanup */
427 * table of function pointers for generic manipulation functions
428 * A regex_t's re_fns points to one of these.
432 void FUNCPTR(free, (regex_t *));
438 * the insides of a regex_t, hidden behind a void *
443 #define GUTSMAGIC 0xfed9
444 int cflags; /* copy of compile flags */
445 long info; /* copy of re_info */
446 size_t nsub; /* copy of re_nsub */
448 struct cnfa search; /* for fast preliminary search */
450 struct colormap cmap;
451 int FUNCPTR(compare, (const chr *, const chr *, size_t));
452 struct subre *lacons; /* lookahead-constraint vector */
453 int nlacons; /* size of lacons */