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 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 */
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.
167 /* the tree itself */
170 color ccolor[BYTTAB];
174 union tree *pptr[BYTTAB];
178 struct colors colors;
182 #define tcolor colors.ccolor
183 #define tptr ptrs.pptr
186 * Per-color data structure for the compile-time color machinery
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.
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.
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 */
212 * The color map itself
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.
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 */
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)
241 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
243 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
245 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
248 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
253 * Interface definitions for locale-interface functions in regc_locale.c.
257 * Representation of a set of characters. chrs[] represents individual
258 * code points, ranges[] represents ranges in the form min..max inclusive.
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.
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 */
276 * definitions for NFA internal representation
278 * Having a "from" pointer within each arc may seem redundant, but it
279 * saves a lot of hassle.
285 int type; /* 0 if free, else an NFA arc type code */
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 */
297 { /* for bulk allocation of arcs */
298 struct arcbatch *next;
300 struct arc a[ABSIZE];
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 */
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 */
343 * definitions for compacted NFA
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
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.
358 color co; /* COLORLESS is list terminator */
359 int to; /* next-state number */
364 int nstates; /* number of states */
365 int ncolors; /* number of colors (max color in use + 1) */
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 */
379 #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
380 #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
383 * Used to limit the maximum NFA size to something sane. [Tcl Bug 1810264]
385 #ifndef REG_MAX_STATES
386 #define REG_MAX_STATES 100000
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
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.
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.
412 char op; /* see type codes above */
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 */
443 * table of function pointers for generic manipulation functions
444 * A regex_t's re_fns points to one of these.
448 void FUNCPTR(free, (regex_t *));
449 int FUNCPTR(cancel_requested, (void));
452 #define CANCEL_REQUESTED(re) \
453 ((*((struct fns *) (re)->re_fns)->cancel_requested) ())
457 * the insides of a regex_t, hidden behind a void *
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 */
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 */