Import regex from tcl 8.4.5

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/RXSPENCER@3276 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Ryan Norton
1999-08-05 01:16:56 +00:00
parent 376c9a9ab8
commit 2e792cdea3
2 changed files with 818 additions and 824 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,7 @@
* DFA routines * DFA routines
* This file is #included by regexec.c. * This file is #included by regexec.c.
* *
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
* *
* Development of this software was funded, in part, by Cray Research Inc., * Development of this software was funded, in part, by Cray Research Inc.,
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
@@ -28,27 +28,27 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Header$
*
*/ */
/* /*
* longest - longest-preferred matching engine - longest - longest-preferred matching engine
^ static chr *longest(struct vars *, struct dfa *, chr *, chr *, int *);
*/ */
static chr * /* endpoint, or NULL */ static chr * /* endpoint, or NULL */
longest(struct vars * v, /* used only for debug and exec flags */ longest(v, d, start, stop, hitstopp)
struct dfa * d, struct vars *v; /* used only for debug and exec flags */
chr *start, /* where the match should start */ struct dfa *d;
chr *stop, /* match must end at or before here */ chr *start; /* where the match should start */
int *hitstopp) /* record whether hit v->stop, if non-NULL */ chr *stop; /* match must end at or before here */
int *hitstopp; /* record whether hit v->stop, if non-NULL */
{ {
chr *cp; chr *cp;
chr *realstop = (stop == v->stop) ? stop : stop + 1; chr *realstop = (stop == v->stop) ? stop : stop + 1;
color co; color co;
struct sset *css; struct sset *css;
struct sset *ss; struct sset *ss;
chr *post; chr *post;
int i; int i;
struct colormap *cm = d->cm; struct colormap *cm = d->cm;
/* initialize */ /* initialize */
@@ -59,15 +59,12 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* startup */ /* startup */
FDEBUG(("+++ startup +++\n")); FDEBUG(("+++ startup +++\n"));
if (cp == v->start) if (cp == v->start) {
{ co = d->cnfa->bos[(v->eflags&REG_NOTBOL) ? 0 : 1];
co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1]; FDEBUG(("color %ld\n", (long)co));
FDEBUG(("color %ld\n", (long) co)); } else {
}
else
{
co = GETCOLOR(cm, *(cp - 1)); co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co)); FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
} }
css = miss(v, d, css, co, cp, start); css = miss(v, d, css, co, cp, start);
if (css == NULL) if (css == NULL)
@@ -75,33 +72,29 @@ longest(struct vars * v, /* used only for debug and exec flags */
css->lastseen = cp; css->lastseen = cp;
/* main loop */ /* main loop */
if (v->eflags & REG_FTRACE) if (v->eflags&REG_FTRACE)
while (cp < realstop) while (cp < realstop) {
{
FDEBUG(("+++ at c%d +++\n", css - d->ssets)); FDEBUG(("+++ at c%d +++\n", css - d->ssets));
co = GETCOLOR(cm, *cp); co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co)); FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
ss = css->outs[co]; ss = css->outs[co];
if (ss == NULL) if (ss == NULL) {
{ ss = miss(v, d, css, co, cp+1, start);
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL) if (ss == NULL)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
cp++; cp++;
ss->lastseen = cp; ss->lastseen = cp;
css = ss; css = ss;
} }
else else
while (cp < realstop) while (cp < realstop) {
{
co = GETCOLOR(cm, *cp); co = GETCOLOR(cm, *cp);
ss = css->outs[co]; ss = css->outs[co];
if (ss == NULL) if (ss == NULL) {
{ ss = miss(v, d, css, co, cp+1, start);
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL) if (ss == NULL)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
cp++; cp++;
ss->lastseen = cp; ss->lastseen = cp;
@@ -110,15 +103,14 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* shutdown */ /* shutdown */
FDEBUG(("+++ shutdown at c%d +++\n", css - d->ssets)); FDEBUG(("+++ shutdown at c%d +++\n", css - d->ssets));
if (cp == v->stop && stop == v->stop) if (cp == v->stop && stop == v->stop) {
{
if (hitstopp != NULL) if (hitstopp != NULL)
*hitstopp = 1; *hitstopp = 1;
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1]; co = d->cnfa->eos[(v->eflags&REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co)); FDEBUG(("color %ld\n", (long)co));
ss = miss(v, d, css, co, cp, start); ss = miss(v, d, css, co, cp, start);
/* special case: match ended at eol? */ /* special case: match ended at eol? */
if (ss != NULL && (ss->flags & POSTSTATE)) if (ss != NULL && (ss->flags&POSTSTATE))
return cp; return cp;
else if (ss != NULL) else if (ss != NULL)
ss->lastseen = cp; /* to be tidy */ ss->lastseen = cp; /* to be tidy */
@@ -127,32 +119,34 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* find last match, if any */ /* find last match, if any */
post = d->lastpost; post = d->lastpost;
for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
if ((ss->flags & POSTSTATE) && post != ss->lastseen && if ((ss->flags&POSTSTATE) && post != ss->lastseen &&
(post == NULL || post < ss->lastseen)) (post == NULL || post < ss->lastseen))
post = ss->lastseen; post = ss->lastseen;
if (post != NULL) /* found one */ if (post != NULL) /* found one */
return post - 1; return post - 1;
return NULL; return NULL;
} }
/* /*
* shortest - shortest-preferred matching engine - shortest - shortest-preferred matching engine
^ static chr *shortest(struct vars *, struct dfa *, chr *, chr *, chr *,
^ chr **, int *);
*/ */
static chr * /* endpoint, or NULL */ static chr * /* endpoint, or NULL */
shortest(struct vars * v, shortest(v, d, start, min, max, coldp, hitstopp)
struct dfa * d, struct vars *v;
chr *start, /* where the match should start */ struct dfa *d;
chr *min, /* match must end at or after here */ chr *start; /* where the match should start */
chr *max, /* match must end at or before here */ chr *min; /* match must end at or after here */
chr **coldp, /* store coldstart pointer here, if chr *max; /* match must end at or before here */
* nonNULL */ chr **coldp; /* store coldstart pointer here, if nonNULL */
int *hitstopp) /* record whether hit v->stop, if non-NULL */ int *hitstopp; /* record whether hit v->stop, if non-NULL */
{ {
chr *cp; chr *cp;
chr *realmin = (min == v->stop) ? min : min + 1; chr *realmin = (min == v->stop) ? min : min + 1;
chr *realmax = (max == v->stop) ? max : max + 1; chr *realmax = (max == v->stop) ? max : max + 1;
color co; color co;
struct sset *css; struct sset *css;
struct sset *ss; struct sset *ss;
struct colormap *cm = d->cm; struct colormap *cm = d->cm;
@@ -165,15 +159,12 @@ shortest(struct vars * v,
/* startup */ /* startup */
FDEBUG(("--- startup ---\n")); FDEBUG(("--- startup ---\n"));
if (cp == v->start) if (cp == v->start) {
{ co = d->cnfa->bos[(v->eflags&REG_NOTBOL) ? 0 : 1];
co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1]; FDEBUG(("color %ld\n", (long)co));
FDEBUG(("color %ld\n", (long) co)); } else {
}
else
{
co = GETCOLOR(cm, *(cp - 1)); co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co)); FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
} }
css = miss(v, d, css, co, cp, start); css = miss(v, d, css, co, cp, start);
if (css == NULL) if (css == NULL)
@@ -182,116 +173,110 @@ shortest(struct vars * v,
ss = css; ss = css;
/* main loop */ /* main loop */
if (v->eflags & REG_FTRACE) if (v->eflags&REG_FTRACE)
while (cp < realmax) while (cp < realmax) {
{
FDEBUG(("--- at c%d ---\n", css - d->ssets)); FDEBUG(("--- at c%d ---\n", css - d->ssets));
co = GETCOLOR(cm, *cp); co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co)); FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
ss = css->outs[co]; ss = css->outs[co];
if (ss == NULL) if (ss == NULL) {
{ ss = miss(v, d, css, co, cp+1, start);
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL) if (ss == NULL)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
cp++; cp++;
ss->lastseen = cp; ss->lastseen = cp;
css = ss; css = ss;
if ((ss->flags & POSTSTATE) && cp >= realmin) if ((ss->flags&POSTSTATE) && cp >= realmin)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
else else
while (cp < realmax) while (cp < realmax) {
{
co = GETCOLOR(cm, *cp); co = GETCOLOR(cm, *cp);
ss = css->outs[co]; ss = css->outs[co];
if (ss == NULL) if (ss == NULL) {
{ ss = miss(v, d, css, co, cp+1, start);
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL) if (ss == NULL)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
cp++; cp++;
ss->lastseen = cp; ss->lastseen = cp;
css = ss; css = ss;
if ((ss->flags & POSTSTATE) && cp >= realmin) if ((ss->flags&POSTSTATE) && cp >= realmin)
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
if (ss == NULL) if (ss == NULL)
return NULL; return NULL;
if (coldp != NULL) /* report last no-progress state set, if if (coldp != NULL) /* report last no-progress state set, if any */
* any */
*coldp = lastcold(v, d); *coldp = lastcold(v, d);
if ((ss->flags & POSTSTATE) && cp > min) if ((ss->flags&POSTSTATE) && cp > min) {
{
assert(cp >= realmin); assert(cp >= realmin);
cp--; cp--;
} } else if (cp == v->stop && max == v->stop) {
else if (cp == v->stop && max == v->stop) co = d->cnfa->eos[(v->eflags&REG_NOTEOL) ? 0 : 1];
{ FDEBUG(("color %ld\n", (long)co));
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, start); ss = miss(v, d, css, co, cp, start);
/* match might have ended at eol */ /* match might have ended at eol */
if ((ss == NULL || !(ss->flags & POSTSTATE)) && hitstopp != NULL) if ((ss == NULL || !(ss->flags&POSTSTATE)) && hitstopp != NULL)
*hitstopp = 1; *hitstopp = 1;
} }
if (ss == NULL || !(ss->flags & POSTSTATE)) if (ss == NULL || !(ss->flags&POSTSTATE))
return NULL; return NULL;
return cp; return cp;
} }
/* /*
* lastcold - determine last point at which no progress had been made - lastcold - determine last point at which no progress had been made
^ static chr *lastcold(struct vars *, struct dfa *);
*/ */
static chr * /* endpoint, or NULL */ static chr * /* endpoint, or NULL */
lastcold(struct vars * v, lastcold(v, d)
struct dfa * d) struct vars *v;
struct dfa *d;
{ {
struct sset *ss; struct sset *ss;
chr *nopr; chr *nopr;
int i; int i;
nopr = d->lastnopr; nopr = d->lastnopr;
if (nopr == NULL) if (nopr == NULL)
nopr = v->start; nopr = v->start;
for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
if ((ss->flags & NOPROGRESS) && nopr < ss->lastseen) if ((ss->flags&NOPROGRESS) && nopr < ss->lastseen)
nopr = ss->lastseen; nopr = ss->lastseen;
return nopr; return nopr;
} }
/* /*
* newdfa - set up a fresh DFA - newdfa - set up a fresh DFA
^ static struct dfa *newdfa(struct vars *, struct cnfa *,
^ struct colormap *, struct smalldfa *);
*/ */
static struct dfa * static struct dfa *
newdfa(struct vars * v, newdfa(v, cnfa, cm, small)
struct cnfa * cnfa, struct vars *v;
struct colormap * cm, struct cnfa *cnfa;
struct smalldfa * small) /* preallocated space, may be NULL */ struct colormap *cm;
struct smalldfa *small; /* preallocated space, may be NULL */
{ {
struct dfa *d; struct dfa *d;
size_t nss = cnfa->nstates * 2; size_t nss = cnfa->nstates * 2;
int wordsper = (cnfa->nstates + UBITS - 1) / UBITS; int wordsper = (cnfa->nstates + UBITS - 1) / UBITS;
struct smalldfa *smallwas = small; struct smalldfa *smallwas = small;
assert(cnfa != NULL && cnfa->nstates != 0); assert(cnfa != NULL && cnfa->nstates != 0);
if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS) if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS) {
{
assert(wordsper == 1); assert(wordsper == 1);
if (small == NULL) if (small == NULL) {
{ small = (struct smalldfa *)MALLOC(
small = (struct smalldfa *) MALLOC( sizeof(struct smalldfa));
sizeof(struct smalldfa)); if (small == NULL) {
if (small == NULL)
{
ERR(REG_ESPACE); ERR(REG_ESPACE);
return NULL; return NULL;
} }
@@ -303,36 +288,32 @@ newdfa(struct vars * v,
d->outsarea = small->outsarea; d->outsarea = small->outsarea;
d->incarea = small->incarea; d->incarea = small->incarea;
d->cptsmalloced = 0; d->cptsmalloced = 0;
d->mallocarea = (smallwas == NULL) ? (char *) small : NULL; d->mallocarea = (smallwas == NULL) ? (char *)small : NULL;
} } else {
else d = (struct dfa *)MALLOC(sizeof(struct dfa));
{ if (d == NULL) {
d = (struct dfa *) MALLOC(sizeof(struct dfa));
if (d == NULL)
{
ERR(REG_ESPACE); ERR(REG_ESPACE);
return NULL; return NULL;
} }
d->ssets = (struct sset *) MALLOC(nss * sizeof(struct sset)); d->ssets = (struct sset *)MALLOC(nss * sizeof(struct sset));
d->statesarea = (unsigned *) MALLOC((nss + WORK) * wordsper * d->statesarea = (unsigned *)MALLOC((nss+WORK) * wordsper *
sizeof(unsigned)); sizeof(unsigned));
d->work = &d->statesarea[nss * wordsper]; d->work = &d->statesarea[nss * wordsper];
d->outsarea = (struct sset **) MALLOC(nss * cnfa->ncolors * d->outsarea = (struct sset **)MALLOC(nss * cnfa->ncolors *
sizeof(struct sset *)); sizeof(struct sset *));
d->incarea = (struct arcp *) MALLOC(nss * cnfa->ncolors * d->incarea = (struct arcp *)MALLOC(nss * cnfa->ncolors *
sizeof(struct arcp)); sizeof(struct arcp));
d->cptsmalloced = 1; d->cptsmalloced = 1;
d->mallocarea = (char *) d; d->mallocarea = (char *)d;
if (d->ssets == NULL || d->statesarea == NULL || if (d->ssets == NULL || d->statesarea == NULL ||
d->outsarea == NULL || d->incarea == NULL) d->outsarea == NULL || d->incarea == NULL) {
{
freedfa(d); freedfa(d);
ERR(REG_ESPACE); ERR(REG_ESPACE);
return NULL; return NULL;
} }
} }
d->nssets = (v->eflags & REG_SMALL) ? 7 : nss; d->nssets = (v->eflags&REG_SMALL) ? 7 : nss;
d->nssused = 0; d->nssused = 0;
d->nstates = cnfa->nstates; d->nstates = cnfa->nstates;
d->ncolors = cnfa->ncolors; d->ncolors = cnfa->ncolors;
@@ -349,13 +330,14 @@ newdfa(struct vars * v,
} }
/* /*
* freedfa - free a DFA - freedfa - free a DFA
^ static VOID freedfa(struct dfa *);
*/ */
static void static VOID
freedfa(struct dfa * d) freedfa(d)
struct dfa *d;
{ {
if (d->cptsmalloced) if (d->cptsmalloced) {
{
if (d->ssets != NULL) if (d->ssets != NULL)
FREE(d->ssets); FREE(d->ssets);
if (d->statesarea != NULL) if (d->statesarea != NULL)
@@ -371,16 +353,17 @@ freedfa(struct dfa * d)
} }
/* /*
* hash - construct a hash code for a bitvector - hash - construct a hash code for a bitvector
*
* There are probably better ways, but they're more expensive. * There are probably better ways, but they're more expensive.
^ static unsigned hash(unsigned *, int);
*/ */
static unsigned static unsigned
hash(unsigned *uv, hash(uv, n)
int n) unsigned *uv;
int n;
{ {
int i; int i;
unsigned h; unsigned h;
h = 0; h = 0;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
@@ -389,28 +372,29 @@ hash(unsigned *uv,
} }
/* /*
* initialize - hand-craft a cache entry for startup, otherwise get ready - initialize - hand-craft a cache entry for startup, otherwise get ready
^ static struct sset *initialize(struct vars *, struct dfa *, chr *);
*/ */
static struct sset * static struct sset *
initialize(struct vars * v, /* used only for debug flags */ initialize(v, d, start)
struct dfa * d, struct vars *v; /* used only for debug flags */
chr *start) struct dfa *d;
chr *start;
{ {
struct sset *ss; struct sset *ss;
int i; int i;
/* is previous one still there? */ /* is previous one still there? */
if (d->nssused > 0 && (d->ssets[0].flags & STARTER)) if (d->nssused > 0 && (d->ssets[0].flags&STARTER))
ss = &d->ssets[0]; ss = &d->ssets[0];
else else { /* no, must (re)build it */
{ /* no, must (re)build it */
ss = getvacant(v, d, start, start); ss = getvacant(v, d, start, start);
for (i = 0; i < d->wordsper; i++) for (i = 0; i < d->wordsper; i++)
ss->states[i] = 0; ss->states[i] = 0;
BSET(ss->states, d->cnfa->pre); BSET(ss->states, d->cnfa->pre);
ss->hash = HASH(ss->states, d->wordsper); ss->hash = HASH(ss->states, d->wordsper);
assert(d->cnfa->pre != d->cnfa->post); assert(d->cnfa->pre != d->cnfa->post);
ss->flags = STARTER | LOCKED | NOPROGRESS; ss->flags = STARTER|LOCKED|NOPROGRESS;
/* lastseen dealt with below */ /* lastseen dealt with below */
} }
@@ -423,30 +407,32 @@ initialize(struct vars * v, /* used only for debug flags */
} }
/* /*
* miss - handle a cache miss - miss - handle a cache miss
^ static struct sset *miss(struct vars *, struct dfa *, struct sset *,
^ pcolor, chr *, chr *);
*/ */
static struct sset * /* NULL if goes to empty set */ static struct sset * /* NULL if goes to empty set */
miss(struct vars * v, /* used only for debug flags */ miss(v, d, css, co, cp, start)
struct dfa * d, struct vars *v; /* used only for debug flags */
struct sset * css, struct dfa *d;
pcolor co, struct sset *css;
chr *cp, /* next chr */ pcolor co;
chr *start) /* where the attempt got started */ chr *cp; /* next chr */
chr *start; /* where the attempt got started */
{ {
struct cnfa *cnfa = d->cnfa; struct cnfa *cnfa = d->cnfa;
int i; int i;
unsigned h; unsigned h;
struct carc *ca; struct carc *ca;
struct sset *p; struct sset *p;
int ispost; int ispost;
int noprogress; int noprogress;
int gotstate; int gotstate;
int dolacons; int dolacons;
int sawlacons; int sawlacons;
/* for convenience, we can be called even if it might not be a miss */ /* for convenience, we can be called even if it might not be a miss */
if (css->outs[co] != NULL) if (css->outs[co] != NULL) {
{
FDEBUG(("hit\n")); FDEBUG(("hit\n"));
return css->outs[co]; return css->outs[co];
} }
@@ -460,9 +446,8 @@ miss(struct vars * v, /* used only for debug flags */
gotstate = 0; gotstate = 0;
for (i = 0; i < d->nstates; i++) for (i = 0; i < d->nstates; i++)
if (ISBSET(css->states, i)) if (ISBSET(css->states, i))
for (ca = cnfa->states[i] + 1; ca->co != COLORLESS; ca++) for (ca = cnfa->states[i]+1; ca->co != COLORLESS; ca++)
if (ca->co == co) if (ca->co == co) {
{
BSET(d->work, ca->to); BSET(d->work, ca->to);
gotstate = 1; gotstate = 1;
if (ca->to == cnfa->post) if (ca->to == cnfa->post)
@@ -471,23 +456,21 @@ miss(struct vars * v, /* used only for debug flags */
noprogress = 0; noprogress = 0;
FDEBUG(("%d -> %d\n", i, ca->to)); FDEBUG(("%d -> %d\n", i, ca->to));
} }
dolacons = (gotstate) ? (cnfa->flags & HASLACONS) : 0; dolacons = (gotstate) ? (cnfa->flags&HASLACONS) : 0;
sawlacons = 0; sawlacons = 0;
while (dolacons) while (dolacons) { /* transitive closure */
{ /* transitive closure */
dolacons = 0; dolacons = 0;
for (i = 0; i < d->nstates; i++) for (i = 0; i < d->nstates; i++)
if (ISBSET(d->work, i)) if (ISBSET(d->work, i))
for (ca = cnfa->states[i] + 1; ca->co != COLORLESS; for (ca = cnfa->states[i]+1; ca->co != COLORLESS;
ca++) ca++) {
{
if (ca->co <= cnfa->ncolors) if (ca->co <= cnfa->ncolors)
continue; /* NOTE CONTINUE */ continue; /* NOTE CONTINUE */
sawlacons = 1; sawlacons = 1;
if (ISBSET(d->work, ca->to)) if (ISBSET(d->work, ca->to))
continue; /* NOTE CONTINUE */ continue; /* NOTE CONTINUE */
if (!lacon(v, cnfa, cp, ca->co)) if (!lacon(v, cnfa, cp, ca->co))
continue; /* NOTE CONTINUE */ continue; /* NOTE CONTINUE */
BSET(d->work, ca->to); BSET(d->work, ca->to);
dolacons = 1; dolacons = 1;
if (ca->to == cnfa->post) if (ca->to == cnfa->post)
@@ -503,13 +486,11 @@ miss(struct vars * v, /* used only for debug flags */
/* next, is that in the cache? */ /* next, is that in the cache? */
for (p = d->ssets, i = d->nssused; i > 0; p++, i--) for (p = d->ssets, i = d->nssused; i > 0; p++, i--)
if (HIT(h, d->work, p, d->wordsper)) if (HIT(h, d->work, p, d->wordsper)) {
{
FDEBUG(("cached c%d\n", p - d->ssets)); FDEBUG(("cached c%d\n", p - d->ssets));
break; /* NOTE BREAK OUT */ break; /* NOTE BREAK OUT */
} }
if (i == 0) if (i == 0) { /* nope, need a new cache entry */
{ /* nope, need a new cache entry */
p = getvacant(v, d, cp, start); p = getvacant(v, d, cp, start);
assert(p != css); assert(p != css);
for (i = 0; i < d->wordsper; i++) for (i = 0; i < d->wordsper; i++)
@@ -521,97 +502,96 @@ miss(struct vars * v, /* used only for debug flags */
/* lastseen to be dealt with by caller */ /* lastseen to be dealt with by caller */
} }
if (!sawlacons) if (!sawlacons) { /* lookahead conds. always cache miss */
{ /* lookahead conds. always cache miss */
FDEBUG(("c%d[%d]->c%d\n", css - d->ssets, co, p - d->ssets)); FDEBUG(("c%d[%d]->c%d\n", css - d->ssets, co, p - d->ssets));
css->outs[co] = p; css->outs[co] = p;
css->inchain[co] = p->ins; css->inchain[co] = p->ins;
p->ins.ss = css; p->ins.ss = css;
p->ins.co = (color) co; p->ins.co = (color)co;
} }
return p; return p;
} }
/* /*
* lacon - lookahead-constraint checker for miss() - lacon - lookahead-constraint checker for miss()
^ static int lacon(struct vars *, struct cnfa *, chr *, pcolor);
*/ */
static int /* predicate: constraint satisfied? */ static int /* predicate: constraint satisfied? */
lacon(struct vars * v, lacon(v, pcnfa, cp, co)
struct cnfa * pcnfa, /* parent cnfa */ struct vars *v;
chr *cp, struct cnfa *pcnfa; /* parent cnfa */
pcolor co) /* "color" of the lookahead constraint */ chr *cp;
pcolor co; /* "color" of the lookahead constraint */
{ {
int n; int n;
struct subre *sub; struct subre *sub;
struct dfa *d; struct dfa *d;
struct smalldfa sd; struct smalldfa sd;
chr *end; chr *end;
n = co - pcnfa->ncolors; n = co - pcnfa->ncolors;
assert(n < v->g->nlacons && v->g->lacons != NULL); assert(n < v->g->nlacons && v->g->lacons != NULL);
FDEBUG(("=== testing lacon %d\n", n)); FDEBUG(("=== testing lacon %d\n", n));
sub = &v->g->lacons[n]; sub = &v->g->lacons[n];
d = newdfa(v, &sub->cnfa, &v->g->cmap, &sd); d = newdfa(v, &sub->cnfa, &v->g->cmap, &sd);
if (d == NULL) if (d == NULL) {
{
ERR(REG_ESPACE); ERR(REG_ESPACE);
return 0; return 0;
} }
end = longest(v, d, cp, v->stop, (int *) NULL); end = longest(v, d, cp, v->stop, (int *)NULL);
freedfa(d); freedfa(d);
FDEBUG(("=== lacon %d match %d\n", n, (end != NULL))); FDEBUG(("=== lacon %d match %d\n", n, (end != NULL)));
return (sub->subno) ? (end != NULL) : (end == NULL); return (sub->subno) ? (end != NULL) : (end == NULL);
} }
/* /*
* getvacant - get a vacant state set - getvacant - get a vacant state set
* This routine clears out the inarcs and outarcs, but does not otherwise * This routine clears out the inarcs and outarcs, but does not otherwise
* clear the innards of the state set -- that's up to the caller. * clear the innards of the state set -- that's up to the caller.
^ static struct sset *getvacant(struct vars *, struct dfa *, chr *, chr *);
*/ */
static struct sset * static struct sset *
getvacant(struct vars * v, /* used only for debug flags */ getvacant(v, d, cp, start)
struct dfa * d, struct vars *v; /* used only for debug flags */
chr *cp, struct dfa *d;
chr *start) chr *cp;
chr *start;
{ {
int i; int i;
struct sset *ss; struct sset *ss;
struct sset *p; struct sset *p;
struct arcp ap; struct arcp ap;
struct arcp lastap; struct arcp lastap;
color co; color co;
ss = pickss(v, d, cp, start); ss = pickss(v, d, cp, start);
assert(!(ss->flags & LOCKED)); assert(!(ss->flags&LOCKED));
/* clear out its inarcs, including self-referential ones */ /* clear out its inarcs, including self-referential ones */
ap = ss->ins; ap = ss->ins;
while ((p = ap.ss) != NULL) while ((p = ap.ss) != NULL) {
{
co = ap.co; co = ap.co;
FDEBUG(("zapping c%d's %ld outarc\n", p - d->ssets, (long) co)); FDEBUG(("zapping c%d's %ld outarc\n", p - d->ssets, (long)co));
p->outs[co] = NULL; p->outs[co] = NULL;
ap = p->inchain[co]; ap = p->inchain[co];
p->inchain[co].ss = NULL; /* paranoia */ p->inchain[co].ss = NULL; /* paranoia */
} }
ss->ins.ss = NULL; ss->ins.ss = NULL;
/* take it off the inarc chains of the ssets reached by its outarcs */ /* take it off the inarc chains of the ssets reached by its outarcs */
for (i = 0; i < d->ncolors; i++) for (i = 0; i < d->ncolors; i++) {
{
p = ss->outs[i]; p = ss->outs[i];
assert(p != ss); /* not self-referential */ assert(p != ss); /* not self-referential */
if (p == NULL) if (p == NULL)
continue; /* NOTE CONTINUE */ continue; /* NOTE CONTINUE */
FDEBUG(("del outarc %d from c%d's in chn\n", i, p - d->ssets)); FDEBUG(("del outarc %d from c%d's in chn\n", i, p - d->ssets));
if (p->ins.ss == ss && p->ins.co == i) if (p->ins.ss == ss && p->ins.co == i)
p->ins = ss->inchain[i]; p->ins = ss->inchain[i];
else else {
{
assert(p->ins.ss != NULL); assert(p->ins.ss != NULL);
for (ap = p->ins; ap.ss != NULL && for (ap = p->ins; ap.ss != NULL &&
!(ap.ss == ss && ap.co == i); !(ap.ss == ss && ap.co == i);
ap = ap.ss->inchain[ap.co]) ap = ap.ss->inchain[ap.co])
lastap = ap; lastap = ap;
assert(ap.ss != NULL); assert(ap.ss != NULL);
lastap.ss->inchain[lastap.co] = ss->inchain[i]; lastap.ss->inchain[lastap.co] = ss->inchain[i];
@@ -621,35 +601,36 @@ getvacant(struct vars * v, /* used only for debug flags */
} }
/* if ss was a success state, may need to remember location */ /* if ss was a success state, may need to remember location */
if ((ss->flags & POSTSTATE) && ss->lastseen != d->lastpost && if ((ss->flags&POSTSTATE) && ss->lastseen != d->lastpost &&
(d->lastpost == NULL || d->lastpost < ss->lastseen)) (d->lastpost == NULL || d->lastpost < ss->lastseen))
d->lastpost = ss->lastseen; d->lastpost = ss->lastseen;
/* likewise for a no-progress state */ /* likewise for a no-progress state */
if ((ss->flags & NOPROGRESS) && ss->lastseen != d->lastnopr && if ((ss->flags&NOPROGRESS) && ss->lastseen != d->lastnopr &&
(d->lastnopr == NULL || d->lastnopr < ss->lastseen)) (d->lastnopr == NULL || d->lastnopr < ss->lastseen))
d->lastnopr = ss->lastseen; d->lastnopr = ss->lastseen;
return ss; return ss;
} }
/* /*
* pickss - pick the next stateset to be used - pickss - pick the next stateset to be used
^ static struct sset *pickss(struct vars *, struct dfa *, chr *, chr *);
*/ */
static struct sset * static struct sset *
pickss(struct vars * v, /* used only for debug flags */ pickss(v, d, cp, start)
struct dfa * d, struct vars *v; /* used only for debug flags */
chr *cp, struct dfa *d;
chr *start) chr *cp;
chr *start;
{ {
int i; int i;
struct sset *ss; struct sset *ss;
struct sset *end; struct sset *end;
chr *ancient; chr *ancient;
/* shortcut for cases where cache isn't full */ /* shortcut for cases where cache isn't full */
if (d->nssused < d->nssets) if (d->nssused < d->nssets) {
{
i = d->nssused; i = d->nssused;
d->nssused++; d->nssused++;
ss = &d->ssets[i]; ss = &d->ssets[i];
@@ -661,8 +642,7 @@ pickss(struct vars * v, /* used only for debug flags */
ss->ins.co = WHITE; /* give it some value */ ss->ins.co = WHITE; /* give it some value */
ss->outs = &d->outsarea[i * d->ncolors]; ss->outs = &d->outsarea[i * d->ncolors];
ss->inchain = &d->incarea[i * d->ncolors]; ss->inchain = &d->incarea[i * d->ncolors];
for (i = 0; i < d->ncolors; i++) for (i = 0; i < d->ncolors; i++) {
{
ss->outs[i] = NULL; ss->outs[i] = NULL;
ss->inchain[i].ss = NULL; ss->inchain[i].ss = NULL;
} }
@@ -670,22 +650,20 @@ pickss(struct vars * v, /* used only for debug flags */
} }
/* look for oldest, or old enough anyway */ /* look for oldest, or old enough anyway */
if (cp - start > d->nssets * 2 / 3) /* oldest 33% are expendable */ if (cp - start > d->nssets*2/3) /* oldest 33% are expendable */
ancient = cp - d->nssets * 2 / 3; ancient = cp - d->nssets*2/3;
else else
ancient = start; ancient = start;
for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++) for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) && if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED)) !(ss->flags&LOCKED)) {
{
d->search = ss + 1; d->search = ss + 1;
FDEBUG(("replacing c%d\n", ss - d->ssets)); FDEBUG(("replacing c%d\n", ss - d->ssets));
return ss; return ss;
} }
for (ss = d->ssets, end = d->search; ss < end; ss++) for (ss = d->ssets, end = d->search; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) && if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED)) !(ss->flags&LOCKED)) {
{
d->search = ss + 1; d->search = ss + 1;
FDEBUG(("replacing c%d\n", ss - d->ssets)); FDEBUG(("replacing c%d\n", ss - d->ssets));
return ss; return ss;