PostgreSQL Source Code  git master
regcomp.c File Reference
#include "regex/regguts.h"
#include "regc_lex.c"
#include "regc_color.c"
#include "regc_nfa.c"
#include "regc_cvec.c"
#include "regc_pg_locale.c"
#include "regc_locale.c"
Include dependency graph for regcomp.c:

Go to the source code of this file.

Data Structures

struct  vars
 

Macros

#define INCOMPATIBLE   1 /* destroys arc */
 
#define SATISFIED   2 /* constraint satisfied */
 
#define COMPATIBLE   3 /* compatible but not satisfied yet */
 
#define REPLACEARC   4 /* replace arc's color with constraint color */
 
#define NEXT()   (next(v)) /* advance by one token */
 
#define SEE(t)   (v->nexttype == (t)) /* is next token this? */
 
#define EAT(t)   (SEE(t) && next(v)) /* if next is this, swallow it */
 
#define VISERR(vv)   ((vv)->err != 0) /* have we seen an error yet? */
 
#define ISERR()   VISERR(v)
 
#define VERR(vv, e)
 
#define ERR(e)   VERR(v, e) /* record an error */
 
#define NOERR()   {if (ISERR()) return;} /* if error seen, return */
 
#define NOERRN()   {if (ISERR()) return NULL;} /* NOERR with retval */
 
#define NOERRZ()   {if (ISERR()) return 0;} /* NOERR with retval */
 
#define INSIST(c, e)   do { if (!(c)) ERR(e); } while (0) /* error if c false */
 
#define NOTE(b)   (v->re->re_info |= (b)) /* note visible condition */
 
#define EMPTYARC(x, y)   newarc(v->nfa, EMPTY, 0, x, y)
 
#define EMPTY   'n' /* no token present */
 
#define EOS   'e' /* end of string */
 
#define PLAIN   'p' /* ordinary character */
 
#define DIGIT   'd' /* digit (in bound) */
 
#define BACKREF   'b' /* back reference */
 
#define COLLEL   'I' /* start of [. */
 
#define ECLASS   'E' /* start of [= */
 
#define CCLASS   'C' /* start of [: */
 
#define END   'X' /* end of [. [= [: */
 
#define CCLASSS   's' /* char class shorthand escape */
 
#define CCLASSC   'c' /* complement char class shorthand escape */
 
#define RANGE   'R' /* - within [] which might be range delim. */
 
#define LACON   'L' /* lookaround constraint subRE */
 
#define AHEAD   'a' /* color-lookahead arc */
 
#define BEHIND   'r' /* color-lookbehind arc */
 
#define WBDRY   'w' /* word boundary constraint */
 
#define NWBDRY   'W' /* non-word-boundary constraint */
 
#define SBEGIN   'A' /* beginning of string (even if not BOL) */
 
#define SEND   'Z' /* end of string (even if not EOL) */
 
#define COLORED(a)
 
#define CNOERR()   { if (ISERR()) return freev(v, v->err); }
 
#define ARCV(t, val)   newarc(v->nfa, t, val, lp, rp)
 
#define SOME   2
 
#define INF   3
 
#define PAIR(x, y)   ((x)*4 + (y))
 
#define REDUCE(x)   ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
 

Functions

static void moresubs (struct vars *, int)
 
static int freev (struct vars *, int)
 
static void makesearch (struct vars *, struct nfa *)
 
static struct subreparse (struct vars *, int, int, struct state *, struct state *)
 
static struct subreparsebranch (struct vars *, int, int, struct state *, struct state *, int)
 
static struct subreparseqatom (struct vars *, int, int, struct state *, struct state *, struct subre *)
 
static void nonword (struct vars *, int, struct state *, struct state *)
 
static void word (struct vars *, int, struct state *, struct state *)
 
static void charclass (struct vars *, enum char_classes, struct state *, struct state *)
 
static void charclasscomplement (struct vars *, enum char_classes, struct state *, struct state *)
 
static int scannum (struct vars *)
 
static void repeat (struct vars *, struct state *, struct state *, int, int)
 
static void bracket (struct vars *, struct state *, struct state *)
 
static void cbracket (struct vars *, struct state *, struct state *)
 
static void brackpart (struct vars *, struct state *, struct state *, bool *)
 
static const chrscanplain (struct vars *)
 
static void onechr (struct vars *, chr, struct state *, struct state *)
 
static void optimizebracket (struct vars *, struct state *, struct state *)
 
static void wordchrs (struct vars *)
 
static void processlacon (struct vars *, struct state *, struct state *, int, struct state *, struct state *)
 
static struct subresubre (struct vars *, int, int, struct state *, struct state *)
 
static void freesubre (struct vars *, struct subre *)
 
static void freesubreandsiblings (struct vars *, struct subre *)
 
static void freesrnode (struct vars *, struct subre *)
 
static void removecaptures (struct vars *, struct subre *)
 
static int numst (struct subre *, int)
 
static void markst (struct subre *)
 
static void cleanst (struct vars *)
 
static long nfatree (struct vars *, struct subre *, FILE *)
 
static long nfanode (struct vars *, struct subre *, int, FILE *)
 
static int newlacon (struct vars *, struct state *, struct state *, int)
 
static void freelacons (struct subre *, int)
 
static void rfree (regex_t *)
 
static int rcancelrequested (void)
 
static int rstacktoodeep (void)
 
static void lexstart (struct vars *)
 
static void prefixes (struct vars *)
 
static int next (struct vars *)
 
static int lexescape (struct vars *)
 
static chr lexdigits (struct vars *, int, int, int)
 
static int brenext (struct vars *, chr)
 
static void skip (struct vars *)
 
static chr newline (void)
 
static chr chrnamed (struct vars *, const chr *, const chr *, chr)
 
static void initcm (struct vars *, struct colormap *)
 
static void freecm (struct colormap *)
 
static color maxcolor (struct colormap *)
 
static color newcolor (struct colormap *)
 
static void freecolor (struct colormap *, color)
 
static color pseudocolor (struct colormap *)
 
static color subcolor (struct colormap *, chr)
 
static color subcolorhi (struct colormap *, color *)
 
static color newsub (struct colormap *, color)
 
static int newhicolorrow (struct colormap *, int)
 
static void newhicolorcols (struct colormap *)
 
static void subcolorcvec (struct vars *, struct cvec *, struct state *, struct state *)
 
static void subcoloronechr (struct vars *, chr, struct state *, struct state *, color *)
 
static void subcoloronerange (struct vars *, chr, chr, struct state *, struct state *, color *)
 
static void subcoloronerow (struct vars *, int, struct state *, struct state *, color *)
 
static void okcolors (struct nfa *, struct colormap *)
 
static void colorchain (struct colormap *, struct arc *)
 
static void uncolorchain (struct colormap *, struct arc *)
 
static void rainbow (struct nfa *, struct colormap *, int, color, struct state *, struct state *)
 
static void colorcomplement (struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)
 
static struct nfanewnfa (struct vars *, struct colormap *, struct nfa *)
 
static void freenfa (struct nfa *)
 
static struct statenewstate (struct nfa *)
 
static struct statenewfstate (struct nfa *, int flag)
 
static void dropstate (struct nfa *, struct state *)
 
static void freestate (struct nfa *, struct state *)
 
static void newarc (struct nfa *, int, color, struct state *, struct state *)
 
static void createarc (struct nfa *, int, color, struct state *, struct state *)
 
static struct arcallocarc (struct nfa *)
 
static void freearc (struct nfa *, struct arc *)
 
static void changearcsource (struct arc *, struct state *)
 
static void changearctarget (struct arc *, struct state *)
 
static int hasnonemptyout (struct state *)
 
static struct arcfindarc (struct state *, int, color)
 
static void cparc (struct nfa *, struct arc *, struct state *, struct state *)
 
static void sortins (struct nfa *, struct state *)
 
static int sortins_cmp (const void *, const void *)
 
static void sortouts (struct nfa *, struct state *)
 
static int sortouts_cmp (const void *, const void *)
 
static void moveins (struct nfa *, struct state *, struct state *)
 
static void copyins (struct nfa *, struct state *, struct state *)
 
static void mergeins (struct nfa *, struct state *, struct arc **, int)
 
static void moveouts (struct nfa *, struct state *, struct state *)
 
static void copyouts (struct nfa *, struct state *, struct state *)
 
static void cloneouts (struct nfa *, struct state *, struct state *, struct state *, int)
 
static void delsub (struct nfa *, struct state *, struct state *)
 
static void deltraverse (struct nfa *, struct state *, struct state *)
 
static void dupnfa (struct nfa *, struct state *, struct state *, struct state *, struct state *)
 
static void duptraverse (struct nfa *, struct state *, struct state *)
 
static void removeconstraints (struct nfa *, struct state *, struct state *)
 
static void removetraverse (struct nfa *, struct state *)
 
static void cleartraverse (struct nfa *, struct state *)
 
static struct statesingle_color_transition (struct state *, struct state *)
 
static void specialcolors (struct nfa *)
 
static long optimize (struct nfa *, FILE *)
 
static void pullback (struct nfa *, FILE *)
 
static int pull (struct nfa *, struct arc *, struct state **)
 
static void pushfwd (struct nfa *, FILE *)
 
static int push (struct nfa *, struct arc *, struct state **)
 
static int combine (struct nfa *nfa, struct arc *con, struct arc *a)
 
static void fixempties (struct nfa *, FILE *)
 
static struct stateemptyreachable (struct nfa *, struct state *, struct state *, struct arc **)
 
static int isconstraintarc (struct arc *)
 
static int hasconstraintout (struct state *)
 
static void fixconstraintloops (struct nfa *, FILE *)
 
static int findconstraintloop (struct nfa *, struct state *)
 
static void breakconstraintloop (struct nfa *, struct state *)
 
static void clonesuccessorstates (struct nfa *, struct state *, struct state *, struct state *, struct arc *, char *, char *, int)
 
static void cleanup (struct nfa *)
 
static void markreachable (struct nfa *, struct state *, struct state *, struct state *)
 
static void markcanreach (struct nfa *, struct state *, struct state *, struct state *)
 
static long analyze (struct nfa *)
 
static void checkmatchall (struct nfa *)
 
static bool checkmatchall_recurse (struct nfa *, struct state *, bool **)
 
static bool check_out_colors_match (struct state *, color, color)
 
static bool check_in_colors_match (struct state *, color, color)
 
static void compact (struct nfa *, struct cnfa *)
 
static void carcsort (struct carc *, size_t)
 
static int carc_cmp (const void *, const void *)
 
static void freecnfa (struct cnfa *)
 
static void dumpnfa (struct nfa *, FILE *)
 
static struct cvecnewcvec (int, int)
 
static struct cvecclearcvec (struct cvec *)
 
static void addchr (struct cvec *, chr)
 
static void addrange (struct cvec *, chr, chr)
 
static struct cvecgetcvec (struct vars *, int, int)
 
static void freecvec (struct cvec *)
 
static int pg_wc_isdigit (pg_wchar c)
 
static int pg_wc_isalpha (pg_wchar c)
 
static int pg_wc_isalnum (pg_wchar c)
 
static int pg_wc_isword (pg_wchar c)
 
static int pg_wc_isupper (pg_wchar c)
 
static int pg_wc_islower (pg_wchar c)
 
static int pg_wc_isgraph (pg_wchar c)
 
static int pg_wc_isprint (pg_wchar c)
 
static int pg_wc_ispunct (pg_wchar c)
 
static int pg_wc_isspace (pg_wchar c)
 
static pg_wchar pg_wc_toupper (pg_wchar c)
 
static pg_wchar pg_wc_tolower (pg_wchar c)
 
static chr element (struct vars *, const chr *, const chr *)
 
static struct cvecrange (struct vars *, chr, chr, int)
 
static int before (chr, chr)
 
static struct cvececlass (struct vars *, chr, int)
 
static enum char_classes lookupcclass (struct vars *, const chr *, const chr *)
 
static struct cveccclasscvec (struct vars *, enum char_classes, int)
 
static int cclass_column_index (struct colormap *, chr)
 
static struct cvecallcases (struct vars *, chr)
 
static int cmp (const chr *, const chr *, size_t)
 
static int casecmp (const chr *, const chr *, size_t)
 
int pg_regcomp (regex_t *re, const chr *string, size_t len, int flags, Oid collation)
 

Variables

static const struct fns functions
 

Macro Definition Documentation

◆ AHEAD

#define AHEAD   'a' /* color-lookahead arc */

◆ ARCV

#define ARCV (   t,
  val 
)    newarc(v->nfa, t, val, lp, rp)

Referenced by parseqatom().

◆ BACKREF

#define BACKREF   'b' /* back reference */

Definition at line 289 of file regcomp.c.

Referenced by brenext(), lexescape(), and parseqatom().

◆ BEHIND

#define BEHIND   'r' /* color-lookbehind arc */

◆ CCLASS

#define CCLASS   'C' /* start of [: */

Definition at line 292 of file regcomp.c.

Referenced by brackpart(), next(), and scanplain().

◆ CCLASSC

#define CCLASSC   'c' /* complement char class shorthand escape */

Definition at line 295 of file regcomp.c.

Referenced by brackpart(), lexescape(), next(), and parseqatom().

◆ CCLASSS

#define CCLASSS   's' /* char class shorthand escape */

Definition at line 294 of file regcomp.c.

Referenced by brackpart(), lexescape(), next(), and parseqatom().

◆ CNOERR

#define CNOERR ( )    { if (ISERR()) return freev(v, v->err); }

Referenced by pg_regcomp().

◆ COLLEL

#define COLLEL   'I' /* start of [. */

Definition at line 290 of file regcomp.c.

Referenced by brackpart(), next(), and scanplain().

◆ COLORED

#define COLORED (   a)
Value:
((a)->co >= 0 && \
((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND))
#define BEHIND
Definition: regcomp.c:299
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
#define AHEAD
Definition: regcomp.c:298

Definition at line 307 of file regcomp.c.

Referenced by createarc(), and freearc().

◆ COMPATIBLE

#define COMPATIBLE   3 /* compatible but not satisfied yet */

Definition at line 166 of file regcomp.c.

Referenced by combine(), pull(), and push().

◆ DIGIT

#define DIGIT   'd' /* digit (in bound) */

Definition at line 288 of file regcomp.c.

Referenced by next(), parseqatom(), and scannum().

◆ EAT

#define EAT (   t)    (SEE(t) && next(v)) /* if next is this, swallow it */

Definition at line 271 of file regcomp.c.

Referenced by parse(), and parseqatom().

◆ ECLASS

#define ECLASS   'E' /* start of [= */

Definition at line 291 of file regcomp.c.

Referenced by brackpart(), next(), and scanplain().

◆ EMPTY

#define EMPTY   'n' /* no token present */

◆ EMPTYARC

#define EMPTYARC (   x,
 
)    newarc(v->nfa, EMPTY, 0, x, y)

Definition at line 282 of file regcomp.c.

Referenced by parse(), parsebranch(), parseqatom(), and repeat().

◆ END

#define END   'X' /* end of [. [= [: */

Definition at line 293 of file regcomp.c.

Referenced by scanplain().

◆ EOS

#define EOS   'e' /* end of string */

Definition at line 286 of file regcomp.c.

Referenced by bracket(), next(), parse(), parsebranch(), parseqatom(), and pg_regcomp().

◆ ERR

#define ERR (   e)    VERR(v, e) /* record an error */

Definition at line 276 of file regcomp.c.

Referenced by brackpart(), freev(), moresubs(), newlacon(), parse(), parseqatom(), repeat(), scannum(), and subre().

◆ INCOMPATIBLE

#define INCOMPATIBLE   1 /* destroys arc */

Definition at line 164 of file regcomp.c.

Referenced by combine(), pull(), and push().

◆ INF

#define INF   3

Referenced by repeat().

◆ INSIST

#define INSIST (   c,
  e 
)    do { if (!(c)) ERR(e); } while (0) /* error if c false */

Definition at line 280 of file regcomp.c.

Referenced by brackpart(), and parseqatom().

◆ ISERR

#define ISERR ( )    VISERR(v)

Definition at line 273 of file regcomp.c.

Referenced by bracket(), lexescape(), longest(), miss(), newnfa(), next(), nfanode(), parseqatom(), and scanplain().

◆ LACON

#define LACON   'L' /* lookaround constraint subRE */

◆ NEXT

#define NEXT ( )    (next(v)) /* advance by one token */

Definition at line 269 of file regcomp.c.

Referenced by bracket(), brackpart(), parseqatom(), scannum(), and scanplain().

◆ NOERR

#define NOERR ( )    {if (ISERR()) return;} /* if error seen, return */

◆ NOERRN

#define NOERRN ( )    {if (ISERR()) return NULL;} /* NOERR with retval */

Definition at line 278 of file regcomp.c.

Referenced by parse(), parsebranch(), parseqatom(), and range().

◆ NOERRZ

#define NOERRZ ( )    {if (ISERR()) return 0;} /* NOERR with retval */

Definition at line 279 of file regcomp.c.

Referenced by nfanode().

◆ NOTE

#define NOTE (   b)    (v->re->re_info |= (b)) /* note visible condition */

◆ NWBDRY

#define NWBDRY   'W' /* non-word-boundary constraint */

Definition at line 301 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

◆ PAIR

#define PAIR (   x,
 
)    ((x)*4 + (y))

Referenced by repeat().

◆ PLAIN

◆ RANGE

#define RANGE   'R' /* - within [] which might be range delim. */

Definition at line 296 of file regcomp.c.

Referenced by brackpart(), and next().

◆ REDUCE

#define REDUCE (   x)    ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )

Referenced by repeat().

◆ REPLACEARC

#define REPLACEARC   4 /* replace arc's color with constraint color */

Definition at line 167 of file regcomp.c.

Referenced by combine(), pull(), and push().

◆ SATISFIED

#define SATISFIED   2 /* constraint satisfied */

Definition at line 165 of file regcomp.c.

Referenced by combine(), pull(), and push().

◆ SBEGIN

#define SBEGIN   'A' /* beginning of string (even if not BOL) */

Definition at line 302 of file regcomp.c.

Referenced by lexescape(), next(), and parseqatom().

◆ SEE

#define SEE (   t)    (v->nexttype == (t)) /* is next token this? */

Definition at line 270 of file regcomp.c.

Referenced by bracket(), brackpart(), parse(), parsebranch(), parseqatom(), pg_regcomp(), scannum(), and scanplain().

◆ SEND

#define SEND   'Z' /* end of string (even if not EOL) */

Definition at line 303 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

◆ SOME

#define SOME   2

Referenced by repeat().

◆ VERR

#define VERR (   vv,
  e 
)
Value:
((vv)->nexttype = EOS, \
(vv)->err = ((vv)->err ? (vv)->err : (e)))
#define EOS
Definition: regcomp.c:286
e
Definition: preproc-init.c:82

Definition at line 274 of file regcomp.c.

◆ VISERR

#define VISERR (   vv)    ((vv)->err != 0) /* have we seen an error yet? */

Definition at line 272 of file regcomp.c.

◆ WBDRY

#define WBDRY   'w' /* word boundary constraint */

Definition at line 300 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

Function Documentation

◆ addchr()

static void addchr ( struct cvec ,
chr   
)
static

◆ addrange()

static void addrange ( struct cvec ,
chr  ,
chr   
)
static

◆ allcases()

static struct cvec* allcases ( struct vars ,
chr   
)
static

Referenced by onechr().

◆ allocarc()

static struct arc* allocarc ( struct nfa )
static

◆ analyze()

static long analyze ( struct nfa )
static

◆ before()

static int before ( chr  ,
chr   
)
static

◆ bracket()

static void bracket ( struct vars v,
struct state lp,
struct state rp 
)
static

Definition at line 1629 of file regcomp.c.

References assert, brackpart(), charclasscomplement(), vars::cm, EOS, i, ISERR, NEXT, vars::nfa, NOERR, NUM_CCLASSES, okcolors(), optimizebracket(), and SEE.

Referenced by cbracket(), and parseqatom().

1632 {
1633  /*
1634  * We can't process complemented char classes (e.g. \W) immediately while
1635  * scanning the bracket expression, else color bookkeeping gets confused.
1636  * Instead, remember whether we saw any in have_cclassc[], and process
1637  * them at the end.
1638  */
1639  bool have_cclassc[NUM_CCLASSES];
1640  bool any_cclassc;
1641  int i;
1642 
1643  memset(have_cclassc, false, sizeof(have_cclassc));
1644 
1645  assert(SEE('['));
1646  NEXT();
1647  while (!SEE(']') && !SEE(EOS))
1648  brackpart(v, lp, rp, have_cclassc);
1649  assert(SEE(']') || ISERR());
1650 
1651  /* close up open subcolors from the positive bracket elements */
1652  okcolors(v->nfa, v->cm);
1653  NOERR();
1654 
1655  /* now handle any complemented elements */
1656  any_cclassc = false;
1657  for (i = 0; i < NUM_CCLASSES; i++)
1658  {
1659  if (have_cclassc[i])
1660  {
1661  charclasscomplement(v, (enum char_classes) i, lp, rp);
1662  NOERR();
1663  any_cclassc = true;
1664  }
1665  }
1666 
1667  /*
1668  * If we had any complemented elements, see if we can optimize the bracket
1669  * into a rainbow. Since a complemented element is the only way a WHITE
1670  * arc could get into the result, there's no point in checking otherwise.
1671  */
1672  if (any_cclassc)
1673  optimizebracket(v, lp, rp);
1674 }
static void okcolors(struct nfa *, struct colormap *)
static void brackpart(struct vars *, struct state *, struct state *, bool *)
Definition: regcomp.c:1719
#define NEXT()
Definition: regcomp.c:269
#define ISERR()
Definition: regcomp.c:273
#define NUM_CCLASSES
Definition: regguts.h:139
struct colormap * cm
Definition: regcomp.c:253
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:270
struct nfa * nfa
Definition: regcomp.c:252
char_classes
Definition: regguts.h:133
#define EOS
Definition: regcomp.c:286
static void charclasscomplement(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1474
static void optimizebracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1893
#define NOERR()
Definition: regcomp.c:277
int i

◆ brackpart()

static void brackpart ( struct vars v,
struct state lp,
struct state rp,
bool have_cclassc 
)
static

Definition at line 1719 of file regcomp.c.

References CCLASS, CCLASSC, CCLASSS, vars::cflags, charclass(), COLLEL, vars::cv, eclass(), ECLASS, element(), ERR, INSIST, lookupcclass(), NEXT, vars::nexttype, vars::nextvalue, NOERR, NOTE, vars::now, onechr(), PLAIN, range(), RANGE, REG_ASSERT, REG_ECOLLATE, REG_ECTYPE, REG_ERANGE, REG_ICASE, REG_UUNPORT, scanplain(), SEE, and subcolorcvec().

Referenced by bracket().

1723 {
1724  chr startc;
1725  chr endc;
1726  struct cvec *cv;
1727  enum char_classes cls;
1728  const chr *startp;
1729  const chr *endp;
1730 
1731  /* parse something, get rid of special cases, take shortcuts */
1732  switch (v->nexttype)
1733  {
1734  case RANGE: /* a-b-c or other botch */
1735  ERR(REG_ERANGE);
1736  return;
1737  break;
1738  case PLAIN:
1739  startc = v->nextvalue;
1740  NEXT();
1741  /* shortcut for ordinary chr (not range) */
1742  if (!SEE(RANGE))
1743  {
1744  onechr(v, startc, lp, rp);
1745  return;
1746  }
1747  NOERR();
1748  break;
1749  case COLLEL:
1750  startp = v->now;
1751  endp = scanplain(v);
1752  INSIST(startp < endp, REG_ECOLLATE);
1753  NOERR();
1754  startc = element(v, startp, endp);
1755  NOERR();
1756  break;
1757  case ECLASS:
1758  startp = v->now;
1759  endp = scanplain(v);
1760  INSIST(startp < endp, REG_ECOLLATE);
1761  NOERR();
1762  startc = element(v, startp, endp);
1763  NOERR();
1764  cv = eclass(v, startc, (v->cflags & REG_ICASE));
1765  NOERR();
1766  subcolorcvec(v, cv, lp, rp);
1767  return;
1768  break;
1769  case CCLASS:
1770  startp = v->now;
1771  endp = scanplain(v);
1772  INSIST(startp < endp, REG_ECTYPE);
1773  NOERR();
1774  cls = lookupcclass(v, startp, endp);
1775  NOERR();
1776  charclass(v, cls, lp, rp);
1777  return;
1778  break;
1779  case CCLASSS:
1780  charclass(v, (enum char_classes) v->nextvalue, lp, rp);
1781  NEXT();
1782  return;
1783  break;
1784  case CCLASSC:
1785  /* we cannot call charclasscomplement() immediately */
1786  have_cclassc[v->nextvalue] = true;
1787  NEXT();
1788  return;
1789  break;
1790  default:
1791  ERR(REG_ASSERT);
1792  return;
1793  break;
1794  }
1795 
1796  if (SEE(RANGE))
1797  {
1798  NEXT();
1799  switch (v->nexttype)
1800  {
1801  case PLAIN:
1802  case RANGE:
1803  endc = v->nextvalue;
1804  NEXT();
1805  NOERR();
1806  break;
1807  case COLLEL:
1808  startp = v->now;
1809  endp = scanplain(v);
1810  INSIST(startp < endp, REG_ECOLLATE);
1811  NOERR();
1812  endc = element(v, startp, endp);
1813  NOERR();
1814  break;
1815  default:
1816  ERR(REG_ERANGE);
1817  return;
1818  break;
1819  }
1820  }
1821  else
1822  endc = startc;
1823 
1824  /*
1825  * Ranges are unportable. Actually, standard C does guarantee that digits
1826  * are contiguous, but making that an exception is just too complicated.
1827  */
1828  if (startc != endc)
1829  NOTE(REG_UUNPORT);
1830  cv = range(v, startc, endc, (v->cflags & REG_ICASE));
1831  NOERR();
1832  subcolorcvec(v, cv, lp, rp);
1833 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
#define RANGE
Definition: regcomp.c:296
int nexttype
Definition: regcomp.c:245
#define CCLASS
Definition: regcomp.c:292
chr nextvalue
Definition: regcomp.c:246
static void charclass(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1450
static chr element(struct vars *, const chr *, const chr *)
#define NEXT()
Definition: regcomp.c:269
static struct cvec * range(struct vars *, chr, chr, int)
static struct cvec * eclass(struct vars *, chr, int)
static void onechr(struct vars *, chr, struct state *, struct state *)
Definition: regcomp.c:1867
#define REG_ICASE
Definition: regex.h:108
#define ERR(e)
Definition: regcomp.c:276
pg_wchar chr
Definition: regcustom.h:66
#define CCLASSS
Definition: regcomp.c:294
static const chr * scanplain(struct vars *)
Definition: regcomp.c:1842
#define SEE(t)
Definition: regcomp.c:270
#define REG_ECOLLATE
Definition: regex.h:142
Definition: regguts.h:273
#define CCLASSC
Definition: regcomp.c:295
char_classes
Definition: regguts.h:133
#define REG_UUNPORT
Definition: regex.h:71
#define PLAIN
Definition: regcomp.c:287
#define REG_ECTYPE
Definition: regex.h:143
#define REG_ASSERT
Definition: regex.h:153
#define REG_ERANGE
Definition: regex.h:150
const chr * now
Definition: regcomp.c:240
#define COLLEL
Definition: regcomp.c:290
#define NOTE(b)
Definition: regcomp.c:281
#define NOERR()
Definition: regcomp.c:277
int cflags
Definition: regcomp.c:243
static enum char_classes lookupcclass(struct vars *, const chr *, const chr *)
#define INSIST(c, e)
Definition: regcomp.c:280
#define ECLASS
Definition: regcomp.c:291

◆ breakconstraintloop()

static void breakconstraintloop ( struct nfa ,
struct state  
)
static

◆ brenext()

static int brenext ( struct vars ,
chr   
)
static

◆ carc_cmp()

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

◆ carcsort()

static void carcsort ( struct carc ,
size_t   
)
static

◆ casecmp()

static int casecmp ( const chr ,
const chr ,
size_t   
)
static

Referenced by pg_regcomp().

◆ cbracket()

static void cbracket ( struct vars v,
struct state lp,
struct state rp 
)
static

Definition at line 1685 of file regcomp.c.

References assert, bracket(), vars::cflags, vars::cm, colorcomplement(), dropstate(), freestate(), newarc(), newstate(), vars::nfa, state::nins, vars::nlcolor, NOERR, state::nouts, PLAIN, and REG_NLSTOP.

Referenced by parseqatom().

1688 {
1689  struct state *left = newstate(v->nfa);
1690  struct state *right = newstate(v->nfa);
1691 
1692  NOERR();
1693  bracket(v, left, right);
1694 
1695  /* in NLSTOP mode, ensure newline is not part of the result set */
1696  if (v->cflags & REG_NLSTOP)
1697  newarc(v->nfa, PLAIN, v->nlcolor, left, right);
1698  NOERR();
1699 
1700  assert(lp->nouts == 0); /* all outarcs will be ours */
1701 
1702  /*
1703  * Easy part of complementing, and all there is to do since the MCCE code
1704  * was removed. Note that the result of colorcomplement() cannot be a
1705  * rainbow, since we don't allow empty brackets; so there's no point in
1706  * calling optimizebracket() again.
1707  */
1708  colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
1709  NOERR();
1710  dropstate(v->nfa, left);
1711  assert(right->nins == 0);
1712  freestate(v->nfa, right);
1713 }
#define REG_NLSTOP
Definition: regex.h:111
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1629
int nins
Definition: regguts.h:322
color nlcolor
Definition: regcomp.c:254
struct colormap * cm
Definition: regcomp.c:253
#define assert(TEST)
Definition: imath.c:73
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:252
static void freestate(struct nfa *, struct state *)
#define PLAIN
Definition: regcomp.c:287
Definition: regguts.h:317
static void newarc(struct nfa *, int, color, struct state *, struct state *)
int nouts
Definition: regguts.h:323
#define NOERR()
Definition: regcomp.c:277
int cflags
Definition: regcomp.c:243
static void dropstate(struct nfa *, struct state *)
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)

◆ cclass_column_index()

static int cclass_column_index ( struct colormap ,
chr   
)
static

◆ cclasscvec()

static struct cvec* cclasscvec ( struct vars ,
enum  char_classes,
int   
)
static

◆ changearcsource()

static void changearcsource ( struct arc ,
struct state  
)
static

◆ changearctarget()

static void changearctarget ( struct arc ,
struct state  
)
static

◆ charclass()

static void charclass ( struct vars v,
enum char_classes  cls,
struct state lp,
struct state rp 
)
static

Definition at line 1450 of file regcomp.c.

References cclasscvec(), vars::cflags, vars::cv, NOERR, NOTE, REG_ICASE, REG_ULOCALE, and subcolorcvec().

Referenced by brackpart(), and parseqatom().

1454 {
1455  struct cvec *cv;
1456 
1457  /* obtain possibly-cached cvec for char class */
1458  NOTE(REG_ULOCALE);
1459  cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
1460  NOERR();
1461 
1462  /* build the arcs; this may cause color splitting */
1463  subcolorcvec(v, cv, lp, rp);
1464 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
#define REG_ULOCALE
Definition: regex.h:72
#define REG_ICASE
Definition: regex.h:108
static struct cvec * cclasscvec(struct vars *, enum char_classes, int)
Definition: regguts.h:273
#define NOTE(b)
Definition: regcomp.c:281
#define NOERR()
Definition: regcomp.c:277
int cflags
Definition: regcomp.c:243

◆ charclasscomplement()

static void charclasscomplement ( struct vars v,
enum char_classes  cls,
struct state lp,
struct state rp 
)
static

Definition at line 1474 of file regcomp.c.

References cclasscvec(), vars::cflags, vars::cm, colorcomplement(), vars::cv, dropstate(), newstate(), vars::nfa, NOERR, NOTE, okcolors(), PLAIN, REG_ICASE, REG_ULOCALE, and subcolorcvec().

Referenced by bracket(), and parseqatom().

1478 {
1479  struct state *cstate;
1480  struct cvec *cv;
1481 
1482  /* make dummy state to hang temporary arcs on */
1483  cstate = newstate(v->nfa);
1484  NOERR();
1485 
1486  /* obtain possibly-cached cvec for char class */
1487  NOTE(REG_ULOCALE);
1488  cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
1489  NOERR();
1490 
1491  /* build arcs for char class; this may cause color splitting */
1492  subcolorcvec(v, cv, cstate, cstate);
1493  NOERR();
1494 
1495  /* clean up any subcolors in the arc set */
1496  okcolors(v->nfa, v->cm);
1497  NOERR();
1498 
1499  /* now build output arcs for the complement of the char class */
1500  colorcomplement(v->nfa, v->cm, PLAIN, cstate, lp, rp);
1501  NOERR();
1502 
1503  /* clean up dummy state */
1504  dropstate(v->nfa, cstate);
1505 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
static void okcolors(struct nfa *, struct colormap *)
#define REG_ULOCALE
Definition: regex.h:72
#define REG_ICASE
Definition: regex.h:108
struct colormap * cm
Definition: regcomp.c:253
static struct cvec * cclasscvec(struct vars *, enum char_classes, int)
static struct state * newstate(struct nfa *)
Definition: regguts.h:273
struct nfa * nfa
Definition: regcomp.c:252
#define PLAIN
Definition: regcomp.c:287
Definition: regguts.h:317
#define NOTE(b)
Definition: regcomp.c:281
#define NOERR()
Definition: regcomp.c:277
int cflags
Definition: regcomp.c:243
static void dropstate(struct nfa *, struct state *)
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)

◆ check_in_colors_match()

static bool check_in_colors_match ( struct state ,
color  ,
color   
)
static

◆ check_out_colors_match()

static bool check_out_colors_match ( struct state ,
color  ,
color   
)
static

◆ checkmatchall()

static void checkmatchall ( struct nfa )
static

◆ checkmatchall_recurse()

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

◆ chrnamed()

static chr chrnamed ( struct vars ,
const chr ,
const chr ,
chr   
)
static

◆ cleanst()

static void cleanst ( struct vars v)
static

Definition at line 2268 of file regcomp.c.

References subre::chain, subre::flags, FREE, INUSE, next(), vars::treechain, and vars::treefree.

Referenced by freev(), and pg_regcomp().

2269 {
2270  struct subre *t;
2271  struct subre *next;
2272 
2273  for (t = v->treechain; t != NULL; t = next)
2274  {
2275  next = t->chain;
2276  if (!(t->flags & INUSE))
2277  FREE(t);
2278  }
2279  v->treechain = NULL;
2280  v->treefree = NULL; /* just on general principles */
2281 }
struct subre * treefree
Definition: regcomp.c:258
struct subre * treechain
Definition: regcomp.c:257
#define FREE(ptr)
Definition: cryptohash.c:37
struct subre * chain
Definition: regguts.h:501
static int next(struct vars *)
char flags
Definition: regguts.h:474
#define INUSE
Definition: regguts.h:481
Definition: regguts.h:471

◆ cleanup()

static void cleanup ( struct nfa )
static

◆ clearcvec()

static struct cvec* clearcvec ( struct cvec )
static

◆ cleartraverse()

static void cleartraverse ( struct nfa ,
struct state  
)
static

◆ cloneouts()

static void cloneouts ( struct nfa ,
struct state ,
struct state ,
struct state ,
int   
)
static

Referenced by processlacon(), and word().

◆ clonesuccessorstates()

static void clonesuccessorstates ( struct nfa ,
struct state ,
struct state ,
struct state ,
struct arc ,
char *  ,
char *  ,
int   
)
static

◆ cmp()

static int cmp ( const chr ,
const chr ,
size_t   
)
static

Referenced by pg_regcomp().

◆ colorchain()

static void colorchain ( struct colormap ,
struct arc  
)
static

◆ colorcomplement()

static void colorcomplement ( struct nfa ,
struct colormap ,
int  ,
struct state ,
struct state ,
struct state  
)
static

◆ combine()

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

◆ compact()

static void compact ( struct nfa ,
struct cnfa  
)
static

Referenced by nfanode(), and pg_regcomp().

◆ copyins()

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

◆ copyouts()

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

Referenced by makesearch().

◆ cparc()

static void cparc ( struct nfa ,
struct arc ,
struct state ,
struct state  
)
static

Referenced by makesearch().

◆ createarc()

static void createarc ( struct nfa ,
int  ,
color  ,
struct state ,
struct state  
)
static

◆ delsub()

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

Referenced by parseqatom(), and repeat().

◆ deltraverse()

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

◆ dropstate()

static void dropstate ( struct nfa ,
struct state  
)
static

Referenced by cbracket(), and charclasscomplement().

◆ dumpnfa()

static void dumpnfa ( struct nfa ,
FILE *   
)
static

Referenced by pg_regcomp().

◆ dupnfa()

static void dupnfa ( struct nfa ,
struct state ,
struct state ,
struct state ,
struct state  
)
static

Referenced by nfanode(), parseqatom(), and repeat().

◆ duptraverse()

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

◆ eclass()

static struct cvec* eclass ( struct vars ,
chr  ,
int   
)
static

Referenced by brackpart().

◆ element()

static chr element ( struct vars ,
const chr ,
const chr  
)
static

Referenced by brackpart().

◆ emptyreachable()

static struct state* emptyreachable ( struct nfa ,
struct state ,
struct state ,
struct arc **   
)
static

◆ findarc()

static struct arc* findarc ( struct state ,
int  ,
color   
)
static

◆ findconstraintloop()

static int findconstraintloop ( struct nfa ,
struct state  
)
static

◆ fixconstraintloops()

static void fixconstraintloops ( struct nfa ,
FILE *   
)
static

◆ fixempties()

static void fixempties ( struct nfa ,
FILE *   
)
static

◆ freearc()

static void freearc ( struct nfa ,
struct arc  
)
static

Referenced by makesearch(), and optimizebracket().

◆ freecm()

static void freecm ( struct colormap )
static

Referenced by rfree().

◆ freecnfa()

static void freecnfa ( struct cnfa )
static

Referenced by freelacons(), freesrnode(), and rfree().

◆ freecolor()

static void freecolor ( struct colormap ,
color   
)
static

◆ freecvec()

static void freecvec ( struct cvec )
static

Referenced by freev().

◆ freelacons()

static void freelacons ( struct subre subs,
int  n 
)
static

Definition at line 2385 of file regcomp.c.

References assert, subre::cnfa, FREE, freecnfa(), i, and NULLCNFA.

Referenced by freev(), and rfree().

2387 {
2388  struct subre *sub;
2389  int i;
2390 
2391  assert(n > 0);
2392  for (sub = subs + 1, i = n - 1; i > 0; sub++, i--) /* no 0th */
2393  if (!NULLCNFA(sub->cnfa))
2394  freecnfa(&sub->cnfa);
2395  FREE(subs);
2396 }
#define NULLCNFA(cnfa)
Definition: regguts.h:431
#define FREE(ptr)
Definition: cryptohash.c:37
#define assert(TEST)
Definition: imath.c:73
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:500
int i
static void freecnfa(struct cnfa *)

◆ freenfa()

static void freenfa ( struct nfa )
static

Referenced by freev(), and nfanode().

◆ freesrnode()

static void freesrnode ( struct vars v,
struct subre sr 
)
static

Definition at line 2143 of file regcomp.c.

References subre::begin, subre::child, subre::cnfa, subre::end, subre::flags, FREE, freecnfa(), NULLCNFA, subre::sibling, vars::treechain, and vars::treefree.

Referenced by freesubre(), parse(), and parseqatom().

2145 {
2146  if (sr == NULL)
2147  return;
2148 
2149  if (!NULLCNFA(sr->cnfa))
2150  freecnfa(&sr->cnfa);
2151  sr->flags = 0; /* in particular, not INUSE */
2152  sr->child = sr->sibling = NULL;
2153  sr->begin = sr->end = NULL;
2154 
2155  if (v != NULL && v->treechain != NULL)
2156  {
2157  /* we're still parsing, maybe we can reuse the subre */
2158  sr->child = v->treefree;
2159  v->treefree = sr;
2160  }
2161  else
2162  FREE(sr);
2163 }
struct subre * treefree
Definition: regcomp.c:258
struct subre * child
Definition: regguts.h:496
struct subre * treechain
Definition: regcomp.c:257
#define NULLCNFA(cnfa)
Definition: regguts.h:431
#define FREE(ptr)
Definition: cryptohash.c:37
struct state * begin
Definition: regguts.h:498
struct subre * sibling
Definition: regguts.h:497
char flags
Definition: regguts.h:474
struct state * end
Definition: regguts.h:499
struct cnfa cnfa
Definition: regguts.h:500
static void freecnfa(struct cnfa *)

◆ freestate()

static void freestate ( struct nfa ,
struct state  
)
static

Referenced by cbracket().

◆ freesubre()

static void freesubre ( struct vars v,
struct subre sr 
)
static

Definition at line 2108 of file regcomp.c.

References subre::child, freesrnode(), and freesubreandsiblings().

Referenced by freesubreandsiblings(), freev(), parseqatom(), and rfree().

2110 {
2111  if (sr == NULL)
2112  return;
2113 
2114  if (sr->child != NULL)
2115  freesubreandsiblings(v, sr->child);
2116 
2117  freesrnode(v, sr);
2118 }
struct subre * child
Definition: regguts.h:496
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2143
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2127

◆ freesubreandsiblings()

static void freesubreandsiblings ( struct vars v,
struct subre sr 
)
static

Definition at line 2127 of file regcomp.c.

References freesubre(), next(), and subre::sibling.

Referenced by freesubre(), parse(), parseqatom(), and removecaptures().

2129 {
2130  while (sr != NULL)
2131  {
2132  struct subre *next = sr->sibling;
2133 
2134  freesubre(v, sr);
2135  sr = next;
2136  }
2137 }
struct subre * sibling
Definition: regguts.h:497
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2108
static int next(struct vars *)
Definition: regguts.h:471

◆ freev()

static int freev ( struct vars v,
int  err 
)
static

Definition at line 548 of file regcomp.c.

References cleanst(), vars::cv, vars::cv2, vars::err, ERR, FREE, freecvec(), freelacons(), freenfa(), freesubre(), vars::lacons, vars::nfa, vars::nlacons, vars::re, rfree(), vars::sub10, vars::subs, vars::tree, and vars::treechain.

Referenced by pg_regcomp().

550 {
551  if (v->re != NULL)
552  rfree(v->re);
553  if (v->subs != v->sub10)
554  FREE(v->subs);
555  if (v->nfa != NULL)
556  freenfa(v->nfa);
557  if (v->tree != NULL)
558  freesubre(v, v->tree);
559  if (v->treechain != NULL)
560  cleanst(v);
561  if (v->cv != NULL)
562  freecvec(v->cv);
563  if (v->cv2 != NULL)
564  freecvec(v->cv2);
565  if (v->lacons != NULL)
566  freelacons(v->lacons, v->nlacons);
567  ERR(err); /* nop if err==0 */
568 
569  return v->err;
570 }
static void freelacons(struct subre *, int)
Definition: regcomp.c:2385
static void rfree(regex_t *)
Definition: regcomp.c:2402
struct subre * treechain
Definition: regcomp.c:257
#define FREE(ptr)
Definition: cryptohash.c:37
#define ERR(e)
Definition: regcomp.c:276
regex_t * re
Definition: regcomp.c:239
int nlacons
Definition: regcomp.c:263
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2108
struct subre ** subs
Definition: regcomp.c:249
int err
Definition: regcomp.c:242
struct nfa * nfa
Definition: regcomp.c:252
struct cvec * cv
Definition: regcomp.c:260
struct cvec * cv2
Definition: regcomp.c:261
static void freecvec(struct cvec *)
struct subre * sub10[10]
Definition: regcomp.c:251
static void freenfa(struct nfa *)
struct subre * tree
Definition: regcomp.c:256
static void cleanst(struct vars *)
Definition: regcomp.c:2268
struct subre * lacons
Definition: regcomp.c:262

◆ getcvec()

static struct cvec* getcvec ( struct vars ,
int  ,
int   
)
static

◆ hasconstraintout()

static int hasconstraintout ( struct state )
static

◆ hasnonemptyout()

static int hasnonemptyout ( struct state )
static

◆ initcm()

static void initcm ( struct vars ,
struct colormap  
)
static

Referenced by pg_regcomp().

◆ isconstraintarc()

static int isconstraintarc ( struct arc )
static

◆ lexdigits()

static chr lexdigits ( struct vars ,
int  ,
int  ,
int   
)
static

◆ lexescape()

static int lexescape ( struct vars )
static

◆ lexstart()

static void lexstart ( struct vars )
static

Referenced by pg_regcomp().

◆ lookupcclass()

static enum char_classes lookupcclass ( struct vars ,
const chr ,
const chr  
)
static

Referenced by brackpart().

◆ makesearch()

static void makesearch ( struct vars v,
struct nfa nfa 
)
static

Definition at line 577 of file regcomp.c.

References assert, nfa::bos, vars::cm, arc::co, COLORLESS, copyouts(), cparc(), DUPINF, nfa::flags, freearc(), arc::from, arc::inchain, state::ins, MATCHALL, nfa::maxmatchall, newarc(), newstate(), NOERR, arc::outchain, state::outs, PLAIN, nfa::pre, rainbow(), s2, state::tmp, arc::to, and arc::type.

Referenced by nfanode(), and pg_regcomp().

579 {
580  struct arc *a;
581  struct arc *b;
582  struct state *pre = nfa->pre;
583  struct state *s;
584  struct state *s2;
585  struct state *slist;
586 
587  /* no loops are needed if it's anchored */
588  for (a = pre->outs; a != NULL; a = a->outchain)
589  {
590  assert(a->type == PLAIN);
591  if (a->co != nfa->bos[0] && a->co != nfa->bos[1])
592  break;
593  }
594  if (a != NULL)
595  {
596  /* add implicit .* in front */
597  rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
598 
599  /* and ^* and \A* too -- not always necessary, but harmless */
600  newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
601  newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
602 
603  /*
604  * The pattern is still MATCHALL if it was before, but the max match
605  * length is now infinity.
606  */
607  if (nfa->flags & MATCHALL)
608  nfa->maxmatchall = DUPINF;
609  }
610 
611  /*
612  * Now here's the subtle part. Because many REs have no lookback
613  * constraints, often knowing when you were in the pre state tells you
614  * little; it's the next state(s) that are informative. But some of them
615  * may have other inarcs, i.e. it may be possible to make actual progress
616  * and then return to one of them. We must de-optimize such cases,
617  * splitting each such state into progress and no-progress states.
618  */
619 
620  /* first, make a list of the states reachable from pre and elsewhere */
621  slist = NULL;
622  for (a = pre->outs; a != NULL; a = a->outchain)
623  {
624  s = a->to;
625  for (b = s->ins; b != NULL; b = b->inchain)
626  {
627  if (b->from != pre)
628  break;
629  }
630 
631  /*
632  * We want to mark states as being in the list already by having non
633  * NULL tmp fields, but we can't just store the old slist value in tmp
634  * because that doesn't work for the first such state. Instead, the
635  * first list entry gets its own address in tmp.
636  */
637  if (b != NULL && s->tmp == NULL)
638  {
639  s->tmp = (slist != NULL) ? slist : s;
640  slist = s;
641  }
642  }
643 
644  /* do the splits */
645  for (s = slist; s != NULL; s = s2)
646  {
647  s2 = newstate(nfa);
648  NOERR();
649  copyouts(nfa, s, s2);
650  NOERR();
651  for (a = s->ins; a != NULL; a = b)
652  {
653  b = a->inchain;
654  if (a->from != pre)
655  {
656  cparc(nfa, a, a->from, s2);
657  freearc(nfa, a);
658  }
659  }
660  s2 = (s->tmp != s) ? s->tmp : NULL;
661  s->tmp = NULL; /* clean up while we're at it */
662  }
663 }
static void cparc(struct nfa *, struct arc *, struct state *, struct state *)
struct state * from
Definition: regguts.h:294
int type
Definition: regguts.h:292
Definition: regguts.h:290
int flags
Definition: regguts.h:361
int maxmatchall
Definition: regguts.h:363
struct colormap * cm
Definition: regcomp.c:253
static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *)
struct arc * outchain
Definition: regguts.h:296
color bos[2]
Definition: regguts.h:359
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
static struct state * newstate(struct nfa *)
#define MATCHALL
Definition: regguts.h:407
static void copyouts(struct nfa *, struct state *, struct state *)
struct state * tmp
Definition: regguts.h:326
struct state * to
Definition: regguts.h:295
char * s2
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
#define DUPINF
Definition: regguts.h:94
Definition: regguts.h:317
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define NOERR()
Definition: regcomp.c:277
struct arc * inchain
Definition: regguts.h:299
#define COLORLESS
Definition: regguts.h:153
static void freearc(struct nfa *, struct arc *)
struct arc * ins
Definition: regguts.h:324
struct state * pre
Definition: regguts.h:345

◆ markcanreach()

static void markcanreach ( struct nfa ,
struct state ,
struct state ,
struct state  
)
static

◆ markreachable()

static void markreachable ( struct nfa ,
struct state ,
struct state ,
struct state  
)
static

◆ markst()

static void markst ( struct subre t)
static

Definition at line 2253 of file regcomp.c.

References assert, subre::child, subre::flags, INUSE, and subre::sibling.

Referenced by pg_regcomp().

2254 {
2255  struct subre *t2;
2256 
2257  assert(t != NULL);
2258 
2259  t->flags |= INUSE;
2260  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2261  markst(t2);
2262 }
struct subre * child
Definition: regguts.h:496
struct subre * sibling
Definition: regguts.h:497
static void markst(struct subre *)
Definition: regcomp.c:2253
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
#define INUSE
Definition: regguts.h:481
Definition: regguts.h:471

◆ maxcolor()

static color maxcolor ( struct colormap )
static

◆ mergeins()

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

◆ moresubs()

static void moresubs ( struct vars v,
int  wanted 
)
static

Definition at line 511 of file regcomp.c.

References assert, ERR, MALLOC, vars::nsubs, REALLOC, REG_ESPACE, vars::sub10, vars::subs, and VS.

Referenced by parseqatom().

513 {
514  struct subre **p;
515  size_t n;
516 
517  assert(wanted > 0 && (size_t) wanted >= v->nsubs);
518  n = (size_t) wanted * 3 / 2 + 1;
519 
520  if (v->subs == v->sub10)
521  {
522  p = (struct subre **) MALLOC(n * sizeof(struct subre *));
523  if (p != NULL)
524  memcpy(VS(p), VS(v->subs),
525  v->nsubs * sizeof(struct subre *));
526  }
527  else
528  p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
529  if (p == NULL)
530  {
531  ERR(REG_ESPACE);
532  return;
533  }
534  v->subs = p;
535  for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
536  *p = NULL;
537  assert(v->nsubs == n);
538  assert((size_t) wanted < v->nsubs);
539 }
#define REG_ESPACE
Definition: regex.h:151
#define MALLOC(n)
Definition: regcustom.h:60
#define ERR(e)
Definition: regcomp.c:276
#define REALLOC(p, n)
Definition: regcustom.h:62
struct subre ** subs
Definition: regcomp.c:249
#define assert(TEST)
Definition: imath.c:73
size_t nsubs
Definition: regcomp.c:250
#define VS(x)
Definition: regguts.h:61
Definition: regguts.h:471
struct subre * sub10[10]
Definition: regcomp.c:251

◆ moveins()

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

Referenced by parsebranch(), parseqatom(), and repeat().

◆ moveouts()

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

Referenced by parseqatom(), and repeat().

◆ newarc()

static void newarc ( struct nfa ,
int  ,
color  ,
struct state ,
struct state  
)
static

◆ newcolor()

static color newcolor ( struct colormap )
static

◆ newcvec()

static struct cvec* newcvec ( int  ,
int   
)
static

Referenced by pg_regcomp().

◆ newfstate()

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

◆ newhicolorcols()

static void newhicolorcols ( struct colormap )
static

◆ newhicolorrow()

static int newhicolorrow ( struct colormap ,
int   
)
static

◆ newlacon()

static int newlacon ( struct vars v,
struct state begin,
struct state end,
int  latype 
)
static

Definition at line 2346 of file regcomp.c.

References subre::begin, subre::cnfa, subre::end, ERR, vars::lacons, subre::latype, MALLOC, vars::nlacons, REALLOC, REG_ESPACE, and ZAPCNFA.

Referenced by processlacon().

2350 {
2351  int n;
2352  struct subre *newlacons;
2353  struct subre *sub;
2354 
2355  if (v->nlacons == 0)
2356  {
2357  n = 1; /* skip 0th */
2358  newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
2359  }
2360  else
2361  {
2362  n = v->nlacons;
2363  newlacons = (struct subre *) REALLOC(v->lacons,
2364  (n + 1) * sizeof(struct subre));
2365  }
2366  if (newlacons == NULL)
2367  {
2368  ERR(REG_ESPACE);
2369  return 0;
2370  }
2371  v->lacons = newlacons;
2372  v->nlacons = n + 1;
2373  sub = &v->lacons[n];
2374  sub->begin = begin;
2375  sub->end = end;
2376  sub->latype = latype;
2377  ZAPCNFA(sub->cnfa);
2378  return n;
2379 }
#define REG_ESPACE
Definition: regex.h:151
struct state * begin
Definition: regguts.h:498
char latype
Definition: regguts.h:490
#define MALLOC(n)
Definition: regcustom.h:60
#define ERR(e)
Definition: regcomp.c:276
#define REALLOC(p, n)
Definition: regcustom.h:62
#define ZAPCNFA(cnfa)
Definition: regguts.h:429
int nlacons
Definition: regcomp.c:263
struct state * end
Definition: regguts.h:499
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:500
struct subre * lacons
Definition: regcomp.c:262

◆ newline()

static chr newline ( void  )
static

Referenced by pg_regcomp().

◆ newnfa()

static struct nfa* newnfa ( struct vars ,
struct colormap ,
struct nfa  
)
static

Referenced by nfanode(), and pg_regcomp().

◆ newstate()

static struct state* newstate ( struct nfa )
static

◆ newsub()

static color newsub ( struct colormap ,
color   
)
static

◆ next()

static int next ( struct vars )
static

Referenced by cleanst(), and freesubreandsiblings().

◆ nfanode()

static long nfanode ( struct vars v,
struct subre t,
int  converttosearch,
FILE *  f 
)
static

Definition at line 2307 of file regcomp.c.

References assert, subre::begin, vars::cm, subre::cnfa, compact(), dupnfa(), subre::end, nfa::final, fprintf, freenfa(), nfa::init, ISERR, makesearch(), newnfa(), vars::nfa, NOERRZ, optimize(), and specialcolors().

Referenced by nfatree(), and pg_regcomp().

2311 {
2312  struct nfa *nfa;
2313  long ret = 0;
2314 
2315  assert(t->begin != NULL);
2316 
2317 #ifdef REG_DEBUG
2318  if (f != NULL)
2319  {
2320  char idbuf[50];
2321 
2322  fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
2323  stid(t, idbuf, sizeof(idbuf)));
2324  }
2325 #endif
2326  nfa = newnfa(v, v->cm, v->nfa);
2327  NOERRZ();
2328  dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
2329  if (!ISERR())
2330  specialcolors(nfa);
2331  if (!ISERR())
2332  ret = optimize(nfa, f);
2333  if (converttosearch && !ISERR())
2334  makesearch(v, nfa);
2335  if (!ISERR())
2336  compact(nfa, &t->cnfa);
2337 
2338  freenfa(nfa);
2339  return ret;
2340 }
static long optimize(struct nfa *, FILE *)
struct state * final
Definition: regguts.h:347
static void specialcolors(struct nfa *)
struct state * begin
Definition: regguts.h:498
Definition: regguts.h:343
#define ISERR()
Definition: regcomp.c:273
#define fprintf
Definition: port.h:220
static void makesearch(struct vars *, struct nfa *)
Definition: regcomp.c:577
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
struct colormap * cm
Definition: regcomp.c:253
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:252
struct state * end
Definition: regguts.h:499
#define NOERRZ()
Definition: regcomp.c:279
struct cnfa cnfa
Definition: regguts.h:500
struct state * init
Definition: regguts.h:346
static void freenfa(struct nfa *)
static void compact(struct nfa *, struct cnfa *)

◆ nfatree()

static long nfatree ( struct vars v,
struct subre t,
FILE *  f 
)
static

Definition at line 2287 of file regcomp.c.

References assert, subre::begin, subre::child, DISCARD, nfanode(), and subre::sibling.

Referenced by pg_regcomp().

2290 {
2291  struct subre *t2;
2292 
2293  assert(t != NULL && t->begin != NULL);
2294 
2295  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2296  (DISCARD) nfatree(v, t2, f);
2297 
2298  return nfanode(v, t, 0, f);
2299 }
struct subre * child
Definition: regguts.h:496
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:2287
struct state * begin
Definition: regguts.h:498
struct subre * sibling
Definition: regguts.h:497
#define assert(TEST)
Definition: imath.c:73
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:2307
Definition: regguts.h:471
#define DISCARD
Definition: regguts.h:58

◆ nonword()

static void nonword ( struct vars v,
int  dir,
struct state lp,
struct state rp 
)
static

Definition at line 1414 of file regcomp.c.

References AHEAD, assert, BEHIND, vars::cm, colorcomplement(), newarc(), vars::nfa, and vars::wordchrs.

Referenced by parseqatom().

1418 {
1419  int anchor = (dir == AHEAD) ? '$' : '^';
1420 
1421  assert(dir == AHEAD || dir == BEHIND);
1422  newarc(v->nfa, anchor, 1, lp, rp);
1423  newarc(v->nfa, anchor, 0, lp, rp);
1424  colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
1425  /* (no need for special attention to \n) */
1426 }
#define BEHIND
Definition: regcomp.c:299
struct colormap * cm
Definition: regcomp.c:253
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:252
struct state * wordchrs
Definition: regcomp.c:255
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:298
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)

◆ numst()

static int numst ( struct subre t,
int  start 
)
static

Definition at line 2220 of file regcomp.c.

References assert, subre::child, i, subre::id, subre::sibling, and vars::start.

Referenced by pg_regcomp().

2222 {
2223  int i;
2224  struct subre *t2;
2225 
2226  assert(t != NULL);
2227 
2228  i = start;
2229  t->id = i++;
2230  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2231  i = numst(t2, i);
2232  return i;
2233 }
struct subre * child
Definition: regguts.h:496
static int numst(struct subre *, int)
Definition: regcomp.c:2220
int id
Definition: regguts.h:491
struct subre * sibling
Definition: regguts.h:497
#define assert(TEST)
Definition: imath.c:73
Definition: regguts.h:471
int i

◆ okcolors()

static void okcolors ( struct nfa ,
struct colormap  
)
static

◆ onechr()

static void onechr ( struct vars v,
chr  c,
struct state lp,
struct state rp 
)
static

Definition at line 1867 of file regcomp.c.

References allcases(), vars::cflags, COLORLESS, REG_ICASE, subcolorcvec(), and subcoloronechr().

Referenced by brackpart(), and parseqatom().

1871 {
1872  if (!(v->cflags & REG_ICASE))
1873  {
1874  color lastsubcolor = COLORLESS;
1875 
1876  subcoloronechr(v, c, lp, rp, &lastsubcolor);
1877  return;
1878  }
1879 
1880  /* rats, need general case anyway... */
1881  subcolorcvec(v, allcases(v, c), lp, rp);
1882 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
short color
Definition: regguts.h:150
static void subcoloronechr(struct vars *, chr, struct state *, struct state *, color *)
#define REG_ICASE
Definition: regex.h:108
char * c
static struct cvec * allcases(struct vars *, chr)
int cflags
Definition: regcomp.c:243
#define COLORLESS
Definition: regguts.h:153

◆ optimize()

static long optimize ( struct nfa ,
FILE *   
)
static

Referenced by nfanode(), and pg_regcomp().

◆ optimizebracket()

static void optimizebracket ( struct vars v,
struct state lp,
struct state rp 
)
static

Definition at line 1893 of file regcomp.c.

References assert, colormap::cd, CDEND, vars::cm, arc::co, COLMARK, colordesc::flags, freearc(), newarc(), vars::nfa, arc::outchain, state::outs, PLAIN, PSEUDO, RAINBOW, arc::to, arc::type, and UNUSEDCOLOR.

Referenced by bracket().

1896 {
1897  struct colordesc *cd;
1898  struct colordesc *end = CDEND(v->cm);
1899  struct arc *a;
1900  bool israinbow;
1901 
1902  /*
1903  * Scan lp's out-arcs and transiently mark the mentioned colors. We
1904  * expect that all of lp's out-arcs are plain, non-RAINBOW arcs to rp.
1905  * (Note: there shouldn't be any pseudocolors yet, but check anyway.)
1906  */
1907  for (a = lp->outs; a != NULL; a = a->outchain)
1908  {
1909  assert(a->type == PLAIN);
1910  assert(a->co >= 0); /* i.e. not RAINBOW */
1911  assert(a->to == rp);
1912  cd = &v->cm->cd[a->co];
1913  assert(!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO));
1914  cd->flags |= COLMARK;
1915  }
1916 
1917  /* Scan colors, clear transient marks, check for unmarked live colors */
1918  israinbow = true;
1919  for (cd = v->cm->cd; cd < end; cd++)
1920  {
1921  if (cd->flags & COLMARK)
1922  cd->flags &= ~COLMARK;
1923  else if (!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO))
1924  israinbow = false;
1925  }
1926 
1927  /* Can't do anything if not all colors have arcs */
1928  if (!israinbow)
1929  return;
1930 
1931  /* OK, drop existing arcs and replace with a rainbow */
1932  while ((a = lp->outs) != NULL)
1933  freearc(v->nfa, a);
1934  newarc(v->nfa, PLAIN, RAINBOW, lp, rp);
1935 }
int type
Definition: regguts.h:292
Definition: regguts.h:290
#define RAINBOW
Definition: regguts.h:154
struct colordesc * cd
Definition: regguts.h:231
struct colormap * cm
Definition: regcomp.c:253
#define COLMARK
Definition: regguts.h:182
struct arc * outchain
Definition: regguts.h:296
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:325
#define UNUSEDCOLOR(cd)
Definition: regguts.h:185
struct nfa * nfa
Definition: regcomp.c:252
#define CDEND(cm)
Definition: regguts.h:232
struct state * to
Definition: regguts.h:295
#define PLAIN
Definition: regcomp.c:287
color co
Definition: regguts.h:293
int flags
Definition: regguts.h:179
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define PSEUDO
Definition: regguts.h:181
static void freearc(struct nfa *, struct arc *)

◆ parse()

static struct subre * parse ( struct vars v,
int  stopper,
int  type,
struct state init,
struct state final 
)
static

Definition at line 673 of file regcomp.c.

References assert, subre::child, EAT, EMPTYARC, EOS, ERR, subre::flags, freesrnode(), freesubreandsiblings(), LONGER, MESSY, newstate(), vars::nfa, NOERRN, subre::op, parsebranch(), REG_EPAREN, SEE, subre::sibling, subre(), and UP.

Referenced by _outPlannerInfo(), add_foreign_final_paths(), add_foreign_grouping_paths(), add_foreign_ordered_paths(), add_paths_to_grouping_rel(), build_minmax_path(), can_partial_agg(), check_sql_fn_retval(), consider_groupingsets_paths(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), create_degenerate_grouping_paths(), create_final_distinct_paths(), create_grouping_paths(), create_limit_plan(), create_partial_distinct_paths(), create_partial_grouping_paths(), distribute_row_identity_vars(), expand_single_inheritance_child(), flatten_simple_union_all(), get_number_of_groups(), grouping_planner(), is_degenerate_grouping(), json_manifest_array_end(), json_manifest_array_start(), json_manifest_object_end(), json_manifest_object_field_start(), json_manifest_object_start(), json_manifest_scalar(), json_parse_manifest(), make_group_input_target(), make_partial_grouping_target(), make_sort_input_target(), make_window_input_target(), max_parallel_hazard(), parseqatom(), perform_pullup_replace_vars(), pg_regcomp(), plan_set_operations(), planner(), preprocess_groupclause(), preprocess_grouping_sets(), preprocess_limit(), preprocess_minmax_aggregates(), preprocess_rowmarks(), preprocess_targetlist(), pull_up_constant_function(), pull_up_simple_subquery(), pull_up_simple_values(), query_planner(), remove_useless_groupby_columns(), resolve_unique_index_expr(), set_subquery_pathlist(), standard_qp_callback(), and subquery_planner().

678 {
679  struct subre *branches; /* top level */
680  struct subre *lastbranch; /* latest branch */
681 
682  assert(stopper == ')' || stopper == EOS);
683 
684  branches = subre(v, '|', LONGER, init, final);
685  NOERRN();
686  lastbranch = NULL;
687  do
688  { /* a branch */
689  struct subre *branch;
690  struct state *left; /* scaffolding for branch */
691  struct state *right;
692 
693  left = newstate(v->nfa);
694  right = newstate(v->nfa);
695  NOERRN();
696  EMPTYARC(init, left);
697  EMPTYARC(right, final);
698  NOERRN();
699  branch = parsebranch(v, stopper, type, left, right, 0);
700  NOERRN();
701  if (lastbranch)
702  lastbranch->sibling = branch;
703  else
704  branches->child = branch;
705  branches->flags |= UP(branches->flags | branch->flags);
706  lastbranch = branch;
707  } while (EAT('|'));
708  assert(SEE(stopper) || SEE(EOS));
709 
710  if (!SEE(stopper))
711  {
712  assert(stopper == ')' && SEE(EOS));
713  ERR(REG_EPAREN);
714  }
715 
716  /* optimize out simple cases */
717  if (lastbranch == branches->child)
718  { /* only one branch */
719  assert(lastbranch->sibling == NULL);
720  freesrnode(v, branches);
721  branches = lastbranch;
722  }
723  else if (!MESSY(branches->flags))
724  { /* no interesting innards */
725  freesubreandsiblings(v, branches->child);
726  branches->child = NULL;
727  branches->op = '=';
728  }
729 
730  return branches;
731 }
#define EAT(t)
Definition: regcomp.c:271
struct subre * child
Definition: regguts.h:496
#define REG_EPAREN
Definition: regex.h:147
#define NOERRN()
Definition: regcomp.c:278
#define MESSY(f)
Definition: regguts.h:486
char op
Definition: regguts.h:473
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:2051
#define ERR(e)
Definition: regcomp.c:276
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2143
struct subre * sibling
Definition: regguts.h:497
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2127
#define SEE(t)
Definition: regcomp.c:270
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:252
#define EMPTYARC(x, y)
Definition: regcomp.c:282
static struct subre * parsebranch(struct vars *, int, int, struct state *, struct state *, int)
Definition: regcomp.c:741
#define EOS
Definition: regcomp.c:286
Definition: regguts.h:317
Definition: regguts.h:471
#define LONGER
Definition: regguts.h:475
#define UP(f)
Definition: regguts.h:485

◆ parsebranch()

static struct subre * parsebranch ( struct vars v,
int  stopper,
int  type,
struct state left,
struct state right,
int  partial 
)
static

Definition at line 741 of file regcomp.c.

References assert, EMPTYARC, EOS, moveins(), newstate(), vars::nfa, NOERRN, NOTE, parseqatom(), REG_UUNSPEC, SEE, and subre().

Referenced by parse(), and parseqatom().

747 {
748  struct state *lp; /* left end of current construct */
749  int seencontent; /* is there anything in this branch yet? */
750  struct subre *t;
751 
752  lp = left;
753  seencontent = 0;
754  t = subre(v, '=', 0, left, right); /* op '=' is tentative */
755  NOERRN();
756  while (!SEE('|') && !SEE(stopper) && !SEE(EOS))
757  {
758  if (seencontent)
759  { /* implicit concat operator */
760  lp = newstate(v->nfa);
761  NOERRN();
762  moveins(v->nfa, right, lp);
763  }
764  seencontent = 1;
765 
766  /* NB, recursion in parseqatom() may swallow rest of branch */
767  t = parseqatom(v, stopper, type, lp, right, t);
768  NOERRN();
769  }
770 
771  if (!seencontent)
772  { /* empty branch */
773  if (!partial)
774  NOTE(REG_UUNSPEC);
775  assert(lp == left);
776  EMPTYARC(left, right);
777  }
778 
779  return t;
780 }
#define NOERRN()
Definition: regcomp.c:278
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:2051
#define assert(TEST)
Definition: imath.c:73
static void moveins(struct nfa *, struct state *, struct state *)
#define SEE(t)
Definition: regcomp.c:270
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:252
#define EMPTYARC(x, y)
Definition: regcomp.c:282
#define EOS
Definition: regcomp.c:286
#define REG_UUNSPEC
Definition: regex.h:69
Definition: regguts.h:317
static struct subre * parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *)
Definition: regcomp.c:794
Definition: regguts.h:471
#define NOTE(b)
Definition: regcomp.c:281

◆ parseqatom()

static struct subre * parseqatom ( struct vars v,
int  stopper,
int  type,
struct state lp,
struct state rp,
struct subre top 
)
static

Definition at line 794 of file regcomp.c.

References AHEAD, ARCV, assert, subre::backno, BACKR, BACKREF, subre::begin, BEHIND, bracket(), BRUSE, CAP, subre::capno, cbracket(), CCLASSC, CCLASSS, vars::cflags, charclass(), charclasscomplement(), subre::child, vars::cm, COLORLESS, COMBINE, delsub(), DIGIT, DUPINF, dupnfa(), EAT, EMPTYARC, subre::end, EOS, ERR, subre::flags, freesrnode(), freesubre(), freesubreandsiblings(), INSIST, ISERR, LACON, subre::latype, LONGER, subre::max, MESSY, subre::min, MIXED, moresubs(), moveins(), moveouts(), newstate(), NEXT, vars::nexttype, vars::nextvalue, vars::nfa, state::nins, vars::nlcolor, NOERRN, nonword(), NOTE, state::nouts, vars::nsubexp, vars::nsubs, NWBDRY, okcolors(), onechr(), subre::op, parse(), parsebranch(), PLAIN, PREF, processlacon(), rainbow(), REG_ADVANCED, REG_ASSERT, REG_BADBR, REG_BADRPT, REG_EPAREN, REG_ESUBREG, REG_EXTENDED, REG_NLANCH, REG_NLSTOP, REG_UPBOTCH, removeconstraints(), repeat(), s2, SBEGIN, scannum(), SEE, SEND, SHORTER, subre::sibling, subre(), vars::subs, UP, WBDRY, word(), and vars::wordchrs.

Referenced by parsebranch().

800 {
801  struct state *s; /* temporaries for new states */
802  struct state *s2;
803 
804 #define ARCV(t, val) newarc(v->nfa, t, val, lp, rp)
805  int m,
806  n;
807  struct subre *atom; /* atom's subtree */
808  struct subre *t;
809  int cap; /* capturing parens? */
810  int latype; /* lookaround constraint type */
811  int subno; /* capturing-parens or backref number */
812  int atomtype;
813  int qprefer; /* quantifier short/long preference */
814  int f;
815  struct subre **atomp; /* where the pointer to atom is */
816 
817  /* initial bookkeeping */
818  atom = NULL;
819  assert(lp->nouts == 0); /* must string new code */
820  assert(rp->nins == 0); /* between lp and rp */
821  subno = 0; /* just to shut lint up */
822 
823  /* an atom or constraint... */
824  atomtype = v->nexttype;
825  switch (atomtype)
826  {
827  /* first, constraints, which end by returning */
828  case '^':
829  ARCV('^', 1);
830  if (v->cflags & REG_NLANCH)
831  ARCV(BEHIND, v->nlcolor);
832  NEXT();
833  return top;
834  break;
835  case '$':
836  ARCV('$', 1);
837  if (v->cflags & REG_NLANCH)
838  ARCV(AHEAD, v->nlcolor);
839  NEXT();
840  return top;
841  break;
842  case SBEGIN:
843  ARCV('^', 1); /* BOL */
844  ARCV('^', 0); /* or BOS */
845  NEXT();
846  return top;
847  break;
848  case SEND:
849  ARCV('$', 1); /* EOL */
850  ARCV('$', 0); /* or EOS */
851  NEXT();
852  return top;
853  break;
854  case '<':
855  wordchrs(v);
856  s = newstate(v->nfa);
857  NOERRN();
858  nonword(v, BEHIND, lp, s);
859  word(v, AHEAD, s, rp);
860  NEXT();
861  return top;
862  break;
863  case '>':
864  wordchrs(v);
865  s = newstate(v->nfa);
866  NOERRN();
867  word(v, BEHIND, lp, s);
868  nonword(v, AHEAD, s, rp);
869  NEXT();
870  return top;
871  break;
872  case WBDRY:
873  wordchrs(v);
874  s = newstate(v->nfa);
875  NOERRN();
876  nonword(v, BEHIND, lp, s);
877  word(v, AHEAD, s, rp);
878  s = newstate(v->nfa);
879  NOERRN();
880  word(v, BEHIND, lp, s);
881  nonword(v, AHEAD, s, rp);
882  NEXT();
883  return top;
884  break;
885  case NWBDRY:
886  wordchrs(v);
887  s = newstate(v->nfa);
888  NOERRN();
889  word(v, BEHIND, lp, s);
890  word(v, AHEAD, s, rp);
891  s = newstate(v->nfa);
892  NOERRN();
893  nonword(v, BEHIND, lp, s);
894  nonword(v, AHEAD, s, rp);
895  NEXT();
896  return top;
897  break;
898  case LACON: /* lookaround constraint */
899  latype = v->nextvalue;
900  NEXT();
901  s = newstate(v->nfa);
902  s2 = newstate(v->nfa);
903  NOERRN();
904  t = parse(v, ')', LACON, s, s2);
905  freesubre(v, t); /* internal structure irrelevant */
906  NOERRN();
907  assert(SEE(')'));
908  NEXT();
909  processlacon(v, s, s2, latype, lp, rp);
910  return top;
911  break;
912  /* then errors, to get them out of the way */
913  case '*':
914  case '+':
915  case '?':
916  case '{':
917  ERR(REG_BADRPT);
918  return top;
919  break;
920  default:
921  ERR(REG_ASSERT);
922  return top;
923  break;
924  /* then plain characters, and minor variants on that theme */
925  case ')': /* unbalanced paren */
926  if ((v->cflags & REG_ADVANCED) != REG_EXTENDED)
927  {
928  ERR(REG_EPAREN);
929  return top;
930  }
931  /* legal in EREs due to specification botch */
932  NOTE(REG_UPBOTCH);
933  /* fall through into case PLAIN */
934  /* FALLTHROUGH */
935  case PLAIN:
936  onechr(v, v->nextvalue, lp, rp);
937  okcolors(v->nfa, v->cm);
938  NOERRN();
939  NEXT();
940  break;
941  case '[':
942  if (v->nextvalue == 1)
943  bracket(v, lp, rp);
944  else
945  cbracket(v, lp, rp);
946  assert(SEE(']') || ISERR());
947  NEXT();
948  break;
949  case CCLASSS:
950  charclass(v, (enum char_classes) v->nextvalue, lp, rp);
951  okcolors(v->nfa, v->cm);
952  NEXT();
953  break;
954  case CCLASSC:
955  charclasscomplement(v, (enum char_classes) v->nextvalue, lp, rp);
956  /* charclasscomplement() did okcolors() internally */
957  NEXT();
958  break;
959  case '.':
960  rainbow(v->nfa, v->cm, PLAIN,
961  (v->cflags & REG_NLSTOP) ? v->nlcolor : COLORLESS,
962  lp, rp);
963  NEXT();
964  break;
965  /* and finally the ugly stuff */
966  case '(': /* value flags as capturing or non */
967  cap = (type == LACON) ? 0 : v->nextvalue;
968  if (cap)
969  {
970  v->nsubexp++;
971  subno = v->nsubexp;
972  if ((size_t) subno >= v->nsubs)
973  moresubs(v, subno);
974  }
975  else
976  atomtype = PLAIN; /* something that's not '(' */
977  NEXT();
978 
979  /*
980  * Make separate endpoint states to keep this sub-NFA distinct
981  * from what surrounds it. We need to be sure that when we
982  * duplicate the sub-NFA for a backref, we get the right
983  * states/arcs and no others. In particular, letting a backref
984  * duplicate the sub-NFA from lp to rp would be quite wrong,
985  * because we may add quantification superstructure around this
986  * atom below. (Perhaps we could skip the extra states for
987  * non-capturing parens, but it seems not worth the trouble.)
988  */
989  s = newstate(v->nfa);
990  s2 = newstate(v->nfa);
991  NOERRN();
992  /* We may not need these arcs, but keep things connected for now */
993  EMPTYARC(lp, s);
994  EMPTYARC(s2, rp);
995  NOERRN();
996  atom = parse(v, ')', type, s, s2);
997  assert(SEE(')') || ISERR());
998  NEXT();
999  NOERRN();
1000  if (cap)
1001  {
1002  if (atom->capno == 0)
1003  {
1004  /* normal case: just mark the atom as capturing */
1005  atom->flags |= CAP;
1006  atom->capno = subno;
1007  }
1008  else
1009  {
1010  /* generate no-op wrapper node to handle "((x))" */
1011  t = subre(v, '(', atom->flags | CAP, s, s2);
1012  NOERRN();
1013  t->capno = subno;
1014  t->child = atom;
1015  atom = t;
1016  }
1017  assert(v->subs[subno] == NULL);
1018  v->subs[subno] = atom;
1019  }
1020  /* postpone everything else pending possible {0} */
1021  break;
1022  case BACKREF: /* the Feature From The Black Lagoon */
1024  subno = v->nextvalue;
1025  assert(subno > 0);
1026  INSIST(subno < v->nsubs, REG_ESUBREG);
1027  NOERRN();
1028  INSIST(v->subs[subno] != NULL, REG_ESUBREG);
1029  NOERRN();
1030  atom = subre(v, 'b', BACKR, lp, rp);
1031  NOERRN();
1032  atom->backno = subno;
1033  v->subs[subno]->flags |= BRUSE;
1034  EMPTYARC(lp, rp); /* temporarily, so there's something */
1035  NEXT();
1036  break;
1037  }
1038 
1039  /* ...and an atom may be followed by a quantifier */
1040  switch (v->nexttype)
1041  {
1042  case '*':
1043  m = 0;
1044  n = DUPINF;
1045  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1046  NEXT();
1047  break;
1048  case '+':
1049  m = 1;
1050  n = DUPINF;
1051  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1052  NEXT();
1053  break;
1054  case '?':
1055  m = 0;
1056  n = 1;
1057  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1058  NEXT();
1059  break;
1060  case '{':
1061  NEXT();
1062  m = scannum(v);
1063  if (EAT(','))
1064  {
1065  if (SEE(DIGIT))
1066  n = scannum(v);
1067  else
1068  n = DUPINF;
1069  if (m > n)
1070  {
1071  ERR(REG_BADBR);
1072  return top;
1073  }
1074  /* {m,n} exercises preference, even if it's {m,m} */
1075  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1076  }
1077  else
1078  {
1079  n = m;
1080  /* {m} passes operand's preference through */
1081  qprefer = 0;
1082  }
1083  if (!SEE('}'))
1084  { /* catches errors too */
1085  ERR(REG_BADBR);
1086  return top;
1087  }
1088  NEXT();
1089  break;
1090  default: /* no quantifier */
1091  m = n = 1;
1092  qprefer = 0;
1093  break;
1094  }
1095 
1096  /* annoying special case: {0} or {0,0} cancels everything */
1097  if (m == 0 && n == 0)
1098  {
1099  /*
1100  * If we had capturing subexpression(s) within the atom, we don't want
1101  * to destroy them, because it's legal (if useless) to back-ref them
1102  * later. Hence, just unlink the atom from lp/rp and then ignore it.
1103  */
1104  if (atom != NULL && (atom->flags & CAP))
1105  {
1106  delsub(v->nfa, lp, atom->begin);
1107  delsub(v->nfa, atom->end, rp);
1108  }
1109  else
1110  {
1111  /* Otherwise, we can clean up any subre infrastructure we made */
1112  if (atom != NULL)
1113  freesubre(v, atom);
1114  delsub(v->nfa, lp, rp);
1115  }
1116  EMPTYARC(lp, rp);
1117  return top;
1118  }
1119 
1120  /* if not a messy case, avoid hard part */
1121  assert(!MESSY(top->flags));
1122  f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
1123  if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f)))
1124  {
1125  if (!(m == 1 && n == 1))
1126  repeat(v, lp, rp, m, n);
1127  if (atom != NULL)
1128  freesubre(v, atom);
1129  top->flags = f;
1130  return top;
1131  }
1132 
1133  /*
1134  * hard part: something messy
1135  *
1136  * That is, capturing parens, back reference, short/long clash, or an atom
1137  * with substructure containing one of those.
1138  */
1139 
1140  /* now we'll need a subre for the contents even if they're boring */
1141  if (atom == NULL)
1142  {
1143  atom = subre(v, '=', 0, lp, rp);
1144  NOERRN();
1145  }
1146 
1147  /*
1148  * For what follows, we need the atom to have its own begin/end states
1149  * that are distinct from lp/rp, so that we can wrap iteration structure
1150  * around it. The parenthesized-atom case above already made suitable
1151  * states (and we don't want to modify a capturing subre, since it's
1152  * already recorded in v->subs[]). Otherwise, we need more states.
1153  */
1154  if (atom->begin == lp || atom->end == rp)
1155  {
1156  s = newstate(v->nfa);
1157  s2 = newstate(v->nfa);
1158  NOERRN();
1159  moveouts(v->nfa, lp, s);
1160  moveins(v->nfa, rp, s2);
1161  atom->begin = s;
1162  atom->end = s2;
1163  }
1164  else
1165  {
1166  /* The atom's OK, but we must temporarily disconnect it from lp/rp */
1167  /* (this removes the EMPTY arcs we made above) */
1168  delsub(v->nfa, lp, atom->begin);
1169  delsub(v->nfa, atom->end, rp);
1170  }
1171 
1172  /*----------
1173  * Prepare a general-purpose state skeleton.
1174  *
1175  * In the no-backrefs case, we want this:
1176  *
1177  * [lp] ---> [s] ---prefix---> ---atom---> ---rest---> [rp]
1178  *
1179  * where prefix is some repetitions of atom, and "rest" is the remainder
1180  * of the branch. In the general case we need:
1181  *
1182  * [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp]
1183  *
1184  * where the iterator wraps around the atom.
1185  *
1186  * We make the s state here for both cases; s2 is made below if needed
1187  *----------
1188  */
1189  s = newstate(v->nfa); /* set up starting state */
1190  NOERRN();
1191  EMPTYARC(lp, s);
1192  NOERRN();
1193 
1194  /* break remaining subRE into x{...} and what follows */
1195  t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
1196  NOERRN();
1197  t->child = atom;
1198  atomp = &t->child;
1199 
1200  /*
1201  * Here we should recurse to fill t->child->sibling ... but we must
1202  * postpone that to the end. One reason is that t->child may be replaced
1203  * below, and we don't want to worry about its sibling link.
1204  */
1205 
1206  /*
1207  * Convert top node to a concatenation of the prefix (top->child, covering
1208  * whatever we parsed previously) and remaining (t). Note that the prefix
1209  * could be empty, in which case this concatenation node is unnecessary.
1210  * To keep things simple, we operate in a general way for now, and get rid
1211  * of unnecessary subres below.
1212  */
1213  assert(top->op == '=' && top->child == NULL);
1214  top->child = subre(v, '=', top->flags, top->begin, lp);
1215  NOERRN();
1216  top->op = '.';
1217  top->child->sibling = t;
1218  /* top->flags will get updated later */
1219 
1220  /* if it's a backref, now is the time to replicate the subNFA */
1221  if (atomtype == BACKREF)
1222  {
1223  assert(atom->begin->nouts == 1); /* just the EMPTY */
1224  delsub(v->nfa, atom->begin, atom->end);
1225  assert(v->subs[subno] != NULL);
1226 
1227  /*
1228  * And here's why the recursion got postponed: it must wait until the
1229  * skeleton is filled in, because it may hit a backref that wants to
1230  * copy the filled-in skeleton.
1231  */
1232  dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
1233  atom->begin, atom->end);
1234  NOERRN();
1235 
1236  /* The backref node's NFA should not enforce any constraints */
1237  removeconstraints(v->nfa, atom->begin, atom->end);
1238  NOERRN();
1239  }
1240 
1241  /*
1242  * It's quantifier time. If the atom is just a backref, we'll let it deal
1243  * with quantifiers internally.
1244  */
1245  if (atomtype == BACKREF)
1246  {
1247  /* special case: backrefs have internal quantifiers */
1248  EMPTYARC(s, atom->begin); /* empty prefix */
1249  /* just stuff everything into atom */
1250  repeat(v, atom->begin, atom->end, m, n);
1251  atom->min = (short) m;
1252  atom->max = (short) n;
1253  atom->flags |= COMBINE(qprefer, atom->flags);
1254  /* rest of branch can be strung starting from atom->end */
1255  s2 = atom->end;
1256  }
1257  else if (m == 1 && n == 1 &&
1258  (qprefer == 0 ||
1259  (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
1260  qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
1261  {
1262  /* no/vacuous quantifier: done */
1263  EMPTYARC(s, atom->begin); /* empty prefix */
1264  /* rest of branch can be strung starting from atom->end */
1265  s2 = atom->end;
1266  }
1267  else if (!(atom->flags & (CAP | BACKR)))
1268  {
1269  /*
1270  * If there's no captures nor backrefs in the atom being repeated, we
1271  * don't really care where the submatches of the iteration are, so we
1272  * don't need an iteration node. Make a plain DFA node instead.
1273  */
1274  EMPTYARC(s, atom->begin); /* empty prefix */
1275  repeat(v, atom->begin, atom->end, m, n);
1276  f = COMBINE(qprefer, atom->flags);
1277  t = subre(v, '=', f, atom->begin, atom->end);
1278  NOERRN();
1279  freesubre(v, atom);
1280  *atomp = t;
1281  /* rest of branch can be strung starting from t->end */
1282  s2 = t->end;
1283  }
1284  else if (m > 0 && !(atom->flags & BACKR))
1285  {
1286  /*
1287  * If there's no backrefs involved, we can turn x{m,n} into
1288  * x{m-1,n-1}x, with capturing parens in only the second x. This is
1289  * valid because we only care about capturing matches from the final
1290  * iteration of the quantifier. It's a win because we can implement
1291  * the backref-free left side as a plain DFA node, since we don't
1292  * really care where its submatches are.
1293  */
1294  dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
1295  assert(m >= 1 && m != DUPINF && n >= 1);
1296  repeat(v, s, atom->begin, m - 1, (n == DUPINF) ? n : n - 1);
1297  f = COMBINE(qprefer, atom->flags);
1298  t = subre(v, '.', f, s, atom->end); /* prefix and atom */
1299  NOERRN();
1300  t->child = subre(v, '=', PREF(f), s, atom->begin);
1301  NOERRN();
1302  t->child->sibling = atom;
1303  *atomp = t;
1304  /* rest of branch can be strung starting from atom->end */
1305  s2 = atom->end;
1306  }
1307  else
1308  {
1309  /* general case: need an iteration node */
1310  s2 = newstate(v->nfa);
1311  NOERRN();
1312  moveouts(v->nfa, atom->end, s2);
1313  NOERRN();
1314  dupnfa(v->nfa, atom->begin, atom->end, s, s2);
1315  repeat(v, s, s2, m, n);
1316  f = COMBINE(qprefer, atom->flags);
1317  t = subre(v, '*', f, s, s2);
1318  NOERRN();
1319  t->min = (short) m;
1320  t->max = (short) n;
1321  t->child = atom;
1322  *atomp = t;
1323  /* rest of branch is to be strung from iteration's end state */
1324  }
1325 
1326  /* and finally, look after that postponed recursion */
1327  t = top->child->sibling;
1328  if (!(SEE('|') || SEE(stopper) || SEE(EOS)))
1329  {
1330  /* parse all the rest of the branch, and insert in t->child->sibling */
1331  t->child->sibling = parsebranch(v, stopper, type, s2, rp, 1);
1332  NOERRN();
1333  assert(SEE('|') || SEE(stopper) || SEE(EOS));
1334 
1335  /* here's the promised update of the flags */
1336  t->flags |= COMBINE(t->flags, t->child->sibling->flags);
1337  top->flags |= COMBINE(top->flags, t->flags);
1338 
1339  /* neither t nor top could be directly marked for capture as yet */
1340  assert(t->capno == 0);
1341  assert(top->capno == 0);
1342 
1343  /*
1344  * At this point both top and t are concatenation (op == '.') subres,
1345  * and we have top->child = prefix of branch, top->child->sibling = t,
1346  * t->child = messy atom (with quantification superstructure if
1347  * needed), t->child->sibling = rest of branch.
1348  *
1349  * If the messy atom was the first thing in the branch, then
1350  * top->child is vacuous and we can get rid of one level of
1351  * concatenation.
1352  */
1353  assert(top->child->op == '=');
1354  if (top->child->begin == top->child->end)
1355  {
1356  assert(!MESSY(top->child->flags));
1357  freesubre(v, top->child);
1358  top->child = t->child;
1359  freesrnode(v, t);
1360  }
1361 
1362  /*
1363  * Otherwise, it's possible that t->child is not messy in itself, but
1364  * we considered it messy because its greediness conflicts with what
1365  * preceded it. Then it could be that the combination of t->child and
1366  * the rest of the branch is also not messy, in which case we can get
1367  * rid of the child concatenation by merging t->child and the rest of
1368  * the branch into one plain DFA node.
1369  */
1370  else if (t->child->op == '=' &&
1371  t->child->sibling->op == '=' &&
1372  !MESSY(UP(t->child->flags | t->child->sibling->flags)))
1373  {
1374  t->op = '=';
1375  t->flags = COMBINE(t->child->flags, t->child->sibling->flags);
1376  freesubreandsiblings(v, t->child);
1377  t->child = NULL;
1378  }
1379  }
1380  else
1381  {
1382  /*
1383  * There's nothing left in the branch, so we don't need the second
1384  * concatenation node 't'. Just link s2 straight to rp.
1385  */
1386  EMPTYARC(s2, rp);
1387  top->child->sibling = t->child;
1388  top->flags |= COMBINE(top->flags, top->child->sibling->flags);
1389  freesrnode(v, t);
1390 
1391  /*
1392  * Again, it could be that top->child is vacuous (if the messy atom
1393  * was in fact the only thing in the branch). In that case we need no
1394  * concatenation at all; just replace top with top->child->sibling.
1395  */
1396  assert(top->child->op == '=');
1397  if (top->child->begin == top->child->end)
1398  {
1399  assert(!MESSY(top->child->flags));
1400  t = top->child->sibling;
1401  top->child->sibling = NULL;
1402  freesubre(v, top);
1403  top = t;
1404  }
1405  }
1406 
1407  return top;
1408 }
#define EAT(t)
Definition: regcomp.c:271
static int scannum(struct vars *)
Definition: regcomp.c:1511
#define REG_ESUBREG
Definition: regex.h:145
int nexttype
Definition: regcomp.c:245
#define REG_NLSTOP
Definition: regex.h:111
#define BRUSE
Definition: regguts.h:480
static void okcolors(struct nfa *, struct colormap *)
struct subre * child
Definition: regguts.h:496
chr nextvalue
Definition: regcomp.c:246
int capno
Definition: regguts.h:492
static void charclass(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1450
#define REG_EPAREN
Definition: regex.h:147
#define NOERRN()
Definition: regcomp.c:278
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1629
static void nonword(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1414
#define NEXT()
Definition: regcomp.c:269
#define MESSY(f)
Definition: regguts.h:486
int nins
Definition: regguts.h:322
color nlcolor
Definition: regcomp.c:254
char op
Definition: regguts.h:473
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:2051
short min
Definition: regguts.h:494
static void onechr(struct vars *, chr, struct state *, struct state *)
Definition: regcomp.c:1867
static void wordchrs(struct vars *)
Definition: regcomp.c:1949
struct state * begin
Definition: regguts.h:498
#define COMBINE(f1, f2)
Definition: regguts.h:489
#define ISERR()
Definition: regcomp.c:273
char latype
Definition: regguts.h:490
#define BEHIND
Definition: regcomp.c:299
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
int nsubexp
Definition: regcomp.c:248
#define ERR(e)
Definition: regcomp.c:276
#define CCLASSS
Definition: regcomp.c:294
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2143
struct colormap * cm
Definition: regcomp.c:253
static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *)
#define LACON
Definition: regcomp.c:297
static void removeconstraints(struct nfa *, struct state *, struct state *)
struct subre * sibling
Definition: regguts.h:497
#define REG_BADBR
Definition: regex.h:149
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2108
#define REG_BADRPT
Definition: regex.h:152
static void processlacon(struct vars *, struct state *, struct state *, int, struct state *, struct state *)
Definition: regcomp.c:1985
char flags
Definition: regguts.h:474
struct subre ** subs
Definition: regcomp.c:249
#define assert(TEST)
Definition: imath.c:73
static void moveins(struct nfa *, struct state *, struct state *)
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2127
static void moveouts(struct nfa *, struct state *, struct state *)
#define REG_ADVANCED
Definition: regex.h:105
#define SEE(t)
Definition: regcomp.c:270
size_t nsubs
Definition: regcomp.c:250
static struct state * newstate(struct nfa *)
#define SBEGIN
Definition: regcomp.c:302
static void delsub(struct nfa *, struct state *, struct state *)
#define BACKREF
Definition: regcomp.c:289
#define CCLASSC
Definition: regcomp.c:295
#define REG_EXTENDED
Definition: regex.h:103
struct nfa * nfa
Definition: regcomp.c:252
#define PREF(f)
Definition: regguts.h:487
int backno
Definition: regguts.h:493
char * s2
char_classes
Definition: regguts.h:133
#define PLAIN
Definition: regcomp.c:287
#define EMPTYARC(x, y)
Definition: regcomp.c:282
#define BACKR
Definition: regguts.h:479
static struct subre * parsebranch(struct vars *, int, int, struct state *, struct state *, int)
Definition: regcomp.c:741
#define EOS
Definition: regcomp.c:286
static void cbracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1685
#define NWBDRY
Definition: regcomp.c:301
#define DUPINF
Definition: regguts.h:94
#define REG_ASSERT
Definition: regex.h:153
#define MIXED
Definition: regguts.h:477
Definition: regguts.h:317
short max
Definition: regguts.h:495
#define REG_NLANCH
Definition: regex.h:112
struct state * end
Definition: regguts.h:499
Definition: regguts.h:471
#define NOTE(b)
Definition: regcomp.c:281
#define LONGER
Definition: regguts.h:475
static void repeat(struct vars *, struct state *, struct state *, int, int)
Definition: regcomp.c:1542
static void charclasscomplement(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1474
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1432
int nouts
Definition: regguts.h:323
#define CAP
Definition: regguts.h:478
#define UP(f)
Definition: regguts.h:485
#define SEND
Definition: regcomp.c:303
int cflags
Definition: regcomp.c:243
#define ARCV(t, val)
#define SHORTER
Definition: regguts.h:476
#define COLORLESS
Definition: regguts.h:153
#define WBDRY
Definition: regcomp.c:300
#define INSIST(c, e)
Definition: regcomp.c:280
static void moresubs(struct vars *, int)
Definition: regcomp.c:511
#define DIGIT
Definition: regcomp.c:288
#define AHEAD
Definition: regcomp.c:298
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673
#define REG_UPBOTCH
Definition: regex.h:65

◆ pg_regcomp()

int pg_regcomp ( regex_t re,
const chr string,
size_t  len,
int  flags,
Oid  collation 
)

Definition at line 328 of file regcomp.c.

References assert, casecmp(), vars::cflags, guts::cflags, cleanst(), vars::cm, guts::cmap, cmp(), CNOERR, COLORLESS, compact(), vars::cv, vars::cv2, debug, DISCARD, dumpnfa(), EOS, vars::err, nfa::final, subre::flags, fprintf, freev(), vars::g, GUTSMAGIC, i, guts::info, nfa::init, initcm(), vars::lacons, guts::lacons, subre::latype, LATYPE_IS_AHEAD, lexstart(), guts::magic, makesearch(), MALLOC, markst(), newcvec(), newline(), newnfa(), vars::nfa, nfanode(), nfatree(), vars::nlacons, guts::nlacons, vars::nlcolor, NOTE, vars::now, guts::nsub, vars::nsubexp, vars::nsubs, vars::ntree, guts::ntree, numst(), okcolors(), optimize(), parse(), pg_set_regex_collation(), PLAIN, vars::re, regex_t::re_collation, regex_t::re_csize, regex_t::re_fns, regex_t::re_guts, regex_t::re_info, regex_t::re_magic, regex_t::re_nsub, REG_ADVANCED, REG_ADVF, REG_DUMP, REG_ESPACE, REG_EXPANDED, REG_EXTENDED, REG_ICASE, REG_INVARG, REG_NEWLINE, REG_NLANCH, REG_NLSTOP, REG_NOSUB, REG_PROGRESS, REG_QUOTE, REG_USHORTEST, REMAGIC, removecaptures(), guts::search, SEE, SHORTER, vars::spaceused, specialcolors(), generate_unaccent_rules::stdout, vars::stop, vars::sub10, subcolor(), vars::subs, vars::tree, guts::tree, vars::treechain, vars::treefree, VS, vars::wordchrs, and ZAPCNFA.

Referenced by NIAddAffix(), parse_ident_line(), RE_compile(), RE_compile_and_cache(), and test_re_compile().

333 {
334  struct vars var;
335  struct vars *v = &var;
336  struct guts *g;
337  int i;
338  size_t j;
339 
340 #ifdef REG_DEBUG
341  FILE *debug = (flags & REG_PROGRESS) ? stdout : (FILE *) NULL;
342 #else
343  FILE *debug = (FILE *) NULL;
344 #endif
345 
346 #define CNOERR() { if (ISERR()) return freev(v, v->err); }
347 
348  /* sanity checks */
349 
350  if (re == NULL || string == NULL)
351  return REG_INVARG;
352  if ((flags & REG_QUOTE) &&
353  (flags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)))
354  return REG_INVARG;
355  if (!(flags & REG_EXTENDED) && (flags & REG_ADVF))
356  return REG_INVARG;
357 
358  /* Initialize locale-dependent support */
359  pg_set_regex_collation(collation);
360 
361  /* initial setup (after which freev() is callable) */
362  v->re = re;
363  v->now = string;
364  v->stop = v->now + len;
365  v->err = 0;
366  v->cflags = flags;
367  v->nsubexp = 0;
368  v->subs = v->sub10;
369  v->nsubs = 10;
370  for (j = 0; j < v->nsubs; j++)
371  v->subs[j] = NULL;
372  v->nfa = NULL;
373  v->cm = NULL;
374  v->nlcolor = COLORLESS;
375  v->wordchrs = NULL;
376  v->tree = NULL;
377  v->treechain = NULL;
378  v->treefree = NULL;
379  v->cv = NULL;
380  v->cv2 = NULL;
381  v->lacons = NULL;
382  v->nlacons = 0;
383  v->spaceused = 0;
384  re->re_magic = REMAGIC;
385  re->re_info = 0; /* bits get set during parse */
386  re->re_csize = sizeof(chr);
387  re->re_collation = collation;
388  re->re_guts = NULL;
389  re->re_fns = VS(&functions);
390 
391  /* more complex setup, malloced things */
392  re->re_guts = VS(MALLOC(sizeof(struct guts)));
393  if (re->re_guts == NULL)
394  return freev(v, REG_ESPACE);
395  g = (struct guts *) re->re_guts;
396  g->tree = NULL;
397  initcm(v, &g->cmap);
398  v->cm = &g->cmap;
399  g->lacons = NULL;
400  g->nlacons = 0;
401  ZAPCNFA(g->search);
402  v->nfa = newnfa(v, v->cm, (struct nfa *) NULL);
403  CNOERR();
404  /* set up a reasonably-sized transient cvec for getcvec usage */
405  v->cv = newcvec(100, 20);
406  if (v->cv == NULL)
407  return freev(v, REG_ESPACE);
408 
409  /* parsing */
410  lexstart(v); /* also handles prefixes */
411  if ((v->cflags & REG_NLSTOP) || (v->cflags & REG_NLANCH))
412  {
413  /* assign newline a unique color */
414  v->nlcolor = subcolor(v->cm, newline());
415  okcolors(v->nfa, v->cm);
416  }
417  CNOERR();
418  v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
419  assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */
420  CNOERR();
421  assert(v->tree != NULL);
422 
423  /* finish setup of nfa and its subre tree */
424  specialcolors(v->nfa);
425  CNOERR();
426 #ifdef REG_DEBUG
427  if (debug != NULL)
428  {
429  fprintf(debug, "\n\n\n========= RAW ==========\n");
430  dumpnfa(v->nfa, debug);
431  dumpst(v->tree, debug, 1);
432  }
433 #endif
434  if (v->cflags & REG_NOSUB)
435  removecaptures(v, v->tree);
436  v->ntree = numst(v->tree, 1);
437  markst(v->tree);
438  cleanst(v);
439 #ifdef REG_DEBUG
440  if (debug != NULL)
441  {
442  fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
443  dumpst(v->tree, debug, 1);
444  }
445 #endif
446 
447  /* build compacted NFAs for tree and lacons */
448  re->re_info |= nfatree(v, v->tree, debug);
449  CNOERR();
450  assert(v->nlacons == 0 || v->lacons != NULL);
451  for (i = 1; i < v->nlacons; i++)
452  {
453  struct subre *lasub = &v->lacons[i];
454 
455 #ifdef REG_DEBUG
456  if (debug != NULL)
457  fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
458 #endif
459 
460  /* Prepend .* to pattern if it's a lookbehind LACON */
461  nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->latype), debug);
462  }
463  CNOERR();
464  if (v->tree->flags & SHORTER)
466 
467  /* build compacted NFAs for tree, lacons, fast search */
468 #ifdef REG_DEBUG
469  if (debug != NULL)
470  fprintf(debug, "\n\n\n========= SEARCH ==========\n");
471 #endif
472  /* can sacrifice main NFA now, so use it as work area */
473  (DISCARD) optimize(v->nfa, debug);
474  CNOERR();
475  makesearch(v, v->nfa);
476  CNOERR();
477  compact(v->nfa, &g->search);
478  CNOERR();
479 
480  /* looks okay, package it up */
481  re->re_nsub = v->nsubexp;
482  v->re = NULL; /* freev no longer frees re */
483  g->magic = GUTSMAGIC;
484  g->cflags = v->cflags;
485  g->info = re->re_info;
486  g->nsub = re->re_nsub;
487  g->tree = v->tree;
488  v->tree = NULL;
489  g->ntree = v->ntree;
490  g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
491  g->lacons = v->lacons;
492  v->lacons = NULL;
493  g->nlacons = v->nlacons;
494 
495 #ifdef REG_DEBUG
496  if (flags & REG_DUMP)
497  {
498  dump(re, stdout);
499  fflush(stdout);
500  }
501 #endif
502 
503  assert(v->err == 0);
504  return freev(v, 0);
505 }
#define REG_USHORTEST
Definition: regex.h:75
static chr newline(void)
struct subre * treefree
Definition: regcomp.c:258
#define REG_NLSTOP
Definition: regex.h:111
char * re_fns
Definition: regex.h:81
static void okcolors(struct nfa *, struct colormap *)
static int numst(struct subre *, int)
Definition: regcomp.c:2220
static long optimize(struct nfa *, FILE *)
int ntree
Definition: regcomp.c:259
static bool debug
Definition: initdb.c:140
struct subre * lacons
Definition: regguts.h:539
struct subre * tree
Definition: regguts.h:534
struct state * final
Definition: regguts.h:347
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:2287
struct subre * treechain
Definition: regcomp.c:257
#define REG_ESPACE
Definition: regex.h:151
static int freev(struct vars *, int)
Definition: regcomp.c:548
static void specialcolors(struct nfa *)
#define REG_QUOTE
Definition: regex.h:106
static struct cvec * newcvec(int, int)
color nlcolor
Definition: regcomp.c:254
int nlacons
Definition: regguts.h:540
#define REMAGIC
Definition: regguts.h:96
static color subcolor(struct colormap *, chr)
Definition: regguts.h:343
char latype
Definition: regguts.h:490
#define fprintf
Definition: port.h:220
#define REG_ICASE
Definition: regex.h:108
int re_csize
Definition: regex.h:76
static void makesearch(struct vars *, struct nfa *)
Definition: regcomp.c:577
int nsubexp
Definition: regcomp.c:248
#define MALLOC(n)
Definition: regcustom.h:60
#define REG_PROGRESS
Definition: regex.h:119
int re_magic
Definition: regex.h:57
struct cnfa search
Definition: regguts.h:535
pg_wchar chr
Definition: regcustom.h:66
size_t re_nsub
Definition: regex.h:58
struct colormap * cm
Definition: regcomp.c:253
#define ZAPCNFA(cnfa)
Definition: regguts.h:429
static void dumpnfa(struct nfa *, FILE *)
regex_t * re
Definition: regcomp.c:239
int nlacons
Definition: regcomp.c:263
int magic
Definition: regguts.h:529
#define REG_INVARG
Definition: regex.h:154
static void markst(struct subre *)
Definition: regcomp.c:2253
char flags
Definition: regguts.h:474
struct subre ** subs
Definition: regcomp.c:249
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
char string[11]
Definition: preproc-type.c:46
struct colormap cmap
Definition: regguts.h:537
#define assert(TEST)
Definition: imath.c:73
#define LATYPE_IS_AHEAD(la)
Definition: regguts.h:104
#define REG_NEWLINE
Definition: regex.h:113
#define REG_ADVANCED
Definition: regex.h:105
#define SEE(t)
Definition: regcomp.c:270
size_t nsubs
Definition: regcomp.c:250
int err
Definition: regcomp.c:242
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:2307
struct nfa * nfa
Definition: regcomp.c:252
#define REG_EXTENDED
Definition: regex.h:103
struct cvec * cv
Definition: regcomp.c:260
struct cvec * cv2
Definition: regcomp.c:261
#define REG_ADVF
Definition: regex.h:104
#define PLAIN
Definition: regcomp.c:287
struct state * wordchrs
Definition: regcomp.c:255
long re_info
Definition: regex.h:59
#define REG_DUMP
Definition: regex.h:117
Definition: regguts.h:527
int ntree
Definition: regguts.h:536
#define EOS
Definition: regcomp.c:286
#define GUTSMAGIC
Definition: regguts.h:530
void pg_set_regex_collation(Oid collation)
const chr * now
Definition: regcomp.c:240
#define REG_NLANCH
Definition: regex.h:112
static const struct fns functions
Definition: regcomp.c:313
#define REG_NOSUB
Definition: regex.h:109
#define VS(x)
Definition: regguts.h:61
static int cmp(const chr *, const chr *, size_t)
static void initcm(struct vars *, struct colormap *)
long info
Definition: regguts.h:532
Definition: regguts.h:471
#define NOTE(b)
Definition: regcomp.c:281
#define CNOERR()
const chr * stop
Definition: regcomp.c:241
char * re_guts
Definition: regex.h:80
int cflags
Definition: regguts.h:531
struct state * init
Definition: regguts.h:346
int cflags
Definition: regcomp.c:243
size_t nsub
Definition: regguts.h:533
static void lexstart(struct vars *)
static int casecmp(const chr *, const chr *, size_t)
#define SHORTER
Definition: regguts.h:476
int i
struct subre * sub10[10]
Definition: regcomp.c:251
#define COLORLESS
Definition: regguts.h:153
#define REG_EXPANDED
Definition: regex.h:110
#define DISCARD
Definition: regguts.h:58
struct subre * tree
Definition: regcomp.c:256
static void cleanst(struct vars *)
Definition: regcomp.c:2268
static void removecaptures(struct vars *, struct subre *)
Definition: regcomp.c:2174
size_t spaceused
Definition: regcomp.c:265
struct subre * lacons
Definition: regcomp.c:262
Definition: regcomp.c:237
Oid re_collation
Definition: regex.h:78
static void compact(struct nfa *, struct cnfa *)
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ pg_wc_isalnum()

static int pg_wc_isalnum ( pg_wchar  c)
static

◆ pg_wc_isalpha()

static int pg_wc_isalpha ( pg_wchar  c)
static

◆ pg_wc_isdigit()

static int pg_wc_isdigit ( pg_wchar  c)
static

◆ pg_wc_isgraph()

static int pg_wc_isgraph ( pg_wchar  c)
static

◆ pg_wc_islower()

static int pg_wc_islower ( pg_wchar  c)
static

◆ pg_wc_isprint()

static int pg_wc_isprint ( pg_wchar  c)
static

◆ pg_wc_ispunct()

static int pg_wc_ispunct ( pg_wchar  c)
static

◆ pg_wc_isspace()

static int pg_wc_isspace ( pg_wchar  c)
static

◆ pg_wc_isupper()

static int pg_wc_isupper ( pg_wchar  c)
static

◆ pg_wc_isword()

static int pg_wc_isword ( pg_wchar  c)
static

◆ pg_wc_tolower()

static pg_wchar pg_wc_tolower ( pg_wchar  c)
static

◆ pg_wc_toupper()

static pg_wchar pg_wc_toupper ( pg_wchar  c)
static

◆ prefixes()

static void prefixes ( struct vars )
static

◆ processlacon()

static void processlacon ( struct vars v,
struct state begin,
struct state end,
int  latype,
struct state lp,
struct state rp 
)
static

Definition at line 1985 of file regcomp.c.

References AHEAD, assert, BEHIND, cloneouts(), vars::cm, colorcomplement(), LACON, LATYPE_AHEAD_NEG, LATYPE_AHEAD_POS, LATYPE_BEHIND_NEG, LATYPE_BEHIND_POS, newarc(), newlacon(), vars::nfa, NOTREACHED, s1, and single_color_transition().

Referenced by parseqatom().

1991 {
1992  struct state *s1;
1993  int n;
1994 
1995  /*
1996  * Check for lookaround RE consisting of a single plain color arc (or set
1997  * of arcs); this would typically be a simple chr or a bracket expression.
1998  */
1999  s1 = single_color_transition(begin, end);
2000  switch (latype)
2001  {
2002  case LATYPE_AHEAD_POS:
2003  /* If lookahead RE is just colorset C, convert to AHEAD(C) */
2004  if (s1 != NULL)
2005  {
2006  cloneouts(v->nfa, s1, lp, rp, AHEAD);
2007  return;
2008  }
2009  break;
2010  case LATYPE_AHEAD_NEG:
2011  /* If lookahead RE is just colorset C, convert to AHEAD(^C)|$ */
2012  if (s1 != NULL)
2013  {
2014  colorcomplement(v->nfa, v->cm, AHEAD, s1, lp, rp);
2015  newarc(v->nfa, '$', 1, lp, rp);
2016  newarc(v->nfa, '$', 0, lp, rp);
2017  return;
2018  }
2019  break;
2020  case LATYPE_BEHIND_POS:
2021  /* If lookbehind RE is just colorset C, convert to BEHIND(C) */
2022  if (s1 != NULL)
2023  {
2024  cloneouts(v->nfa, s1, lp, rp, BEHIND);
2025  return;
2026  }
2027  break;
2028  case LATYPE_BEHIND_NEG:
2029  /* If lookbehind RE is just colorset C, convert to BEHIND(^C)|^ */
2030  if (s1 != NULL)
2031  {
2032  colorcomplement(v->nfa, v->cm, BEHIND, s1, lp, rp);
2033  newarc(v->nfa, '^', 1, lp, rp);
2034  newarc(v->nfa, '^', 0, lp, rp);
2035  return;
2036  }
2037  break;
2038  default:
2039  assert(NOTREACHED);
2040  }
2041 
2042  /* General case: we need a LACON subre and arc */
2043  n = newlacon(v, begin, end, latype);
2044  newarc(v->nfa, LACON, n, lp, rp);
2045 }
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
#define LATYPE_BEHIND_POS
Definition: regguts.h:101
#define LATYPE_AHEAD_NEG
Definition: regguts.h:100
#define BEHIND
Definition: regcomp.c:299
#define NOTREACHED
Definition: regguts.h:91
#define LATYPE_AHEAD_POS
Definition: regguts.h:99
static struct state * single_color_transition(struct state *, struct state *)
struct colormap * cm
Definition: regcomp.c:253
char * s1
#define LACON
Definition: regcomp.c:297
#define assert(TEST)
Definition: imath.c:73
#define LATYPE_BEHIND_NEG
Definition: regguts.h:102
struct nfa * nfa
Definition: regcomp.c:252
Definition: regguts.h:317
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:298
static int newlacon(struct vars *, struct state *, struct state *, int)
Definition: regcomp.c:2346
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)

◆ pseudocolor()

static color pseudocolor ( struct colormap )
static

◆ pull()

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

◆ pullback()

static void pullback ( struct nfa ,
FILE *   
)
static

◆ push()

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

◆ pushfwd()

static void pushfwd ( struct nfa ,
FILE *   
)
static

◆ rainbow()

static void rainbow ( struct nfa ,
struct colormap ,
int  ,
color  ,
struct state ,
struct state  
)
static

Referenced by makesearch(), and parseqatom().

◆ range()

static struct cvec* range ( struct vars ,
chr  ,
chr  ,
int   
)
static

Referenced by brackpart().

◆ rcancelrequested()

static int rcancelrequested ( void  )
static

Definition at line 2438 of file regcomp.c.

References InterruptPending, ProcDiePending, and QueryCancelPending.

2439 {
2441 }
volatile sig_atomic_t QueryCancelPending
Definition: globals.c:31
volatile sig_atomic_t ProcDiePending
Definition: globals.c:32
volatile sig_atomic_t InterruptPending
Definition: globals.c:30

◆ removecaptures()

static void removecaptures ( struct vars v,
struct subre t 
)
static

Definition at line 2174 of file regcomp.c.

References assert, BACKR, BRUSE, CAP, subre::capno, subre::child, subre::flags, freesubreandsiblings(), MIXED, subre::op, and subre::sibling.

Referenced by pg_regcomp().

2176 {
2177  struct subre *t2;
2178 
2179  assert(t != NULL);
2180 
2181  /*
2182  * If this isn't itself a backref target, clear capno and tentatively
2183  * clear CAP flag.
2184  */
2185  if (!(t->flags & BRUSE))
2186  {
2187  t->capno = 0;
2188  t->flags &= ~CAP;
2189  }
2190 
2191  /* Now recurse to children */
2192  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2193  {
2194  removecaptures(v, t2);
2195  /* Propagate child CAP flag back up, if it's still set */
2196  if (t2->flags & CAP)
2197  t->flags |= CAP;
2198  }
2199 
2200  /*
2201  * If t now contains neither captures nor backrefs, there's no longer any
2202  * need to care where its sub-match boundaries are, so we can reduce it to
2203  * a simple DFA node. (Note in particular that MIXED child greediness is
2204  * not a hindrance here, so we don't use the MESSY() macro.)
2205  */
2206  if ((t->flags & (CAP | BACKR)) == 0)
2207  {
2208  if (t->child)
2209  freesubreandsiblings(v, t->child);
2210  t->child = NULL;
2211  t->op = '=';
2212  t->flags &= ~MIXED;
2213  }
2214 }
#define BRUSE
Definition: regguts.h:480
struct subre * child
Definition: regguts.h:496
int capno
Definition: regguts.h:492
char op
Definition: regguts.h:473
struct subre * sibling
Definition: regguts.h:497
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2127
#define BACKR
Definition: regguts.h:479
#define MIXED
Definition: regguts.h:477
Definition: regguts.h:471
#define CAP
Definition: regguts.h:478
static void removecaptures(struct vars *, struct subre *)
Definition: regcomp.c:2174

◆ removeconstraints()

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

Referenced by parseqatom().

◆ removetraverse()

static void removetraverse ( struct nfa ,
struct state  
)
static

◆ repeat()

static void repeat ( struct vars v,
struct state lp,
struct state rp,
int  m,
int  n 
)
static

Definition at line 1542 of file regcomp.c.

References delsub(), dupnfa(), EMPTYARC, ERR, INF, moveins(), moveouts(), newstate(), vars::nfa, NOERR, PAIR, REDUCE, REG_ASSERT, s2, and SOME.

Referenced by parseqatom().

1547 {
1548 #define SOME 2
1549 #define INF 3
1550 #define PAIR(x, y) ((x)*4 + (y))
1551 #define REDUCE(x) ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
1552  const int rm = REDUCE(m);
1553  const int rn = REDUCE(n);
1554  struct state *s;
1555  struct state *s2;
1556 
1557  switch (PAIR(rm, rn))
1558  {
1559  case PAIR(0, 0): /* empty string */
1560  delsub(v->nfa, lp, rp);
1561  EMPTYARC(lp, rp);
1562  break;
1563  case PAIR(0, 1): /* do as x| */
1564  EMPTYARC(lp, rp);
1565  break;
1566  case PAIR(0, SOME): /* do as x{1,n}| */
1567  repeat(v, lp, rp, 1, n);
1568  NOERR();
1569  EMPTYARC(lp, rp);
1570  break;
1571  case PAIR(0, INF): /* loop x around */
1572  s = newstate(v->nfa);
1573  NOERR();
1574  moveouts(v->nfa, lp, s);
1575  moveins(v->nfa, rp, s);
1576  EMPTYARC(lp, s);
1577  EMPTYARC(s, rp);
1578  break;
1579  case PAIR(1, 1): /* no action required */
1580  break;
1581  case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */
1582  s = newstate(v->nfa);
1583  NOERR();
1584  moveouts(v->nfa, lp, s);
1585  dupnfa(v->nfa, s, rp, lp, s);
1586  NOERR();
1587  repeat(v, lp, s, 1, n - 1);
1588  NOERR();
1589  EMPTYARC(lp, s);
1590  break;
1591  case PAIR(1, INF): /* add loopback arc */
1592  s = newstate(v->nfa);
1593  s2 = newstate(v->nfa);
1594  NOERR();
1595  moveouts(v->nfa, lp, s);
1596  moveins(v->nfa, rp, s2);
1597  EMPTYARC(lp, s);
1598  EMPTYARC(s2, rp);
1599  EMPTYARC(s2, s);
1600  break;
1601  case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */
1602  s = newstate(v->nfa);
1603  NOERR();
1604  moveouts(v->nfa, lp, s);
1605  dupnfa(v->nfa, s, rp, lp, s);
1606  NOERR();
1607  repeat(v, lp, s, m - 1, n - 1);
1608  break;
1609  case PAIR(SOME, INF): /* do as x{m-1,}x */
1610  s = newstate(v->nfa);
1611  NOERR();
1612  moveouts(v->nfa, lp, s);
1613  dupnfa(v->nfa, s, rp, lp, s);
1614  NOERR();
1615  repeat(v, lp, s, m - 1, n);
1616  break;
1617  default:
1618  ERR(REG_ASSERT);
1619  break;
1620  }
1621 }
#define PAIR(x, y)
#define REDUCE(x)
Definition: regguts.h:343
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
#define SOME
#define ERR(e)
Definition: regcomp.c:276
static void moveins(struct nfa *, struct state *, struct state *)
static void moveouts(struct nfa *, struct state *, struct state *)
static struct state * newstate(struct nfa *)
static void delsub(struct nfa *, struct state *, struct state *)
struct nfa * nfa
Definition: regcomp.c:252
char * s2
#define EMPTYARC(x, y)
Definition: regcomp.c:282
#define REG_ASSERT
Definition: regex.h:153
Definition: regguts.h:317
static void repeat(struct vars *, struct state *, struct state *, int, int)
Definition: regcomp.c:1542
#define NOERR()
Definition: regcomp.c:277
#define INF

◆ rfree()

static void rfree ( regex_t re)
static

Definition at line 2402 of file regcomp.c.

References guts::cmap, FREE, freecm(), freecnfa(), freelacons(), freesubre(), vars::g, guts::lacons, guts::magic, guts::nlacons, NULLCNFA, regex_t::re_fns, regex_t::re_guts, regex_t::re_magic, REMAGIC, guts::search, and guts::tree.

Referenced by freev().

2403 {
2404  struct guts *g;
2405 
2406  if (re == NULL || re->re_magic != REMAGIC)
2407  return;
2408 
2409  re->re_magic = 0; /* invalidate RE */
2410  g = (struct guts *) re->re_guts;
2411  re->re_guts = NULL;
2412  re->re_fns = NULL;
2413  if (g != NULL)
2414  {
2415  g->magic = 0;
2416  freecm(&g->cmap);
2417  if (g->tree != NULL)
2418  freesubre((struct vars *) NULL, g->tree);
2419  if (g->lacons != NULL)
2420  freelacons(g->lacons, g->nlacons);
2421  if (!NULLCNFA(g->search))
2422  freecnfa(&g->search);
2423  FREE(g);
2424  }
2425 }
char * re_fns
Definition: regex.h:81
static void freelacons(struct subre *, int)
Definition: regcomp.c:2385
struct subre * lacons
Definition: regguts.h:539
struct subre * tree
Definition: regguts.h:534
static void freecm(struct colormap *)
#define NULLCNFA(cnfa)
Definition: regguts.h:431
int nlacons
Definition: regguts.h:540
#define REMAGIC
Definition: regguts.h:96
#define FREE(ptr)
Definition: cryptohash.c:37
int re_magic
Definition: regex.h:57
struct cnfa search
Definition: regguts.h:535
int magic
Definition: regguts.h:529
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2108
struct colormap cmap
Definition: regguts.h:537
Definition: regguts.h:527
char * re_guts
Definition: regex.h:80
static void freecnfa(struct cnfa *)
Definition: regcomp.c:237

◆ rstacktoodeep()

◆ scannum()

static int scannum ( struct vars v)
static

Definition at line 1511 of file regcomp.c.

References DIGIT, DUPMAX, ERR, NEXT, vars::nextvalue, REG_BADBR, and SEE.

Referenced by parseqatom().

1512 {
1513  int n = 0;
1514 
1515  while (SEE(DIGIT) && n < DUPMAX)
1516  {
1517  n = n * 10 + v->nextvalue;
1518  NEXT();
1519  }
1520  if (SEE(DIGIT) || n > DUPMAX)
1521  {
1522  ERR(REG_BADBR);
1523  return 0;
1524  }
1525  return n;
1526 }
chr nextvalue
Definition: regcomp.c:246
#define DUPMAX
Definition: regguts.h:93
#define NEXT()
Definition: regcomp.c:269
#define ERR(e)
Definition: regcomp.c:276
#define REG_BADBR
Definition: regex.h:149
#define SEE(t)
Definition: regcomp.c:270
#define DIGIT
Definition: regcomp.c:288

◆ scanplain()

static const chr * scanplain ( struct vars v)
static

Definition at line 1842 of file regcomp.c.

References assert, CCLASS, COLLEL, ECLASS, END, ISERR, NEXT, vars::now, PLAIN, and SEE.

Referenced by brackpart().

1843 {
1844  const chr *endp;
1845 
1846  assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
1847  NEXT();
1848 
1849  endp = v->now;
1850  while (SEE(PLAIN))
1851  {
1852  endp = v->now;
1853  NEXT();
1854  }
1855 
1856  assert(SEE(END) || ISERR());
1857  NEXT();
1858 
1859  return endp;
1860 }
#define END
Definition: regcomp.c:293
#define CCLASS
Definition: regcomp.c:292
#define NEXT()
Definition: regcomp.c:269
#define ISERR()
Definition: regcomp.c:273
pg_wchar chr
Definition: regcustom.h:66
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:270
#define PLAIN
Definition: regcomp.c:287
const chr * now
Definition: regcomp.c:240
#define COLLEL
Definition: regcomp.c:290
#define ECLASS
Definition: regcomp.c:291

◆ single_color_transition()

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

Referenced by processlacon().

◆ skip()

static void skip ( struct vars )
static

◆ sortins()

static void sortins ( struct nfa ,
struct state  
)
static

◆ sortins_cmp()

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

◆ sortouts()

static void sortouts ( struct nfa ,
struct state  
)
static

◆ sortouts_cmp()

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

◆ specialcolors()

static void specialcolors ( struct nfa )
static

Referenced by nfanode(), and pg_regcomp().

◆ subcolor()

static color subcolor ( struct colormap ,
chr   
)
static

Referenced by pg_regcomp().

◆ subcolorcvec()

static void subcolorcvec ( struct vars ,
struct cvec ,
struct state ,
struct state  
)
static

◆ subcolorhi()

static color subcolorhi ( struct colormap ,
color  
)
static

◆ subcoloronechr()

static void subcoloronechr ( struct vars ,
chr  ,
struct state ,
struct state ,
color  
)
static

Referenced by onechr().

◆ subcoloronerange()

static void subcoloronerange ( struct vars ,
chr  ,
chr  ,
struct state ,
struct state ,
color  
)
static

◆ subcoloronerow()

static void subcoloronerow ( struct vars ,
int  ,
struct state ,
struct state ,
color  
)
static

◆ subre()

static struct subre * subre ( struct vars v,
int  op,
int  flags,
struct state begin,
struct state end 
)
static

Definition at line 2051 of file regcomp.c.

References assert, subre::backno, subre::begin, subre::capno, subre::chain, subre::child, subre::cnfa, subre::end, ERR, subre::flags, subre::id, subre::latype, MALLOC, subre::max, subre::min, subre::op, vars::re, REG_ESPACE, REG_ETOOBIG, subre::sibling, STACK_TOO_DEEP, vars::treechain, vars::treefree, and ZAPCNFA.

Referenced by parse(), parsebranch(), and parseqatom().

2056 {
2057  struct subre *ret = v->treefree;
2058 
2059  /*
2060  * Checking for stack overflow here is sufficient to protect parse() and
2061  * its recursive subroutines.
2062  */
2063  if (STACK_TOO_DEEP(v->re))
2064  {
2065  ERR(REG_ETOOBIG);
2066  return NULL;
2067  }
2068 
2069  if (ret != NULL)
2070  v->treefree = ret->child;
2071  else
2072  {
2073  ret = (struct subre *) MALLOC(sizeof(struct subre));
2074  if (ret == NULL)
2075  {
2076  ERR(REG_ESPACE);
2077  return NULL;
2078  }
2079  ret->chain = v->treechain;
2080  v->treechain = ret;
2081  }
2082 
2083  assert(strchr("=b|.*(", op) != NULL);
2084 
2085  ret->op = op;
2086  ret->flags = flags;
2087  ret->latype = (char) -1;
2088  ret->id = 0; /* will be assigned later */
2089  ret->capno = 0;
2090  ret->backno = 0;
2091  ret->min = ret->max = 1;
2092  ret->child = NULL;
2093  ret->sibling = NULL;
2094  ret->begin = begin;
2095  ret->end = end;
2096  ZAPCNFA(ret->cnfa);
2097 
2098  return ret;
2099 }
struct subre * treefree
Definition: regcomp.c:258
struct subre * child
Definition: regguts.h:496
int capno
Definition: regguts.h:492
struct subre * treechain
Definition: regcomp.c:257
#define REG_ESPACE
Definition: regex.h:151
char op
Definition: regguts.h:473
short min
Definition: regguts.h:494
struct state * begin
Definition: regguts.h:498
char latype
Definition: regguts.h:490
#define MALLOC(n)
Definition: regcustom.h:60
#define ERR(e)
Definition: regcomp.c:276
struct subre * chain
Definition: regguts.h:501
int id
Definition: regguts.h:491
#define ZAPCNFA(cnfa)
Definition: regguts.h:429
regex_t * re
Definition: regcomp.c:239
struct subre * sibling
Definition: regguts.h:497
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
int backno
Definition: regguts.h:493
short max
Definition: regguts.h:495
#define REG_ETOOBIG
Definition: regex.h:157
struct state * end
Definition: regguts.h:499
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:500
#define STACK_TOO_DEEP(re)
Definition: regguts.h:520

◆ uncolorchain()

static void uncolorchain ( struct colormap ,
struct arc  
)
static

◆ word()

static void word ( struct vars v,
int  dir,
struct state lp,
struct state rp 
)
static

Definition at line 1432 of file regcomp.c.

References AHEAD, assert, BEHIND, cloneouts(), vars::nfa, and vars::wordchrs.

Referenced by addNorm(), AddStem(), CheckCompoundAffixes(), cmpspell(), compareWORD(), dxsyn_lexize(), hlfinditem(), itsabbr(), parseqatom(), pg_ceil_log2_64(), pg_rotate_right32(), and unicode_to_utf8word().

1436 {
1437  assert(dir == AHEAD || dir == BEHIND);
1438  cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
1439  /* (no need for special attention to \n) */
1440 }
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
#define BEHIND
Definition: regcomp.c:299
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:252
struct state * wordchrs
Definition: regcomp.c:255
#define AHEAD
Definition: regcomp.c:298

◆ wordchrs()

static void wordchrs ( struct vars v)
static

Definition at line 1949 of file regcomp.c.

References CC_WORD, cclasscvec(), vars::cflags, vars::cm, vars::cv, newstate(), vars::nfa, NOERR, NOTE, okcolors(), REG_ICASE, REG_ULOCALE, subcolorcvec(), and vars::wordchrs.

1950 {
1951  struct state *cstate;
1952  struct cvec *cv;
1953 
1954  if (v->wordchrs != NULL)
1955  return; /* done already */
1956 
1957  /* make dummy state to hang the cache arcs on */
1958  cstate = newstate(v->nfa);
1959  NOERR();
1960 
1961  /* obtain possibly-cached cvec for \w characters */
1962  NOTE(REG_ULOCALE);
1963  cv = cclasscvec(v, CC_WORD, (v->cflags & REG_ICASE));
1964  NOERR();
1965 
1966  /* build the arcs; this may cause color splitting */
1967  subcolorcvec(v, cv, cstate, cstate);
1968  NOERR();
1969 
1970  /* close new open subcolors to ensure the cache entry is self-contained */
1971  okcolors(v->nfa, v->cm);
1972  NOERR();
1973 
1974  /* success! save the cache pointer */
1975  v->wordchrs = cstate;
1976 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
static void okcolors(struct nfa *, struct colormap *)
#define REG_ULOCALE
Definition: regex.h:72
#define REG_ICASE
Definition: regex.h:108
struct colormap * cm
Definition: regcomp.c:253
static struct cvec * cclasscvec(struct vars *, enum char_classes, int)
static struct state * newstate(struct nfa *)
Definition: regguts.h:273
struct nfa * nfa
Definition: regcomp.c:252
struct state * wordchrs
Definition: regcomp.c:255
Definition: regguts.h:317
#define NOTE(b)
Definition: regcomp.c:281
#define NOERR()
Definition: regcomp.c:277
int cflags
Definition: regcomp.c:243

Variable Documentation

◆ functions

const struct fns functions
static