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, int, 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 290 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 293 of file regcomp.c.

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

◆ CCLASSC

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

Definition at line 296 of file regcomp.c.

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

◆ CCLASSS

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

Definition at line 295 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 291 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:300
#define PLAIN
Definition: regcomp.c:288
color co
Definition: regguts.h:293
#define AHEAD
Definition: regcomp.c:299

Definition at line 308 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 289 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 272 of file regcomp.c.

Referenced by parse(), and parseqatom().

◆ ECLASS

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

Definition at line 292 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 283 of file regcomp.c.

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

◆ END

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

Definition at line 294 of file regcomp.c.

Referenced by scanplain().

◆ EOS

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

Definition at line 287 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 277 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 281 of file regcomp.c.

Referenced by brackpart(), and parseqatom().

◆ ISERR

#define ISERR ( )    VISERR(v)

Definition at line 274 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 270 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 279 of file regcomp.c.

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

◆ NOERRZ

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

Definition at line 280 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 302 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 297 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 303 of file regcomp.c.

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

◆ SEE

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

Definition at line 271 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 304 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:287
e
Definition: preproc-init.c:82

Definition at line 275 of file regcomp.c.

◆ VISERR

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

Definition at line 273 of file regcomp.c.

◆ WBDRY

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

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

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

◆ brackpart()

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

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

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

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

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

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

Referenced by brackpart(), and parseqatom().

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

◆ charclasscomplement()

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

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

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

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

Referenced by freev(), and pg_regcomp().

2194 {
2195  struct subre *t;
2196  struct subre *next;
2197 
2198  for (t = v->treechain; t != NULL; t = next)
2199  {
2200  next = t->chain;
2201  if (!(t->flags & INUSE))
2202  FREE(t);
2203  }
2204  v->treechain = NULL;
2205  v->treefree = NULL; /* just on general principles */
2206 }
struct subre * treefree
Definition: regcomp.c:259
struct subre * treechain
Definition: regcomp.c:258
#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 2310 of file regcomp.c.

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

Referenced by freev(), and rfree().

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

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

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

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

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

◆ freesubreandsiblings()

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

Definition at line 2089 of file regcomp.c.

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

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

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

◆ freev()

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

Definition at line 548 of file regcomp.c.

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

Referenced by pg_regcomp().

550 {
551  if (v->re != NULL)
552  rfree(v->re);
553  if (v->subs != v->sub10)
554  FREE(v->subs);
555  if (v->nfa != NULL)
556  freenfa(v->nfa);
557  if (v->tree != NULL)
558  freesubre(v, v->tree);
559  if (v->treechain != NULL)
560  cleanst(v);
561  if (v->cv != NULL)
562  freecvec(v->cv);
563  if (v->cv2 != NULL)
564  freecvec(v->cv2);
565  if (v->lacons != NULL)
566  freelacons(v->lacons, v->nlacons);
567  ERR(err); /* nop if err==0 */
568 
569  return v->err;
570 }
static void freelacons(struct subre *, int)
Definition: regcomp.c:2310
static void rfree(regex_t *)
Definition: regcomp.c:2327
struct subre * treechain
Definition: regcomp.c:258
#define FREE(ptr)
Definition: cryptohash.c:37
#define ERR(e)
Definition: regcomp.c:277
regex_t * re
Definition: regcomp.c:240
int nlacons
Definition: regcomp.c:264
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:2070
struct subre ** subs
Definition: regcomp.c:250
int err
Definition: regcomp.c:243
struct nfa * nfa
Definition: regcomp.c:253
struct cvec * cv
Definition: regcomp.c:261
struct cvec * cv2
Definition: regcomp.c:262
static void freecvec(struct cvec *)
struct subre * sub10[10]
Definition: regcomp.c:252
static void freenfa(struct nfa *)
struct subre * tree
Definition: regcomp.c:257
static void cleanst(struct vars *)
Definition: regcomp.c:2193
struct subre * lacons
Definition: regcomp.c:263

◆ getcvec()

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

◆ hasconstraintout()

static int hasconstraintout ( struct state )
static

◆ hasnonemptyout()

static int hasnonemptyout ( struct state )
static

◆ initcm()

static void initcm ( struct vars ,
struct colormap  
)
static

Referenced by pg_regcomp().

◆ isconstraintarc()

static int isconstraintarc ( struct arc )
static

◆ lexdigits()

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

◆ lexescape()

static int lexescape ( struct vars )
static

◆ lexstart()

static void lexstart ( struct vars )
static

Referenced by pg_regcomp().

◆ lookupcclass()

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

Referenced by brackpart().

◆ makesearch()

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

Definition at line 577 of file regcomp.c.

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

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

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

Referenced by pg_regcomp().

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

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

Referenced by parseqatom().

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

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

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

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

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

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

Referenced by pg_regcomp().

2215 {
2216  struct subre *t2;
2217 
2218  assert(t != NULL && t->begin != NULL);
2219 
2220  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
2221  (DISCARD) nfatree(v, t2, f);
2222 
2223  return nfanode(v, t, 0, f);
2224 }
struct subre * child
Definition: regguts.h:495
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:2212
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:2232
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 1376 of file regcomp.c.

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

Referenced by parseqatom().

1380 {
1381  int anchor = (dir == AHEAD) ? '$' : '^';
1382 
1383  assert(dir == AHEAD || dir == BEHIND);
1384  newarc(v->nfa, anchor, 1, lp, rp);
1385  newarc(v->nfa, anchor, 0, lp, rp);
1386  colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
1387  /* (no need for special attention to \n) */
1388 }
#define BEHIND
Definition: regcomp.c:300
struct colormap * cm
Definition: regcomp.c:254
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:253
struct state * wordchrs
Definition: regcomp.c:256
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:299
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 2145 of file regcomp.c.

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

Referenced by pg_regcomp().

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

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

Referenced by brackpart(), and parseqatom().

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

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

Referenced by pg_regcomp().

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

◆ parse()

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

Definition at line 666 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(), expand_single_inheritance_child(), flatten_simple_union_all(), get_number_of_groups(), grouping_planner(), inheritance_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().

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

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

◆ parseqatom()

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

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

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

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

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

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

References InterruptPending, ProcDiePending, and QueryCancelPending.

2364 {
2366 }
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 1504 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().

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

◆ rfree()

static void rfree ( regex_t re)
static

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

2328 {
2329  struct guts *g;
2330 
2331  if (re == NULL || re->re_magic != REMAGIC)
2332  return;
2333 
2334  re->re_magic = 0; /* invalidate RE */
2335  g = (struct guts *) re->re_guts;
2336  re->re_guts = NULL;
2337  re->re_fns = NULL;
2338  if (g != NULL)
2339  {
2340  g->magic = 0;
2341  freecm(&g->cmap);
2342  if (g->tree != NULL)
2343  freesubre((struct vars *) NULL, g->tree);
2344  if (g->lacons != NULL)
2345  freelacons(g->lacons, g->nlacons);
2346  if (!NULLCNFA(g->search))
2347  freecnfa(&g->search);
2348  FREE(g);
2349  }
2350 }
char * re_fns
Definition: regex.h:81
static void freelacons(struct subre *, int)
Definition: regcomp.c:2310
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:2070
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:238

◆ rstacktoodeep()

◆ scannum()

static int scannum ( struct vars v)
static

Definition at line 1473 of file regcomp.c.

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

Referenced by parseqatom().

1474 {
1475  int n = 0;
1476 
1477  while (SEE(DIGIT) && n < DUPMAX)
1478  {
1479  n = n * 10 + v->nextvalue;
1480  NEXT();
1481  }
1482  if (SEE(DIGIT) || n > DUPMAX)
1483  {
1484  ERR(REG_BADBR);
1485  return 0;
1486  }
1487  return n;
1488 }
chr nextvalue
Definition: regcomp.c:247
#define DUPMAX
Definition: regguts.h:93
#define NEXT()
Definition: regcomp.c:270
#define ERR(e)
Definition: regcomp.c:277
#define REG_BADBR
Definition: regex.h:149
#define SEE(t)
Definition: regcomp.c:271
#define DIGIT
Definition: regcomp.c:289

◆ scanplain()

static const chr * scanplain ( struct vars v)
static

Definition at line 1804 of file regcomp.c.

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

Referenced by brackpart().

1805 {
1806  const chr *endp;
1807 
1808  assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
1809  NEXT();
1810 
1811  endp = v->now;
1812  while (SEE(PLAIN))
1813  {
1814  endp = v->now;
1815  NEXT();
1816  }
1817 
1818  assert(SEE(END) || ISERR());
1819  NEXT();
1820 
1821  return endp;
1822 }
#define END
Definition: regcomp.c:294
#define CCLASS
Definition: regcomp.c:293
#define NEXT()
Definition: regcomp.c:270
#define ISERR()
Definition: regcomp.c:274
pg_wchar chr
Definition: regcustom.h:66
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:271
#define PLAIN
Definition: regcomp.c:288
const chr * now
Definition: regcomp.c:241
#define COLLEL
Definition: regcomp.c:291
#define ECLASS
Definition: regcomp.c:292

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

2018 {
2019  struct subre *ret = v->treefree;
2020 
2021  /*
2022  * Checking for stack overflow here is sufficient to protect parse() and
2023  * its recursive subroutines.
2024  */
2025  if (STACK_TOO_DEEP(v->re))
2026  {
2027  ERR(REG_ETOOBIG);
2028  return NULL;
2029  }
2030 
2031  if (ret != NULL)
2032  v->treefree = ret->child;
2033  else
2034  {
2035  ret = (struct subre *) MALLOC(sizeof(struct subre));
2036  if (ret == NULL)
2037  {
2038  ERR(REG_ESPACE);
2039  return NULL;
2040  }
2041  ret->chain = v->treechain;
2042  v->treechain = ret;
2043  }
2044 
2045  assert(strchr("=b|.*(", op) != NULL);
2046 
2047  ret->op = op;
2048  ret->flags = flags;
2049  ret->latype = (char) -1;
2050  ret->id = 0; /* will be assigned later */
2051  ret->capno = 0;
2052  ret->backno = 0;
2053  ret->min = ret->max = 1;
2054  ret->child = NULL;
2055  ret->sibling = NULL;
2056  ret->begin = begin;
2057  ret->end = end;
2058  ZAPCNFA(ret->cnfa);
2059 
2060  return ret;
2061 }
struct subre * treefree
Definition: regcomp.c:259
struct subre * child
Definition: regguts.h:495
int capno
Definition: regguts.h:491
struct subre * treechain
Definition: regcomp.c:258
#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:277
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:240
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 1394 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().

1398 {
1399  assert(dir == AHEAD || dir == BEHIND);
1400  cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
1401  /* (no need for special attention to \n) */
1402 }
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
#define BEHIND
Definition: regcomp.c:300
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:253
struct state * wordchrs
Definition: regcomp.c:256
#define AHEAD
Definition: regcomp.c:299

◆ wordchrs()

static void wordchrs ( struct vars v)
static

Definition at line 1911 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.

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

Variable Documentation

◆ functions

const struct fns functions
static