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 void parseqatom (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 optst (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(), 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 1590 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().

1593 {
1594  /*
1595  * We can't process complemented char classes (e.g. \W) immediately while
1596  * scanning the bracket expression, else color bookkeeping gets confused.
1597  * Instead, remember whether we saw any in have_cclassc[], and process
1598  * them at the end.
1599  */
1600  bool have_cclassc[NUM_CCLASSES];
1601  bool any_cclassc;
1602  int i;
1603 
1604  memset(have_cclassc, false, sizeof(have_cclassc));
1605 
1606  assert(SEE('['));
1607  NEXT();
1608  while (!SEE(']') && !SEE(EOS))
1609  brackpart(v, lp, rp, have_cclassc);
1610  assert(SEE(']') || ISERR());
1611 
1612  /* close up open subcolors from the positive bracket elements */
1613  okcolors(v->nfa, v->cm);
1614  NOERR();
1615 
1616  /* now handle any complemented elements */
1617  any_cclassc = false;
1618  for (i = 0; i < NUM_CCLASSES; i++)
1619  {
1620  if (have_cclassc[i])
1621  {
1622  charclasscomplement(v, (enum char_classes) i, lp, rp);
1623  NOERR();
1624  any_cclassc = true;
1625  }
1626  }
1627 
1628  /*
1629  * If we had any complemented elements, see if we can optimize the bracket
1630  * into a rainbow. Since a complemented element is the only way a WHITE
1631  * arc could get into the result, there's no point in checking otherwise.
1632  */
1633  if (any_cclassc)
1634  optimizebracket(v, lp, rp);
1635 }
static void okcolors(struct nfa *, struct colormap *)
static void brackpart(struct vars *, struct state *, struct state *, bool *)
Definition: regcomp.c:1680
#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:1435
static void optimizebracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1854
#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 1680 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().

1684 {
1685  chr startc;
1686  chr endc;
1687  struct cvec *cv;
1688  enum char_classes cls;
1689  const chr *startp;
1690  const chr *endp;
1691 
1692  /* parse something, get rid of special cases, take shortcuts */
1693  switch (v->nexttype)
1694  {
1695  case RANGE: /* a-b-c or other botch */
1696  ERR(REG_ERANGE);
1697  return;
1698  break;
1699  case PLAIN:
1700  startc = v->nextvalue;
1701  NEXT();
1702  /* shortcut for ordinary chr (not range) */
1703  if (!SEE(RANGE))
1704  {
1705  onechr(v, startc, lp, rp);
1706  return;
1707  }
1708  NOERR();
1709  break;
1710  case COLLEL:
1711  startp = v->now;
1712  endp = scanplain(v);
1713  INSIST(startp < endp, REG_ECOLLATE);
1714  NOERR();
1715  startc = element(v, startp, endp);
1716  NOERR();
1717  break;
1718  case ECLASS:
1719  startp = v->now;
1720  endp = scanplain(v);
1721  INSIST(startp < endp, REG_ECOLLATE);
1722  NOERR();
1723  startc = element(v, startp, endp);
1724  NOERR();
1725  cv = eclass(v, startc, (v->cflags & REG_ICASE));
1726  NOERR();
1727  subcolorcvec(v, cv, lp, rp);
1728  return;
1729  break;
1730  case CCLASS:
1731  startp = v->now;
1732  endp = scanplain(v);
1733  INSIST(startp < endp, REG_ECTYPE);
1734  NOERR();
1735  cls = lookupcclass(v, startp, endp);
1736  NOERR();
1737  charclass(v, cls, lp, rp);
1738  return;
1739  break;
1740  case CCLASSS:
1741  charclass(v, (enum char_classes) v->nextvalue, lp, rp);
1742  NEXT();
1743  return;
1744  break;
1745  case CCLASSC:
1746  /* we cannot call charclasscomplement() immediately */
1747  have_cclassc[v->nextvalue] = true;
1748  NEXT();
1749  return;
1750  break;
1751  default:
1752  ERR(REG_ASSERT);
1753  return;
1754  break;
1755  }
1756 
1757  if (SEE(RANGE))
1758  {
1759  NEXT();
1760  switch (v->nexttype)
1761  {
1762  case PLAIN:
1763  case RANGE:
1764  endc = v->nextvalue;
1765  NEXT();
1766  NOERR();
1767  break;
1768  case COLLEL:
1769  startp = v->now;
1770  endp = scanplain(v);
1771  INSIST(startp < endp, REG_ECOLLATE);
1772  NOERR();
1773  endc = element(v, startp, endp);
1774  NOERR();
1775  break;
1776  default:
1777  ERR(REG_ERANGE);
1778  return;
1779  break;
1780  }
1781  }
1782  else
1783  endc = startc;
1784 
1785  /*
1786  * Ranges are unportable. Actually, standard C does guarantee that digits
1787  * are contiguous, but making that an exception is just too complicated.
1788  */
1789  if (startc != endc)
1790  NOTE(REG_UUNPORT);
1791  cv = range(v, startc, endc, (v->cflags & REG_ICASE));
1792  NOERR();
1793  subcolorcvec(v, cv, lp, rp);
1794 }
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:1411
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:1828
#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:1803
#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 1646 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().

1649 {
1650  struct state *left = newstate(v->nfa);
1651  struct state *right = newstate(v->nfa);
1652 
1653  NOERR();
1654  bracket(v, left, right);
1655 
1656  /* in NLSTOP mode, ensure newline is not part of the result set */
1657  if (v->cflags & REG_NLSTOP)
1658  newarc(v->nfa, PLAIN, v->nlcolor, left, right);
1659  NOERR();
1660 
1661  assert(lp->nouts == 0); /* all outarcs will be ours */
1662 
1663  /*
1664  * Easy part of complementing, and all there is to do since the MCCE code
1665  * was removed. Note that the result of colorcomplement() cannot be a
1666  * rainbow, since we don't allow empty brackets; so there's no point in
1667  * calling optimizebracket() again.
1668  */
1669  colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
1670  NOERR();
1671  dropstate(v->nfa, left);
1672  assert(right->nins == 0);
1673  freestate(v->nfa, right);
1674 }
#define REG_NLSTOP
Definition: regex.h:111
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1590
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 1411 of file regcomp.c.

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

Referenced by brackpart(), and parseqatom().

1415 {
1416  struct cvec *cv;
1417 
1418  /* obtain possibly-cached cvec for char class */
1419  NOTE(REG_ULOCALE);
1420  cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
1421  NOERR();
1422 
1423  /* build the arcs; this may cause color splitting */
1424  subcolorcvec(v, cv, lp, rp);
1425 }
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 1435 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().

1439 {
1440  struct state *cstate;
1441  struct cvec *cv;
1442 
1443  /* make dummy state to hang temporary arcs on */
1444  cstate = newstate(v->nfa);
1445  NOERR();
1446 
1447  /* obtain possibly-cached cvec for char class */
1448  NOTE(REG_ULOCALE);
1449  cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
1450  NOERR();
1451 
1452  /* build arcs for char class; this may cause color splitting */
1453  subcolorcvec(v, cv, cstate, cstate);
1454  NOERR();
1455 
1456  /* clean up any subcolors in the arc set */
1457  okcolors(v->nfa, v->cm);
1458  NOERR();
1459 
1460  /* now build output arcs for the complement of the char class */
1461  colorcomplement(v->nfa, v->cm, PLAIN, cstate, lp, rp);
1462  NOERR();
1463 
1464  /* clean up dummy state */
1465  dropstate(v->nfa, cstate);
1466 }
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 2192 of file regcomp.c.

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

Referenced by freev(), and pg_regcomp().

2193 {
2194  struct subre *t;
2195  struct subre *next;
2196 
2197  for (t = v->treechain; t != NULL; t = next)
2198  {
2199  next = t->chain;
2200  if (!(t->flags & INUSE))
2201  FREE(t);
2202  }
2203  v->treechain = NULL;
2204  v->treefree = NULL; /* just on general principles */
2205 }
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:500
static int next(struct vars *)
char flags
Definition: regguts.h:474
#define INUSE
Definition: regguts.h:480
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 2309 of file regcomp.c.

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

Referenced by freev(), and rfree().

2311 {
2312  struct subre *sub;
2313  int i;
2314 
2315  assert(n > 0);
2316  for (sub = subs + 1, i = n - 1; i > 0; sub++, i--) /* no 0th */
2317  if (!NULLCNFA(sub->cnfa))
2318  freecnfa(&sub->cnfa);
2319  FREE(subs);
2320 }
#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:499
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 2104 of file regcomp.c.

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

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

2106 {
2107  if (sr == NULL)
2108  return;
2109 
2110  if (!NULLCNFA(sr->cnfa))
2111  freecnfa(&sr->cnfa);
2112  sr->flags = 0;
2113 
2114  if (v != NULL && v->treechain != NULL)
2115  {
2116  /* we're still parsing, maybe we can reuse the subre */
2117  sr->child = v->treefree;
2118  v->treefree = sr;
2119  }
2120  else
2121  FREE(sr);
2122 }
struct subre * treefree
Definition: regcomp.c:258
struct subre * child
Definition: regguts.h:495
struct subre * treechain
Definition: regcomp.c:257
#define NULLCNFA(cnfa)
Definition: regguts.h:431
#define FREE(ptr)
Definition: cryptohash.c:37
char flags
Definition: regguts.h:474
struct cnfa cnfa
Definition: regguts.h:499
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 2069 of file regcomp.c.

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

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

2071 {
2072  if (sr == NULL)
2073  return;
2074 
2075  if (sr->child != NULL)
2076  freesubreandsiblings(v, sr->child);
2077 
2078  freesrnode(v, sr);
2079 }
struct subre * child
Definition: regguts.h:495
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2104
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2088

◆ freesubreandsiblings()

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

Definition at line 2088 of file regcomp.c.

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

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

2090 {
2091  while (sr != NULL)
2092  {
2093  struct subre *next = sr->sibling;
2094 
2095  freesubre(v, sr);
2096  sr = next;
2097  }
2098 }
struct subre * sibling
Definition: regguts.h:496
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2069
static int next(struct vars *)
Definition: regguts.h:471

◆ freev()

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

Definition at line 547 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().

549 {
550  if (v->re != NULL)
551  rfree(v->re);
552  if (v->subs != v->sub10)
553  FREE(v->subs);
554  if (v->nfa != NULL)
555  freenfa(v->nfa);
556  if (v->tree != NULL)
557  freesubre(v, v->tree);
558  if (v->treechain != NULL)
559  cleanst(v);
560  if (v->cv != NULL)
561  freecvec(v->cv);
562  if (v->cv2 != NULL)
563  freecvec(v->cv2);
564  if (v->lacons != NULL)
565  freelacons(v->lacons, v->nlacons);
566  ERR(err); /* nop if err==0 */
567 
568  return v->err;
569 }
static void freelacons(struct subre *, int)
Definition: regcomp.c:2309
static void rfree(regex_t *)
Definition: regcomp.c:2326
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:2069
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:2192
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 576 of file regcomp.c.

References assert, nfa::bos, vars::cm, arc::co, COLORLESS, copyouts(), cparc(), freearc(), arc::from, arc::inchain, state::ins, 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().

578 {
579  struct arc *a;
580  struct arc *b;
581  struct state *pre = nfa->pre;
582  struct state *s;
583  struct state *s2;
584  struct state *slist;
585 
586  /* no loops are needed if it's anchored */
587  for (a = pre->outs; a != NULL; a = a->outchain)
588  {
589  assert(a->type == PLAIN);
590  if (a->co != nfa->bos[0] && a->co != nfa->bos[1])
591  break;
592  }
593  if (a != NULL)
594  {
595  /* add implicit .* in front */
596  rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
597 
598  /* and ^* and \A* too -- not always necessary, but harmless */
599  newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
600  newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
601  }
602 
603  /*
604  * Now here's the subtle part. Because many REs have no lookback
605  * constraints, often knowing when you were in the pre state tells you
606  * little; it's the next state(s) that are informative. But some of them
607  * may have other inarcs, i.e. it may be possible to make actual progress
608  * and then return to one of them. We must de-optimize such cases,
609  * splitting each such state into progress and no-progress states.
610  */
611 
612  /* first, make a list of the states reachable from pre and elsewhere */
613  slist = NULL;
614  for (a = pre->outs; a != NULL; a = a->outchain)
615  {
616  s = a->to;
617  for (b = s->ins; b != NULL; b = b->inchain)
618  {
619  if (b->from != pre)
620  break;
621  }
622 
623  /*
624  * We want to mark states as being in the list already by having non
625  * NULL tmp fields, but we can't just store the old slist value in tmp
626  * because that doesn't work for the first such state. Instead, the
627  * first list entry gets its own address in tmp.
628  */
629  if (b != NULL && s->tmp == NULL)
630  {
631  s->tmp = (slist != NULL) ? slist : s;
632  slist = s;
633  }
634  }
635 
636  /* do the splits */
637  for (s = slist; s != NULL; s = s2)
638  {
639  s2 = newstate(nfa);
640  NOERR();
641  copyouts(nfa, s, s2);
642  NOERR();
643  for (a = s->ins; a != NULL; a = b)
644  {
645  b = a->inchain;
646  if (a->from != pre)
647  {
648  cparc(nfa, a, a->from, s2);
649  freearc(nfa, a);
650  }
651  }
652  s2 = (s->tmp != s) ? s->tmp : NULL;
653  s->tmp = NULL; /* clean up while we're at it */
654  }
655 }
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
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 *)
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
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 2177 of file regcomp.c.

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

Referenced by pg_regcomp().

2178 {
2179  struct subre *t2;
2180 
2181  assert(t != NULL);
2182 
2183  t->flags |= INUSE;
2184  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2185  markst(t2);
2186 }
struct subre * child
Definition: regguts.h:495
struct subre * sibling
Definition: regguts.h:496
static void markst(struct subre *)
Definition: regcomp.c:2177
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
#define INUSE
Definition: regguts.h:480
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 510 of file regcomp.c.

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

Referenced by parseqatom().

512 {
513  struct subre **p;
514  size_t n;
515 
516  assert(wanted > 0 && (size_t) wanted >= v->nsubs);
517  n = (size_t) wanted * 3 / 2 + 1;
518 
519  if (v->subs == v->sub10)
520  {
521  p = (struct subre **) MALLOC(n * sizeof(struct subre *));
522  if (p != NULL)
523  memcpy(VS(p), VS(v->subs),
524  v->nsubs * sizeof(struct subre *));
525  }
526  else
527  p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
528  if (p == NULL)
529  {
530  ERR(REG_ESPACE);
531  return;
532  }
533  v->subs = p;
534  for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
535  *p = NULL;
536  assert(v->nsubs == n);
537  assert((size_t) wanted < v->nsubs);
538 }
#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 2270 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().

2274 {
2275  int n;
2276  struct subre *newlacons;
2277  struct subre *sub;
2278 
2279  if (v->nlacons == 0)
2280  {
2281  n = 1; /* skip 0th */
2282  newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
2283  }
2284  else
2285  {
2286  n = v->nlacons;
2287  newlacons = (struct subre *) REALLOC(v->lacons,
2288  (n + 1) * sizeof(struct subre));
2289  }
2290  if (newlacons == NULL)
2291  {
2292  ERR(REG_ESPACE);
2293  return 0;
2294  }
2295  v->lacons = newlacons;
2296  v->nlacons = n + 1;
2297  sub = &v->lacons[n];
2298  sub->begin = begin;
2299  sub->end = end;
2300  sub->latype = latype;
2301  ZAPCNFA(sub->cnfa);
2302  return n;
2303 }
#define REG_ESPACE
Definition: regex.h:151
struct state * begin
Definition: regguts.h:497
char latype
Definition: regguts.h:489
#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:498
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:499
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 2231 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().

2235 {
2236  struct nfa *nfa;
2237  long ret = 0;
2238 
2239  assert(t->begin != NULL);
2240 
2241 #ifdef REG_DEBUG
2242  if (f != NULL)
2243  {
2244  char idbuf[50];
2245 
2246  fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
2247  stid(t, idbuf, sizeof(idbuf)));
2248  }
2249 #endif
2250  nfa = newnfa(v, v->cm, v->nfa);
2251  NOERRZ();
2252  dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
2253  if (!ISERR())
2254  specialcolors(nfa);
2255  if (!ISERR())
2256  ret = optimize(nfa, f);
2257  if (converttosearch && !ISERR())
2258  makesearch(v, nfa);
2259  if (!ISERR())
2260  compact(nfa, &t->cnfa);
2261 
2262  freenfa(nfa);
2263  return ret;
2264 }
static long optimize(struct nfa *, FILE *)
struct state * final
Definition: regguts.h:347
static void specialcolors(struct nfa *)
struct state * begin
Definition: regguts.h:497
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:576
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:498
#define NOERRZ()
Definition: regcomp.c:279
struct cnfa cnfa
Definition: regguts.h:499
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 2211 of file regcomp.c.

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

Referenced by pg_regcomp().

2214 {
2215  struct subre *t2;
2216 
2217  assert(t != NULL && t->begin != NULL);
2218 
2219  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2220  (DISCARD) nfatree(v, t2, f);
2221 
2222  return nfanode(v, t, 0, f);
2223 }
struct subre * child
Definition: regguts.h:495
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:2211
struct state * begin
Definition: regguts.h:497
struct subre * sibling
Definition: regguts.h:496
#define assert(TEST)
Definition: imath.c:73
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:2231
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 1375 of file regcomp.c.

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

Referenced by parseqatom().

1379 {
1380  int anchor = (dir == AHEAD) ? '$' : '^';
1381 
1382  assert(dir == AHEAD || dir == BEHIND);
1383  newarc(v->nfa, anchor, 1, lp, rp);
1384  newarc(v->nfa, anchor, 0, lp, rp);
1385  colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
1386  /* (no need for special attention to \n) */
1387 }
#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 2144 of file regcomp.c.

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

Referenced by pg_regcomp().

2146 {
2147  int i;
2148  struct subre *t2;
2149 
2150  assert(t != NULL);
2151 
2152  i = start;
2153  t->id = i++;
2154  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2155  i = numst(t2, i);
2156  return i;
2157 }
struct subre * child
Definition: regguts.h:495
static int numst(struct subre *, int)
Definition: regcomp.c:2144
int id
Definition: regguts.h:490
struct subre * sibling
Definition: regguts.h:496
#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 1828 of file regcomp.c.

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

Referenced by brackpart(), and parseqatom().

1832 {
1833  if (!(v->cflags & REG_ICASE))
1834  {
1835  color lastsubcolor = COLORLESS;
1836 
1837  subcoloronechr(v, c, lp, rp, &lastsubcolor);
1838  return;
1839  }
1840 
1841  /* rats, need general case anyway... */
1842  subcolorcvec(v, allcases(v, c), lp, rp);
1843 }
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 1854 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().

1857 {
1858  struct colordesc *cd;
1859  struct colordesc *end = CDEND(v->cm);
1860  struct arc *a;
1861  bool israinbow;
1862 
1863  /*
1864  * Scan lp's out-arcs and transiently mark the mentioned colors. We
1865  * expect that all of lp's out-arcs are plain, non-RAINBOW arcs to rp.
1866  * (Note: there shouldn't be any pseudocolors yet, but check anyway.)
1867  */
1868  for (a = lp->outs; a != NULL; a = a->outchain)
1869  {
1870  assert(a->type == PLAIN);
1871  assert(a->co >= 0); /* i.e. not RAINBOW */
1872  assert(a->to == rp);
1873  cd = &v->cm->cd[a->co];
1874  assert(!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO));
1875  cd->flags |= COLMARK;
1876  }
1877 
1878  /* Scan colors, clear transient marks, check for unmarked live colors */
1879  israinbow = true;
1880  for (cd = v->cm->cd; cd < end; cd++)
1881  {
1882  if (cd->flags & COLMARK)
1883  cd->flags &= ~COLMARK;
1884  else if (!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO))
1885  israinbow = false;
1886  }
1887 
1888  /* Can't do anything if not all colors have arcs */
1889  if (!israinbow)
1890  return;
1891 
1892  /* OK, drop existing arcs and replace with a rainbow */
1893  while ((a = lp->outs) != NULL)
1894  freearc(v->nfa, a);
1895  newarc(v->nfa, PLAIN, RAINBOW, lp, rp);
1896 }
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 *)

◆ optst()

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

Definition at line 2128 of file regcomp.c.

Referenced by pg_regcomp().

2130 {
2131  /*
2132  * DGP (2007-11-13): I assume it was the programmer's intent to eventually
2133  * come back and add code to optimize subRE trees, but the routine coded
2134  * just spends effort traversing the tree and doing nothing. We can do
2135  * nothing with less effort.
2136  */
2137  return;
2138 }

◆ parse()

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

Definition at line 665 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_distinct_paths(), create_grouping_paths(), create_limit_plan(), 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().

670 {
671  struct subre *branches; /* top level */
672  struct subre *lastbranch; /* latest branch */
673 
674  assert(stopper == ')' || stopper == EOS);
675 
676  branches = subre(v, '|', LONGER, init, final);
677  NOERRN();
678  lastbranch = NULL;
679  do
680  { /* a branch */
681  struct subre *branch;
682  struct state *left; /* scaffolding for branch */
683  struct state *right;
684 
685  left = newstate(v->nfa);
686  right = newstate(v->nfa);
687  NOERRN();
688  EMPTYARC(init, left);
689  EMPTYARC(right, final);
690  NOERRN();
691  branch = parsebranch(v, stopper, type, left, right, 0);
692  NOERRN();
693  if (lastbranch)
694  lastbranch->sibling = branch;
695  else
696  branches->child = branch;
697  branches->flags |= UP(branches->flags | branch->flags);
698  lastbranch = branch;
699  } while (EAT('|'));
700  assert(SEE(stopper) || SEE(EOS));
701 
702  if (!SEE(stopper))
703  {
704  assert(stopper == ')' && SEE(EOS));
705  ERR(REG_EPAREN);
706  }
707 
708  /* optimize out simple cases */
709  if (lastbranch == branches->child)
710  { /* only one branch */
711  assert(lastbranch->sibling == NULL);
712  freesrnode(v, branches);
713  branches = lastbranch;
714  }
715  else if (!MESSY(branches->flags))
716  { /* no interesting innards */
717  freesubreandsiblings(v, branches->child);
718  branches->child = NULL;
719  branches->op = '=';
720  }
721 
722  return branches;
723 }
#define EAT(t)
Definition: regcomp.c:271
struct subre * child
Definition: regguts.h:495
#define REG_EPAREN
Definition: regex.h:147
#define NOERRN()
Definition: regcomp.c:278
#define MESSY(f)
Definition: regguts.h:485
char op
Definition: regguts.h:473
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:2012
#define ERR(e)
Definition: regcomp.c:276
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2104
struct subre * sibling
Definition: regguts.h:496
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
static void freesubreandsiblings(struct vars *, struct subre *)
Definition: regcomp.c:2088
#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:733
#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:484

◆ parsebranch()

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

Definition at line 733 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().

739 {
740  struct state *lp; /* left end of current construct */
741  int seencontent; /* is there anything in this branch yet? */
742  struct subre *t;
743 
744  lp = left;
745  seencontent = 0;
746  t = subre(v, '=', 0, left, right); /* op '=' is tentative */
747  NOERRN();
748  while (!SEE('|') && !SEE(stopper) && !SEE(EOS))
749  {
750  if (seencontent)
751  { /* implicit concat operator */
752  lp = newstate(v->nfa);
753  NOERRN();
754  moveins(v->nfa, right, lp);
755  }
756  seencontent = 1;
757 
758  /* NB, recursion in parseqatom() may swallow rest of branch */
759  parseqatom(v, stopper, type, lp, right, t);
760  NOERRN();
761  }
762 
763  if (!seencontent)
764  { /* empty branch */
765  if (!partial)
766  NOTE(REG_UUNSPEC);
767  assert(lp == left);
768  EMPTYARC(left, right);
769  }
770 
771  return t;
772 }
#define NOERRN()
Definition: regcomp.c:278
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:2012
static void parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *)
Definition: regcomp.c:782
#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
Definition: regguts.h:471
#define NOTE(b)
Definition: regcomp.c:281

◆ parseqatom()

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

Definition at line 782 of file regcomp.c.

References AHEAD, ARCV, assert, subre::backno, BACKR, BACKREF, subre::begin, BEHIND, bracket(), 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(), subre::id, 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, NOERR, 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().

788 {
789  struct state *s; /* temporaries for new states */
790  struct state *s2;
791 
792 #define ARCV(t, val) newarc(v->nfa, t, val, lp, rp)
793  int m,
794  n;
795  struct subre *atom; /* atom's subtree */
796  struct subre *t;
797  int cap; /* capturing parens? */
798  int latype; /* lookaround constraint type */
799  int subno; /* capturing-parens or backref number */
800  int atomtype;
801  int qprefer; /* quantifier short/long preference */
802  int f;
803  struct subre **atomp; /* where the pointer to atom is */
804 
805  /* initial bookkeeping */
806  atom = NULL;
807  assert(lp->nouts == 0); /* must string new code */
808  assert(rp->nins == 0); /* between lp and rp */
809  subno = 0; /* just to shut lint up */
810 
811  /* an atom or constraint... */
812  atomtype = v->nexttype;
813  switch (atomtype)
814  {
815  /* first, constraints, which end by returning */
816  case '^':
817  ARCV('^', 1);
818  if (v->cflags & REG_NLANCH)
819  ARCV(BEHIND, v->nlcolor);
820  NEXT();
821  return;
822  break;
823  case '$':
824  ARCV('$', 1);
825  if (v->cflags & REG_NLANCH)
826  ARCV(AHEAD, v->nlcolor);
827  NEXT();
828  return;
829  break;
830  case SBEGIN:
831  ARCV('^', 1); /* BOL */
832  ARCV('^', 0); /* or BOS */
833  NEXT();
834  return;
835  break;
836  case SEND:
837  ARCV('$', 1); /* EOL */
838  ARCV('$', 0); /* or EOS */
839  NEXT();
840  return;
841  break;
842  case '<':
843  wordchrs(v);
844  s = newstate(v->nfa);
845  NOERR();
846  nonword(v, BEHIND, lp, s);
847  word(v, AHEAD, s, rp);
848  NEXT();
849  return;
850  break;
851  case '>':
852  wordchrs(v);
853  s = newstate(v->nfa);
854  NOERR();
855  word(v, BEHIND, lp, s);
856  nonword(v, AHEAD, s, rp);
857  NEXT();
858  return;
859  break;
860  case WBDRY:
861  wordchrs(v);
862  s = newstate(v->nfa);
863  NOERR();
864  nonword(v, BEHIND, lp, s);
865  word(v, AHEAD, s, rp);
866  s = newstate(v->nfa);
867  NOERR();
868  word(v, BEHIND, lp, s);
869  nonword(v, AHEAD, s, rp);
870  NEXT();
871  return;
872  break;
873  case NWBDRY:
874  wordchrs(v);
875  s = newstate(v->nfa);
876  NOERR();
877  word(v, BEHIND, lp, s);
878  word(v, AHEAD, s, rp);
879  s = newstate(v->nfa);
880  NOERR();
881  nonword(v, BEHIND, lp, s);
882  nonword(v, AHEAD, s, rp);
883  NEXT();
884  return;
885  break;
886  case LACON: /* lookaround constraint */
887  latype = v->nextvalue;
888  NEXT();
889  s = newstate(v->nfa);
890  s2 = newstate(v->nfa);
891  NOERR();
892  t = parse(v, ')', LACON, s, s2);
893  freesubre(v, t); /* internal structure irrelevant */
894  NOERR();
895  assert(SEE(')'));
896  NEXT();
897  processlacon(v, s, s2, latype, lp, rp);
898  return;
899  break;
900  /* then errors, to get them out of the way */
901  case '*':
902  case '+':
903  case '?':
904  case '{':
905  ERR(REG_BADRPT);
906  return;
907  break;
908  default:
909  ERR(REG_ASSERT);
910  return;
911  break;
912  /* then plain characters, and minor variants on that theme */
913  case ')': /* unbalanced paren */
914  if ((v->cflags & REG_ADVANCED) != REG_EXTENDED)
915  {
916  ERR(REG_EPAREN);
917  return;
918  }
919  /* legal in EREs due to specification botch */
920  NOTE(REG_UPBOTCH);
921  /* fall through into case PLAIN */
922  /* FALLTHROUGH */
923  case PLAIN:
924  onechr(v, v->nextvalue, lp, rp);
925  okcolors(v->nfa, v->cm);
926  NOERR();
927  NEXT();
928  break;
929  case '[':
930  if (v->nextvalue == 1)
931  bracket(v, lp, rp);
932  else
933  cbracket(v, lp, rp);
934  assert(SEE(']') || ISERR());
935  NEXT();
936  break;
937  case CCLASSS:
938  charclass(v, (enum char_classes) v->nextvalue, lp, rp);
939  okcolors(v->nfa, v->cm);
940  NEXT();
941  break;
942  case CCLASSC:
943  charclasscomplement(v, (enum char_classes) v->nextvalue, lp, rp);
944  /* charclasscomplement() did okcolors() internally */
945  NEXT();
946  break;
947  case '.':
948  rainbow(v->nfa, v->cm, PLAIN,
949  (v->cflags & REG_NLSTOP) ? v->nlcolor : COLORLESS,
950  lp, rp);
951  NEXT();
952  break;
953  /* and finally the ugly stuff */
954  case '(': /* value flags as capturing or non */
955  cap = (type == LACON) ? 0 : v->nextvalue;
956  if (cap)
957  {
958  v->nsubexp++;
959  subno = v->nsubexp;
960  if ((size_t) subno >= v->nsubs)
961  moresubs(v, subno);
962  }
963  else
964  atomtype = PLAIN; /* something that's not '(' */
965  NEXT();
966 
967  /*
968  * Make separate endpoints to ensure we keep this sub-NFA cleanly
969  * separate from what surrounds it. We need to be sure that when
970  * we duplicate the sub-NFA for a backref, we get the right states
971  * and no others.
972  */
973  s = newstate(v->nfa);
974  s2 = newstate(v->nfa);
975  NOERR();
976  EMPTYARC(lp, s);
977  EMPTYARC(s2, rp);
978  NOERR();
979  atom = parse(v, ')', type, s, s2);
980  assert(SEE(')') || ISERR());
981  NEXT();
982  NOERR();
983  if (cap)
984  {
985  assert(v->subs[subno] == NULL);
986  v->subs[subno] = atom;
987  if (atom->capno == 0)
988  {
989  /* normal case: just mark the atom as capturing */
990  atom->flags |= CAP;
991  atom->capno = subno;
992  }
993  else
994  {
995  /* generate no-op wrapper node to handle "((x))" */
996  t = subre(v, '(', atom->flags | CAP, lp, rp);
997  NOERR();
998  t->capno = subno;
999  t->child = atom;
1000  atom = t;
1001  }
1002  }
1003  /* postpone everything else pending possible {0} */
1004  break;
1005  case BACKREF: /* the Feature From The Black Lagoon */
1007  INSIST(v->nextvalue < v->nsubs, REG_ESUBREG);
1008  INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG);
1009  NOERR();
1010  assert(v->nextvalue > 0);
1011  atom = subre(v, 'b', BACKR, lp, rp);
1012  NOERR();
1013  subno = v->nextvalue;
1014  atom->backno = subno;
1015  EMPTYARC(lp, rp); /* temporarily, so there's something */
1016  NEXT();
1017  break;
1018  }
1019 
1020  /* ...and an atom may be followed by a quantifier */
1021  switch (v->nexttype)
1022  {
1023  case '*':
1024  m = 0;
1025  n = DUPINF;
1026  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1027  NEXT();
1028  break;
1029  case '+':
1030  m = 1;
1031  n = DUPINF;
1032  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1033  NEXT();
1034  break;
1035  case '?':
1036  m = 0;
1037  n = 1;
1038  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1039  NEXT();
1040  break;
1041  case '{':
1042  NEXT();
1043  m = scannum(v);
1044  if (EAT(','))
1045  {
1046  if (SEE(DIGIT))
1047  n = scannum(v);
1048  else
1049  n = DUPINF;
1050  if (m > n)
1051  {
1052  ERR(REG_BADBR);
1053  return;
1054  }
1055  /* {m,n} exercises preference, even if it's {m,m} */
1056  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1057  }
1058  else
1059  {
1060  n = m;
1061  /* {m} passes operand's preference through */
1062  qprefer = 0;
1063  }
1064  if (!SEE('}'))
1065  { /* catches errors too */
1066  ERR(REG_BADBR);
1067  return;
1068  }
1069  NEXT();
1070  break;
1071  default: /* no quantifier */
1072  m = n = 1;
1073  qprefer = 0;
1074  break;
1075  }
1076 
1077  /* annoying special case: {0} or {0,0} cancels everything */
1078  if (m == 0 && n == 0)
1079  {
1080  if (atom != NULL)
1081  freesubre(v, atom);
1082  if (atomtype == '(')
1083  v->subs[subno] = NULL;
1084  delsub(v->nfa, lp, rp);
1085  EMPTYARC(lp, rp);
1086  return;
1087  }
1088 
1089  /* if not a messy case, avoid hard part */
1090  assert(!MESSY(top->flags));
1091  f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
1092  if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f)))
1093  {
1094  if (!(m == 1 && n == 1))
1095  repeat(v, lp, rp, m, n);
1096  if (atom != NULL)
1097  freesubre(v, atom);
1098  top->flags = f;
1099  return;
1100  }
1101 
1102  /*
1103  * hard part: something messy
1104  *
1105  * That is, capturing parens, back reference, short/long clash, or an atom
1106  * with substructure containing one of those.
1107  */
1108 
1109  /* now we'll need a subre for the contents even if they're boring */
1110  if (atom == NULL)
1111  {
1112  atom = subre(v, '=', 0, lp, rp);
1113  NOERR();
1114  }
1115 
1116  /*----------
1117  * Prepare a general-purpose state skeleton.
1118  *
1119  * In the no-backrefs case, we want this:
1120  *
1121  * [lp] ---> [s] ---prefix---> [begin] ---atom---> [end] ---rest---> [rp]
1122  *
1123  * where prefix is some repetitions of atom. In the general case we need
1124  *
1125  * [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp]
1126  *
1127  * where the iterator wraps around [begin] ---atom---> [end]
1128  *
1129  * We make the s state here for both cases; s2 is made below if needed
1130  *----------
1131  */
1132  s = newstate(v->nfa); /* first, new endpoints for the atom */
1133  s2 = newstate(v->nfa);
1134  NOERR();
1135  moveouts(v->nfa, lp, s);
1136  moveins(v->nfa, rp, s2);
1137  NOERR();
1138  atom->begin = s;
1139  atom->end = s2;
1140  s = newstate(v->nfa); /* set up starting state */
1141  NOERR();
1142  EMPTYARC(lp, s);
1143  NOERR();
1144 
1145  /* break remaining subRE into x{...} and what follows */
1146  t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
1147  NOERR();
1148  t->child = atom;
1149  atomp = &t->child;
1150 
1151  /*
1152  * Here we should recurse to fill t->child->sibling ... but we must
1153  * postpone that to the end. One reason is that t->child may be replaced
1154  * below, and we don't want to worry about its sibling link.
1155  */
1156 
1157  /*
1158  * Convert top node to a concatenation of the prefix (top->child, covering
1159  * whatever we parsed previously) and remaining (t). Note that the prefix
1160  * could be empty, in which case this concatenation node is unnecessary.
1161  * To keep things simple, we operate in a general way for now, and get rid
1162  * of unnecessary subres below.
1163  */
1164  assert(top->op == '=' && top->child == NULL);
1165  top->child = subre(v, '=', top->flags, top->begin, lp);
1166  NOERR();
1167  top->op = '.';
1168  top->child->sibling = t;
1169  /* top->flags will get updated later */
1170 
1171  /* if it's a backref, now is the time to replicate the subNFA */
1172  if (atomtype == BACKREF)
1173  {
1174  assert(atom->begin->nouts == 1); /* just the EMPTY */
1175  delsub(v->nfa, atom->begin, atom->end);
1176  assert(v->subs[subno] != NULL);
1177 
1178  /*
1179  * And here's why the recursion got postponed: it must wait until the
1180  * skeleton is filled in, because it may hit a backref that wants to
1181  * copy the filled-in skeleton.
1182  */
1183  dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
1184  atom->begin, atom->end);
1185  NOERR();
1186 
1187  /* The backref node's NFA should not enforce any constraints */
1188  removeconstraints(v->nfa, atom->begin, atom->end);
1189  NOERR();
1190  }
1191 
1192  /*
1193  * It's quantifier time. If the atom is just a backref, we'll let it deal
1194  * with quantifiers internally.
1195  */
1196  if (atomtype == BACKREF)
1197  {
1198  /* special case: backrefs have internal quantifiers */
1199  EMPTYARC(s, atom->begin); /* empty prefix */
1200  /* just stuff everything into atom */
1201  repeat(v, atom->begin, atom->end, m, n);
1202  atom->min = (short) m;
1203  atom->max = (short) n;
1204  atom->flags |= COMBINE(qprefer, atom->flags);
1205  /* rest of branch can be strung starting from atom->end */
1206  s2 = atom->end;
1207  }
1208  else if (m == 1 && n == 1 &&
1209  (qprefer == 0 ||
1210  (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
1211  qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
1212  {
1213  /* no/vacuous quantifier: done */
1214  EMPTYARC(s, atom->begin); /* empty prefix */
1215  /* rest of branch can be strung starting from atom->end */
1216  s2 = atom->end;
1217  }
1218  else if (!(atom->flags & (CAP | BACKR)))
1219  {
1220  /*
1221  * If there's no captures nor backrefs in the atom being repeated, we
1222  * don't really care where the submatches of the iteration are, so we
1223  * don't need an iteration node. Make a plain DFA node instead.
1224  */
1225  EMPTYARC(s, atom->begin); /* empty prefix */
1226  repeat(v, atom->begin, atom->end, m, n);
1227  f = COMBINE(qprefer, atom->flags);
1228  t = subre(v, '=', f, atom->begin, atom->end);
1229  NOERR();
1230  freesubre(v, atom);
1231  *atomp = t;
1232  /* rest of branch can be strung starting from t->end */
1233  s2 = t->end;
1234  }
1235  else if (m > 0 && !(atom->flags & BACKR))
1236  {
1237  /*
1238  * If there's no backrefs involved, we can turn x{m,n} into
1239  * x{m-1,n-1}x, with capturing parens in only the second x. This is
1240  * valid because we only care about capturing matches from the final
1241  * iteration of the quantifier. It's a win because we can implement
1242  * the backref-free left side as a plain DFA node, since we don't
1243  * really care where its submatches are.
1244  */
1245  dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
1246  assert(m >= 1 && m != DUPINF && n >= 1);
1247  repeat(v, s, atom->begin, m - 1, (n == DUPINF) ? n : n - 1);
1248  f = COMBINE(qprefer, atom->flags);
1249  t = subre(v, '.', f, s, atom->end); /* prefix and atom */
1250  NOERR();
1251  t->child = subre(v, '=', PREF(f), s, atom->begin);
1252  NOERR();
1253  t->child->sibling = atom;
1254  *atomp = t;
1255  /* rest of branch can be strung starting from atom->end */
1256  s2 = atom->end;
1257  }
1258  else
1259  {
1260  /* general case: need an iteration node */
1261  s2 = newstate(v->nfa);
1262  NOERR();
1263  moveouts(v->nfa, atom->end, s2);
1264  NOERR();
1265  dupnfa(v->nfa, atom->begin, atom->end, s, s2);
1266  repeat(v, s, s2, m, n);
1267  f = COMBINE(qprefer, atom->flags);
1268  t = subre(v, '*', f, s, s2);
1269  NOERR();
1270  t->min = (short) m;
1271  t->max = (short) n;
1272  t->child = atom;
1273  *atomp = t;
1274  /* rest of branch is to be strung from iteration's end state */
1275  }
1276 
1277  /* and finally, look after that postponed recursion */
1278  t = top->child->sibling;
1279  if (!(SEE('|') || SEE(stopper) || SEE(EOS)))
1280  {
1281  /* parse all the rest of the branch, and insert in t->child->sibling */
1282  t->child->sibling = parsebranch(v, stopper, type, s2, rp, 1);
1283  NOERR();
1284  assert(SEE('|') || SEE(stopper) || SEE(EOS));
1285 
1286  /* here's the promised update of the flags */
1287  t->flags |= COMBINE(t->flags, t->child->sibling->flags);
1288  top->flags |= COMBINE(top->flags, t->flags);
1289 
1290  /* neither t nor top could be directly marked for capture as yet */
1291  assert(t->capno == 0);
1292  assert(top->capno == 0);
1293 
1294  /*
1295  * At this point both top and t are concatenation (op == '.') subres,
1296  * and we have top->child = prefix of branch, top->child->sibling = t,
1297  * t->child = messy atom (with quantification superstructure if
1298  * needed), t->child->sibling = rest of branch.
1299  *
1300  * If the messy atom was the first thing in the branch, then
1301  * top->child is vacuous and we can get rid of one level of
1302  * concatenation. Since the caller is holding a pointer to the top
1303  * node, we can't remove that node; but we're allowed to change its
1304  * properties.
1305  */
1306  assert(top->child->op == '=');
1307  if (top->child->begin == top->child->end)
1308  {
1309  assert(!MESSY(top->child->flags));
1310  freesubre(v, top->child);
1311  top->child = t->child;
1312  freesrnode(v, t);
1313  }
1314 
1315  /*
1316  * Otherwise, it's possible that t->child is not messy in itself, but
1317  * we considered it messy because its greediness conflicts with what
1318  * preceded it. Then it could be that the combination of t->child and
1319  * the rest of the branch is also not messy, in which case we can get
1320  * rid of the child concatenation by merging t->child and the rest of
1321  * the branch into one plain DFA node.
1322  */
1323  else if (t->child->op == '=' &&
1324  t->child->sibling->op == '=' &&
1325  !MESSY(UP(t->child->flags | t->child->sibling->flags)))
1326  {
1327  t->op = '=';
1328  t->flags = COMBINE(t->child->flags, t->child->sibling->flags);
1329  freesubreandsiblings(v, t->child);
1330  t->child = NULL;
1331  }
1332  }
1333  else
1334  {
1335  /*
1336  * There's nothing left in the branch, so we don't need the second
1337  * concatenation node 't'. Just link s2 straight to rp.
1338  */
1339  EMPTYARC(s2, rp);
1340  top->child->sibling = t->child;
1341  top->flags |= COMBINE(top->flags, top->child->sibling->flags);
1342  freesrnode(v, t);
1343 
1344  /*
1345  * Again, it could be that top->child is vacuous (if the messy atom
1346  * was in fact the only thing in the branch). In that case we need no
1347  * concatenation at all; just replace top with top->child->sibling.
1348  */
1349  assert(top->child->op == '=');
1350  if (top->child->begin == top->child->end)
1351  {
1352  assert(!MESSY(top->child->flags));
1353  t = top->child->sibling;
1354  freesubre(v, top->child);
1355  top->op = t->op;
1356  top->flags = t->flags;
1357  top->latype = t->latype;
1358  top->id = t->id;
1359  top->capno = t->capno;
1360  top->backno = t->backno;
1361  top->min = t->min;
1362  top->max = t->max;
1363  top->child = t->child;
1364  top->begin = t->begin;
1365  top->end = t->end;
1366  freesrnode(v, t);
1367  }
1368  }
1369 }
#define EAT(t)
Definition: regcomp.c:271
static int scannum(struct vars *)
Definition: regcomp.c:1472
#define REG_ESUBREG
Definition: regex.h:145
int nexttype
Definition: regcomp.c:245
#define REG_NLSTOP
Definition: regex.h:111
static void okcolors(struct nfa *, struct colormap *)
struct subre * child
Definition: regguts.h:495
chr nextvalue
Definition: regcomp.c:246
int capno
Definition: regguts.h:491
static void charclass(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1411
#define REG_EPAREN
Definition: regex.h:147
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1590
static void nonword(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1375
#define NEXT()
Definition: regcomp.c:269
#define MESSY(f)
Definition: regguts.h:485
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:2012
short min
Definition: regguts.h:493
static void onechr(struct vars *, chr, struct state *, struct state *)
Definition: regcomp.c:1828
static void wordchrs(struct vars *)
Definition: regcomp.c:1910
struct state * begin
Definition: regguts.h:497
#define COMBINE(f1, f2)
Definition: regguts.h:488
#define ISERR()
Definition: regcomp.c:273
char latype
Definition: regguts.h:489
#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
int id
Definition: regguts.h:490
#define CCLASSS
Definition: regcomp.c:294
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:2104
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:496
#define REG_BADBR
Definition: regex.h:149
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2069
#define REG_BADRPT
Definition: regex.h:152
static void processlacon(struct vars *, struct state *, struct state *, int, struct state *, struct state *)
Definition: regcomp.c:1946
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:2088
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:486
int backno
Definition: regguts.h:492
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:733
#define EOS
Definition: regcomp.c:286
static void cbracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1646
#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:494
#define REG_NLANCH
Definition: regex.h:112
struct state * end
Definition: regguts.h:498
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:1503
static void charclasscomplement(struct vars *, enum char_classes, struct state *, struct state *)
Definition: regcomp.c:1435
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1393
int nouts
Definition: regguts.h:323
#define CAP
Definition: regguts.h:478
#define NOERR()
Definition: regcomp.c:277
#define UP(f)
Definition: regguts.h:484
#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:510
#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:665
#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(), optst(), 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_PROGRESS, REG_QUOTE, REG_USHORTEST, REMAGIC, 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  optst(v, v->tree);
435  v->ntree = numst(v->tree, 1);
436  markst(v->tree);
437  cleanst(v);
438 #ifdef REG_DEBUG
439  if (debug != NULL)
440  {
441  fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
442  dumpst(v->tree, debug, 1);
443  }
444 #endif
445 
446  /* build compacted NFAs for tree and lacons */
447  re->re_info |= nfatree(v, v->tree, debug);
448  CNOERR();
449  assert(v->nlacons == 0 || v->lacons != NULL);
450  for (i = 1; i < v->nlacons; i++)
451  {
452  struct subre *lasub = &v->lacons[i];
453 
454 #ifdef REG_DEBUG
455  if (debug != NULL)
456  fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
457 #endif
458 
459  /* Prepend .* to pattern if it's a lookbehind LACON */
460  nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->latype), debug);
461  }
462  CNOERR();
463  if (v->tree->flags & SHORTER)
465 
466  /* build compacted NFAs for tree, lacons, fast search */
467 #ifdef REG_DEBUG
468  if (debug != NULL)
469  fprintf(debug, "\n\n\n========= SEARCH ==========\n");
470 #endif
471  /* can sacrifice main NFA now, so use it as work area */
472  (DISCARD) optimize(v->nfa, debug);
473  CNOERR();
474  makesearch(v, v->nfa);
475  CNOERR();
476  compact(v->nfa, &g->search);
477  CNOERR();
478 
479  /* looks okay, package it up */
480  re->re_nsub = v->nsubexp;
481  v->re = NULL; /* freev no longer frees re */
482  g->magic = GUTSMAGIC;
483  g->cflags = v->cflags;
484  g->info = re->re_info;
485  g->nsub = re->re_nsub;
486  g->tree = v->tree;
487  v->tree = NULL;
488  g->ntree = v->ntree;
489  g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
490  g->lacons = v->lacons;
491  v->lacons = NULL;
492  g->nlacons = v->nlacons;
493 
494 #ifdef REG_DEBUG
495  if (flags & REG_DUMP)
496  {
497  dump(re, stdout);
498  fflush(stdout);
499  }
500 #endif
501 
502  assert(v->err == 0);
503  return freev(v, 0);
504 }
#define REG_USHORTEST
Definition: regex.h:75
static chr newline(void)
struct subre * treefree
Definition: regcomp.c:258
static void optst(struct vars *, struct subre *)
Definition: regcomp.c:2128
#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:2144
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:538
struct subre * tree
Definition: regguts.h:533
struct state * final
Definition: regguts.h:347
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:2211
struct subre * treechain
Definition: regcomp.c:257
#define REG_ESPACE
Definition: regex.h:151
static int freev(struct vars *, int)
Definition: regcomp.c:547
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:539
#define REMAGIC
Definition: regguts.h:96
static color subcolor(struct colormap *, chr)
Definition: regguts.h:343
char latype
Definition: regguts.h:489
#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:576
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:534
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:528
#define REG_INVARG
Definition: regex.h:154
static void markst(struct subre *)
Definition: regcomp.c:2177
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:536
#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:2231
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:526
int ntree
Definition: regguts.h:535
#define EOS
Definition: regcomp.c:286
#define GUTSMAGIC
Definition: regguts.h:529
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 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:531
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:530
struct state * init
Definition: regguts.h:346
int cflags
Definition: regcomp.c:243
size_t nsub
Definition: regguts.h:532
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:2192
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:665

◆ 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 1946 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().

1952 {
1953  struct state *s1;
1954  int n;
1955 
1956  /*
1957  * Check for lookaround RE consisting of a single plain color arc (or set
1958  * of arcs); this would typically be a simple chr or a bracket expression.
1959  */
1960  s1 = single_color_transition(begin, end);
1961  switch (latype)
1962  {
1963  case LATYPE_AHEAD_POS:
1964  /* If lookahead RE is just colorset C, convert to AHEAD(C) */
1965  if (s1 != NULL)
1966  {
1967  cloneouts(v->nfa, s1, lp, rp, AHEAD);
1968  return;
1969  }
1970  break;
1971  case LATYPE_AHEAD_NEG:
1972  /* If lookahead RE is just colorset C, convert to AHEAD(^C)|$ */
1973  if (s1 != NULL)
1974  {
1975  colorcomplement(v->nfa, v->cm, AHEAD, s1, lp, rp);
1976  newarc(v->nfa, '$', 1, lp, rp);
1977  newarc(v->nfa, '$', 0, lp, rp);
1978  return;
1979  }
1980  break;
1981  case LATYPE_BEHIND_POS:
1982  /* If lookbehind RE is just colorset C, convert to BEHIND(C) */
1983  if (s1 != NULL)
1984  {
1985  cloneouts(v->nfa, s1, lp, rp, BEHIND);
1986  return;
1987  }
1988  break;
1989  case LATYPE_BEHIND_NEG:
1990  /* If lookbehind RE is just colorset C, convert to BEHIND(^C)|^ */
1991  if (s1 != NULL)
1992  {
1993  colorcomplement(v->nfa, v->cm, BEHIND, s1, lp, rp);
1994  newarc(v->nfa, '^', 1, lp, rp);
1995  newarc(v->nfa, '^', 0, lp, rp);
1996  return;
1997  }
1998  break;
1999  default:
2000  assert(NOTREACHED);
2001  }
2002 
2003  /* General case: we need a LACON subre and arc */
2004  n = newlacon(v, begin, end, latype);
2005  newarc(v->nfa, LACON, n, lp, rp);
2006 }
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:2270
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 2362 of file regcomp.c.

References InterruptPending, ProcDiePending, and QueryCancelPending.

2363 {
2365 }
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

◆ 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 1503 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().

1508 {
1509 #define SOME 2
1510 #define INF 3
1511 #define PAIR(x, y) ((x)*4 + (y))
1512 #define REDUCE(x) ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
1513  const int rm = REDUCE(m);
1514  const int rn = REDUCE(n);
1515  struct state *s;
1516  struct state *s2;
1517 
1518  switch (PAIR(rm, rn))
1519  {
1520  case PAIR(0, 0): /* empty string */
1521  delsub(v->nfa, lp, rp);
1522  EMPTYARC(lp, rp);
1523  break;
1524  case PAIR(0, 1): /* do as x| */
1525  EMPTYARC(lp, rp);
1526  break;
1527  case PAIR(0, SOME): /* do as x{1,n}| */
1528  repeat(v, lp, rp, 1, n);
1529  NOERR();
1530  EMPTYARC(lp, rp);
1531  break;
1532  case PAIR(0, INF): /* loop x around */
1533  s = newstate(v->nfa);
1534  NOERR();
1535  moveouts(v->nfa, lp, s);
1536  moveins(v->nfa, rp, s);
1537  EMPTYARC(lp, s);
1538  EMPTYARC(s, rp);
1539  break;
1540  case PAIR(1, 1): /* no action required */
1541  break;
1542  case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */
1543  s = newstate(v->nfa);
1544  NOERR();
1545  moveouts(v->nfa, lp, s);
1546  dupnfa(v->nfa, s, rp, lp, s);
1547  NOERR();
1548  repeat(v, lp, s, 1, n - 1);
1549  NOERR();
1550  EMPTYARC(lp, s);
1551  break;
1552  case PAIR(1, INF): /* add loopback arc */
1553  s = newstate(v->nfa);
1554  s2 = newstate(v->nfa);
1555  NOERR();
1556  moveouts(v->nfa, lp, s);
1557  moveins(v->nfa, rp, s2);
1558  EMPTYARC(lp, s);
1559  EMPTYARC(s2, rp);
1560  EMPTYARC(s2, s);
1561  break;
1562  case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */
1563  s = newstate(v->nfa);
1564  NOERR();
1565  moveouts(v->nfa, lp, s);
1566  dupnfa(v->nfa, s, rp, lp, s);
1567  NOERR();
1568  repeat(v, lp, s, m - 1, n - 1);
1569  break;
1570  case PAIR(SOME, INF): /* do as x{m-1,}x */
1571  s = newstate(v->nfa);
1572  NOERR();
1573  moveouts(v->nfa, lp, s);
1574  dupnfa(v->nfa, s, rp, lp, s);
1575  NOERR();
1576  repeat(v, lp, s, m - 1, n);
1577  break;
1578  default:
1579  ERR(REG_ASSERT);
1580  break;
1581  }
1582 }
#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:1503
#define NOERR()
Definition: regcomp.c:277
#define INF

◆ rfree()

static void rfree ( regex_t re)
static

Definition at line 2326 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().

2327 {
2328  struct guts *g;
2329 
2330  if (re == NULL || re->re_magic != REMAGIC)
2331  return;
2332 
2333  re->re_magic = 0; /* invalidate RE */
2334  g = (struct guts *) re->re_guts;
2335  re->re_guts = NULL;
2336  re->re_fns = NULL;
2337  if (g != NULL)
2338  {
2339  g->magic = 0;
2340  freecm(&g->cmap);
2341  if (g->tree != NULL)
2342  freesubre((struct vars *) NULL, g->tree);
2343  if (g->lacons != NULL)
2344  freelacons(g->lacons, g->nlacons);
2345  if (!NULLCNFA(g->search))
2346  freecnfa(&g->search);
2347  FREE(g);
2348  }
2349 }
char * re_fns
Definition: regex.h:81
static void freelacons(struct subre *, int)
Definition: regcomp.c:2309
struct subre * lacons
Definition: regguts.h:538
struct subre * tree
Definition: regguts.h:533
static void freecm(struct colormap *)
#define NULLCNFA(cnfa)
Definition: regguts.h:431
int nlacons
Definition: regguts.h:539
#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:534
int magic
Definition: regguts.h:528
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2069
struct colormap cmap
Definition: regguts.h:536
Definition: regguts.h:526
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 1472 of file regcomp.c.

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

Referenced by parseqatom().

1473 {
1474  int n = 0;
1475 
1476  while (SEE(DIGIT) && n < DUPMAX)
1477  {
1478  n = n * 10 + v->nextvalue;
1479  NEXT();
1480  }
1481  if (SEE(DIGIT) || n > DUPMAX)
1482  {
1483  ERR(REG_BADBR);
1484  return 0;
1485  }
1486  return n;
1487 }
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 1803 of file regcomp.c.

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

Referenced by brackpart().

1804 {
1805  const chr *endp;
1806 
1807  assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
1808  NEXT();
1809 
1810  endp = v->now;
1811  while (SEE(PLAIN))
1812  {
1813  endp = v->now;
1814  NEXT();
1815  }
1816 
1817  assert(SEE(END) || ISERR());
1818  NEXT();
1819 
1820  return endp;
1821 }
#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 2012 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().

2017 {
2018  struct subre *ret = v->treefree;
2019 
2020  /*
2021  * Checking for stack overflow here is sufficient to protect parse() and
2022  * its recursive subroutines.
2023  */
2024  if (STACK_TOO_DEEP(v->re))
2025  {
2026  ERR(REG_ETOOBIG);
2027  return NULL;
2028  }
2029 
2030  if (ret != NULL)
2031  v->treefree = ret->child;
2032  else
2033  {
2034  ret = (struct subre *) MALLOC(sizeof(struct subre));
2035  if (ret == NULL)
2036  {
2037  ERR(REG_ESPACE);
2038  return NULL;
2039  }
2040  ret->chain = v->treechain;
2041  v->treechain = ret;
2042  }
2043 
2044  assert(strchr("=b|.*(", op) != NULL);
2045 
2046  ret->op = op;
2047  ret->flags = flags;
2048  ret->latype = (char) -1;
2049  ret->id = 0; /* will be assigned later */
2050  ret->capno = 0;
2051  ret->backno = 0;
2052  ret->min = ret->max = 1;
2053  ret->child = NULL;
2054  ret->sibling = NULL;
2055  ret->begin = begin;
2056  ret->end = end;
2057  ZAPCNFA(ret->cnfa);
2058 
2059  return ret;
2060 }
struct subre * treefree
Definition: regcomp.c:258
struct subre * child
Definition: regguts.h:495
int capno
Definition: regguts.h:491
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:493
struct state * begin
Definition: regguts.h:497
char latype
Definition: regguts.h:489
#define MALLOC(n)
Definition: regcustom.h:60
#define ERR(e)
Definition: regcomp.c:276
struct subre * chain
Definition: regguts.h:500
int id
Definition: regguts.h:490
#define ZAPCNFA(cnfa)
Definition: regguts.h:429
regex_t * re
Definition: regcomp.c:239
struct subre * sibling
Definition: regguts.h:496
char flags
Definition: regguts.h:474
#define assert(TEST)
Definition: imath.c:73
int backno
Definition: regguts.h:492
short max
Definition: regguts.h:494
#define REG_ETOOBIG
Definition: regex.h:157
struct state * end
Definition: regguts.h:498
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:499
#define STACK_TOO_DEEP(re)
Definition: regguts.h:519

◆ 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 1393 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().

1397 {
1398  assert(dir == AHEAD || dir == BEHIND);
1399  cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
1400  /* (no need for special attention to \n) */
1401 }
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 1910 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.

1911 {
1912  struct state *cstate;
1913  struct cvec *cv;
1914 
1915  if (v->wordchrs != NULL)
1916  return; /* done already */
1917 
1918  /* make dummy state to hang the cache arcs on */
1919  cstate = newstate(v->nfa);
1920  NOERR();
1921 
1922  /* obtain possibly-cached cvec for \w characters */
1923  NOTE(REG_ULOCALE);
1924  cv = cclasscvec(v, CC_WORD, (v->cflags & REG_ICASE));
1925  NOERR();
1926 
1927  /* build the arcs; this may cause color splitting */
1928  subcolorcvec(v, cv, cstate, cstate);
1929  NOERR();
1930 
1931  /* close new open subcolors to ensure the cache entry is self-contained */
1932  okcolors(v->nfa, v->cm);
1933  NOERR();
1934 
1935  /* success! save the cache pointer */
1936  v->wordchrs = cstate;
1937 }
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