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

@@ -28,19 +28,19 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* 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 */
longest(struct vars * v, /* used only for debug and exec flags */
struct dfa * d,
chr *start, /* where the match should start */
chr *stop, /* match must end at or before here */
int *hitstopp) /* record whether hit v->stop, if non-NULL */
longest(v, d, start, stop, hitstopp)
struct vars *v; /* used only for debug and exec flags */
struct dfa *d;
chr *start; /* where the match should start */
chr *stop; /* match must end at or before here */
int *hitstopp; /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realstop = (stop == v->stop) ? stop : stop + 1;
@@ -59,13 +59,10 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* startup */
FDEBUG(("+++ startup +++\n"));
if (cp == v->start)
{
if (cp == v->start) {
co = d->cnfa->bos[(v->eflags&REG_NOTBOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long)co));
}
else
{
} else {
co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
}
@@ -76,14 +73,12 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* main loop */
if (v->eflags&REG_FTRACE)
while (cp < realstop)
{
while (cp < realstop) {
FDEBUG(("+++ at c%d +++\n", css - d->ssets));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
ss = css->outs[co];
if (ss == NULL)
{
if (ss == NULL) {
ss = miss(v, d, css, co, cp+1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
@@ -93,12 +88,10 @@ longest(struct vars * v, /* used only for debug and exec flags */
css = ss;
}
else
while (cp < realstop)
{
while (cp < realstop) {
co = GETCOLOR(cm, *cp);
ss = css->outs[co];
if (ss == NULL)
{
if (ss == NULL) {
ss = miss(v, d, css, co, cp+1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
@@ -110,8 +103,7 @@ longest(struct vars * v, /* used only for debug and exec flags */
/* shutdown */
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)
*hitstopp = 1;
co = d->cnfa->eos[(v->eflags&REG_NOTEOL) ? 0 : 1];
@@ -137,17 +129,19 @@ longest(struct vars * v, /* used only for debug and exec flags */
}
/*
* 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 */
shortest(struct vars * v,
struct dfa * d,
chr *start, /* where the match should start */
chr *min, /* match must end at or after here */
chr *max, /* match must end at or before here */
chr **coldp, /* store coldstart pointer here, if
* nonNULL */
int *hitstopp) /* record whether hit v->stop, if non-NULL */
shortest(v, d, start, min, max, coldp, hitstopp)
struct vars *v;
struct dfa *d;
chr *start; /* where the match should start */
chr *min; /* match must end at or after here */
chr *max; /* match must end at or before here */
chr **coldp; /* store coldstart pointer here, if nonNULL */
int *hitstopp; /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realmin = (min == v->stop) ? min : min + 1;
@@ -165,13 +159,10 @@ shortest(struct vars * v,
/* startup */
FDEBUG(("--- startup ---\n"));
if (cp == v->start)
{
if (cp == v->start) {
co = d->cnfa->bos[(v->eflags&REG_NOTBOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long)co));
}
else
{
} else {
co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char)*(cp-1), (long)co));
}
@@ -183,14 +174,12 @@ shortest(struct vars * v,
/* main loop */
if (v->eflags&REG_FTRACE)
while (cp < realmax)
{
while (cp < realmax) {
FDEBUG(("--- at c%d ---\n", css - d->ssets));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char)*cp, (long)co));
ss = css->outs[co];
if (ss == NULL)
{
if (ss == NULL) {
ss = miss(v, d, css, co, cp+1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
@@ -202,12 +191,10 @@ shortest(struct vars * v,
break; /* NOTE BREAK OUT */
}
else
while (cp < realmax)
{
while (cp < realmax) {
co = GETCOLOR(cm, *cp);
ss = css->outs[co];
if (ss == NULL)
{
if (ss == NULL) {
ss = miss(v, d, css, co, cp+1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
@@ -222,17 +209,13 @@ shortest(struct vars * v,
if (ss == NULL)
return NULL;
if (coldp != NULL) /* report last no-progress state set, if
* any */
if (coldp != NULL) /* report last no-progress state set, if any */
*coldp = lastcold(v, d);
if ((ss->flags & POSTSTATE) && cp > min)
{
if ((ss->flags&POSTSTATE) && cp > min) {
assert(cp >= realmin);
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));
ss = miss(v, d, css, co, cp, start);
@@ -248,11 +231,13 @@ shortest(struct vars * v,
}
/*
* 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 */
lastcold(struct vars * v,
struct dfa * d)
lastcold(v, d)
struct vars *v;
struct dfa *d;
{
struct sset *ss;
chr *nopr;
@@ -268,13 +253,16 @@ lastcold(struct vars * v,
}
/*
* 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 *
newdfa(struct vars * v,
struct cnfa * cnfa,
struct colormap * cm,
struct smalldfa * small) /* preallocated space, may be NULL */
newdfa(v, cnfa, cm, small)
struct vars *v;
struct cnfa *cnfa;
struct colormap *cm;
struct smalldfa *small; /* preallocated space, may be NULL */
{
struct dfa *d;
size_t nss = cnfa->nstates * 2;
@@ -283,15 +271,12 @@ newdfa(struct vars * v,
assert(cnfa != NULL && cnfa->nstates != 0);
if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS)
{
if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS) {
assert(wordsper == 1);
if (small == NULL)
{
if (small == NULL) {
small = (struct smalldfa *)MALLOC(
sizeof(struct smalldfa));
if (small == NULL)
{
if (small == NULL) {
ERR(REG_ESPACE);
return NULL;
}
@@ -304,12 +289,9 @@ newdfa(struct vars * v,
d->incarea = small->incarea;
d->cptsmalloced = 0;
d->mallocarea = (smallwas == NULL) ? (char *)small : NULL;
}
else
{
} else {
d = (struct dfa *)MALLOC(sizeof(struct dfa));
if (d == NULL)
{
if (d == NULL) {
ERR(REG_ESPACE);
return NULL;
}
@@ -324,8 +306,7 @@ newdfa(struct vars * v,
d->cptsmalloced = 1;
d->mallocarea = (char *)d;
if (d->ssets == NULL || d->statesarea == NULL ||
d->outsarea == NULL || d->incarea == NULL)
{
d->outsarea == NULL || d->incarea == NULL) {
freedfa(d);
ERR(REG_ESPACE);
return NULL;
@@ -349,13 +330,14 @@ newdfa(struct vars * v,
}
/*
* freedfa - free a DFA
- freedfa - free a DFA
^ static VOID freedfa(struct dfa *);
*/
static void
freedfa(struct dfa * d)
{
if (d->cptsmalloced)
static VOID
freedfa(d)
struct dfa *d;
{
if (d->cptsmalloced) {
if (d->ssets != NULL)
FREE(d->ssets);
if (d->statesarea != NULL)
@@ -371,13 +353,14 @@ 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.
^ static unsigned hash(unsigned *, int);
*/
static unsigned
hash(unsigned *uv,
int n)
hash(uv, n)
unsigned *uv;
int n;
{
int i;
unsigned h;
@@ -389,12 +372,14 @@ 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 *
initialize(struct vars * v, /* used only for debug flags */
struct dfa * d,
chr *start)
initialize(v, d, start)
struct vars *v; /* used only for debug flags */
struct dfa *d;
chr *start;
{
struct sset *ss;
int i;
@@ -402,8 +387,7 @@ initialize(struct vars * v, /* used only for debug flags */
/* is previous one still there? */
if (d->nssused > 0 && (d->ssets[0].flags&STARTER))
ss = &d->ssets[0];
else
{ /* no, must (re)build it */
else { /* no, must (re)build it */
ss = getvacant(v, d, start, start);
for (i = 0; i < d->wordsper; i++)
ss->states[i] = 0;
@@ -423,15 +407,18 @@ 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 */
miss(struct vars * v, /* used only for debug flags */
struct dfa * d,
struct sset * css,
pcolor co,
chr *cp, /* next chr */
chr *start) /* where the attempt got started */
miss(v, d, css, co, cp, start)
struct vars *v; /* used only for debug flags */
struct dfa *d;
struct sset *css;
pcolor co;
chr *cp; /* next chr */
chr *start; /* where the attempt got started */
{
struct cnfa *cnfa = d->cnfa;
int i;
@@ -445,8 +432,7 @@ miss(struct vars * v, /* used only for debug flags */
int sawlacons;
/* 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"));
return css->outs[co];
}
@@ -461,8 +447,7 @@ miss(struct vars * v, /* used only for debug flags */
for (i = 0; i < d->nstates; i++)
if (ISBSET(css->states, i))
for (ca = cnfa->states[i]+1; ca->co != COLORLESS; ca++)
if (ca->co == co)
{
if (ca->co == co) {
BSET(d->work, ca->to);
gotstate = 1;
if (ca->to == cnfa->post)
@@ -473,14 +458,12 @@ miss(struct vars * v, /* used only for debug flags */
}
dolacons = (gotstate) ? (cnfa->flags&HASLACONS) : 0;
sawlacons = 0;
while (dolacons)
{ /* transitive closure */
while (dolacons) { /* transitive closure */
dolacons = 0;
for (i = 0; i < d->nstates; i++)
if (ISBSET(d->work, i))
for (ca = cnfa->states[i]+1; ca->co != COLORLESS;
ca++)
{
ca++) {
if (ca->co <= cnfa->ncolors)
continue; /* NOTE CONTINUE */
sawlacons = 1;
@@ -503,13 +486,11 @@ miss(struct vars * v, /* used only for debug flags */
/* next, is that in the cache? */
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));
break; /* NOTE BREAK OUT */
}
if (i == 0)
{ /* nope, need a new cache entry */
if (i == 0) { /* nope, need a new cache entry */
p = getvacant(v, d, cp, start);
assert(p != css);
for (i = 0; i < d->wordsper; i++)
@@ -521,8 +502,7 @@ miss(struct vars * v, /* used only for debug flags */
/* lastseen to be dealt with by caller */
}
if (!sawlacons)
{ /* lookahead conds. always cache miss */
if (!sawlacons) { /* lookahead conds. always cache miss */
FDEBUG(("c%d[%d]->c%d\n", css - d->ssets, co, p - d->ssets));
css->outs[co] = p;
css->inchain[co] = p->ins;
@@ -533,13 +513,15 @@ miss(struct vars * v, /* used only for debug flags */
}
/*
* 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? */
lacon(struct vars * v,
struct cnfa * pcnfa, /* parent cnfa */
chr *cp,
pcolor co) /* "color" of the lookahead constraint */
lacon(v, pcnfa, cp, co)
struct vars *v;
struct cnfa *pcnfa; /* parent cnfa */
chr *cp;
pcolor co; /* "color" of the lookahead constraint */
{
int n;
struct subre *sub;
@@ -552,8 +534,7 @@ lacon(struct vars * v,
FDEBUG(("=== testing lacon %d\n", n));
sub = &v->g->lacons[n];
d = newdfa(v, &sub->cnfa, &v->g->cmap, &sd);
if (d == NULL)
{
if (d == NULL) {
ERR(REG_ESPACE);
return 0;
}
@@ -564,15 +545,17 @@ lacon(struct vars * v,
}
/*
* getvacant - get a vacant state set
- getvacant - get a vacant state set
* 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.
^ static struct sset *getvacant(struct vars *, struct dfa *, chr *, chr *);
*/
static struct sset *
getvacant(struct vars * v, /* used only for debug flags */
struct dfa * d,
chr *cp,
chr *start)
getvacant(v, d, cp, start)
struct vars *v; /* used only for debug flags */
struct dfa *d;
chr *cp;
chr *start;
{
int i;
struct sset *ss;
@@ -586,8 +569,7 @@ getvacant(struct vars * v, /* used only for debug flags */
/* clear out its inarcs, including self-referential ones */
ap = ss->ins;
while ((p = ap.ss) != NULL)
{
while ((p = ap.ss) != NULL) {
co = ap.co;
FDEBUG(("zapping c%d's %ld outarc\n", p - d->ssets, (long)co));
p->outs[co] = NULL;
@@ -597,8 +579,7 @@ getvacant(struct vars * v, /* used only for debug flags */
ss->ins.ss = NULL;
/* 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];
assert(p != ss); /* not self-referential */
if (p == NULL)
@@ -606,8 +587,7 @@ getvacant(struct vars * v, /* used only for debug flags */
FDEBUG(("del outarc %d from c%d's in chn\n", i, p - d->ssets));
if (p->ins.ss == ss && p->ins.co == i)
p->ins = ss->inchain[i];
else
{
else {
assert(p->ins.ss != NULL);
for (ap = p->ins; ap.ss != NULL &&
!(ap.ss == ss && ap.co == i);
@@ -634,13 +614,15 @@ getvacant(struct vars * v, /* used only for debug flags */
}
/*
* 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 *
pickss(struct vars * v, /* used only for debug flags */
struct dfa * d,
chr *cp,
chr *start)
pickss(v, d, cp, start)
struct vars *v; /* used only for debug flags */
struct dfa *d;
chr *cp;
chr *start;
{
int i;
struct sset *ss;
@@ -648,8 +630,7 @@ pickss(struct vars * v, /* used only for debug flags */
chr *ancient;
/* shortcut for cases where cache isn't full */
if (d->nssused < d->nssets)
{
if (d->nssused < d->nssets) {
i = d->nssused;
d->nssused++;
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->outs = &d->outsarea[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->inchain[i].ss = NULL;
}
@@ -676,16 +656,14 @@ pickss(struct vars * v, /* used only for debug flags */
ancient = start;
for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED))
{
!(ss->flags&LOCKED)) {
d->search = ss + 1;
FDEBUG(("replacing c%d\n", ss - d->ssets));
return ss;
}
for (ss = d->ssets, end = d->search; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED))
{
!(ss->flags&LOCKED)) {
d->search = ss + 1;
FDEBUG(("replacing c%d\n", ss - d->ssets));
return ss;