3 * This file is #included by regexec.c.
5 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
7 * Development of this software was funded, in part, by Cray Research Inc.,
8 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
9 * Corporation, none of whom are responsible for the results. The author
12 * Redistribution and use in source and binary forms -- with or without
13 * modification -- are permitted for any purpose, provided that
14 * redistributions in source form retain this entire copyright notice and
15 * indicate the origin and nature of any modifications.
17 * I'd appreciate being given credit for this package in the documentation
18 * of software which uses it, but that is not a requirement.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 - longest - longest-preferred matching engine
35 ^ static chr *longest(struct vars *, struct dfa *, chr *, chr *, int *);
37 static chr * /* endpoint, or NULL */
39 struct vars *const v, /* used only for debug and exec flags */
41 chr *const start, /* where the match should start */
42 chr *const stop, /* match must end at or before here */
43 int *const hitstopp) /* record whether hit v->stop, if non-NULL */
46 chr *realstop = (stop == v->stop) ? stop : stop + 1;
48 struct sset *css, *ss;
51 struct colormap *cm = d->cm;
57 css = initialize(v, d, start);
59 if (hitstopp != NULL) {
67 FDEBUG(("+++ startup +++\n"));
69 co = d->cnfa->bos[(v->eflags®_NOTBOL) ? 0 : 1];
70 FDEBUG(("color %ld\n", (long)co));
72 co = GETCOLOR(cm, *(cp - 1));
73 FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
75 css = miss(v, d, css, co, cp, start);
85 if (v->eflags®_FTRACE) {
86 while (cp < realstop) {
87 FDEBUG(("+++ at c%d +++\n", (int) (css - d->ssets)));
88 co = GETCOLOR(cm, *cp);
89 FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
92 ss = miss(v, d, css, co, cp+1, start);
94 break; /* NOTE BREAK OUT */
102 while (cp < realstop) {
103 co = GETCOLOR(cm, *cp);
106 ss = miss(v, d, css, co, cp+1, start);
108 break; /* NOTE BREAK OUT */
121 FDEBUG(("+++ shutdown at c%d +++\n", (int) (css - d->ssets)));
122 if (cp == v->stop && stop == v->stop) {
123 if (hitstopp != NULL) {
126 co = d->cnfa->eos[(v->eflags®_NOTEOL) ? 0 : 1];
127 FDEBUG(("color %ld\n", (long)co));
128 ss = miss(v, d, css, co, cp, start);
131 * Special case: match ended at eol?
134 if (ss != NULL && (ss->flags&POSTSTATE)) {
136 } else if (ss != NULL) {
137 ss->lastseen = cp; /* to be tidy */
142 * Find last match, if any.
146 for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) {
147 if ((ss->flags&POSTSTATE) && (post != ss->lastseen) &&
148 (post == NULL || post < ss->lastseen)) {
152 if (post != NULL) { /* found one */
160 - shortest - shortest-preferred matching engine
161 ^ static chr *shortest(struct vars *, struct dfa *, chr *, chr *, chr *,
164 static chr * /* endpoint, or NULL */
166 struct vars *const v,
168 chr *const start, /* where the match should start */
169 chr *const min, /* match must end at or after here */
170 chr *const max, /* match must end at or before here */
171 chr **const coldp, /* store coldstart pointer here, if nonNULL */
172 int *const hitstopp) /* record whether hit v->stop, if non-NULL */
175 chr *realmin = (min == v->stop) ? min : min + 1;
176 chr *realmax = (max == v->stop) ? max : max + 1;
178 struct sset *css, *ss;
179 struct colormap *cm = d->cm;
185 css = initialize(v, d, start);
187 if (hitstopp != NULL) {
195 FDEBUG(("--- startup ---\n"));
196 if (cp == v->start) {
197 co = d->cnfa->bos[(v->eflags®_NOTBOL) ? 0 : 1];
198 FDEBUG(("color %ld\n", (long)co));
200 co = GETCOLOR(cm, *(cp - 1));
201 FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
203 css = miss(v, d, css, co, cp, start);
214 if (v->eflags®_FTRACE) {
215 while (cp < realmax) {
216 FDEBUG(("--- at c%d ---\n", (int) (css - d->ssets)));
217 co = GETCOLOR(cm, *cp);
218 FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
221 ss = miss(v, d, css, co, cp+1, start);
223 break; /* NOTE BREAK OUT */
229 if ((ss->flags&POSTSTATE) && cp >= realmin) {
230 break; /* NOTE BREAK OUT */
234 while (cp < realmax) {
235 co = GETCOLOR(cm, *cp);
238 ss = miss(v, d, css, co, cp+1, start);
240 break; /* NOTE BREAK OUT */
246 if ((ss->flags&POSTSTATE) && cp >= realmin) {
247 break; /* NOTE BREAK OUT */
256 if (coldp != NULL) { /* report last no-progress state set, if any */
257 *coldp = lastCold(v, d);
260 if ((ss->flags&POSTSTATE) && cp > min) {
261 assert(cp >= realmin);
263 } else if (cp == v->stop && max == v->stop) {
264 co = d->cnfa->eos[(v->eflags®_NOTEOL) ? 0 : 1];
265 FDEBUG(("color %ld\n", (long)co));
266 ss = miss(v, d, css, co, cp, start);
269 * Match might have ended at eol.
272 if ((ss == NULL || !(ss->flags&POSTSTATE)) && hitstopp != NULL) {
277 if (ss == NULL || !(ss->flags&POSTSTATE)) {
285 - lastCold - determine last point at which no progress had been made
286 ^ static chr *lastCold(struct vars *, struct dfa *);
288 static chr * /* endpoint, or NULL */
290 struct vars *const v,
294 chr *nopr = d->lastnopr;
300 for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) {
301 if ((ss->flags&NOPROGRESS) && nopr < ss->lastseen) {
309 - newDFA - set up a fresh DFA
310 ^ static struct dfa *newDFA(struct vars *, struct cnfa *,
311 ^ struct colormap *, struct smalldfa *);
315 struct vars *const v,
316 struct cnfa *const cnfa,
317 struct colormap *const cm,
318 struct smalldfa *sml) /* preallocated space, may be NULL */
321 size_t nss = cnfa->nstates * 2;
322 int wordsper = (cnfa->nstates + UBITS - 1) / UBITS;
323 struct smalldfa *smallwas = sml;
325 assert(cnfa != NULL && cnfa->nstates != 0);
327 if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS) {
328 assert(wordsper == 1);
330 sml = (struct smalldfa *) MALLOC(sizeof(struct smalldfa));
337 d->ssets = sml->ssets;
338 d->statesarea = sml->statesarea;
339 d->work = &d->statesarea[nss];
340 d->outsarea = sml->outsarea;
341 d->incarea = sml->incarea;
343 d->mallocarea = (smallwas == NULL) ? (char *)sml : NULL;
345 d = (struct dfa *) MALLOC(sizeof(struct dfa));
350 d->ssets = (struct sset *) MALLOC(nss * sizeof(struct sset));
351 d->statesarea = (unsigned *)
352 MALLOC((nss+WORK) * wordsper * sizeof(unsigned));
353 d->work = &d->statesarea[nss * wordsper];
354 d->outsarea = (struct sset **)
355 MALLOC(nss * cnfa->ncolors * sizeof(struct sset *));
356 d->incarea = (struct arcp *)
357 MALLOC(nss * cnfa->ncolors * sizeof(struct arcp));
359 d->mallocarea = (char *)d;
360 if (d->ssets == NULL || d->statesarea == NULL ||
361 d->outsarea == NULL || d->incarea == NULL) {
368 d->nssets = (v->eflags®_SMALL) ? 7 : nss;
370 d->nstates = cnfa->nstates;
371 d->ncolors = cnfa->ncolors;
372 d->wordsper = wordsper;
377 d->search = d->ssets;
380 * Initialization of sset fields is done as needed.
387 - freeDFA - free a DFA
388 ^ static void freeDFA(struct dfa *);
394 if (d->cptsmalloced) {
395 if (d->ssets != NULL) {
398 if (d->statesarea != NULL) {
401 if (d->outsarea != NULL) {
404 if (d->incarea != NULL) {
409 if (d->mallocarea != NULL) {
415 - hash - construct a hash code for a bitvector
416 * There are probably better ways, but they're more expensive.
417 ^ static unsigned hash(unsigned *, int);
428 for (i = 0; i < n; i++) {
435 - initialize - hand-craft a cache entry for startup, otherwise get ready
436 ^ static struct sset *initialize(struct vars *, struct dfa *, chr *);
440 struct vars *const v, /* used only for debug flags */
448 * Is previous one still there?
451 if (d->nssused > 0 && (d->ssets[0].flags&STARTER)) {
453 } else { /* no, must (re)build it */
454 ss = getVacantSS(v, d, start, start);
455 for (i = 0; i < d->wordsper; i++) {
458 BSET(ss->states, d->cnfa->pre);
459 ss->hash = HASH(ss->states, d->wordsper);
460 assert(d->cnfa->pre != d->cnfa->post);
461 ss->flags = STARTER|LOCKED|NOPROGRESS;
464 * lastseen dealt with below
468 for (i = 0; i < d->nssused; i++) {
469 d->ssets[i].lastseen = NULL;
471 ss->lastseen = start; /* maybe untrue, but harmless */
478 - miss - handle a cache miss
479 ^ static struct sset *miss(struct vars *, struct dfa *, struct sset *,
480 ^ pcolor, chr *, chr *);
482 static struct sset * /* NULL if goes to empty set */
484 struct vars *const v, /* used only for debug flags */
486 struct sset *const css,
488 chr *const cp, /* next chr */
489 chr *const start) /* where the attempt got started */
491 struct cnfa *cnfa = d->cnfa;
495 int i, isPost, noProgress, gotState, doLAConstraints, sawLAConstraints;
498 * For convenience, we can be called even if it might not be a miss.
501 if (css->outs[co] != NULL) {
503 return css->outs[co];
508 * First, what set of states would we end up in?
511 for (i = 0; i < d->wordsper; i++) {
517 for (i = 0; i < d->nstates; i++) {
518 if (ISBSET(css->states, i)) {
519 for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++) {
521 BSET(d->work, ca->to);
523 if (ca->to == cnfa->post) {
526 if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS)) {
529 FDEBUG(("%d -> %d\n", i, ca->to));
534 doLAConstraints = (gotState ? (cnfa->flags&HASLACONS) : 0);
535 sawLAConstraints = 0;
536 while (doLAConstraints) { /* transitive closure */
538 for (i = 0; i < d->nstates; i++) {
539 if (ISBSET(d->work, i)) {
540 for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++) {
541 if (ca->co < cnfa->ncolors) {
542 continue; /* NOTE CONTINUE */
544 sawLAConstraints = 1;
545 if (ISBSET(d->work, ca->to)) {
546 continue; /* NOTE CONTINUE */
548 if (!checkLAConstraint(v, cnfa, cp, ca->co)) {
549 continue; /* NOTE CONTINUE */
551 BSET(d->work, ca->to);
553 if (ca->to == cnfa->post) {
556 if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS)) {
559 FDEBUG(("%d :> %d\n", i, ca->to));
567 h = HASH(d->work, d->wordsper);
570 * Next, is that in the cache?
573 for (p = d->ssets, i = d->nssused; i > 0; p++, i--) {
574 if (HIT(h, d->work, p, d->wordsper)) {
575 FDEBUG(("cached c%d\n", (int) (p - d->ssets)));
576 break; /* NOTE BREAK OUT */
579 if (i == 0) { /* nope, need a new cache entry */
580 p = getVacantSS(v, d, cp, start);
582 for (i = 0; i < d->wordsper; i++) {
583 p->states[i] = d->work[i];
586 p->flags = (isPost ? POSTSTATE : 0);
588 p->flags |= NOPROGRESS;
592 * lastseen to be dealt with by caller
596 if (!sawLAConstraints) { /* lookahead conds. always cache miss */
597 FDEBUG(("c%d[%d]->c%d\n",
598 (int) (css - d->ssets), co, (int) (p - d->ssets)));
600 css->inchain[co] = p->ins;
602 p->ins.co = (color) co;
608 - checkLAConstraint - lookahead-constraint checker for miss()
609 ^ static int checkLAConstraint(struct vars *, struct cnfa *, chr *, pcolor);
611 static int /* predicate: constraint satisfied? */
613 struct vars *const v,
614 struct cnfa *const pcnfa, /* parent cnfa */
616 const pcolor co) /* "color" of the lookahead constraint */
624 n = co - pcnfa->ncolors;
625 assert(n < v->g->nlacons && v->g->lacons != NULL);
626 FDEBUG(("=== testing lacon %d\n", n));
627 sub = &v->g->lacons[n];
628 d = newDFA(v, &sub->cnfa, &v->g->cmap, &sd);
633 end = longest(v, d, cp, v->stop, NULL);
635 FDEBUG(("=== lacon %d match %d\n", n, (end != NULL)));
636 return (sub->subno) ? (end != NULL) : (end == NULL);
640 - getVacantSS - get a vacant state set
641 * This routine clears out the inarcs and outarcs, but does not otherwise
642 * clear the innards of the state set -- that's up to the caller.
643 ^ static struct sset *getVacantSS(struct vars *, struct dfa *, chr *, chr *);
647 struct vars *const v, /* used only for debug flags */
654 struct arcp ap, lastap = {NULL, 0}; /* silence gcc 4 warning */
657 ss = pickNextSS(v, d, cp, start);
658 assert(!(ss->flags&LOCKED));
661 * Clear out its inarcs, including self-referential ones.
665 while ((p = ap.ss) != NULL) {
667 FDEBUG(("zapping c%d's %ld outarc\n", (int) (p - d->ssets), (long)co));
670 p->inchain[co].ss = NULL; /* paranoia */
675 * Take it off the inarc chains of the ssets reached by its outarcs.
678 for (i = 0; i < d->ncolors; i++) {
680 assert(p != ss); /* not self-referential */
682 continue; /* NOTE CONTINUE */
684 FDEBUG(("del outarc %d from c%d's in chn\n", i, (int) (p - d->ssets)));
685 if (p->ins.ss == ss && p->ins.co == i) {
686 p->ins = ss->inchain[i];
688 assert(p->ins.ss != NULL);
689 for (ap = p->ins; ap.ss != NULL && !(ap.ss == ss && ap.co == i);
690 ap = ap.ss->inchain[ap.co]) {
693 assert(ap.ss != NULL);
694 lastap.ss->inchain[lastap.co] = ss->inchain[i];
697 ss->inchain[i].ss = NULL;
701 * If ss was a success state, may need to remember location.
704 if ((ss->flags&POSTSTATE) && ss->lastseen != d->lastpost &&
705 (d->lastpost == NULL || d->lastpost < ss->lastseen)) {
706 d->lastpost = ss->lastseen;
710 * Likewise for a no-progress state.
713 if ((ss->flags&NOPROGRESS) && ss->lastseen != d->lastnopr &&
714 (d->lastnopr == NULL || d->lastnopr < ss->lastseen)) {
715 d->lastnopr = ss->lastseen;
722 - pickNextSS - pick the next stateset to be used
723 ^ static struct sset *pickNextSS(struct vars *, struct dfa *, chr *, chr *);
727 struct vars *const v, /* used only for debug flags */
733 struct sset *ss, *end;
737 * Shortcut for cases where cache isn't full.
740 if (d->nssused < d->nssets) {
744 FDEBUG(("new c%d\n", i));
750 ss->states = &d->statesarea[i * d->wordsper];
753 ss->ins.co = WHITE; /* give it some value */
754 ss->outs = &d->outsarea[i * d->ncolors];
755 ss->inchain = &d->incarea[i * d->ncolors];
756 for (i = 0; i < d->ncolors; i++) {
758 ss->inchain[i].ss = NULL;
764 * Look for oldest, or old enough anyway.
767 if (cp - start > d->nssets*2/3) { /* oldest 33% are expendable */
768 ancient = cp - d->nssets*2/3;
772 for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++) {
773 if ((ss->lastseen == NULL || ss->lastseen < ancient)
774 && !(ss->flags&LOCKED)) {
776 FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
780 for (ss = d->ssets, end = d->search; ss < end; ss++) {
781 if ((ss->lastseen == NULL || ss->lastseen < ancient)
782 && !(ss->flags&LOCKED)) {
784 FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
790 * Nobody's old enough?!? -- something's really wrong.
793 FDEBUG(("can't find victim to replace!\n"));