OSDN Git Service

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