PostgreSQL Source Code  git master
regc_nfa.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NISERR()   VISERR(nfa->v)
 
#define NERR(e)   VERR(nfa->v, (e))
 
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)    ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))
 
#define CA(ct, at)   (((ct)<<CHAR_BIT) | (at))
 

Functions

static struct nfanewnfa (struct vars *v, struct colormap *cm, struct nfa *parent)
 
static void freenfa (struct nfa *nfa)
 
static struct statenewstate (struct nfa *nfa)
 
static struct statenewfstate (struct nfa *nfa, int flag)
 
static void dropstate (struct nfa *nfa, struct state *s)
 
static void freestate (struct nfa *nfa, struct state *s)
 
static void newarc (struct nfa *nfa, int t, color co, struct state *from, struct state *to)
 
static void createarc (struct nfa *nfa, int t, color co, struct state *from, struct state *to)
 
static struct arcallocarc (struct nfa *nfa)
 
static void freearc (struct nfa *nfa, struct arc *victim)
 
static void changearcsource (struct arc *a, struct state *newfrom)
 
static void changearctarget (struct arc *a, struct state *newto)
 
static int hasnonemptyout (struct state *s)
 
static struct arcfindarc (struct state *s, int type, color co)
 
static void cparc (struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
 
static void sortins (struct nfa *nfa, struct state *s)
 
static int sortins_cmp (const void *a, const void *b)
 
static void sortouts (struct nfa *nfa, struct state *s)
 
static int sortouts_cmp (const void *a, const void *b)
 
static void moveins (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void copyins (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void mergeins (struct nfa *nfa, struct state *s, struct arc **arcarray, int arccount)
 
static void moveouts (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void copyouts (struct nfa *nfa, struct state *oldState, struct state *newState)
 
static void cloneouts (struct nfa *nfa, struct state *old, struct state *from, struct state *to, int type)
 
static void delsub (struct nfa *nfa, struct state *lp, struct state *rp)
 
static void deltraverse (struct nfa *nfa, struct state *leftend, struct state *s)
 
static void dupnfa (struct nfa *nfa, struct state *start, struct state *stop, struct state *from, struct state *to)
 
static void duptraverse (struct nfa *nfa, struct state *s, struct state *stmp)
 
static void removeconstraints (struct nfa *nfa, struct state *start, struct state *stop)
 
static void removetraverse (struct nfa *nfa, struct state *s)
 
static void cleartraverse (struct nfa *nfa, struct state *s)
 
static struct statesingle_color_transition (struct state *s1, struct state *s2)
 
static void specialcolors (struct nfa *nfa)
 
static long optimize (struct nfa *nfa, FILE *f)
 
static void pullback (struct nfa *nfa, FILE *f)
 
static int pull (struct nfa *nfa, struct arc *con, struct state **intermediates)
 
static void pushfwd (struct nfa *nfa, FILE *f)
 
static int push (struct nfa *nfa, struct arc *con, struct state **intermediates)
 
static int combine (struct nfa *nfa, struct arc *con, struct arc *a)
 
static void fixempties (struct nfa *nfa, FILE *f)
 
static struct stateemptyreachable (struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
 
static int isconstraintarc (struct arc *a)
 
static int hasconstraintout (struct state *s)
 
static void fixconstraintloops (struct nfa *nfa, FILE *f)
 
static int findconstraintloop (struct nfa *nfa, struct state *s)
 
static void breakconstraintloop (struct nfa *nfa, struct state *sinitial)
 
static void clonesuccessorstates (struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
 
static void cleanup (struct nfa *nfa)
 
static void markreachable (struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
 
static void markcanreach (struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
 
static long analyze (struct nfa *nfa)
 
static void checkmatchall (struct nfa *nfa)
 
static bool checkmatchall_recurse (struct nfa *nfa, struct state *s, bool **haspaths)
 
static bool check_out_colors_match (struct state *s, color co1, color co2)
 
static bool check_in_colors_match (struct state *s, color co1, color co2)
 
static void compact (struct nfa *nfa, struct cnfa *cnfa)
 
static void carcsort (struct carc *first, size_t n)
 
static int carc_cmp (const void *a, const void *b)
 
static void freecnfa (struct cnfa *cnfa)
 
static void dumpnfa (struct nfa *nfa, FILE *f)
 

Macro Definition Documentation

◆ BULK_ARC_OP_USE_SORT

#define BULK_ARC_OP_USE_SORT (   nsrcarcs,
  ndestarcs 
)     ((nsrcarcs) < 4 ? 0 : ((nsrcarcs) > 32 || (ndestarcs) > 32))

Definition at line 758 of file regc_nfa.c.

◆ CA

#define CA (   ct,
  at 
)    (((ct)<<CHAR_BIT) | (at))

◆ NERR

#define NERR (   e)    VERR(nfa->v, (e))

Definition at line 40 of file regc_nfa.c.

◆ NISERR

#define NISERR ( )    VISERR(nfa->v)

Definition at line 39 of file regc_nfa.c.

Function Documentation

◆ allocarc()

static struct arc* allocarc ( struct nfa nfa)
static

Definition at line 368 of file regc_nfa.c.

369 {
370  struct arc *a;
371 
372  /* first, recycle anything that's on the freelist */
373  if (nfa->freearcs != NULL)
374  {
375  a = nfa->freearcs;
376  nfa->freearcs = a->freechain;
377  }
378  /* otherwise, is there anything left in the last arcbatch? */
379  else if (nfa->lastab != NULL && nfa->lastabused < nfa->lastab->narcs)
380  {
381  a = &nfa->lastab->a[nfa->lastabused++];
382  }
383  /* otherwise, need to allocate a new arcbatch */
384  else
385  {
386  struct arcbatch *newAb;
387  size_t narcs;
388 
390  {
391  NERR(REG_ETOOBIG);
392  return NULL;
393  }
394  narcs = (nfa->lastab != NULL) ? nfa->lastab->narcs * 2 : FIRSTABSIZE;
395  if (narcs > MAXABSIZE)
396  narcs = MAXABSIZE;
397  newAb = (struct arcbatch *) MALLOC(ARCBATCHSIZE(narcs));
398  if (newAb == NULL)
399  {
400  NERR(REG_ESPACE);
401  return NULL;
402  }
404  newAb->narcs = narcs;
405  newAb->next = nfa->lastab;
406  nfa->lastab = newAb;
407  nfa->lastabused = 1;
408  a = &newAb->a[0];
409  }
410 
411  return a;
412 }
int a
Definition: isn.c:69
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define NERR(e)
Definition: regc_nfa.c:40
#define MALLOC(n)
Definition: regcustom.h:52
#define REG_ETOOBIG
Definition: regex.h:155
#define REG_ESPACE
Definition: regex.h:149
#define ARCBATCHSIZE(n)
Definition: regguts.h:317
#define MAXABSIZE
Definition: regguts.h:320
#define FIRSTABSIZE
Definition: regguts.h:319
#define REG_MAX_COMPILE_SPACE
Definition: regguts.h:449
Definition: regguts.h:296
struct arcbatch * next
Definition: regguts.h:313
size_t narcs
Definition: regguts.h:314
struct arc a[FLEXIBLE_ARRAY_MEMBER]
Definition: regguts.h:315
Definition: regguts.h:349
struct vars * v
Definition: regguts.h:369
size_t lastabused
Definition: regguts.h:362
struct arc * freearcs
Definition: regguts.h:358
struct arcbatch * lastab
Definition: regguts.h:360
size_t spaceused
Definition: regcomp.c:308

References a, arcbatch::a, ARCBATCHSIZE, FIRSTABSIZE, nfa::freearcs, if(), nfa::lastab, nfa::lastabused, MALLOC, MAXABSIZE, arcbatch::narcs, NERR, arcbatch::next, REG_ESPACE, REG_ETOOBIG, REG_MAX_COMPILE_SPACE, vars::spaceused, and nfa::v.

Referenced by createarc().

◆ analyze()

static long analyze ( struct nfa nfa)
static

Definition at line 3016 of file regc_nfa.c.

3017 {
3018  struct arc *a;
3019  struct arc *aa;
3020 
3021  if (NISERR())
3022  return 0;
3023 
3024  /* Detect whether NFA can't match anything */
3025  if (nfa->pre->outs == NULL)
3026  return REG_UIMPOSSIBLE;
3027 
3028  /* Detect whether NFA matches all strings (possibly with length bounds) */
3029  checkmatchall(nfa);
3030 
3031  /* Detect whether NFA can possibly match a zero-length string */
3032  for (a = nfa->pre->outs; a != NULL; a = a->outchain)
3033  for (aa = a->to->outs; aa != NULL; aa = aa->outchain)
3034  if (aa->to == nfa->post)
3035  return REG_UEMPTYMATCH;
3036  return 0;
3037 }
#define NISERR()
Definition: regc_nfa.c:39
static void checkmatchall(struct nfa *nfa)
Definition: regc_nfa.c:3062
#define REG_UEMPTYMATCH
Definition: regex.h:71
#define REG_UIMPOSSIBLE
Definition: regex.h:72
struct arc * outchain
Definition: regguts.h:301
struct state * to
Definition: regguts.h:300
struct state * post
Definition: regguts.h:353
struct state * pre
Definition: regguts.h:350
struct arc * outs
Definition: regguts.h:330

References a, checkmatchall(), NISERR, arc::outchain, state::outs, nfa::post, nfa::pre, REG_UEMPTYMATCH, REG_UIMPOSSIBLE, and arc::to.

Referenced by ExecVacuum(), GetCommandLogLevel(), and optimize().

◆ breakconstraintloop()

static void breakconstraintloop ( struct nfa nfa,
struct state sinitial 
)
static

Definition at line 2551 of file regc_nfa.c.

2552 {
2553  struct state *s;
2554  struct state *shead;
2555  struct state *stail;
2556  struct state *sclone;
2557  struct state *nexts;
2558  struct arc *refarc;
2559  struct arc *a;
2560  struct arc *nexta;
2561 
2562  /*
2563  * Start by identifying which loop step we want to break at.
2564  * Preferentially this is one with only one constraint arc. (XXX are
2565  * there any other secondary heuristics we want to use here?) Set refarc
2566  * to point to the selected lone constraint arc, if there is one.
2567  */
2568  refarc = NULL;
2569  s = sinitial;
2570  do
2571  {
2572  nexts = s->tmp;
2573  assert(nexts != s); /* should not see any one-element loops */
2574  if (refarc == NULL)
2575  {
2576  int narcs = 0;
2577 
2578  for (a = s->outs; a != NULL; a = a->outchain)
2579  {
2580  if (a->to == nexts && isconstraintarc(a))
2581  {
2582  refarc = a;
2583  narcs++;
2584  }
2585  }
2586  assert(narcs > 0);
2587  if (narcs > 1)
2588  refarc = NULL; /* multiple constraint arcs here, no good */
2589  }
2590  s = nexts;
2591  } while (s != sinitial);
2592 
2593  if (refarc)
2594  {
2595  /* break at the refarc */
2596  shead = refarc->from;
2597  stail = refarc->to;
2598  assert(stail == shead->tmp);
2599  }
2600  else
2601  {
2602  /* for lack of a better idea, break after sinitial */
2603  shead = sinitial;
2604  stail = sinitial->tmp;
2605  }
2606 
2607  /*
2608  * Reset the tmp fields so that we can use them for local storage in
2609  * clonesuccessorstates. (findconstraintloop won't mind, since it's just
2610  * going to abandon its search anyway.)
2611  */
2612  for (s = nfa->states; s != NULL; s = s->next)
2613  s->tmp = NULL;
2614 
2615  /*
2616  * Recursively build clone state(s) as needed.
2617  */
2618  sclone = newstate(nfa);
2619  if (sclone == NULL)
2620  {
2621  assert(NISERR());
2622  return;
2623  }
2624 
2625  clonesuccessorstates(nfa, stail, sclone, shead, refarc,
2626  NULL, NULL, nfa->nstates);
2627 
2628  if (NISERR())
2629  return;
2630 
2631  /*
2632  * It's possible that sclone has no outarcs at all, in which case it's
2633  * useless. (We don't try extremely hard to get rid of useless states
2634  * here, but this is an easy and fairly common case.)
2635  */
2636  if (sclone->nouts == 0)
2637  {
2638  freestate(nfa, sclone);
2639  sclone = NULL;
2640  }
2641 
2642  /*
2643  * Move shead's constraint-loop arcs to point to sclone, or just drop them
2644  * if we discovered we don't need sclone.
2645  */
2646  for (a = shead->outs; a != NULL; a = nexta)
2647  {
2648  nexta = a->outchain;
2649  if (a->to == stail && isconstraintarc(a))
2650  {
2651  if (sclone)
2652  cparc(nfa, a, shead, sclone);
2653  freearc(nfa, a);
2654  if (NISERR())
2655  break;
2656  }
2657  }
2658 }
static void cparc(struct nfa *nfa, struct arc *oa, struct state *from, struct state *to)
Definition: regc_nfa.c:608
static void clonesuccessorstates(struct nfa *nfa, struct state *ssource, struct state *sclone, struct state *spredecessor, struct arc *refarc, char *curdonemap, char *outerdonemap, int nstates)
Definition: regc_nfa.c:2697
static int isconstraintarc(struct arc *a)
Definition: regc_nfa.c:2324
static void freearc(struct nfa *nfa, struct arc *victim)
Definition: regc_nfa.c:418
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
static void freestate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:242
#define assert(x)
Definition: regcustom.h:56
struct state * from
Definition: regguts.h:299
int nstates
Definition: regguts.h:354
struct state * states
Definition: regguts.h:355
Definition: regguts.h:323
int nouts
Definition: regguts.h:328
struct state * tmp
Definition: regguts.h:331
struct state * next
Definition: regguts.h:332

References a, assert, clonesuccessorstates(), cparc(), freearc(), freestate(), arc::from, isconstraintarc(), newstate(), state::next, NISERR, state::nouts, nfa::nstates, state::outs, nfa::states, state::tmp, and arc::to.

Referenced by findconstraintloop().

◆ carc_cmp()

static int carc_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 3577 of file regc_nfa.c.

3578 {
3579  const struct carc *aa = (const struct carc *) a;
3580  const struct carc *bb = (const struct carc *) b;
3581 
3582  if (aa->co < bb->co)
3583  return -1;
3584  if (aa->co > bb->co)
3585  return +1;
3586  if (aa->to < bb->to)
3587  return -1;
3588  if (aa->to > bb->to)
3589  return +1;
3590  /* This is unreached, since there should be no duplicate arcs now: */
3591  return 0;
3592 }
int b
Definition: isn.c:70
Definition: regguts.h:401
int to
Definition: regguts.h:403
color co
Definition: regguts.h:402

References a, b, carc::co, and carc::to.

Referenced by carcsort().

◆ carcsort()

static void carcsort ( struct carc first,
size_t  n 
)
static

Definition at line 3570 of file regc_nfa.c.

3571 {
3572  if (n > 1)
3573  qsort(first, n, sizeof(struct carc), carc_cmp);
3574 }
#define qsort(a, b, c, d)
Definition: port.h:445
static int carc_cmp(const void *a, const void *b)
Definition: regc_nfa.c:3577

References carc_cmp(), and qsort.

Referenced by compact().

◆ changearcsource()

static void changearcsource ( struct arc a,
struct state newfrom 
)
static

Definition at line 489 of file regc_nfa.c.

490 {
491  struct state *oldfrom = a->from;
492  struct arc *predecessor;
493 
494  assert(oldfrom != newfrom);
495 
496  /* take it off old source's out-chain */
497  assert(oldfrom != NULL);
498  predecessor = a->outchainRev;
499  if (predecessor == NULL)
500  {
501  assert(oldfrom->outs == a);
502  oldfrom->outs = a->outchain;
503  }
504  else
505  {
506  assert(predecessor->outchain == a);
507  predecessor->outchain = a->outchain;
508  }
509  if (a->outchain != NULL)
510  {
511  assert(a->outchain->outchainRev == a);
512  a->outchain->outchainRev = predecessor;
513  }
514  oldfrom->nouts--;
515 
516  a->from = newfrom;
517 
518  /* prepend it to new source's out-chain */
519  a->outchain = newfrom->outs;
520  a->outchainRev = NULL;
521  if (newfrom->outs)
522  newfrom->outs->outchainRev = a;
523  newfrom->outs = a;
524  newfrom->nouts++;
525 }
struct arc * outchainRev
Definition: regguts.h:302

References a, assert, state::nouts, arc::outchain, arc::outchainRev, and state::outs.

Referenced by moveouts().

◆ changearctarget()

static void changearctarget ( struct arc a,
struct state newto 
)
static

Definition at line 533 of file regc_nfa.c.

534 {
535  struct state *oldto = a->to;
536  struct arc *predecessor;
537 
538  assert(oldto != newto);
539 
540  /* take it off old target's in-chain */
541  assert(oldto != NULL);
542  predecessor = a->inchainRev;
543  if (predecessor == NULL)
544  {
545  assert(oldto->ins == a);
546  oldto->ins = a->inchain;
547  }
548  else
549  {
550  assert(predecessor->inchain == a);
551  predecessor->inchain = a->inchain;
552  }
553  if (a->inchain != NULL)
554  {
555  assert(a->inchain->inchainRev == a);
556  a->inchain->inchainRev = predecessor;
557  }
558  oldto->nins--;
559 
560  a->to = newto;
561 
562  /* prepend it to new target's in-chain */
563  a->inchain = newto->ins;
564  a->inchainRev = NULL;
565  if (newto->ins)
566  newto->ins->inchainRev = a;
567  newto->ins = a;
568  newto->nins++;
569 }
struct arc * inchainRev
Definition: regguts.h:305
struct arc * inchain
Definition: regguts.h:304
int nins
Definition: regguts.h:327
struct arc * ins
Definition: regguts.h:329

References a, assert, arc::inchain, arc::inchainRev, state::ins, and state::nins.

Referenced by moveins().

◆ check_in_colors_match()

static bool check_in_colors_match ( struct state s,
color  co1,
color  co2 
)
static

Definition at line 3434 of file regc_nfa.c.

3435 {
3436  bool result = true;
3437  struct arc *a;
3438 
3439  /*
3440  * Identical algorithm to check_out_colors_match, except examine the
3441  * from-states of s' inarcs.
3442  */
3443  for (a = s->ins; a != NULL; a = a->inchain)
3444  {
3445  if (a->co == co1)
3446  {
3447  assert(a->from->tmp == NULL);
3448  a->from->tmp = a->from;
3449  }
3450  }
3451  for (a = s->ins; a != NULL; a = a->inchain)
3452  {
3453  if (a->co == co2)
3454  {
3455  if (a->from->tmp != NULL)
3456  a->from->tmp = NULL;
3457  else
3458  result = false; /* unmatched co2 arc */
3459  }
3460  }
3461  for (a = s->ins; a != NULL; a = a->inchain)
3462  {
3463  if (a->co == co1)
3464  {
3465  if (a->from->tmp != NULL)
3466  {
3467  result = false; /* unmatched co1 arc */
3468  a->from->tmp = NULL;
3469  }
3470  }
3471  }
3472  return result;
3473 }

References a, assert, and state::ins.

Referenced by checkmatchall().

◆ check_out_colors_match()

static bool check_out_colors_match ( struct state s,
color  co1,
color  co2 
)
static

Definition at line 3380 of file regc_nfa.c.

3381 {
3382  bool result = true;
3383  struct arc *a;
3384 
3385  /*
3386  * To do this in linear time, we assume that the NFA contains no duplicate
3387  * arcs. Run through the out-arcs, marking states reachable by arcs of
3388  * color co1. Run through again, un-marking states reachable by arcs of
3389  * color co2; if we see a not-marked state, we know this co2 arc is
3390  * unmatched. Then run through again, checking for still-marked states,
3391  * and in any case leaving all the tmp fields reset to NULL.
3392  */
3393  for (a = s->outs; a != NULL; a = a->outchain)
3394  {
3395  if (a->co == co1)
3396  {
3397  assert(a->to->tmp == NULL);
3398  a->to->tmp = a->to;
3399  }
3400  }
3401  for (a = s->outs; a != NULL; a = a->outchain)
3402  {
3403  if (a->co == co2)
3404  {
3405  if (a->to->tmp != NULL)
3406  a->to->tmp = NULL;
3407  else
3408  result = false; /* unmatched co2 arc */
3409  }
3410  }
3411  for (a = s->outs; a != NULL; a = a->outchain)
3412  {
3413  if (a->co == co1)
3414  {
3415  if (a->to->tmp != NULL)
3416  {
3417  result = false; /* unmatched co1 arc */
3418  a->to->tmp = NULL;
3419  }
3420  }
3421  }
3422  return result;
3423 }

References a, assert, and state::outs.

Referenced by checkmatchall().

◆ checkmatchall()

static void checkmatchall ( struct nfa nfa)
static

Definition at line 3062 of file regc_nfa.c.

3063 {
3064  bool **haspaths;
3065  struct state *s;
3066  int i;
3067 
3068  /*
3069  * If there are too many states, don't bother trying to detect matchall.
3070  * This limit serves to bound the time and memory we could consume below.
3071  * Note that even if the graph is all-RAINBOW, if there are significantly
3072  * more than DUPINF states then it's likely that there are paths of length
3073  * more than DUPINF, which would force us to fail anyhow. In practice,
3074  * plausible ways of writing a matchall regex with maximum finite path
3075  * length K tend not to have very many more than K states.
3076  */
3077  if (nfa->nstates > DUPINF * 2)
3078  return;
3079 
3080  /*
3081  * First, scan all the states to verify that only RAINBOW arcs appear,
3082  * plus pseudocolor arcs adjacent to the pre and post states. This lets
3083  * us quickly eliminate most cases that aren't matchall NFAs.
3084  */
3085  for (s = nfa->states; s != NULL; s = s->next)
3086  {
3087  struct arc *a;
3088 
3089  for (a = s->outs; a != NULL; a = a->outchain)
3090  {
3091  if (a->type != PLAIN)
3092  return; /* any LACONs make it non-matchall */
3093  if (a->co != RAINBOW)
3094  {
3095  if (nfa->cm->cd[a->co].flags & PSEUDO)
3096  {
3097  /*
3098  * Pseudocolor arc: verify it's in a valid place (this
3099  * seems quite unlikely to fail, but let's be sure).
3100  */
3101  if (s == nfa->pre &&
3102  (a->co == nfa->bos[0] || a->co == nfa->bos[1]))
3103  /* okay BOS/BOL arc */ ;
3104  else if (a->to == nfa->post &&
3105  (a->co == nfa->eos[0] || a->co == nfa->eos[1]))
3106  /* okay EOS/EOL arc */ ;
3107  else
3108  return; /* unexpected pseudocolor arc */
3109  /* We'll check these arcs some more below. */
3110  }
3111  else
3112  return; /* any other color makes it non-matchall */
3113  }
3114  }
3115  /* Also, assert that the tmp fields are available for use. */
3116  assert(s->tmp == NULL);
3117  }
3118 
3119  /*
3120  * The next cheapest check we can make is to verify that the BOS/BOL
3121  * outarcs of the pre state reach the same states as its RAINBOW outarcs.
3122  * If they don't, the NFA expresses some constraints on the character
3123  * before the matched string, making it non-matchall. Likewise, the
3124  * EOS/EOL inarcs of the post state must match its RAINBOW inarcs.
3125  */
3126  if (!check_out_colors_match(nfa->pre, RAINBOW, nfa->bos[0]) ||
3130  return;
3131 
3132  /*
3133  * Initialize an array of path-length arrays, in which
3134  * checkmatchall_recurse will return per-state results. This lets us
3135  * memo-ize the recursive search and avoid exponential time consumption.
3136  */
3137  haspaths = (bool **) MALLOC(nfa->nstates * sizeof(bool *));
3138  if (haspaths == NULL)
3139  return; /* fail quietly */
3140  memset(haspaths, 0, nfa->nstates * sizeof(bool *));
3141 
3142  /*
3143  * Recursively search the graph for all-RAINBOW paths to the "post" state,
3144  * starting at the "pre" state, and computing the lengths of the paths.
3145  * (Given the preceding checks, there should be at least one such path.
3146  * However we could get back a false result anyway, in case there are
3147  * multi-state loops, paths exceeding DUPINF+1 length, or non-algorithmic
3148  * failures such as ENOMEM.)
3149  */
3150  if (checkmatchall_recurse(nfa, nfa->pre, haspaths))
3151  {
3152  /* The useful result is the path length array for the pre state */
3153  bool *haspath = haspaths[nfa->pre->no];
3154  int minmatch,
3155  maxmatch,
3156  morematch;
3157 
3158  assert(haspath != NULL);
3159 
3160  /*
3161  * haspath[] now represents the set of possible path lengths; but we
3162  * want to reduce that to a min and max value, because it doesn't seem
3163  * worth complicating regexec.c to deal with nonconsecutive possible
3164  * match lengths. Find min and max of first run of lengths, then
3165  * verify there are no nonconsecutive lengths.
3166  */
3167  for (minmatch = 0; minmatch <= DUPINF + 1; minmatch++)
3168  {
3169  if (haspath[minmatch])
3170  break;
3171  }
3172  assert(minmatch <= DUPINF + 1); /* else checkmatchall_recurse lied */
3173  for (maxmatch = minmatch; maxmatch < DUPINF + 1; maxmatch++)
3174  {
3175  if (!haspath[maxmatch + 1])
3176  break;
3177  }
3178  for (morematch = maxmatch + 1; morematch <= DUPINF + 1; morematch++)
3179  {
3180  if (haspath[morematch])
3181  {
3182  haspath = NULL; /* fail, there are nonconsecutive lengths */
3183  break;
3184  }
3185  }
3186 
3187  if (haspath != NULL)
3188  {
3189  /*
3190  * Success, so record the info. Here we have a fine point: the
3191  * path length from the pre state includes the pre-to-initial
3192  * transition, so it's one more than the actually matched string
3193  * length. (We avoided counting the final-to-post transition
3194  * within checkmatchall_recurse, but not this one.) This is why
3195  * checkmatchall_recurse allows one more level of path length than
3196  * might seem necessary. This decrement also takes care of
3197  * converting checkmatchall_recurse's definition of "infinity" as
3198  * "DUPINF+1" to our normal representation as "DUPINF".
3199  */
3200  assert(minmatch > 0); /* else pre and post states were adjacent */
3201  nfa->minmatchall = minmatch - 1;
3202  nfa->maxmatchall = maxmatch - 1;
3203  nfa->flags |= MATCHALL;
3204  }
3205  }
3206 
3207  /* Clean up */
3208  for (i = 0; i < nfa->nstates; i++)
3209  {
3210  if (haspaths[i] != NULL)
3211  FREE(haspaths[i]);
3212  }
3213  FREE(haspaths);
3214 }
#define FREE(ptr)
Definition: cryptohash.c:37
int i
Definition: isn.c:73
static bool checkmatchall_recurse(struct nfa *nfa, struct state *s, bool **haspaths)
Definition: regc_nfa.c:3242
static bool check_in_colors_match(struct state *s, color co1, color co2)
Definition: regc_nfa.c:3434
static bool check_out_colors_match(struct state *s, color co1, color co2)
Definition: regc_nfa.c:3380
#define PLAIN
Definition: regcomp.c:330
#define PSEUDO
Definition: regguts.h:186
#define RAINBOW
Definition: regguts.h:159
#define DUPINF
Definition: regguts.h:99
#define MATCHALL
Definition: regguts.h:412
int flags
Definition: regguts.h:184
struct colordesc * cd
Definition: regguts.h:236
int minmatchall
Definition: regguts.h:367
int maxmatchall
Definition: regguts.h:368
color bos[2]
Definition: regguts.h:364
color eos[2]
Definition: regguts.h:365
int flags
Definition: regguts.h:366
struct colormap * cm
Definition: regguts.h:363
int no
Definition: regguts.h:324

References a, assert, nfa::bos, colormap::cd, check_in_colors_match(), check_out_colors_match(), checkmatchall_recurse(), nfa::cm, DUPINF, nfa::eos, colordesc::flags, nfa::flags, FREE, i, MALLOC, MATCHALL, nfa::maxmatchall, nfa::minmatchall, state::next, state::no, nfa::nstates, state::outs, PLAIN, nfa::post, nfa::pre, PSEUDO, RAINBOW, nfa::states, and state::tmp.

Referenced by analyze().

◆ checkmatchall_recurse()

static bool checkmatchall_recurse ( struct nfa nfa,
struct state s,
bool **  haspaths 
)
static

Definition at line 3242 of file regc_nfa.c.

3243 {
3244  bool result = false;
3245  bool foundloop = false;
3246  bool *haspath;
3247  struct arc *a;
3248 
3249  /*
3250  * Since this is recursive, it could be driven to stack overflow. But we
3251  * need not treat that as a hard failure; just deem the NFA non-matchall.
3252  */
3253  if (STACK_TOO_DEEP(nfa->v->re))
3254  return false;
3255 
3256  /* In case the search takes a long time, check for cancel */
3257  INTERRUPT(nfa->v->re);
3258 
3259  /* Create a haspath array for this state */
3260  haspath = (bool *) MALLOC((DUPINF + 2) * sizeof(bool));
3261  if (haspath == NULL)
3262  return false; /* again, treat as non-matchall */
3263  memset(haspath, 0, (DUPINF + 2) * sizeof(bool));
3264 
3265  /* Mark this state as being visited */
3266  assert(s->tmp == NULL);
3267  s->tmp = s;
3268 
3269  for (a = s->outs; a != NULL; a = a->outchain)
3270  {
3271  if (a->co != RAINBOW)
3272  continue; /* ignore pseudocolor arcs */
3273  if (a->to == nfa->post)
3274  {
3275  /* We found an all-RAINBOW path to the post state */
3276  result = true;
3277 
3278  /*
3279  * Mark this state as being zero steps away from the string end
3280  * (the transition to the post state isn't counted).
3281  */
3282  haspath[0] = true;
3283  }
3284  else if (a->to == s)
3285  {
3286  /* We found a cycle of length 1, which we'll deal with below. */
3287  foundloop = true;
3288  }
3289  else if (a->to->tmp != NULL)
3290  {
3291  /* It's busy, so we found a cycle of length > 1, so fail. */
3292  result = false;
3293  break;
3294  }
3295  else
3296  {
3297  /* Consider paths forward through this to-state. */
3298  bool *nexthaspath;
3299  int i;
3300 
3301  /* If to-state was not already visited, recurse */
3302  if (haspaths[a->to->no] == NULL)
3303  {
3304  result = checkmatchall_recurse(nfa, a->to, haspaths);
3305  /* Fail if any recursive path fails */
3306  if (!result)
3307  break;
3308  }
3309  else
3310  {
3311  /* The previous visit must have found path(s) to the end */
3312  result = true;
3313  }
3314  assert(a->to->tmp == NULL);
3315  nexthaspath = haspaths[a->to->no];
3316  assert(nexthaspath != NULL);
3317 
3318  /*
3319  * Now, for every path of length i from a->to to the string end,
3320  * there is a path of length i + 1 from s to the string end.
3321  */
3322  if (nexthaspath[DUPINF] != nexthaspath[DUPINF + 1])
3323  {
3324  /*
3325  * a->to has a path of length exactly DUPINF, but not longer;
3326  * or it has paths of all lengths > DUPINF but not one of
3327  * exactly that length. In either case, we cannot represent
3328  * the possible path lengths from s correctly, so fail.
3329  */
3330  result = false;
3331  break;
3332  }
3333  /* Merge knowledge of these path lengths into what we have */
3334  for (i = 0; i < DUPINF; i++)
3335  haspath[i + 1] |= nexthaspath[i];
3336  /* Infinity + 1 is still infinity */
3337  haspath[DUPINF + 1] |= nexthaspath[DUPINF + 1];
3338  }
3339  }
3340 
3341  if (result && foundloop)
3342  {
3343  /*
3344  * If there is a length-1 loop at this state, then find the shortest
3345  * known path length to the end. The loop means that every larger
3346  * path length is possible, too. (It doesn't matter whether any of
3347  * the longer lengths were already known possible.)
3348  */
3349  int i;
3350 
3351  for (i = 0; i <= DUPINF; i++)
3352  {
3353  if (haspath[i])
3354  break;
3355  }
3356  for (i++; i <= DUPINF + 1; i++)
3357  haspath[i] = true;
3358  }
3359 
3360  /* Report out the completed path length map */
3361  assert(s->no < nfa->nstates);
3362  assert(haspaths[s->no] == NULL);
3363  haspaths[s->no] = haspath;
3364 
3365  /* Mark state no longer busy */
3366  s->tmp = NULL;
3367 
3368  return result;
3369 }
unsigned char bool
Definition: c.h:440
#define INTERRUPT(re)
Definition: regcustom.h:55
#define STACK_TOO_DEEP(re)
Definition: regguts.h:521
regex_t * re
Definition: regcomp.c:282

References a, assert, DUPINF, i, INTERRUPT, MALLOC, state::no, nfa::nstates, state::outs, nfa::post, RAINBOW, vars::re, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by checkmatchall().

◆ cleanup()

static void cleanup ( struct nfa nfa)
static

Definition at line 2929 of file regc_nfa.c.

2930 {
2931  struct state *s;
2932  struct state *nexts;
2933  int n;
2934 
2935  if (NISERR())
2936  return;
2937 
2938  /* clear out unreachable or dead-end states */
2939  /* use pre to mark reachable, then post to mark can-reach-post */
2940  markreachable(nfa, nfa->pre, (struct state *) NULL, nfa->pre);
2942  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2943  {
2944  nexts = s->next;
2945  if (s->tmp != nfa->post && !s->flag)
2946  dropstate(nfa, s);
2947  }
2948  assert(NISERR() || nfa->post->nins == 0 || nfa->post->tmp == nfa->post);
2949  cleartraverse(nfa, nfa->pre);
2950  assert(NISERR() || nfa->post->nins == 0 || nfa->post->tmp == NULL);
2951  /* the nins==0 (final unreachable) case will be caught later */
2952 
2953  /* renumber surviving states */
2954  n = 0;
2955  for (s = nfa->states; s != NULL; s = s->next)
2956  s->no = n++;
2957  nfa->nstates = n;
2958 }
static void cleartraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1487
static void dropstate(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:226
static void markcanreach(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:2990
static void markreachable(struct nfa *nfa, struct state *s, struct state *okay, struct state *mark)
Definition: regc_nfa.c:2964
char flag
Definition: regguts.h:326

References assert, cleartraverse(), dropstate(), state::flag, markcanreach(), markreachable(), state::next, state::nins, NISERR, state::no, nfa::nstates, nfa::post, nfa::pre, nfa::states, and state::tmp.

Referenced by optimize().

◆ cleartraverse()

static void cleartraverse ( struct nfa nfa,
struct state s 
)
static

Definition at line 1487 of file regc_nfa.c.

1489 {
1490  struct arc *a;
1491 
1492  /* Since this is recursive, it could be driven to stack overflow */
1493  if (STACK_TOO_DEEP(nfa->v->re))
1494  {
1495  NERR(REG_ETOOBIG);
1496  return;
1497  }
1498 
1499  if (s->tmp == NULL)
1500  return;
1501  s->tmp = NULL;
1502 
1503  for (a = s->outs; a != NULL; a = a->outchain)
1504  cleartraverse(nfa, a->to);
1505 }

References a, NERR, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by cleanup(), dupnfa(), and removeconstraints().

◆ cloneouts()

static void cloneouts ( struct nfa nfa,
struct state old,
struct state from,
struct state to,
int  type 
)
static

Definition at line 1256 of file regc_nfa.c.

1261 {
1262  struct arc *a;
1263 
1264  assert(old != from);
1265  assert(type == AHEAD || type == BEHIND);
1266 
1267  for (a = old->outs; a != NULL; a = a->outchain)
1268  {
1269  assert(a->type == PLAIN);
1270  newarc(nfa, type, a->co, from, to);
1271  }
1272 }
static void newarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:281
#define BEHIND
Definition: regcomp.c:342
#define AHEAD
Definition: regcomp.c:341

References a, AHEAD, assert, BEHIND, arc::from, newarc(), state::outs, PLAIN, arc::to, and generate_unaccent_rules::type.

◆ clonesuccessorstates()

static void clonesuccessorstates ( struct nfa nfa,
struct state ssource,
struct state sclone,
struct state spredecessor,
struct arc refarc,
char *  curdonemap,
char *  outerdonemap,
int  nstates 
)
static

Definition at line 2697 of file regc_nfa.c.

2705 {
2706  char *donemap;
2707  struct arc *a;
2708 
2709  /* Since this is recursive, it could be driven to stack overflow */
2710  if (STACK_TOO_DEEP(nfa->v->re))
2711  {
2712  NERR(REG_ETOOBIG);
2713  return;
2714  }
2715 
2716  /* If this state hasn't already got a donemap, create one */
2717  donemap = curdonemap;
2718  if (donemap == NULL)
2719  {
2720  donemap = (char *) MALLOC(nstates * sizeof(char));
2721  if (donemap == NULL)
2722  {
2723  NERR(REG_ESPACE);
2724  return;
2725  }
2726 
2727  if (outerdonemap != NULL)
2728  {
2729  /*
2730  * Not at outermost recursion level, so copy the outer level's
2731  * donemap; this ensures that we see states in process of being
2732  * visited at outer levels, or already merged into predecessor
2733  * states, as ones we shouldn't traverse back to.
2734  */
2735  memcpy(donemap, outerdonemap, nstates * sizeof(char));
2736  }
2737  else
2738  {
2739  /* At outermost level, only spredecessor is off-limits */
2740  memset(donemap, 0, nstates * sizeof(char));
2741  assert(spredecessor->no < nstates);
2742  donemap[spredecessor->no] = 1;
2743  }
2744  }
2745 
2746  /* Mark ssource as visited in the donemap */
2747  assert(ssource->no < nstates);
2748  assert(donemap[ssource->no] == 0);
2749  donemap[ssource->no] = 1;
2750 
2751  /*
2752  * We proceed by first cloning all of ssource's outarcs, creating new
2753  * clone states as needed but not doing more with them than that. Then in
2754  * a second pass, recurse to process the child clone states. This allows
2755  * us to have only one child clone state per reachable source state, even
2756  * when there are multiple outarcs leading to the same state. Also, when
2757  * we do visit a child state, its set of inarcs is known exactly, which
2758  * makes it safe to apply the constraint-is-already-checked optimization.
2759  * Also, this ensures that we've merged all the states we can into the
2760  * current clone before we recurse to any children, thus possibly saving
2761  * them from making extra images of those states.
2762  *
2763  * While this function runs, child clone states of the current state are
2764  * marked by setting their tmp fields to point to the original state they
2765  * were cloned from. This makes it possible to detect multiple outarcs
2766  * leading to the same state, and also makes it easy to distinguish clone
2767  * states from original states (which will have tmp == NULL).
2768  */
2769  for (a = ssource->outs; a != NULL && !NISERR(); a = a->outchain)
2770  {
2771  struct state *sto = a->to;
2772 
2773  /*
2774  * We do not consider cloning successor states that have no constraint
2775  * outarcs; just link to them as-is. They cannot be part of a
2776  * constraint loop so there is no need to make copies. In particular,
2777  * this rule keeps us from trying to clone the post state, which would
2778  * be a bad idea.
2779  */
2780  if (isconstraintarc(a) && hasconstraintout(sto))
2781  {
2782  struct state *prevclone;
2783  int canmerge;
2784  struct arc *a2;
2785 
2786  /*
2787  * Back-link constraint arcs must not be followed. Nor is there a
2788  * need to revisit states previously merged into this clone.
2789  */
2790  assert(sto->no < nstates);
2791  if (donemap[sto->no] != 0)
2792  continue;
2793 
2794  /*
2795  * Check whether we already have a child clone state for this
2796  * source state.
2797  */
2798  prevclone = NULL;
2799  for (a2 = sclone->outs; a2 != NULL; a2 = a2->outchain)
2800  {
2801  if (a2->to->tmp == sto)
2802  {
2803  prevclone = a2->to;
2804  break;
2805  }
2806  }
2807 
2808  /*
2809  * If this arc is labeled the same as refarc, or the same as any
2810  * arc we must have traversed to get to sclone, then no additional
2811  * constraints need to be met to get to sto, so we should just
2812  * merge its outarcs into sclone.
2813  */
2814  if (refarc && a->type == refarc->type && a->co == refarc->co)
2815  canmerge = 1;
2816  else
2817  {
2818  struct state *s;
2819 
2820  canmerge = 0;
2821  for (s = sclone; s->ins; s = s->ins->from)
2822  {
2823  if (s->nins == 1 &&
2824  a->type == s->ins->type && a->co == s->ins->co)
2825  {
2826  canmerge = 1;
2827  break;
2828  }
2829  }
2830  }
2831 
2832  if (canmerge)
2833  {
2834  /*
2835  * We can merge into sclone. If we previously made a child
2836  * clone state, drop it; there's no need to visit it. (This
2837  * can happen if ssource has multiple pathways to sto, and we
2838  * only just now found one that is provably a no-op.)
2839  */
2840  if (prevclone)
2841  dropstate(nfa, prevclone); /* kills our outarc, too */
2842 
2843  /* Recurse to merge sto's outarcs into sclone */
2845  sto,
2846  sclone,
2847  spredecessor,
2848  refarc,
2849  donemap,
2850  outerdonemap,
2851  nstates);
2852  /* sto should now be marked as previously visited */
2853  assert(NISERR() || donemap[sto->no] == 1);
2854  }
2855  else if (prevclone)
2856  {
2857  /*
2858  * We already have a clone state for this successor, so just
2859  * make another arc to it.
2860  */
2861  cparc(nfa, a, sclone, prevclone);
2862  }
2863  else
2864  {
2865  /*
2866  * We need to create a new successor clone state.
2867  */
2868  struct state *stoclone;
2869 
2870  stoclone = newstate(nfa);
2871  if (stoclone == NULL)
2872  {
2873  assert(NISERR());
2874  break;
2875  }
2876  /* Mark it as to what it's a clone of */
2877  stoclone->tmp = sto;
2878  /* ... and add the outarc leading to it */
2879  cparc(nfa, a, sclone, stoclone);
2880  }
2881  }
2882  else
2883  {
2884  /*
2885  * Non-constraint outarcs just get copied to sclone, as do outarcs
2886  * leading to states with no constraint outarc.
2887  */
2888  cparc(nfa, a, sclone, sto);
2889  }
2890  }
2891 
2892  /*
2893  * If we are at outer level for this clone state, recurse to all its child
2894  * clone states, clearing their tmp fields as we go. (If we're not
2895  * outermost for sclone, leave this to be done by the outer call level.)
2896  * Note that if we have multiple outarcs leading to the same clone state,
2897  * it will only be recursed-to once.
2898  */
2899  if (curdonemap == NULL)
2900  {
2901  for (a = sclone->outs; a != NULL && !NISERR(); a = a->outchain)
2902  {
2903  struct state *stoclone = a->to;
2904  struct state *sto = stoclone->tmp;
2905 
2906  if (sto != NULL)
2907  {
2908  stoclone->tmp = NULL;
2910  sto,
2911  stoclone,
2912  spredecessor,
2913  refarc,
2914  NULL,
2915  donemap,
2916  nstates);
2917  }
2918  }
2919 
2920  /* Don't forget to free sclone's donemap when done with it */
2921  FREE(donemap);
2922  }
2923 }
static const FormData_pg_attribute a2
Definition: heap.c:155
static int hasconstraintout(struct state *s)
Definition: regc_nfa.c:2342
int type
Definition: regguts.h:297
color co
Definition: regguts.h:298

References a, a2, assert, arc::co, cparc(), dropstate(), FREE, arc::from, hasconstraintout(), state::ins, isconstraintarc(), MALLOC, NERR, newstate(), state::nins, NISERR, state::no, state::outs, vars::re, REG_ESPACE, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, arc::type, and nfa::v.

Referenced by breakconstraintloop().

◆ combine()

static int combine ( struct nfa nfa,
struct arc con,
struct arc a 
)
static

Definition at line 1980 of file regc_nfa.c.

1983 {
1984 #define CA(ct,at) (((ct)<<CHAR_BIT) | (at))
1985 
1986  switch (CA(con->type, a->type))
1987  {
1988  case CA('^', PLAIN): /* newlines are handled separately */
1989  case CA('$', PLAIN):
1990  return INCOMPATIBLE;
1991  break;
1992  case CA(AHEAD, PLAIN): /* color constraints meet colors */
1993  case CA(BEHIND, PLAIN):
1994  if (con->co == a->co)
1995  return SATISFIED;
1996  if (con->co == RAINBOW)
1997  {
1998  /* con is satisfied unless arc's color is a pseudocolor */
1999  if (!(nfa->cm->cd[a->co].flags & PSEUDO))
2000  return SATISFIED;
2001  }
2002  else if (a->co == RAINBOW)
2003  {
2004  /* con is incompatible if it's for a pseudocolor */
2005  /* (this is hypothetical; we make no such constraints today) */
2006  if (nfa->cm->cd[con->co].flags & PSEUDO)
2007  return INCOMPATIBLE;
2008  /* otherwise, constraint constrains arc to be only its color */
2009  return REPLACEARC;
2010  }
2011  return INCOMPATIBLE;
2012  break;
2013  case CA('^', '^'): /* collision, similar constraints */
2014  case CA('$', '$'):
2015  if (con->co == a->co) /* true duplication */
2016  return SATISFIED;
2017  return INCOMPATIBLE;
2018  break;
2019  case CA(AHEAD, AHEAD): /* collision, similar constraints */
2020  case CA(BEHIND, BEHIND):
2021  if (con->co == a->co) /* true duplication */
2022  return SATISFIED;
2023  if (con->co == RAINBOW)
2024  {
2025  /* con is satisfied unless arc's color is a pseudocolor */
2026  if (!(nfa->cm->cd[a->co].flags & PSEUDO))
2027  return SATISFIED;
2028  }
2029  else if (a->co == RAINBOW)
2030  {
2031  /* con is incompatible if it's for a pseudocolor */
2032  /* (this is hypothetical; we make no such constraints today) */
2033  if (nfa->cm->cd[con->co].flags & PSEUDO)
2034  return INCOMPATIBLE;
2035  /* otherwise, constraint constrains arc to be only its color */
2036  return REPLACEARC;
2037  }
2038  return INCOMPATIBLE;
2039  break;
2040  case CA('^', BEHIND): /* collision, dissimilar constraints */
2041  case CA(BEHIND, '^'):
2042  case CA('$', AHEAD):
2043  case CA(AHEAD, '$'):
2044  return INCOMPATIBLE;
2045  break;
2046  case CA('^', '$'): /* constraints passing each other */
2047  case CA('^', AHEAD):
2048  case CA(BEHIND, '$'):
2049  case CA(BEHIND, AHEAD):
2050  case CA('$', '^'):
2051  case CA('$', BEHIND):
2052  case CA(AHEAD, '^'):
2053  case CA(AHEAD, BEHIND):
2054  case CA('^', LACON):
2055  case CA(BEHIND, LACON):
2056  case CA('$', LACON):
2057  case CA(AHEAD, LACON):
2058  return COMPATIBLE;
2059  break;
2060  }
2061  assert(NOTREACHED);
2062  return INCOMPATIBLE; /* for benefit of blind compilers */
2063 }
#define CA(ct, at)
#define SATISFIED
Definition: regcomp.c:200
#define INCOMPATIBLE
Definition: regcomp.c:199
#define COMPATIBLE
Definition: regcomp.c:201
#define REPLACEARC
Definition: regcomp.c:202
#define LACON
Definition: regcomp.c:340
#define NOTREACHED
Definition: regguts.h:96

References a, AHEAD, assert, BEHIND, CA, colormap::cd, nfa::cm, arc::co, COMPATIBLE, colordesc::flags, INCOMPATIBLE, LACON, NOTREACHED, PLAIN, PSEUDO, RAINBOW, REPLACEARC, SATISFIED, and arc::type.

Referenced by pull(), and push().

◆ compact()

static void compact ( struct nfa nfa,
struct cnfa cnfa 
)
static

Definition at line 3479 of file regc_nfa.c.

3481 {
3482  struct state *s;
3483  struct arc *a;
3484  size_t nstates;
3485  size_t narcs;
3486  struct carc *ca;
3487  struct carc *first;
3488 
3489  assert(!NISERR());
3490 
3491  nstates = 0;
3492  narcs = 0;
3493  for (s = nfa->states; s != NULL; s = s->next)
3494  {
3495  nstates++;
3496  narcs += s->nouts + 1; /* need one extra for endmarker */
3497  }
3498 
3499  cnfa->stflags = (char *) MALLOC(nstates * sizeof(char));
3500  cnfa->states = (struct carc **) MALLOC(nstates * sizeof(struct carc *));
3501  cnfa->arcs = (struct carc *) MALLOC(narcs * sizeof(struct carc));
3502  if (cnfa->stflags == NULL || cnfa->states == NULL || cnfa->arcs == NULL)
3503  {
3504  if (cnfa->stflags != NULL)
3505  FREE(cnfa->stflags);
3506  if (cnfa->states != NULL)
3507  FREE(cnfa->states);
3508  if (cnfa->arcs != NULL)
3509  FREE(cnfa->arcs);
3510  NERR(REG_ESPACE);
3511  return;
3512  }
3513  cnfa->nstates = nstates;
3514  cnfa->pre = nfa->pre->no;
3515  cnfa->post = nfa->post->no;
3516  cnfa->bos[0] = nfa->bos[0];
3517  cnfa->bos[1] = nfa->bos[1];
3518  cnfa->eos[0] = nfa->eos[0];
3519  cnfa->eos[1] = nfa->eos[1];
3520  cnfa->ncolors = maxcolor(nfa->cm) + 1;
3521  cnfa->flags = nfa->flags;
3524 
3525  ca = cnfa->arcs;
3526  for (s = nfa->states; s != NULL; s = s->next)
3527  {
3528  assert((size_t) s->no < nstates);
3529  cnfa->stflags[s->no] = 0;
3530  cnfa->states[s->no] = ca;
3531  first = ca;
3532  for (a = s->outs; a != NULL; a = a->outchain)
3533  switch (a->type)
3534  {
3535  case PLAIN:
3536  ca->co = a->co;
3537  ca->to = a->to->no;
3538  ca++;
3539  break;
3540  case LACON:
3541  assert(s->no != cnfa->pre);
3542  assert(a->co >= 0);
3543  ca->co = (color) (cnfa->ncolors + a->co);
3544  ca->to = a->to->no;
3545  ca++;
3546  cnfa->flags |= HASLACONS;
3547  break;
3548  default:
3549  NERR(REG_ASSERT);
3550  return;
3551  }
3552  carcsort(first, ca - first);
3553  ca->co = COLORLESS;
3554  ca->to = 0;
3555  ca++;
3556  }
3557  assert(ca == &cnfa->arcs[narcs]);
3558  assert(cnfa->nstates != 0);
3559 
3560  /* mark no-progress states */
3561  for (a = nfa->pre->outs; a != NULL; a = a->outchain)
3562  cnfa->stflags[a->to->no] = CNFA_NOPROGRESS;
3564 }
static color maxcolor(struct colormap *cm)
Definition: regc_color.c:172
static void carcsort(struct carc *first, size_t n)
Definition: regc_nfa.c:3570
#define REG_ASSERT
Definition: regex.h:151
short color
Definition: regguts.h:155
#define CNFA_NOPROGRESS
Definition: regguts.h:418
#define COLORLESS
Definition: regguts.h:158
#define HASLACONS
Definition: regguts.h:411
Definition: regguts.h:407
int pre
Definition: regguts.h:413
color eos[2]
Definition: regguts.h:416
struct carc ** states
Definition: regguts.h:419
int ncolors
Definition: regguts.h:409
int maxmatchall
Definition: regguts.h:424
int flags
Definition: regguts.h:410
int post
Definition: regguts.h:414
int minmatchall
Definition: regguts.h:423
struct carc * arcs
Definition: regguts.h:421
color bos[2]
Definition: regguts.h:415
int nstates
Definition: regguts.h:408
char * stflags
Definition: regguts.h:417

References a, cnfa::arcs, assert, nfa::bos, cnfa::bos, carcsort(), nfa::cm, CNFA_NOPROGRESS, carc::co, COLORLESS, nfa::eos, cnfa::eos, nfa::flags, cnfa::flags, FREE, HASLACONS, LACON, MALLOC, maxcolor(), nfa::maxmatchall, cnfa::maxmatchall, nfa::minmatchall, cnfa::minmatchall, cnfa::ncolors, NERR, state::next, NISERR, state::no, state::nouts, cnfa::nstates, state::outs, PLAIN, nfa::post, cnfa::post, nfa::pre, cnfa::pre, REG_ASSERT, REG_ESPACE, nfa::states, cnfa::states, cnfa::stflags, and carc::to.

◆ copyins()

static void copyins ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 882 of file regc_nfa.c.

885 {
886  assert(oldState != newState);
887  assert(newState->nins == 0); /* see comment above */
888 
889  if (newState->nins == 0)
890  {
891  /* No need for de-duplication */
892  struct arc *a;
893 
894  for (a = oldState->ins; a != NULL; a = a->inchain)
895  createarc(nfa, a->type, a->co, a->from, newState);
896  }
897 #ifdef NOT_USED /* see comment above */
898  else if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins))
899  {
900  /* With not too many arcs, just do them one at a time */
901  struct arc *a;
902 
903  for (a = oldState->ins; a != NULL; a = a->inchain)
904  cparc(nfa, a, a->from, newState);
905  }
906  else
907  {
908  /*
909  * With many arcs, use a sort-merge approach. Note that createarc()
910  * will put new arcs onto the front of newState's chain, so it does
911  * not break our walk through the sorted part of the chain.
912  */
913  struct arc *oa;
914  struct arc *na;
915 
916  /*
917  * Because we bypass newarc() in this code path, we'd better include a
918  * cancel check.
919  */
920  INTERRUPT(nfa->v->re);
921 
922  sortins(nfa, oldState);
923  sortins(nfa, newState);
924  if (NISERR())
925  return; /* might have failed to sort */
926  oa = oldState->ins;
927  na = newState->ins;
928  while (oa != NULL && na != NULL)
929  {
930  struct arc *a = oa;
931 
932  switch (sortins_cmp(&oa, &na))
933  {
934  case -1:
935  /* newState does not have anything matching oa */
936  oa = oa->inchain;
937  createarc(nfa, a->type, a->co, a->from, newState);
938  break;
939  case 0:
940  /* match, advance in both lists */
941  oa = oa->inchain;
942  na = na->inchain;
943  break;
944  case +1:
945  /* advance only na; oa might have a match later */
946  na = na->inchain;
947  break;
948  default:
950  }
951  }
952  while (oa != NULL)
953  {
954  /* newState does not have anything matching oa */
955  struct arc *a = oa;
956 
957  oa = oa->inchain;
958  createarc(nfa, a->type, a->co, a->from, newState);
959  }
960  }
961 #endif /* NOT_USED */
962 }
static int sortins_cmp(const void *a, const void *b)
Definition: regc_nfa.c:662
#define BULK_ARC_OP_USE_SORT(nsrcarcs, ndestarcs)
Definition: regc_nfa.c:758
static void createarc(struct nfa *nfa, int t, color co, struct state *from, struct state *to)
Definition: regc_nfa.c:323
static void sortins(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:620

References a, assert, BULK_ARC_OP_USE_SORT, cparc(), createarc(), arc::inchain, state::ins, INTERRUPT, state::nins, NISERR, NOTREACHED, vars::re, sortins(), sortins_cmp(), and nfa::v.

Referenced by pull().

◆ copyouts()

static void copyouts ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 1167 of file regc_nfa.c.

1170 {
1171  assert(oldState != newState);
1172  assert(newState->nouts == 0); /* see comment above */
1173 
1174  if (newState->nouts == 0)
1175  {
1176  /* No need for de-duplication */
1177  struct arc *a;
1178 
1179  for (a = oldState->outs; a != NULL; a = a->outchain)
1180  createarc(nfa, a->type, a->co, newState, a->to);
1181  }
1182 #ifdef NOT_USED /* see comment above */
1183  else if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts))
1184  {
1185  /* With not too many arcs, just do them one at a time */
1186  struct arc *a;
1187 
1188  for (a = oldState->outs; a != NULL; a = a->outchain)
1189  cparc(nfa, a, newState, a->to);
1190  }
1191  else
1192  {
1193  /*
1194  * With many arcs, use a sort-merge approach. Note that createarc()
1195  * will put new arcs onto the front of newState's chain, so it does
1196  * not break our walk through the sorted part of the chain.
1197  */
1198  struct arc *oa;
1199  struct arc *na;
1200 
1201  /*
1202  * Because we bypass newarc() in this code path, we'd better include a
1203  * cancel check.
1204  */
1205  INTERRUPT(nfa->v->re);
1206 
1207  sortouts(nfa, oldState);
1208  sortouts(nfa, newState);
1209  if (NISERR())
1210  return; /* might have failed to sort */
1211  oa = oldState->outs;
1212  na = newState->outs;
1213  while (oa != NULL && na != NULL)
1214  {
1215  struct arc *a = oa;
1216 
1217  switch (sortouts_cmp(&oa, &na))
1218  {
1219  case -1:
1220  /* newState does not have anything matching oa */
1221  oa = oa->outchain;
1222  createarc(nfa, a->type, a->co, newState, a->to);
1223  break;
1224  case 0:
1225  /* match, advance in both lists */
1226  oa = oa->outchain;
1227  na = na->outchain;
1228  break;
1229  case +1:
1230  /* advance only na; oa might have a match later */
1231  na = na->outchain;
1232  break;
1233  default:
1234  assert(NOTREACHED);
1235  }
1236  }
1237  while (oa != NULL)
1238  {
1239  /* newState does not have anything matching oa */
1240  struct arc *a = oa;
1241 
1242  oa = oa->outchain;
1243  createarc(nfa, a->type, a->co, newState, a->to);
1244  }
1245  }
1246 #endif /* NOT_USED */
1247 }
static void sortouts(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:687
static int sortouts_cmp(const void *a, const void *b)
Definition: regc_nfa.c:729

References a, assert, BULK_ARC_OP_USE_SORT, cparc(), createarc(), INTERRUPT, NISERR, NOTREACHED, state::nouts, arc::outchain, state::outs, vars::re, sortouts(), sortouts_cmp(), and nfa::v.

Referenced by push().

◆ cparc()

static void cparc ( struct nfa nfa,
struct arc oa,
struct state from,
struct state to 
)
static

Definition at line 608 of file regc_nfa.c.

612 {
613  newarc(nfa, oa->type, oa->co, from, to);
614 }

References arc::co, arc::from, newarc(), arc::to, and arc::type.

Referenced by breakconstraintloop(), clonesuccessorstates(), copyins(), copyouts(), duptraverse(), moveins(), moveouts(), pull(), and push().

◆ createarc()

static void createarc ( struct nfa nfa,
int  t,
color  co,
struct state from,
struct state to 
)
static

Definition at line 323 of file regc_nfa.c.

328 {
329  struct arc *a;
330 
331  a = allocarc(nfa);
332  if (NISERR())
333  return;
334  assert(a != NULL);
335 
336  a->type = t;
337  a->co = co;
338  a->to = to;
339  a->from = from;
340 
341  /*
342  * Put the new arc on the beginning, not the end, of the chains; it's
343  * simpler here, and freearc() is the same cost either way. See also the
344  * logic in moveins() and its cohorts, as well as fixempties().
345  */
346  a->inchain = to->ins;
347  a->inchainRev = NULL;
348  if (to->ins)
349  to->ins->inchainRev = a;
350  to->ins = a;
351  a->outchain = from->outs;
352  a->outchainRev = NULL;
353  if (from->outs)
354  from->outs->outchainRev = a;
355  from->outs = a;
356 
357  from->nouts++;
358  to->nins++;
359 
360  if (COLORED(a) && nfa->parent == NULL)
361  colorchain(nfa->cm, a);
362 }
static void colorchain(struct colormap *cm, struct arc *a)
Definition: regc_color.c:984
static struct arc * allocarc(struct nfa *nfa)
Definition: regc_nfa.c:368
#define COLORED(a)
Definition: regcomp.c:350
struct nfa * parent
Definition: regguts.h:370

References a, allocarc(), assert, nfa::cm, arc::co, colorchain(), COLORED, arc::from, arc::inchainRev, state::ins, state::nins, NISERR, state::nouts, arc::outchainRev, state::outs, nfa::parent, and arc::to.

Referenced by copyins(), copyouts(), mergeins(), moveins(), moveouts(), and newarc().

◆ delsub()

static void delsub ( struct nfa nfa,
struct state lp,
struct state rp 
)
static

Definition at line 1281 of file regc_nfa.c.

1284 {
1285  assert(lp != rp);
1286 
1287  rp->tmp = rp; /* mark end */
1288 
1289  deltraverse(nfa, lp, lp);
1290  if (NISERR())
1291  return; /* asserts might not hold after failure */
1292  assert(lp->nouts == 0 && rp->nins == 0); /* did the job */
1293  assert(lp->no != FREESTATE && rp->no != FREESTATE); /* no more */
1294 
1295  rp->tmp = NULL; /* unmark end */
1296  lp->tmp = NULL; /* and begin, marked by deltraverse */
1297 }
static void deltraverse(struct nfa *nfa, struct state *leftend, struct state *s)
Definition: regc_nfa.c:1304
#define FREESTATE(state)
Definition: tuplesort.c:400

References assert, deltraverse(), FREESTATE, state::nins, NISERR, state::no, state::nouts, and state::tmp.

◆ deltraverse()

static void deltraverse ( struct nfa nfa,
struct state leftend,
struct state s 
)
static

Definition at line 1304 of file regc_nfa.c.

1307 {
1308  struct arc *a;
1309  struct state *to;
1310 
1311  /* Since this is recursive, it could be driven to stack overflow */
1312  if (STACK_TOO_DEEP(nfa->v->re))
1313  {
1314  NERR(REG_ETOOBIG);
1315  return;
1316  }
1317 
1318  if (s->nouts == 0)
1319  return; /* nothing to do */
1320  if (s->tmp != NULL)
1321  return; /* already in progress */
1322 
1323  s->tmp = s; /* mark as in progress */
1324 
1325  while ((a = s->outs) != NULL)
1326  {
1327  to = a->to;
1328  deltraverse(nfa, leftend, to);
1329  if (NISERR())
1330  return; /* asserts might not hold after failure */
1331  assert(to->nouts == 0 || to->tmp != NULL);
1332  freearc(nfa, a);
1333  if (to->nins == 0 && to->tmp == NULL)
1334  {
1335  assert(to->nouts == 0);
1336  freestate(nfa, to);
1337  }
1338  }
1339 
1340  assert(s->no != FREESTATE); /* we're still here */
1341  assert(s == leftend || s->nins != 0); /* and still reachable */
1342  assert(s->nouts == 0); /* but have no outarcs */
1343 
1344  s->tmp = NULL; /* we're done here */
1345 }

References a, assert, freearc(), FREESTATE, freestate(), NERR, state::nins, NISERR, state::no, state::nouts, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by delsub().

◆ dropstate()

static void dropstate ( struct nfa nfa,
struct state s 
)
static

Definition at line 226 of file regc_nfa.c.

228 {
229  struct arc *a;
230 
231  while ((a = s->ins) != NULL)
232  freearc(nfa, a);
233  while ((a = s->outs) != NULL)
234  freearc(nfa, a);
235  freestate(nfa, s);
236 }

References a, freearc(), freestate(), state::ins, and state::outs.

Referenced by cleanup(), clonesuccessorstates(), fixconstraintloops(), fixempties(), pullback(), and pushfwd().

◆ dumpnfa()

static void dumpnfa ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 3611 of file regc_nfa.c.

3613 {
3614 #ifdef REG_DEBUG
3615  struct state *s;
3616  int nstates = 0;
3617  int narcs = 0;
3618 
3619  fprintf(f, "pre %d, post %d", nfa->pre->no, nfa->post->no);
3620  if (nfa->bos[0] != COLORLESS)
3621  fprintf(f, ", bos [%ld]", (long) nfa->bos[0]);
3622  if (nfa->bos[1] != COLORLESS)
3623  fprintf(f, ", bol [%ld]", (long) nfa->bos[1]);
3624  if (nfa->eos[0] != COLORLESS)
3625  fprintf(f, ", eos [%ld]", (long) nfa->eos[0]);
3626  if (nfa->eos[1] != COLORLESS)
3627  fprintf(f, ", eol [%ld]", (long) nfa->eos[1]);
3628  if (nfa->flags & HASLACONS)
3629  fprintf(f, ", haslacons");
3630  if (nfa->flags & MATCHALL)
3631  {
3632  fprintf(f, ", minmatchall %d", nfa->minmatchall);
3633  if (nfa->maxmatchall == DUPINF)
3634  fprintf(f, ", maxmatchall inf");
3635  else
3636  fprintf(f, ", maxmatchall %d", nfa->maxmatchall);
3637  }
3638  fprintf(f, "\n");
3639  for (s = nfa->states; s != NULL; s = s->next)
3640  {
3641  dumpstate(s, f);
3642  nstates++;
3643  narcs += s->nouts;
3644  }
3645  fprintf(f, "total of %d states, %d arcs\n", nstates, narcs);
3646  if (nfa->parent == NULL)
3647  dumpcolors(nfa->cm, f);
3648  fflush(f);
3649 #endif
3650 }
static void const char fflush(stdout)
#define fprintf
Definition: port.h:242

References nfa::bos, nfa::cm, COLORLESS, DUPINF, nfa::eos, fflush(), nfa::flags, fprintf, HASLACONS, MATCHALL, nfa::maxmatchall, nfa::minmatchall, state::next, state::no, state::nouts, nfa::parent, nfa::post, nfa::pre, and nfa::states.

Referenced by fixconstraintloops(), fixempties(), optimize(), pullback(), and pushfwd().

◆ dupnfa()

static void dupnfa ( struct nfa nfa,
struct state start,
struct state stop,
struct state from,
struct state to 
)
static

Definition at line 1355 of file regc_nfa.c.

1360 {
1361  if (start == stop)
1362  {
1363  newarc(nfa, EMPTY, 0, from, to);
1364  return;
1365  }
1366 
1367  stop->tmp = to;
1368  duptraverse(nfa, start, from);
1369  /* done, except for clearing out the tmp pointers */
1370 
1371  stop->tmp = NULL;
1372  cleartraverse(nfa, start);
1373 }
static void duptraverse(struct nfa *nfa, struct state *s, struct state *stmp)
Definition: regc_nfa.c:1379
#define EMPTY
Definition: regcomp.c:328

References cleartraverse(), duptraverse(), EMPTY, newarc(), and state::tmp.

◆ duptraverse()

static void duptraverse ( struct nfa nfa,
struct state s,
struct state stmp 
)
static

Definition at line 1379 of file regc_nfa.c.

1382 {
1383  struct arc *a;
1384 
1385  /* Since this is recursive, it could be driven to stack overflow */
1386  if (STACK_TOO_DEEP(nfa->v->re))
1387  {
1388  NERR(REG_ETOOBIG);
1389  return;
1390  }
1391 
1392  if (s->tmp != NULL)
1393  return; /* already done */
1394 
1395  s->tmp = (stmp == NULL) ? newstate(nfa) : stmp;
1396  if (s->tmp == NULL)
1397  {
1398  assert(NISERR());
1399  return;
1400  }
1401 
1402  for (a = s->outs; a != NULL && !NISERR(); a = a->outchain)
1403  {
1404  duptraverse(nfa, a->to, (struct state *) NULL);
1405  if (NISERR())
1406  break;
1407  assert(a->to->tmp != NULL);
1408  cparc(nfa, a, s->tmp, a->to->tmp);
1409  }
1410 }

References a, assert, cparc(), NERR, newstate(), NISERR, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by dupnfa().

◆ emptyreachable()

static struct state* emptyreachable ( struct nfa nfa,
struct state s,
struct state lastfound,
struct arc **  inarcsorig 
)
static

Definition at line 2296 of file regc_nfa.c.

2300 {
2301  struct arc *a;
2302 
2303  /* Since this is recursive, it could be driven to stack overflow */
2304  if (STACK_TOO_DEEP(nfa->v->re))
2305  {
2306  NERR(REG_ETOOBIG);
2307  return lastfound;
2308  }
2309 
2310  s->tmp = lastfound;
2311  lastfound = s;
2312  for (a = inarcsorig[s->no]; a != NULL; a = a->inchain)
2313  {
2314  if (a->type == EMPTY && a->from->tmp == NULL)
2315  lastfound = emptyreachable(nfa, a->from, lastfound, inarcsorig);
2316  }
2317  return lastfound;
2318 }
static struct state * emptyreachable(struct nfa *nfa, struct state *s, struct state *lastfound, struct arc **inarcsorig)
Definition: regc_nfa.c:2296

References a, EMPTY, NERR, state::no, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by fixempties().

◆ findarc()

static struct arc* findarc ( struct state s,
int  type,
color  co 
)
static

Definition at line 592 of file regc_nfa.c.

595 {
596  struct arc *a;
597 
598  for (a = s->outs; a != NULL; a = a->outchain)
599  if (a->type == type && a->co == co)
600  return a;
601  return NULL;
602 }

References a, arc::co, state::outs, and generate_unaccent_rules::type.

Referenced by colorcomplement().

◆ findconstraintloop()

static int findconstraintloop ( struct nfa nfa,
struct state s 
)
static

Definition at line 2462 of file regc_nfa.c.

2463 {
2464  struct arc *a;
2465 
2466  /* Since this is recursive, it could be driven to stack overflow */
2467  if (STACK_TOO_DEEP(nfa->v->re))
2468  {
2469  NERR(REG_ETOOBIG);
2470  return 1; /* to exit as quickly as possible */
2471  }
2472 
2473  if (s->tmp != NULL)
2474  {
2475  /* Already proven uninteresting? */
2476  if (s->tmp == s)
2477  return 0;
2478  /* Found a loop involving s */
2480  /* The tmp fields have been cleaned up by breakconstraintloop */
2481  return 1;
2482  }
2483  for (a = s->outs; a != NULL; a = a->outchain)
2484  {
2485  if (isconstraintarc(a))
2486  {
2487  struct state *sto = a->to;
2488 
2489  assert(sto != s);
2490  s->tmp = sto;
2491  if (findconstraintloop(nfa, sto))
2492  return 1;
2493  }
2494  }
2495 
2496  /*
2497  * If we get here, no constraint loop exists leading out from s. Mark it
2498  * with s->tmp == s so we need not rediscover that fact again later.
2499  */
2500  s->tmp = s;
2501  return 0;
2502 }
static int findconstraintloop(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:2462
static void breakconstraintloop(struct nfa *nfa, struct state *sinitial)
Definition: regc_nfa.c:2551

References a, assert, breakconstraintloop(), isconstraintarc(), NERR, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by fixconstraintloops().

◆ fixconstraintloops()

static void fixconstraintloops ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 2363 of file regc_nfa.c.

2365 {
2366  struct state *s;
2367  struct state *nexts;
2368  struct arc *a;
2369  struct arc *nexta;
2370  int hasconstraints;
2371 
2372  /*
2373  * In the trivial case of a state that loops to itself, we can just drop
2374  * the constraint arc altogether. This is worth special-casing because
2375  * such loops are far more common than loops containing multiple states.
2376  * While we're at it, note whether any constraint arcs survive.
2377  */
2378  hasconstraints = 0;
2379  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2380  {
2381  nexts = s->next;
2382  /* while we're at it, ensure tmp fields are clear for next step */
2383  assert(s->tmp == NULL);
2384  for (a = s->outs; a != NULL && !NISERR(); a = nexta)
2385  {
2386  nexta = a->outchain;
2387  if (isconstraintarc(a))
2388  {
2389  if (a->to == s)
2390  freearc(nfa, a);
2391  else
2392  hasconstraints = 1;
2393  }
2394  }
2395  /* If we removed all the outarcs, the state is useless. */
2396  if (s->nouts == 0 && !s->flag)
2397  dropstate(nfa, s);
2398  }
2399 
2400  /* Nothing to do if no remaining constraint arcs */
2401  if (NISERR() || !hasconstraints)
2402  return;
2403 
2404  /*
2405  * Starting from each remaining NFA state, search outwards for a
2406  * constraint loop. If we find a loop, break the loop, then start the
2407  * search over. (We could possibly retain some state from the first scan,
2408  * but it would complicate things greatly, and multi-state constraint
2409  * loops are rare enough that it's not worth optimizing the case.)
2410  */
2411 restart:
2412  for (s = nfa->states; s != NULL && !NISERR(); s = s->next)
2413  {
2414  if (findconstraintloop(nfa, s))
2415  goto restart;
2416  }
2417 
2418  if (NISERR())
2419  return;
2420 
2421  /*
2422  * Now remove any states that have become useless. (This cleanup is not
2423  * very thorough, and would be even less so if we tried to combine it with
2424  * the previous step; but cleanup() will take care of anything we miss.)
2425  *
2426  * Because findconstraintloop intentionally doesn't reset all tmp fields,
2427  * we have to clear them after it's done. This is a convenient place to
2428  * do that, too.
2429  */
2430  for (s = nfa->states; s != NULL; s = nexts)
2431  {
2432  nexts = s->next;
2433  s->tmp = NULL;
2434  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
2435  dropstate(nfa, s);
2436  }
2437 
2438  if (f != NULL)
2439  dumpnfa(nfa, f);
2440 }
static void dumpnfa(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:3611

References a, assert, dropstate(), dumpnfa(), findconstraintloop(), state::flag, freearc(), isconstraintarc(), state::next, state::nins, NISERR, state::nouts, state::outs, nfa::states, and state::tmp.

Referenced by optimize().

◆ fixempties()

static void fixempties ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 2069 of file regc_nfa.c.

2071 {
2072  struct state *s;
2073  struct state *s2;
2074  struct state *nexts;
2075  struct arc *a;
2076  struct arc *nexta;
2077  int totalinarcs;
2078  struct arc **inarcsorig;
2079  struct arc **arcarray;
2080  int arccount;
2081  int prevnins;
2082  int nskip;
2083 
2084  /*
2085  * First, get rid of any states whose sole out-arc is an EMPTY, since
2086  * they're basically just aliases for their successor. The parsing
2087  * algorithm creates enough of these that it's worth special-casing this.
2088  */
2089  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2090  {
2091  nexts = s->next;
2092  if (s->flag || s->nouts != 1)
2093  continue;
2094  a = s->outs;
2095  assert(a != NULL && a->outchain == NULL);
2096  if (a->type != EMPTY)
2097  continue;
2098  if (s != a->to)
2099  moveins(nfa, s, a->to);
2100  dropstate(nfa, s);
2101  }
2102 
2103  /*
2104  * Similarly, get rid of any state with a single EMPTY in-arc, by folding
2105  * it into its predecessor.
2106  */
2107  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
2108  {
2109  nexts = s->next;
2110  /* while we're at it, ensure tmp fields are clear for next step */
2111  assert(s->tmp == NULL);
2112  if (s->flag || s->nins != 1)
2113  continue;
2114  a = s->ins;
2115  assert(a != NULL && a->inchain == NULL);
2116  if (a->type != EMPTY)
2117  continue;
2118  if (s != a->from)
2119  moveouts(nfa, s, a->from);
2120  dropstate(nfa, s);
2121  }
2122 
2123  if (NISERR())
2124  return;
2125 
2126  /*
2127  * For each remaining NFA state, find all other states from which it is
2128  * reachable by a chain of one or more EMPTY arcs. Then generate new arcs
2129  * that eliminate the need for each such chain.
2130  *
2131  * We could replace a chain of EMPTY arcs that leads from a "from" state
2132  * to a "to" state either by pushing non-EMPTY arcs forward (linking
2133  * directly from "from"'s predecessors to "to") or by pulling them back
2134  * (linking directly from "from" to "to"'s successors). We choose to
2135  * always do the former; this choice is somewhat arbitrary, but the
2136  * approach below requires that we uniformly do one or the other.
2137  *
2138  * Suppose we have a chain of N successive EMPTY arcs (where N can easily
2139  * approach the size of the NFA). All of the intermediate states must
2140  * have additional inarcs and outarcs, else they'd have been removed by
2141  * the steps above. Assuming their inarcs are mostly not empties, we will
2142  * add O(N^2) arcs to the NFA, since a non-EMPTY inarc leading to any one
2143  * state in the chain must be duplicated to lead to all its successor
2144  * states as well. So there is no hope of doing less than O(N^2) work;
2145  * however, we should endeavor to keep the big-O cost from being even
2146  * worse than that, which it can easily become without care. In
2147  * particular, suppose we were to copy all S1's inarcs forward to S2, and
2148  * then also to S3, and then later we consider pushing S2's inarcs forward
2149  * to S3. If we include the arcs already copied from S1 in that, we'd be
2150  * doing O(N^3) work. (The duplicate-arc elimination built into newarc()
2151  * and its cohorts would get rid of the extra arcs, but not without cost.)
2152  *
2153  * We can avoid this cost by treating only arcs that existed at the start
2154  * of this phase as candidates to be pushed forward. To identify those,
2155  * we remember the first inarc each state had to start with. We rely on
2156  * the fact that newarc() and friends put new arcs on the front of their
2157  * to-states' inchains, and that this phase never deletes arcs, so that
2158  * the original arcs must be the last arcs in their to-states' inchains.
2159  *
2160  * So the process here is that, for each state in the NFA, we gather up
2161  * all non-EMPTY inarcs of states that can reach the target state via
2162  * EMPTY arcs. We then sort, de-duplicate, and merge these arcs into the
2163  * target state's inchain. (We can safely use sort-merge for this as long
2164  * as we update each state's original-arcs pointer after we add arcs to
2165  * it; the sort step of mergeins probably changed the order of the old
2166  * arcs.)
2167  *
2168  * Another refinement worth making is that, because we only add non-EMPTY
2169  * arcs during this phase, and all added arcs have the same from-state as
2170  * the non-EMPTY arc they were cloned from, we know ahead of time that any
2171  * states having only EMPTY outarcs will be useless for lack of outarcs
2172  * after we drop the EMPTY arcs. (They cannot gain non-EMPTY outarcs if
2173  * they had none to start with.) So we need not bother to update the
2174  * inchains of such states at all.
2175  */
2176 
2177  /* Remember the states' first original inarcs */
2178  /* ... and while at it, count how many old inarcs there are altogether */
2179  inarcsorig = (struct arc **) MALLOC(nfa->nstates * sizeof(struct arc *));
2180  if (inarcsorig == NULL)
2181  {
2182  NERR(REG_ESPACE);
2183  return;
2184  }
2185  totalinarcs = 0;
2186  for (s = nfa->states; s != NULL; s = s->next)
2187  {
2188  inarcsorig[s->no] = s->ins;
2189  totalinarcs += s->nins;
2190  }
2191 
2192  /*
2193  * Create a workspace for accumulating the inarcs to be added to the
2194  * current target state. totalinarcs is probably a considerable
2195  * overestimate of the space needed, but the NFA is unlikely to be large
2196  * enough at this point to make it worth being smarter.
2197  */
2198  arcarray = (struct arc **) MALLOC(totalinarcs * sizeof(struct arc *));
2199  if (arcarray == NULL)
2200  {
2201  NERR(REG_ESPACE);
2202  FREE(inarcsorig);
2203  return;
2204  }
2205 
2206  /* And iterate over the target states */
2207  for (s = nfa->states; s != NULL && !NISERR(); s = s->next)
2208  {
2209  /* Ignore target states without non-EMPTY outarcs, per note above */
2210  if (!s->flag && !hasnonemptyout(s))
2211  continue;
2212 
2213  /* Find predecessor states and accumulate their original inarcs */
2214  arccount = 0;
2215  for (s2 = emptyreachable(nfa, s, s, inarcsorig); s2 != s; s2 = nexts)
2216  {
2217  /* Add s2's original inarcs to arcarray[], but ignore empties */
2218  for (a = inarcsorig[s2->no]; a != NULL; a = a->inchain)
2219  {
2220  if (a->type != EMPTY)
2221  arcarray[arccount++] = a;
2222  }
2223 
2224  /* Reset the tmp fields as we walk back */
2225  nexts = s2->tmp;
2226  s2->tmp = NULL;
2227  }
2228  s->tmp = NULL;
2229  assert(arccount <= totalinarcs);
2230 
2231  /* Remember how many original inarcs this state has */
2232  prevnins = s->nins;
2233 
2234  /* Add non-duplicate inarcs to target state */
2235  mergeins(nfa, s, arcarray, arccount);
2236 
2237  /* Now we must update the state's inarcsorig pointer */
2238  nskip = s->nins - prevnins;
2239  a = s->ins;
2240  while (nskip-- > 0)
2241  a = a->inchain;
2242  inarcsorig[s->no] = a;
2243  }
2244 
2245  FREE(arcarray);
2246  FREE(inarcsorig);
2247 
2248  if (NISERR())
2249  return;
2250 
2251  /*
2252  * Now remove all the EMPTY arcs, since we don't need them anymore.
2253  */
2254  for (s = nfa->states; s != NULL; s = s->next)
2255  {
2256  for (a = s->outs; a != NULL; a = nexta)
2257  {
2258  nexta = a->outchain;
2259  if (a->type == EMPTY)
2260  freearc(nfa, a);
2261  }
2262  }
2263 
2264  /*
2265  * And remove any states that have become useless. (This cleanup is not
2266  * very thorough, and would be even less so if we tried to combine it with
2267  * the previous step; but cleanup() will take care of anything we miss.)
2268  */
2269  for (s = nfa->states; s != NULL; s = nexts)
2270  {
2271  nexts = s->next;
2272  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
2273  dropstate(nfa, s);
2274  }
2275 
2276  if (f != NULL)
2277  dumpnfa(nfa, f);
2278 }
char * s2
static void mergeins(struct nfa *nfa, struct state *s, struct arc **arcarray, int arccount)
Definition: regc_nfa.c:971
static void moveouts(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:1066
static int hasnonemptyout(struct state *s)
Definition: regc_nfa.c:575
static void moveins(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:778

References a, assert, dropstate(), dumpnfa(), EMPTY, emptyreachable(), state::flag, FREE, freearc(), hasnonemptyout(), state::ins, MALLOC, mergeins(), moveins(), moveouts(), NERR, state::next, state::nins, NISERR, state::no, state::nouts, nfa::nstates, state::outs, REG_ESPACE, s2, nfa::states, and state::tmp.

Referenced by optimize().

◆ freearc()

static void freearc ( struct nfa nfa,
struct arc victim 
)
static

Definition at line 418 of file regc_nfa.c.

420 {
421  struct state *from = victim->from;
422  struct state *to = victim->to;
423  struct arc *predecessor;
424 
425  assert(victim->type != 0);
426 
427  /* take it off color chain if necessary */
428  if (COLORED(victim) && nfa->parent == NULL)
429  uncolorchain(nfa->cm, victim);
430 
431  /* take it off source's out-chain */
432  assert(from != NULL);
433  predecessor = victim->outchainRev;
434  if (predecessor == NULL)
435  {
436  assert(from->outs == victim);
437  from->outs = victim->outchain;
438  }
439  else
440  {
441  assert(predecessor->outchain == victim);
442  predecessor->outchain = victim->outchain;
443  }
444  if (victim->outchain != NULL)
445  {
446  assert(victim->outchain->outchainRev == victim);
447  victim->outchain->outchainRev = predecessor;
448  }
449  from->nouts--;
450 
451  /* take it off target's in-chain */
452  assert(to != NULL);
453  predecessor = victim->inchainRev;
454  if (predecessor == NULL)
455  {
456  assert(to->ins == victim);
457  to->ins = victim->inchain;
458  }
459  else
460  {
461  assert(predecessor->inchain == victim);
462  predecessor->inchain = victim->inchain;
463  }
464  if (victim->inchain != NULL)
465  {
466  assert(victim->inchain->inchainRev == victim);
467  victim->inchain->inchainRev = predecessor;
468  }
469  to->nins--;
470 
471  /* clean up and place on NFA's free list */
472  victim->type = 0;
473  victim->from = NULL; /* precautions... */
474  victim->to = NULL;
475  victim->inchain = NULL;
476  victim->inchainRev = NULL;
477  victim->outchain = NULL;
478  victim->outchainRev = NULL;
479  victim->freechain = nfa->freearcs;
480  nfa->freearcs = victim;
481 }
static void uncolorchain(struct colormap *cm, struct arc *a)
Definition: regc_color.c:1001

References assert, nfa::cm, COLORED, nfa::freearcs, arc::from, arc::inchain, arc::inchainRev, state::ins, state::nins, state::nouts, arc::outchain, arc::outchainRev, state::outs, nfa::parent, arc::to, arc::type, and uncolorchain().

Referenced by breakconstraintloop(), deltraverse(), dropstate(), fixconstraintloops(), fixempties(), moveins(), moveouts(), pull(), pullback(), push(), pushfwd(), and removetraverse().

◆ freecnfa()

static void freecnfa ( struct cnfa cnfa)
static

Definition at line 3598 of file regc_nfa.c.

3599 {
3600  assert(!NULLCNFA(*cnfa)); /* not empty already */
3601  FREE(cnfa->stflags);
3602  FREE(cnfa->states);
3603  FREE(cnfa->arcs);
3604  ZAPCNFA(*cnfa);
3605 }
#define ZAPCNFA(cnfa)
Definition: regguts.h:434
#define NULLCNFA(cnfa)
Definition: regguts.h:436

References cnfa::arcs, assert, FREE, NULLCNFA, cnfa::states, cnfa::stflags, and ZAPCNFA.

◆ freenfa()

static void freenfa ( struct nfa nfa)
static

Definition at line 107 of file regc_nfa.c.

108 {
109  struct statebatch *sb;
110  struct statebatch *sbnext;
111  struct arcbatch *ab;
112  struct arcbatch *abnext;
113 
114  for (sb = nfa->lastsb; sb != NULL; sb = sbnext)
115  {
116  sbnext = sb->next;
118  FREE(sb);
119  }
120  nfa->lastsb = NULL;
121  for (ab = nfa->lastab; ab != NULL; ab = abnext)
122  {
123  abnext = ab->next;
124  nfa->v->spaceused -= ARCBATCHSIZE(ab->narcs);
125  FREE(ab);
126  }
127  nfa->lastab = NULL;
128 
129  nfa->nstates = -1;
130  FREE(nfa);
131 }
#define STATEBATCHSIZE(n)
Definition: regguts.h:343
struct statebatch * lastsb
Definition: regguts.h:359
struct statebatch * next
Definition: regguts.h:339
size_t nstates
Definition: regguts.h:340

References ARCBATCHSIZE, FREE, nfa::lastab, nfa::lastsb, arcbatch::narcs, arcbatch::next, statebatch::next, statebatch::nstates, nfa::nstates, vars::spaceused, STATEBATCHSIZE, and nfa::v.

Referenced by newnfa().

◆ freestate()

static void freestate ( struct nfa nfa,
struct state s 
)
static

Definition at line 242 of file regc_nfa.c.

244 {
245  assert(s != NULL);
246  assert(s->nins == 0 && s->nouts == 0);
247 
248  s->no = FREESTATE;
249  s->flag = 0;
250  if (s->next != NULL)
251  s->next->prev = s->prev;
252  else
253  {
254  assert(s == nfa->slast);
255  nfa->slast = s->prev;
256  }
257  if (s->prev != NULL)
258  s->prev->next = s->next;
259  else
260  {
261  assert(s == nfa->states);
262  nfa->states = s->next;
263  }
264  s->prev = NULL;
265  s->next = nfa->freestates; /* don't delete it, put it on the free list */
266  nfa->freestates = s;
267 }
struct state * slast
Definition: regguts.h:356
struct state * freestates
Definition: regguts.h:357
struct state * prev
Definition: regguts.h:334

References assert, state::flag, FREESTATE, nfa::freestates, state::next, state::nins, state::no, state::nouts, state::prev, nfa::slast, and nfa::states.

Referenced by breakconstraintloop(), deltraverse(), and dropstate().

◆ hasconstraintout()

static int hasconstraintout ( struct state s)
static

Definition at line 2342 of file regc_nfa.c.

2343 {
2344  struct arc *a;
2345 
2346  for (a = s->outs; a != NULL; a = a->outchain)
2347  {
2348  if (isconstraintarc(a))
2349  return 1;
2350  }
2351  return 0;
2352 }

References a, isconstraintarc(), and state::outs.

Referenced by clonesuccessorstates().

◆ hasnonemptyout()

static int hasnonemptyout ( struct state s)
static

Definition at line 575 of file regc_nfa.c.

576 {
577  struct arc *a;
578 
579  for (a = s->outs; a != NULL; a = a->outchain)
580  {
581  if (a->type != EMPTY)
582  return 1;
583  }
584  return 0;
585 }

References a, EMPTY, and state::outs.

Referenced by fixempties().

◆ isconstraintarc()

static int isconstraintarc ( struct arc a)
inlinestatic

Definition at line 2324 of file regc_nfa.c.

2325 {
2326  switch (a->type)
2327  {
2328  case '^':
2329  case '$':
2330  case BEHIND:
2331  case AHEAD:
2332  case LACON:
2333  return 1;
2334  }
2335  return 0;
2336 }

References a, AHEAD, BEHIND, and LACON.

Referenced by breakconstraintloop(), clonesuccessorstates(), findconstraintloop(), fixconstraintloops(), and hasconstraintout().

◆ markcanreach()

static void markcanreach ( struct nfa nfa,
struct state s,
struct state okay,
struct state mark 
)
static

Definition at line 2990 of file regc_nfa.c.

2994 {
2995  struct arc *a;
2996 
2997  /* Since this is recursive, it could be driven to stack overflow */
2998  if (STACK_TOO_DEEP(nfa->v->re))
2999  {
3000  NERR(REG_ETOOBIG);
3001  return;
3002  }
3003 
3004  if (s->tmp != okay)
3005  return;
3006  s->tmp = mark;
3007 
3008  for (a = s->ins; a != NULL; a = a->inchain)
3009  markcanreach(nfa, a->from, okay, mark);
3010 }

References a, state::ins, NERR, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by cleanup().

◆ markreachable()

static void markreachable ( struct nfa nfa,
struct state s,
struct state okay,
struct state mark 
)
static

Definition at line 2964 of file regc_nfa.c.

2968 {
2969  struct arc *a;
2970 
2971  /* Since this is recursive, it could be driven to stack overflow */
2972  if (STACK_TOO_DEEP(nfa->v->re))
2973  {
2974  NERR(REG_ETOOBIG);
2975  return;
2976  }
2977 
2978  if (s->tmp != okay)
2979  return;
2980  s->tmp = mark;
2981 
2982  for (a = s->outs; a != NULL; a = a->outchain)
2983  markreachable(nfa, a->to, okay, mark);
2984 }

References a, NERR, state::outs, vars::re, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by cleanup().

◆ mergeins()

static void mergeins ( struct nfa nfa,
struct state s,
struct arc **  arcarray,
int  arccount 
)
static

Definition at line 971 of file regc_nfa.c.

975 {
976  struct arc *na;
977  int i;
978  int j;
979 
980  if (arccount <= 0)
981  return;
982 
983  /*
984  * Because we bypass newarc() in this code path, we'd better include a
985  * cancel check.
986  */
987  INTERRUPT(nfa->v->re);
988 
989  /* Sort existing inarcs as well as proposed new ones */
990  sortins(nfa, s);
991  if (NISERR())
992  return; /* might have failed to sort */
993 
994  qsort(arcarray, arccount, sizeof(struct arc *), sortins_cmp);
995 
996  /*
997  * arcarray very likely includes dups, so we must eliminate them. (This
998  * could be folded into the next loop, but it's not worth the trouble.)
999  */
1000  j = 0;
1001  for (i = 1; i < arccount; i++)
1002  {
1003  switch (sortins_cmp(&arcarray[j], &arcarray[i]))
1004  {
1005  case -1:
1006  /* non-dup */
1007  arcarray[++j] = arcarray[i];
1008  break;
1009  case 0:
1010  /* dup */
1011  break;
1012  default:
1013  /* trouble */
1014  assert(NOTREACHED);
1015  }
1016  }
1017  arccount = j + 1;
1018 
1019  /*
1020  * Now merge into s' inchain. Note that createarc() will put new arcs
1021  * onto the front of s's chain, so it does not break our walk through the
1022  * sorted part of the chain.
1023  */
1024  i = 0;
1025  na = s->ins;
1026  while (i < arccount && na != NULL)
1027  {
1028  struct arc *a = arcarray[i];
1029 
1030  switch (sortins_cmp(&a, &na))
1031  {
1032  case -1:
1033  /* s does not have anything matching a */
1034  createarc(nfa, a->type, a->co, a->from, s);
1035  i++;
1036  break;
1037  case 0:
1038  /* match, advance in both lists */
1039  i++;
1040  na = na->inchain;
1041  break;
1042  case +1:
1043  /* advance only na; array might have a match later */
1044  na = na->inchain;
1045  break;
1046  default:
1047  assert(NOTREACHED);
1048  }
1049  }
1050  while (i < arccount)
1051  {
1052  /* s does not have anything matching a */
1053  struct arc *a = arcarray[i];
1054 
1055  createarc(nfa, a->type, a->co, a->from, s);
1056  i++;
1057  }
1058 }
int j
Definition: isn.c:74

References a, assert, createarc(), i, arc::inchain, state::ins, INTERRUPT, j, NISERR, NOTREACHED, qsort, vars::re, sortins(), sortins_cmp(), and nfa::v.

Referenced by fixempties().

◆ moveins()

static void moveins ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 778 of file regc_nfa.c.

781 {
782  assert(oldState != newState);
783 
784  if (newState->nins == 0)
785  {
786  /* No need for de-duplication */
787  struct arc *a;
788 
789  while ((a = oldState->ins) != NULL)
790  {
791  createarc(nfa, a->type, a->co, a->from, newState);
792  freearc(nfa, a);
793  }
794  }
795  else if (!BULK_ARC_OP_USE_SORT(oldState->nins, newState->nins))
796  {
797  /* With not too many arcs, just do them one at a time */
798  struct arc *a;
799 
800  while ((a = oldState->ins) != NULL)
801  {
802  cparc(nfa, a, a->from, newState);
803  freearc(nfa, a);
804  }
805  }
806  else
807  {
808  /*
809  * With many arcs, use a sort-merge approach. Note changearctarget()
810  * will put the arc onto the front of newState's chain, so it does not
811  * break our walk through the sorted part of the chain.
812  */
813  struct arc *oa;
814  struct arc *na;
815 
816  /*
817  * Because we bypass newarc() in this code path, we'd better include a
818  * cancel check.
819  */
820  INTERRUPT(nfa->v->re);
821 
822  sortins(nfa, oldState);
823  sortins(nfa, newState);
824  if (NISERR())
825  return; /* might have failed to sort */
826  oa = oldState->ins;
827  na = newState->ins;
828  while (oa != NULL && na != NULL)
829  {
830  struct arc *a = oa;
831 
832  switch (sortins_cmp(&oa, &na))
833  {
834  case -1:
835  /* newState does not have anything matching oa */
836  oa = oa->inchain;
837 
838  /*
839  * Rather than doing createarc+freearc, we can just unlink
840  * and relink the existing arc struct.
841  */
842  changearctarget(a, newState);
843  break;
844  case 0:
845  /* match, advance in both lists */
846  oa = oa->inchain;
847  na = na->inchain;
848  /* ... and drop duplicate arc from oldState */
849  freearc(nfa, a);
850  break;
851  case +1:
852  /* advance only na; oa might have a match later */
853  na = na->inchain;
854  break;
855  default:
857  }
858  }
859  while (oa != NULL)
860  {
861  /* newState does not have anything matching oa */
862  struct arc *a = oa;
863 
864  oa = oa->inchain;
865  changearctarget(a, newState);
866  }
867  }
868 
869  assert(oldState->nins == 0);
870  assert(oldState->ins == NULL);
871 }
static void changearctarget(struct arc *a, struct state *newto)
Definition: regc_nfa.c:533

References a, assert, BULK_ARC_OP_USE_SORT, changearctarget(), cparc(), createarc(), freearc(), arc::inchain, state::ins, INTERRUPT, state::nins, NISERR, NOTREACHED, vars::re, sortins(), sortins_cmp(), and nfa::v.

Referenced by fixempties(), and pull().

◆ moveouts()

static void moveouts ( struct nfa nfa,
struct state oldState,
struct state newState 
)
static

Definition at line 1066 of file regc_nfa.c.

1069 {
1070  assert(oldState != newState);
1071 
1072  if (newState->nouts == 0)
1073  {
1074  /* No need for de-duplication */
1075  struct arc *a;
1076 
1077  while ((a = oldState->outs) != NULL)
1078  {
1079  createarc(nfa, a->type, a->co, newState, a->to);
1080  freearc(nfa, a);
1081  }
1082  }
1083  else if (!BULK_ARC_OP_USE_SORT(oldState->nouts, newState->nouts))
1084  {
1085  /* With not too many arcs, just do them one at a time */
1086  struct arc *a;
1087 
1088  while ((a = oldState->outs) != NULL)
1089  {
1090  cparc(nfa, a, newState, a->to);
1091  freearc(nfa, a);
1092  }
1093  }
1094  else
1095  {
1096  /*
1097  * With many arcs, use a sort-merge approach. Note changearcsource()
1098  * will put the arc onto the front of newState's chain, so it does not
1099  * break our walk through the sorted part of the chain.
1100  */
1101  struct arc *oa;
1102  struct arc *na;
1103 
1104  /*
1105  * Because we bypass newarc() in this code path, we'd better include a
1106  * cancel check.
1107  */
1108  INTERRUPT(nfa->v->re);
1109 
1110  sortouts(nfa, oldState);
1111  sortouts(nfa, newState);
1112  if (NISERR())
1113  return; /* might have failed to sort */
1114  oa = oldState->outs;
1115  na = newState->outs;
1116  while (oa != NULL && na != NULL)
1117  {
1118  struct arc *a = oa;
1119 
1120  switch (sortouts_cmp(&oa, &na))
1121  {
1122  case -1:
1123  /* newState does not have anything matching oa */
1124  oa = oa->outchain;
1125 
1126  /*
1127  * Rather than doing createarc+freearc, we can just unlink
1128  * and relink the existing arc struct.
1129  */
1130  changearcsource(a, newState);
1131  break;
1132  case 0:
1133  /* match, advance in both lists */
1134  oa = oa->outchain;
1135  na = na->outchain;
1136  /* ... and drop duplicate arc from oldState */
1137  freearc(nfa, a);
1138  break;
1139  case +1:
1140  /* advance only na; oa might have a match later */
1141  na = na->outchain;
1142  break;
1143  default:
1144  assert(NOTREACHED);
1145  }
1146  }
1147  while (oa != NULL)
1148  {
1149  /* newState does not have anything matching oa */
1150  struct arc *a = oa;
1151 
1152  oa = oa->outchain;
1153  changearcsource(a, newState);
1154  }
1155  }
1156 
1157  assert(oldState->nouts == 0);
1158  assert(oldState->outs == NULL);
1159 }
static void changearcsource(struct arc *a, struct state *newfrom)
Definition: regc_nfa.c:489

References a, assert, BULK_ARC_OP_USE_SORT, changearcsource(), cparc(), createarc(), freearc(), INTERRUPT, NISERR, NOTREACHED, state::nouts, arc::outchain, state::outs, vars::re, sortouts(), sortouts_cmp(), and nfa::v.

Referenced by fixempties(), and push().

◆ newarc()

static void newarc ( struct nfa nfa,
int  t,
color  co,
struct state from,
struct state to 
)
static

Definition at line 281 of file regc_nfa.c.

286 {
287  struct arc *a;
288 
289  assert(from != NULL && to != NULL);
290 
291  /*
292  * This is a handy place to check for operation cancel during regex
293  * compilation, since no code path will go very long without making a new
294  * state or arc.
295  */
296  INTERRUPT(nfa->v->re);
297 
298  /* check for duplicate arc, using whichever chain is shorter */
299  if (from->nouts <= to->nins)
300  {
301  for (a = from->outs; a != NULL; a = a->outchain)
302  if (a->to == to && a->co == co && a->type == t)
303  return;
304  }
305  else
306  {
307  for (a = to->ins; a != NULL; a = a->inchain)
308  if (a->from == from && a->co == co && a->type == t)
309  return;
310  }
311 
312  /* no dup, so create the arc */
313  createarc(nfa, t, co, from, to);
314 }

References a, assert, arc::co, createarc(), arc::from, state::ins, INTERRUPT, state::nins, state::nouts, state::outs, vars::re, arc::to, and nfa::v.

Referenced by cloneouts(), colorcomplement(), cparc(), dupnfa(), newnfa(), okcolors(), pull(), pullback(), push(), pushfwd(), rainbow(), removetraverse(), subcolorcvec(), subcoloronechr(), and subcoloronerow().

◆ newfstate()

static struct state* newfstate ( struct nfa nfa,
int  flag 
)
static

Definition at line 212 of file regc_nfa.c.

213 {
214  struct state *s;
215 
216  s = newstate(nfa);
217  if (s != NULL)
218  s->flag = (char) flag;
219  return s;
220 }
char * flag(int b)
Definition: test-ctype.c:33

References state::flag, flag(), and newstate().

Referenced by newnfa().

◆ newnfa()

static struct nfa* newnfa ( struct vars v,
struct colormap cm,
struct nfa parent 
)
static

Definition at line 47 of file regc_nfa.c.

50 {
51  struct nfa *nfa;
52 
53  nfa = (struct nfa *) MALLOC(sizeof(struct nfa));
54  if (nfa == NULL)
55  {
56  ERR(REG_ESPACE);
57  return NULL;
58  }
59 
60  /* Make the NFA minimally valid, so freenfa() will behave sanely */
61  nfa->states = NULL;
62  nfa->slast = NULL;
63  nfa->freestates = NULL;
64  nfa->freearcs = NULL;
65  nfa->lastsb = NULL;
66  nfa->lastab = NULL;
67  nfa->lastsbused = 0;
68  nfa->lastabused = 0;
69  nfa->nstates = 0;
70  nfa->cm = cm;
71  nfa->v = v;
72  nfa->bos[0] = nfa->bos[1] = COLORLESS;
73  nfa->eos[0] = nfa->eos[1] = COLORLESS;
74  nfa->flags = 0;
75  nfa->minmatchall = nfa->maxmatchall = -1;
76  nfa->parent = parent; /* Precedes newfstate so parent is valid. */
77 
78  /* Create required infrastructure */
79  nfa->post = newfstate(nfa, '@'); /* number 0 */
80  nfa->pre = newfstate(nfa, '>'); /* number 1 */
81  nfa->init = newstate(nfa); /* may become invalid later */
82  nfa->final = newstate(nfa);
83  if (ISERR())
84  {
85  freenfa(nfa);
86  return NULL;
87  }
89  newarc(nfa, '^', 1, nfa->pre, nfa->init);
90  newarc(nfa, '^', 0, nfa->pre, nfa->init);
92  newarc(nfa, '$', 1, nfa->final, nfa->post);
93  newarc(nfa, '$', 0, nfa->final, nfa->post);
94 
95  if (ISERR())
96  {
97  freenfa(nfa);
98  return NULL;
99  }
100  return nfa;
101 }
#define ERR
Definition: _int.h:161
static void rainbow(struct nfa *nfa, struct colormap *cm, int type, color but, struct state *from, struct state *to)
Definition: regc_color.c:1031
static void freenfa(struct nfa *nfa)
Definition: regc_nfa.c:107
static struct state * newfstate(struct nfa *nfa, int flag)
Definition: regc_nfa.c:212
#define ISERR()
Definition: regcomp.c:316
struct state * final
Definition: regguts.h:352
size_t lastsbused
Definition: regguts.h:361
struct state * init
Definition: regguts.h:351

References nfa::bos, nfa::cm, COLORLESS, nfa::eos, ERR, nfa::final, nfa::flags, nfa::freearcs, freenfa(), nfa::freestates, nfa::init, ISERR, nfa::lastab, nfa::lastabused, nfa::lastsb, nfa::lastsbused, MALLOC, nfa::maxmatchall, nfa::minmatchall, newarc(), newfstate(), newstate(), nfa::nstates, nfa::parent, PLAIN, nfa::post, nfa::pre, rainbow(), REG_ESPACE, nfa::slast, nfa::states, and nfa::v.

◆ newstate()

static struct state* newstate ( struct nfa nfa)
static

Definition at line 137 of file regc_nfa.c.

138 {
139  struct state *s;
140 
141  /*
142  * This is a handy place to check for operation cancel during regex
143  * compilation, since no code path will go very long without making a new
144  * state or arc.
145  */
146  INTERRUPT(nfa->v->re);
147 
148  /* first, recycle anything that's on the freelist */
149  if (nfa->freestates != NULL)
150  {
151  s = nfa->freestates;
152  nfa->freestates = s->next;
153  }
154  /* otherwise, is there anything left in the last statebatch? */
155  else if (nfa->lastsb != NULL && nfa->lastsbused < nfa->lastsb->nstates)
156  {
157  s = &nfa->lastsb->s[nfa->lastsbused++];
158  }
159  /* otherwise, need to allocate a new statebatch */
160  else
161  {
162  struct statebatch *newSb;
163  size_t nstates;
164 
166  {
167  NERR(REG_ETOOBIG);
168  return NULL;
169  }
170  nstates = (nfa->lastsb != NULL) ? nfa->lastsb->nstates * 2 : FIRSTSBSIZE;
171  if (nstates > MAXSBSIZE)
172  nstates = MAXSBSIZE;
173  newSb = (struct statebatch *) MALLOC(STATEBATCHSIZE(nstates));
174  if (newSb == NULL)
175  {
176  NERR(REG_ESPACE);
177  return NULL;
178  }
180  newSb->nstates = nstates;
181  newSb->next = nfa->lastsb;
182  nfa->lastsb = newSb;
183  nfa->lastsbused = 1;
184  s = &newSb->s[0];
185  }
186 
187  assert(nfa->nstates >= 0);
188  s->no = nfa->nstates++;
189  s->flag = 0;
190  if (nfa->states == NULL)
191  nfa->states = s;
192  s->nins = 0;
193  s->ins = NULL;
194  s->nouts = 0;
195  s->outs = NULL;
196  s->tmp = NULL;
197  s->next = NULL;
198  if (nfa->slast != NULL)
199  {
200  assert(nfa->slast->next == NULL);
201  nfa->slast->next = s;
202  }
203  s->prev = nfa->slast;
204  nfa->slast = s;
205  return s;
206 }
#define MAXSBSIZE
Definition: regguts.h:346
#define FIRSTSBSIZE
Definition: regguts.h:345
struct state s[FLEXIBLE_ARRAY_MEMBER]
Definition: regguts.h:341

References assert, FIRSTSBSIZE, state::flag, nfa::freestates, if(), state::ins, INTERRUPT, nfa::lastsb, nfa::lastsbused, MALLOC, MAXSBSIZE, NERR, state::next, statebatch::next, state::nins, state::no, state::nouts, statebatch::nstates, nfa::nstates, state::outs, state::prev, vars::re, REG_ESPACE, REG_ETOOBIG, REG_MAX_COMPILE_SPACE, statebatch::s, nfa::slast, vars::spaceused, STATEBATCHSIZE, nfa::states, state::tmp, and nfa::v.

Referenced by breakconstraintloop(), clonesuccessorstates(), duptraverse(), newfstate(), newnfa(), pull(), push(), and SetMatViewPopulatedState().

◆ optimize()

static long optimize ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1593 of file regc_nfa.c.

1595 {
1596 #ifdef REG_DEBUG
1597  int verbose = (f != NULL) ? 1 : 0;
1598 
1599  if (verbose)
1600  fprintf(f, "\ninitial cleanup:\n");
1601 #endif
1602  cleanup(nfa); /* may simplify situation */
1603 #ifdef REG_DEBUG
1604  if (verbose)
1605  dumpnfa(nfa, f);
1606  if (verbose)
1607  fprintf(f, "\nempties:\n");
1608 #endif
1609  fixempties(nfa, f); /* get rid of EMPTY arcs */
1610 #ifdef REG_DEBUG
1611  if (verbose)
1612  fprintf(f, "\nconstraints:\n");
1613 #endif
1614  fixconstraintloops(nfa, f); /* get rid of constraint loops */
1615  pullback(nfa, f); /* pull back constraints backward */
1616  pushfwd(nfa, f); /* push fwd constraints forward */
1617 #ifdef REG_DEBUG
1618  if (verbose)
1619  fprintf(f, "\nfinal cleanup:\n");
1620 #endif
1621  cleanup(nfa); /* final tidying */
1622 #ifdef REG_DEBUG
1623  if (verbose)
1624  dumpnfa(nfa, f);
1625 #endif
1626  return analyze(nfa); /* and analysis */
1627 }
int verbose
static void cleanup(struct nfa *nfa)
Definition: regc_nfa.c:2929
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:3016
static void fixempties(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:2069
static void pushfwd(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:1804
static void pullback(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:1633
static void fixconstraintloops(struct nfa *nfa, FILE *f)
Definition: regc_nfa.c:2363

References analyze(), cleanup(), dumpnfa(), fixconstraintloops(), fixempties(), fprintf, pullback(), pushfwd(), and verbose.

◆ pull()

static int pull ( struct nfa nfa,
struct arc con,
struct state **  intermediates 
)
static

Definition at line 1713 of file regc_nfa.c.

1716 {
1717  struct state *from = con->from;
1718  struct state *to = con->to;
1719  struct arc *a;
1720  struct arc *nexta;
1721  struct state *s;
1722 
1723  assert(from != to); /* should have gotten rid of this earlier */
1724  if (from->flag) /* can't pull back beyond start */
1725  return 0;
1726  if (from->nins == 0)
1727  { /* unreachable */
1728  freearc(nfa, con);
1729  return 1;
1730  }
1731 
1732  /*
1733  * First, clone from state if necessary to avoid other outarcs. This may
1734  * seem wasteful, but it simplifies the logic, and we'll get rid of the
1735  * clone state again at the bottom.
1736  */
1737  if (from->nouts > 1)
1738  {
1739  s = newstate(nfa);
1740  if (NISERR())
1741  return 0;
1742  copyins(nfa, from, s); /* duplicate inarcs */
1743  cparc(nfa, con, s, to); /* move constraint arc */
1744  freearc(nfa, con);
1745  if (NISERR())
1746  return 0;
1747  from = s;
1748  con = from->outs;
1749  }
1750  assert(from->nouts == 1);
1751 
1752  /* propagate the constraint into the from state's inarcs */
1753  for (a = from->ins; a != NULL && !NISERR(); a = nexta)
1754  {
1755  nexta = a->inchain;
1756  switch (combine(nfa, con, a))
1757  {
1758  case INCOMPATIBLE: /* destroy the arc */
1759  freearc(nfa, a);
1760  break;
1761  case SATISFIED: /* no action needed */
1762  break;
1763  case COMPATIBLE: /* swap the two arcs, more or less */
1764  /* need an intermediate state, but might have one already */
1765  for (s = *intermediates; s != NULL; s = s->tmp)
1766  {
1767  assert(s->nins > 0 && s->nouts > 0);
1768  if (s->ins->from == a->from && s->outs->to == to)
1769  break;
1770  }
1771  if (s == NULL)
1772  {
1773  s = newstate(nfa);
1774  if (NISERR())
1775  return 0;
1776  s->tmp = *intermediates;
1777  *intermediates = s;
1778  }
1779  cparc(nfa, con, a->from, s);
1780  cparc(nfa, a, s, to);
1781  freearc(nfa, a);
1782  break;
1783  case REPLACEARC: /* replace arc's color */
1784  newarc(nfa, a->type, con->co, a->from, to);
1785  freearc(nfa, a);
1786  break;
1787  default:
1788  assert(NOTREACHED);
1789  break;
1790  }
1791  }
1792 
1793  /* remaining inarcs, if any, incorporate the constraint */
1794  moveins(nfa, from, to);
1795  freearc(nfa, con);
1796  /* from state is now useless, but we leave it to pullback() to clean up */
1797  return 1;
1798 }
static int combine(struct nfa *nfa, struct arc *con, struct arc *a)
Definition: regc_nfa.c:1980
static void copyins(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:882

References a, assert, arc::co, combine(), COMPATIBLE, copyins(), cparc(), state::flag, freearc(), arc::from, INCOMPATIBLE, state::ins, moveins(), newarc(), newstate(), state::nins, NISERR, NOTREACHED, state::nouts, state::outs, REPLACEARC, SATISFIED, state::tmp, and arc::to.

Referenced by pullback().

◆ pullback()

static void pullback ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1633 of file regc_nfa.c.

1635 {
1636  struct state *s;
1637  struct state *nexts;
1638  struct arc *a;
1639  struct arc *nexta;
1640  struct state *intermediates;
1641  int progress;
1642 
1643  /* find and pull until there are no more */
1644  do
1645  {
1646  progress = 0;
1647  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
1648  {
1649  nexts = s->next;
1650  intermediates = NULL;
1651  for (a = s->outs; a != NULL && !NISERR(); a = nexta)
1652  {
1653  nexta = a->outchain;
1654  if (a->type == '^' || a->type == BEHIND)
1655  if (pull(nfa, a, &intermediates))
1656  progress = 1;
1657  }
1658  /* clear tmp fields of intermediate states created here */
1659  while (intermediates != NULL)
1660  {
1661  struct state *ns = intermediates->tmp;
1662 
1663  intermediates->tmp = NULL;
1664  intermediates = ns;
1665  }
1666  /* if s is now useless, get rid of it */
1667  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
1668  dropstate(nfa, s);
1669  }
1670  if (progress && f != NULL)
1671  dumpnfa(nfa, f);
1672  } while (progress && !NISERR());
1673  if (NISERR())
1674  return;
1675 
1676  /*
1677  * Any ^ constraints we were able to pull to the start state can now be
1678  * replaced by PLAIN arcs referencing the BOS or BOL colors. There should
1679  * be no other ^ or BEHIND arcs left in the NFA, though we do not check
1680  * that here (compact() will fail if so).
1681  */
1682  for (a = nfa->pre->outs; a != NULL; a = nexta)
1683  {
1684  nexta = a->outchain;
1685  if (a->type == '^')
1686  {
1687  assert(a->co == 0 || a->co == 1);
1688  newarc(nfa, PLAIN, nfa->bos[a->co], a->from, a->to);
1689  freearc(nfa, a);
1690  }
1691  }
1692 }
int progress
Definition: pgbench.c:271
static int pull(struct nfa *nfa, struct arc *con, struct state **intermediates)
Definition: regc_nfa.c:1713

References a, assert, BEHIND, nfa::bos, dropstate(), dumpnfa(), state::flag, freearc(), newarc(), state::next, state::nins, NISERR, state::nouts, state::outs, PLAIN, nfa::pre, progress, pull(), nfa::states, and state::tmp.

Referenced by optimize().

◆ push()

static int push ( struct nfa nfa,
struct arc con,
struct state **  intermediates 
)
static

Definition at line 1884 of file regc_nfa.c.

1887 {
1888  struct state *from = con->from;
1889  struct state *to = con->to;
1890  struct arc *a;
1891  struct arc *nexta;
1892  struct state *s;
1893 
1894  assert(to != from); /* should have gotten rid of this earlier */
1895  if (to->flag) /* can't push forward beyond end */
1896  return 0;
1897  if (to->nouts == 0)
1898  { /* dead end */
1899  freearc(nfa, con);
1900  return 1;
1901  }
1902 
1903  /*
1904  * First, clone to state if necessary to avoid other inarcs. This may
1905  * seem wasteful, but it simplifies the logic, and we'll get rid of the
1906  * clone state again at the bottom.
1907  */
1908  if (to->nins > 1)
1909  {
1910  s = newstate(nfa);
1911  if (NISERR())
1912  return 0;
1913  copyouts(nfa, to, s); /* duplicate outarcs */
1914  cparc(nfa, con, from, s); /* move constraint arc */
1915  freearc(nfa, con);
1916  if (NISERR())
1917  return 0;
1918  to = s;
1919  con = to->ins;
1920  }
1921  assert(to->nins == 1);
1922 
1923  /* propagate the constraint into the to state's outarcs */
1924  for (a = to->outs; a != NULL && !NISERR(); a = nexta)
1925  {
1926  nexta = a->outchain;
1927  switch (combine(nfa, con, a))
1928  {
1929  case INCOMPATIBLE: /* destroy the arc */
1930  freearc(nfa, a);
1931  break;
1932  case SATISFIED: /* no action needed */
1933  break;
1934  case COMPATIBLE: /* swap the two arcs, more or less */
1935  /* need an intermediate state, but might have one already */
1936  for (s = *intermediates; s != NULL; s = s->tmp)
1937  {
1938  assert(s->nins > 0 && s->nouts > 0);
1939  if (s->ins->from == from && s->outs->to == a->to)
1940  break;
1941  }
1942  if (s == NULL)
1943  {
1944  s = newstate(nfa);
1945  if (NISERR())
1946  return 0;
1947  s->tmp = *intermediates;
1948  *intermediates = s;
1949  }
1950  cparc(nfa, con, s, a->to);
1951  cparc(nfa, a, from, s);
1952  freearc(nfa, a);
1953  break;
1954  case REPLACEARC: /* replace arc's color */
1955  newarc(nfa, a->type, con->co, from, a->to);
1956  freearc(nfa, a);
1957  break;
1958  default:
1959  assert(NOTREACHED);
1960  break;
1961  }
1962  }
1963 
1964  /* remaining outarcs, if any, incorporate the constraint */
1965  moveouts(nfa, to, from);
1966  freearc(nfa, con);
1967  /* to state is now useless, but we leave it to pushfwd() to clean up */
1968  return 1;
1969 }
static void copyouts(struct nfa *nfa, struct state *oldState, struct state *newState)
Definition: regc_nfa.c:1167

References a, assert, arc::co, combine(), COMPATIBLE, copyouts(), cparc(), state::flag, freearc(), arc::from, INCOMPATIBLE, state::ins, moveouts(), newarc(), newstate(), state::nins, NISERR, NOTREACHED, state::nouts, state::outs, REPLACEARC, SATISFIED, state::tmp, and arc::to.

Referenced by pushfwd().

◆ pushfwd()

static void pushfwd ( struct nfa nfa,
FILE *  f 
)
static

Definition at line 1804 of file regc_nfa.c.

1806 {
1807  struct state *s;
1808  struct state *nexts;
1809  struct arc *a;
1810  struct arc *nexta;
1811  struct state *intermediates;
1812  int progress;
1813 
1814  /* find and push until there are no more */
1815  do
1816  {
1817  progress = 0;
1818  for (s = nfa->states; s != NULL && !NISERR(); s = nexts)
1819  {
1820  nexts = s->next;
1821  intermediates = NULL;
1822  for (a = s->ins; a != NULL && !NISERR(); a = nexta)
1823  {
1824  nexta = a->inchain;
1825  if (a->type == '$' || a->type == AHEAD)
1826  if (push(nfa, a, &intermediates))
1827  progress = 1;
1828  }
1829  /* clear tmp fields of intermediate states created here */
1830  while (intermediates != NULL)
1831  {
1832  struct state *ns = intermediates->tmp;
1833 
1834  intermediates->tmp = NULL;
1835  intermediates = ns;
1836  }
1837  /* if s is now useless, get rid of it */
1838  if ((s->nins == 0 || s->nouts == 0) && !s->flag)
1839  dropstate(nfa, s);
1840  }
1841  if (progress && f != NULL)
1842  dumpnfa(nfa, f);
1843  } while (progress && !NISERR());
1844  if (NISERR())
1845  return;
1846 
1847  /*
1848  * Any $ constraints we were able to push to the post state can now be
1849  * replaced by PLAIN arcs referencing the EOS or EOL colors. There should
1850  * be no other $ or AHEAD arcs left in the NFA, though we do not check
1851  * that here (compact() will fail if so).
1852  */
1853  for (a = nfa->post->ins; a != NULL; a = nexta)
1854  {
1855  nexta = a->inchain;
1856  if (a->type == '$')
1857  {
1858  assert(a->co == 0 || a->co == 1);
1859  newarc(nfa, PLAIN, nfa->eos[a->co], a->from, a->to);
1860  freearc(nfa, a);
1861  }
1862  }
1863 }
static int push(struct nfa *nfa, struct arc *con, struct state **intermediates)
Definition: regc_nfa.c:1884

References a, AHEAD, assert, dropstate(), dumpnfa(), nfa::eos, state::flag, freearc(), state::ins, newarc(), state::next, state::nins, NISERR, state::nouts, PLAIN, nfa::post, progress, push(), nfa::states, and state::tmp.

Referenced by optimize().

◆ removeconstraints()

static void removeconstraints ( struct nfa nfa,
struct state start,
struct state stop 
)
static

Definition at line 1419 of file regc_nfa.c.

1422 {
1423  if (start == stop)
1424  return;
1425 
1426  stop->tmp = stop;
1427  removetraverse(nfa, start);
1428  /* done, except for clearing out the tmp pointers */
1429 
1430  stop->tmp = NULL;
1431  cleartraverse(nfa, start);
1432 }
static void removetraverse(struct nfa *nfa, struct state *s)
Definition: regc_nfa.c:1438

References cleartraverse(), removetraverse(), and state::tmp.

◆ removetraverse()

static void removetraverse ( struct nfa nfa,
struct state s 
)
static

Definition at line 1438 of file regc_nfa.c.

1440 {
1441  struct arc *a;
1442  struct arc *oa;
1443 
1444  /* Since this is recursive, it could be driven to stack overflow */
1445  if (STACK_TOO_DEEP(nfa->v->re))
1446  {
1447  NERR(REG_ETOOBIG);
1448  return;
1449  }
1450 
1451  if (s->tmp != NULL)
1452  return; /* already done */
1453 
1454  s->tmp = s;
1455  for (a = s->outs; a != NULL && !NISERR(); a = oa)
1456  {
1457  removetraverse(nfa, a->to);
1458  if (NISERR())
1459  break;
1460  oa = a->outchain;
1461  switch (a->type)
1462  {
1463  case PLAIN:
1464  case EMPTY:
1465  /* nothing to do */
1466  break;
1467  case AHEAD:
1468  case BEHIND:
1469  case '^':
1470  case '$':
1471  case LACON:
1472  /* replace it */
1473  newarc(nfa, EMPTY, 0, s, a->to);
1474  freearc(nfa, a);
1475  break;
1476  default:
1477  NERR(REG_ASSERT);
1478  break;
1479  }
1480  }
1481 }

References a, AHEAD, BEHIND, EMPTY, freearc(), LACON, NERR, newarc(), NISERR, state::outs, PLAIN, vars::re, REG_ASSERT, REG_ETOOBIG, STACK_TOO_DEEP, state::tmp, and nfa::v.

Referenced by removeconstraints().

◆ single_color_transition()

static struct state* single_color_transition ( struct state s1,
struct state s2 
)
static

Definition at line 1524 of file regc_nfa.c.

1525 {
1526  struct arc *a;
1527 
1528  /* Ignore leading EMPTY arc, if any */
1529  if (s1->nouts == 1 && s1->outs->type == EMPTY)
1530  s1 = s1->outs->to;
1531  /* Likewise for any trailing EMPTY arc */
1532  if (s2->nins == 1 && s2->ins->type == EMPTY)
1533  s2 = s2->ins->from;
1534  /* Perhaps we could have a single-state loop in between, if so reject */
1535  if (s1 == s2)
1536  return NULL;
1537  /* s1 must have at least one outarc... */
1538  if (s1->outs == NULL)
1539  return NULL;
1540  /* ... and they must all be PLAIN arcs to s2 */
1541  for (a = s1->outs; a != NULL; a = a->outchain)
1542  {
1543  if (a->type != PLAIN || a->to != s2)
1544  return NULL;
1545  }
1546  /* OK, return s1 as the possessor of the relevant outarcs */
1547  return s1;
1548 }
char * s1

References a, EMPTY, PLAIN, s1, and s2.

◆ sortins()

static void sortins ( struct nfa nfa,
struct state s 
)
static

Definition at line 620 of file regc_nfa.c.

622 {
623  struct arc **sortarray;
624  struct arc *a;
625  int n = s->nins;
626  int i;
627 
628  if (n <= 1)
629  return; /* nothing to do */
630  /* make an array of arc pointers ... */
631  sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *));
632  if (sortarray == NULL)
633  {
634  NERR(REG_ESPACE);
635  return;
636  }
637  i = 0;
638  for (a = s->ins; a != NULL; a = a->inchain)
639  sortarray[i++] = a;
640  assert(i == n);
641  /* ... sort the array */
642  qsort(sortarray, n, sizeof(struct arc *), sortins_cmp);
643  /* ... and rebuild arc list in order */
644  /* it seems worth special-casing first and last items to simplify loop */
645  a = sortarray[0];
646  s->ins = a;
647  a->inchain = sortarray[1];
648  a->inchainRev = NULL;
649  for (i = 1; i < n - 1; i++)
650  {
651  a = sortarray[i];
652  a->inchain = sortarray[i + 1];
653  a->inchainRev = sortarray[i - 1];
654  }
655  a = sortarray[i];
656  a->inchain = NULL;
657  a->inchainRev = sortarray[i - 1];
658  FREE(sortarray);
659 }

References a, assert, FREE, i, state::ins, MALLOC, NERR, state::nins, qsort, REG_ESPACE, and sortins_cmp().

Referenced by copyins(), mergeins(), and moveins().

◆ sortins_cmp()

static int sortins_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 662 of file regc_nfa.c.

663 {
664  const struct arc *aa = *((const struct arc *const *) a);
665  const struct arc *bb = *((const struct arc *const *) b);
666 
667  /* we check the fields in the order they are most likely to be different */
668  if (aa->from->no < bb->from->no)
669  return -1;
670  if (aa->from->no > bb->from->no)
671  return 1;
672  if (aa->co < bb->co)
673  return -1;
674  if (aa->co > bb->co)
675  return 1;
676  if (aa->type < bb->type)
677  return -1;
678  if (aa->type > bb->type)
679  return 1;
680  return 0;
681 }

References a, b, arc::co, arc::from, state::no, and arc::type.

Referenced by copyins(), mergeins(), moveins(), and sortins().

◆ sortouts()

static void sortouts ( struct nfa nfa,
struct state s 
)
static

Definition at line 687 of file regc_nfa.c.

689 {
690  struct arc **sortarray;
691  struct arc *a;
692  int n = s->nouts;
693  int i;
694 
695  if (n <= 1)
696  return; /* nothing to do */
697  /* make an array of arc pointers ... */
698  sortarray = (struct arc **) MALLOC(n * sizeof(struct arc *));
699  if (sortarray == NULL)
700  {
701  NERR(REG_ESPACE);
702  return;
703  }
704  i = 0;
705  for (a = s->outs; a != NULL; a = a->outchain)
706  sortarray[i++] = a;
707  assert(i == n);
708  /* ... sort the array */
709  qsort(sortarray, n, sizeof(struct arc *), sortouts_cmp);
710  /* ... and rebuild arc list in order */
711  /* it seems worth special-casing first and last items to simplify loop */
712  a = sortarray[0];
713  s->outs = a;
714  a->outchain = sortarray[1];
715  a->outchainRev = NULL;
716  for (i = 1; i < n - 1; i++)
717  {
718  a = sortarray[i];
719  a->outchain = sortarray[i + 1];
720  a->outchainRev = sortarray[i - 1];
721  }
722  a = sortarray[i];
723  a->outchain = NULL;
724  a->outchainRev = sortarray[i - 1];
725  FREE(sortarray);
726 }

References a, assert, FREE, i, MALLOC, NERR, state::nouts, state::outs, qsort, REG_ESPACE, and sortouts_cmp().

Referenced by copyouts(), and moveouts().

◆ sortouts_cmp()

static int sortouts_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 729 of file regc_nfa.c.

730 {
731  const struct arc *aa = *((const struct arc *const *) a);
732  const struct arc *bb = *((const struct arc *const *) b);
733 
734  /* we check the fields in the order they are most likely to be different */
735  if (aa->to->no < bb->to->no)
736  return -1;
737  if (aa->to->no > bb->to->no)
738  return 1;
739  if (aa->co < bb->co)
740  return -1;
741  if (aa->co > bb->co)
742  return 1;
743  if (aa->type < bb->type)
744  return -1;
745  if (aa->type > bb->type)
746  return 1;
747  return 0;
748 }

References a, b, arc::co, state::no, arc::to, and arc::type.

Referenced by copyouts(), moveouts(), and sortouts().

◆ specialcolors()

static void specialcolors ( struct nfa nfa)
static

Definition at line 1554 of file regc_nfa.c.

1555 {
1556  /* false colors for BOS, BOL, EOS, EOL */
1557  if (nfa->parent == NULL)
1558  {
1559  nfa->bos[0] = pseudocolor(nfa->cm);
1560  nfa->bos[1] = pseudocolor(nfa->cm);
1561  nfa->eos[0] = pseudocolor(nfa->cm);
1562  nfa->eos[1] = pseudocolor(nfa->cm);
1563  }
1564  else
1565  {
1566  assert(nfa->parent->bos[0] != COLORLESS);
1567  nfa->bos[0] = nfa->parent->bos[0];
1568  assert(nfa->parent->bos[1] != COLORLESS);
1569  nfa->bos[1] = nfa->parent->bos[1];
1570  assert(nfa->parent->eos[0] != COLORLESS);
1571  nfa->eos[0] = nfa->parent->eos[0];
1572  assert(nfa->parent->eos[1] != COLORLESS);
1573  nfa->eos[1] = nfa->parent->eos[1];
1574  }
1575 }
static color pseudocolor(struct colormap *cm)
Definition: regc_color.c:312

References assert, nfa::bos, nfa::cm, COLORLESS, nfa::eos, nfa::parent, and pseudocolor().