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 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 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)   ((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND)
 
#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 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 *)
 
static const chrscanplain (struct vars *)
 
static void onechr (struct vars *, chr, 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 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 void lexnest (struct vars *, const chr *, const chr *)
 
static void lexword (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 destroystate (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 *, struct state *)
 
static void freearc (struct nfa *, struct arc *)
 
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 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 arc *, struct arc *)
 
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 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_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 struct cveccclass (struct vars *, const chr *, const chr *, 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 */

Definition at line 285 of file regcomp.c.

Referenced by combine(), dumpnfa(), isconstraintarc(), nonword(), parseqatom(), processlacon(), pushfwd(), and word().

◆ ARCV

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

Referenced by parseqatom().

◆ BACKREF

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

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

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

◆ CNOERR

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

Referenced by pg_regcomp().

◆ COLLEL

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

Definition at line 279 of file regcomp.c.

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

◆ COLORED

#define COLORED (   a)    ((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND)

Definition at line 293 of file regcomp.c.

Referenced by createarc(), and freearc().

◆ COMPATIBLE

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

Definition at line 160 of file regcomp.c.

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

◆ DIGIT

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

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

Referenced by parse(), and parseqatom().

◆ ECLASS

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

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

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

◆ END

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

Definition at line 282 of file regcomp.c.

Referenced by scanplain().

◆ EOS

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

Definition at line 275 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 265 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 158 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 269 of file regcomp.c.

Referenced by brackpart(), and parseqatom().

◆ ISERR

#define ISERR ( )    VISERR(v)

Definition at line 262 of file regcomp.c.

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

◆ LACON

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

Definition at line 284 of file regcomp.c.

Referenced by combine(), compact(), dumpnfa(), isconstraintarc(), next(), parseqatom(), and processlacon().

◆ NEXT

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

Definition at line 258 of file regcomp.c.

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

◆ NOERR

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

◆ NOERRN

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

Definition at line 267 of file regcomp.c.

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

◆ NOERRZ

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

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

Referenced by brackpart(), and next().

◆ REDUCE

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

Referenced by repeat().

◆ SATISFIED

#define SATISFIED   2 /* constraint satisfied */

Definition at line 159 of file regcomp.c.

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

◆ SBEGIN

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

Definition at line 289 of file regcomp.c.

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

◆ SEE

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

◆ SEND

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

Definition at line 290 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:275
e
Definition: preproc-init.c:82

Definition at line 263 of file regcomp.c.

◆ VISERR

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

Definition at line 261 of file regcomp.c.

◆ WBDRY

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

Definition at line 287 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 ,
struct state  
)
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 1377 of file regcomp.c.

References assert, brackpart(), vars::cm, EOS, ISERR, NEXT, vars::nfa, okcolors(), and SEE.

Referenced by cbracket(), parseqatom(), and wordchrs().

1380 {
1381  assert(SEE('['));
1382  NEXT();
1383  while (!SEE(']') && !SEE(EOS))
1384  brackpart(v, lp, rp);
1385  assert(SEE(']') || ISERR());
1386  okcolors(v->nfa, v->cm);
1387 }
static void okcolors(struct nfa *, struct colormap *)
#define NEXT()
Definition: regcomp.c:258
#define ISERR()
Definition: regcomp.c:262
struct colormap * cm
Definition: regcomp.c:242
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:259
struct nfa * nfa
Definition: regcomp.c:241
static void brackpart(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1426
#define EOS
Definition: regcomp.c:275

◆ brackpart()

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

Definition at line 1426 of file regcomp.c.

References cclass(), CCLASS, vars::cflags, COLLEL, vars::cv, eclass(), ECLASS, element(), ERR, INSIST, 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().

1429 {
1430  chr startc;
1431  chr endc;
1432  struct cvec *cv;
1433  const chr *startp;
1434  const chr *endp;
1435  chr c[1];
1436 
1437  /* parse something, get rid of special cases, take shortcuts */
1438  switch (v->nexttype)
1439  {
1440  case RANGE: /* a-b-c or other botch */
1441  ERR(REG_ERANGE);
1442  return;
1443  break;
1444  case PLAIN:
1445  c[0] = v->nextvalue;
1446  NEXT();
1447  /* shortcut for ordinary chr (not range) */
1448  if (!SEE(RANGE))
1449  {
1450  onechr(v, c[0], lp, rp);
1451  return;
1452  }
1453  startc = element(v, c, c + 1);
1454  NOERR();
1455  break;
1456  case COLLEL:
1457  startp = v->now;
1458  endp = scanplain(v);
1459  INSIST(startp < endp, REG_ECOLLATE);
1460  NOERR();
1461  startc = element(v, startp, endp);
1462  NOERR();
1463  break;
1464  case ECLASS:
1465  startp = v->now;
1466  endp = scanplain(v);
1467  INSIST(startp < endp, REG_ECOLLATE);
1468  NOERR();
1469  startc = element(v, startp, endp);
1470  NOERR();
1471  cv = eclass(v, startc, (v->cflags & REG_ICASE));
1472  NOERR();
1473  subcolorcvec(v, cv, lp, rp);
1474  return;
1475  break;
1476  case CCLASS:
1477  startp = v->now;
1478  endp = scanplain(v);
1479  INSIST(startp < endp, REG_ECTYPE);
1480  NOERR();
1481  cv = cclass(v, startp, endp, (v->cflags & REG_ICASE));
1482  NOERR();
1483  subcolorcvec(v, cv, lp, rp);
1484  return;
1485  break;
1486  default:
1487  ERR(REG_ASSERT);
1488  return;
1489  break;
1490  }
1491 
1492  if (SEE(RANGE))
1493  {
1494  NEXT();
1495  switch (v->nexttype)
1496  {
1497  case PLAIN:
1498  case RANGE:
1499  c[0] = v->nextvalue;
1500  NEXT();
1501  endc = element(v, c, c + 1);
1502  NOERR();
1503  break;
1504  case COLLEL:
1505  startp = v->now;
1506  endp = scanplain(v);
1507  INSIST(startp < endp, REG_ECOLLATE);
1508  NOERR();
1509  endc = element(v, startp, endp);
1510  NOERR();
1511  break;
1512  default:
1513  ERR(REG_ERANGE);
1514  return;
1515  break;
1516  }
1517  }
1518  else
1519  endc = startc;
1520 
1521  /*
1522  * Ranges are unportable. Actually, standard C does guarantee that digits
1523  * are contiguous, but making that an exception is just too complicated.
1524  */
1525  if (startc != endc)
1526  NOTE(REG_UUNPORT);
1527  cv = range(v, startc, endc, (v->cflags & REG_ICASE));
1528  NOERR();
1529  subcolorcvec(v, cv, lp, rp);
1530 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
#define RANGE
Definition: regcomp.c:283
int nexttype
Definition: regcomp.c:234
#define CCLASS
Definition: regcomp.c:281
chr nextvalue
Definition: regcomp.c:235
static chr element(struct vars *, const chr *, const chr *)
#define NEXT()
Definition: regcomp.c:258
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:1564
#define REG_ICASE
Definition: regex.h:106
#define ERR(e)
Definition: regcomp.c:265
pg_wchar chr
Definition: regcustom.h:68
char * c
static const chr * scanplain(struct vars *)
Definition: regcomp.c:1539
#define SEE(t)
Definition: regcomp.c:259
#define REG_ECOLLATE
Definition: regex.h:140
Definition: regguts.h:256
#define REG_UUNPORT
Definition: regex.h:69
#define PLAIN
Definition: regcomp.c:276
#define REG_ECTYPE
Definition: regex.h:141
static struct cvec * cclass(struct vars *, const chr *, const chr *, int)
#define REG_ASSERT
Definition: regex.h:151
#define REG_ERANGE
Definition: regex.h:148
const chr * now
Definition: regcomp.c:227
#define COLLEL
Definition: regcomp.c:279
#define NOTE(b)
Definition: regcomp.c:270
#define NOERR()
Definition: regcomp.c:266
int cflags
Definition: regcomp.c:232
#define INSIST(c, e)
Definition: regcomp.c:269
#define ECLASS
Definition: regcomp.c:280

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

1399 {
1400  struct state *left = newstate(v->nfa);
1401  struct state *right = newstate(v->nfa);
1402 
1403  NOERR();
1404  bracket(v, left, right);
1405  if (v->cflags & REG_NLSTOP)
1406  newarc(v->nfa, PLAIN, v->nlcolor, left, right);
1407  NOERR();
1408 
1409  assert(lp->nouts == 0); /* all outarcs will be ours */
1410 
1411  /*
1412  * Easy part of complementing, and all there is to do since the MCCE code
1413  * was removed.
1414  */
1415  colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
1416  NOERR();
1417  dropstate(v->nfa, left);
1418  assert(right->nins == 0);
1419  freestate(v->nfa, right);
1420 }
#define REG_NLSTOP
Definition: regex.h:109
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1377
int nins
Definition: regguts.h:303
color nlcolor
Definition: regcomp.c:243
struct colormap * cm
Definition: regcomp.c:242
#define assert(TEST)
Definition: imath.c:73
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:241
static void freestate(struct nfa *, struct state *)
#define PLAIN
Definition: regcomp.c:276
Definition: regguts.h:298
static void newarc(struct nfa *, int, color, struct state *, struct state *)
int nouts
Definition: regguts.h:305
#define NOERR()
Definition: regcomp.c:266
int cflags
Definition: regcomp.c:232
static void dropstate(struct nfa *, struct state *)
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)

◆ cclass()

static struct cvec* cclass ( struct vars ,
const chr ,
const chr ,
int   
)
static

Referenced by brackpart().

◆ cclass_column_index()

static int cclass_column_index ( struct colormap ,
chr   
)
static

◆ changearctarget()

static void changearctarget ( struct arc ,
struct state  
)
static

◆ chrnamed()

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

◆ cleanst()

static void cleanst ( struct vars v)
static

Definition at line 1848 of file regcomp.c.

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

Referenced by freev(), and pg_regcomp().

1849 {
1850  struct subre *t;
1851  struct subre *next;
1852 
1853  for (t = v->treechain; t != NULL; t = next)
1854  {
1855  next = t->chain;
1856  if (!(t->flags & INUSE))
1857  FREE(t);
1858  }
1859  v->treechain = NULL;
1860  v->treefree = NULL; /* just on general principles */
1861 }
struct subre * treefree
Definition: regcomp.c:247
struct subre * treechain
Definition: regcomp.c:246
struct subre * chain
Definition: regguts.h:436
static int next(struct vars *)
char flags
Definition: regguts.h:411
#define INUSE
Definition: regguts.h:417
Definition: regguts.h:408
#define FREE(size)
Definition: saslprep.c:51

◆ 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

Referenced by cbracket(), nonword(), and processlacon().

◆ combine()

static int combine ( struct arc ,
struct arc  
)
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

◆ destroystate()

static void destroystate ( struct nfa ,
struct state  
)
static

◆ dropstate()

static void dropstate ( struct nfa ,
struct state  
)
static

Referenced by cbracket().

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

◆ 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 1965 of file regcomp.c.

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

Referenced by freev(), and rfree().

1967 {
1968  struct subre *sub;
1969  int i;
1970 
1971  assert(n > 0);
1972  for (sub = subs + 1, i = n - 1; i > 0; sub++, i--) /* no 0th */
1973  if (!NULLCNFA(sub->cnfa))
1974  freecnfa(&sub->cnfa);
1975  FREE(subs);
1976 }
#define NULLCNFA(cnfa)
Definition: regguts.h:372
#define assert(TEST)
Definition: imath.c:73
Definition: regguts.h:408
struct cnfa cnfa
Definition: regguts.h:435
int i
static void freecnfa(struct cnfa *)
#define FREE(size)
Definition: saslprep.c:51

◆ 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 1759 of file regcomp.c.

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

Referenced by freesubre().

1761 {
1762  if (sr == NULL)
1763  return;
1764 
1765  if (!NULLCNFA(sr->cnfa))
1766  freecnfa(&sr->cnfa);
1767  sr->flags = 0;
1768 
1769  if (v != NULL && v->treechain != NULL)
1770  {
1771  /* we're still parsing, maybe we can reuse the subre */
1772  sr->left = v->treefree;
1773  v->treefree = sr;
1774  }
1775  else
1776  FREE(sr);
1777 }
struct subre * treefree
Definition: regcomp.c:247
struct subre * treechain
Definition: regcomp.c:246
#define NULLCNFA(cnfa)
Definition: regguts.h:372
char flags
Definition: regguts.h:411
struct subre * left
Definition: regguts.h:431
struct cnfa cnfa
Definition: regguts.h:435
static void freecnfa(struct cnfa *)
#define FREE(size)
Definition: saslprep.c:51

◆ 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 1741 of file regcomp.c.

References freesrnode(), subre::left, and subre::right.

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

1743 {
1744  if (sr == NULL)
1745  return;
1746 
1747  if (sr->left != NULL)
1748  freesubre(v, sr->left);
1749  if (sr->right != NULL)
1750  freesubre(v, sr->right);
1751 
1752  freesrnode(v, sr);
1753 }
struct subre * right
Definition: regguts.h:432
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:1759
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1741
struct subre * left
Definition: regguts.h:431

◆ freev()

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

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

532 {
533  if (v->re != NULL)
534  rfree(v->re);
535  if (v->subs != v->sub10)
536  FREE(v->subs);
537  if (v->nfa != NULL)
538  freenfa(v->nfa);
539  if (v->tree != NULL)
540  freesubre(v, v->tree);
541  if (v->treechain != NULL)
542  cleanst(v);
543  if (v->cv != NULL)
544  freecvec(v->cv);
545  if (v->cv2 != NULL)
546  freecvec(v->cv2);
547  if (v->lacons != NULL)
548  freelacons(v->lacons, v->nlacons);
549  ERR(err); /* nop if err==0 */
550 
551  return v->err;
552 }
static void freelacons(struct subre *, int)
Definition: regcomp.c:1965
static void rfree(regex_t *)
Definition: regcomp.c:1982
struct subre * treechain
Definition: regcomp.c:246
#define ERR(e)
Definition: regcomp.c:265
regex_t * re
Definition: regcomp.c:226
int nlacons
Definition: regcomp.c:252
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1741
struct subre ** subs
Definition: regcomp.c:238
int err
Definition: regcomp.c:231
struct nfa * nfa
Definition: regcomp.c:241
struct cvec * cv
Definition: regcomp.c:249
struct cvec * cv2
Definition: regcomp.c:250
static void freecvec(struct cvec *)
struct subre * sub10[10]
Definition: regcomp.c:240
static void freenfa(struct nfa *)
struct subre * tree
Definition: regcomp.c:245
static void cleanst(struct vars *)
Definition: regcomp.c:1848
struct subre * lacons
Definition: regcomp.c:251
#define FREE(size)
Definition: saslprep.c:51

◆ 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

◆ lexnest()

static void lexnest ( struct vars ,
const chr ,
const chr  
)
static

◆ lexstart()

static void lexstart ( struct vars )
static

Referenced by pg_regcomp().

◆ lexword()

static void lexword ( struct vars )
static

Referenced by wordchrs().

◆ makesearch()

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

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

561 {
562  struct arc *a;
563  struct arc *b;
564  struct state *pre = nfa->pre;
565  struct state *s;
566  struct state *s2;
567  struct state *slist;
568 
569  /* no loops are needed if it's anchored */
570  for (a = pre->outs; a != NULL; a = a->outchain)
571  {
572  assert(a->type == PLAIN);
573  if (a->co != nfa->bos[0] && a->co != nfa->bos[1])
574  break;
575  }
576  if (a != NULL)
577  {
578  /* add implicit .* in front */
579  rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
580 
581  /* and ^* and \A* too -- not always necessary, but harmless */
582  newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
583  newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
584  }
585 
586  /*
587  * Now here's the subtle part. Because many REs have no lookback
588  * constraints, often knowing when you were in the pre state tells you
589  * little; it's the next state(s) that are informative. But some of them
590  * may have other inarcs, i.e. it may be possible to make actual progress
591  * and then return to one of them. We must de-optimize such cases,
592  * splitting each such state into progress and no-progress states.
593  */
594 
595  /* first, make a list of the states reachable from pre and elsewhere */
596  slist = NULL;
597  for (a = pre->outs; a != NULL; a = a->outchain)
598  {
599  s = a->to;
600  for (b = s->ins; b != NULL; b = b->inchain)
601  {
602  if (b->from != pre)
603  break;
604  }
605 
606  /*
607  * We want to mark states as being in the list already by having non
608  * NULL tmp fields, but we can't just store the old slist value in tmp
609  * because that doesn't work for the first such state. Instead, the
610  * first list entry gets its own address in tmp.
611  */
612  if (b != NULL && s->tmp == NULL)
613  {
614  s->tmp = (slist != NULL) ? slist : s;
615  slist = s;
616  }
617  }
618 
619  /* do the splits */
620  for (s = slist; s != NULL; s = s2)
621  {
622  s2 = newstate(nfa);
623  NOERR();
624  copyouts(nfa, s, s2);
625  NOERR();
626  for (a = s->ins; a != NULL; a = b)
627  {
628  b = a->inchain;
629  if (a->from != pre)
630  {
631  cparc(nfa, a, a->from, s2);
632  freearc(nfa, a);
633  }
634  }
635  s2 = (s->tmp != s) ? s->tmp : NULL;
636  s->tmp = NULL; /* clean up while we're at it */
637  }
638 }
static void cparc(struct nfa *, struct arc *, struct state *, struct state *)
struct state * from
Definition: regguts.h:280
int type
Definition: regguts.h:278
Definition: regguts.h:276
struct colormap * cm
Definition: regcomp.c:242
static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *)
struct arc * outchain
Definition: regguts.h:282
color bos[2]
Definition: regguts.h:326
#define assert(TEST)
Definition: imath.c:73
struct arc * outs
Definition: regguts.h:306
static struct state * newstate(struct nfa *)
static void copyouts(struct nfa *, struct state *, struct state *)
struct state * tmp
Definition: regguts.h:308
struct state * to
Definition: regguts.h:281
char * s2
#define PLAIN
Definition: regcomp.c:276
color co
Definition: regguts.h:279
Definition: regguts.h:298
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define NOERR()
Definition: regcomp.c:266
struct arc * inchain
Definition: regguts.h:285
#define COLORLESS
Definition: regguts.h:137
static void freearc(struct nfa *, struct arc *)
struct arc * ins
Definition: regguts.h:304
struct state * pre
Definition: regguts.h:317

◆ 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 1833 of file regcomp.c.

References assert, subre::flags, INUSE, subre::left, and subre::right.

Referenced by pg_regcomp().

1834 {
1835  assert(t != NULL);
1836 
1837  t->flags |= INUSE;
1838  if (t->left != NULL)
1839  markst(t->left);
1840  if (t->right != NULL)
1841  markst(t->right);
1842 }
struct subre * right
Definition: regguts.h:432
static void markst(struct subre *)
Definition: regcomp.c:1833
char flags
Definition: regguts.h:411
#define assert(TEST)
Definition: imath.c:73
struct subre * left
Definition: regguts.h:431
#define INUSE
Definition: regguts.h:417

◆ 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 493 of file regcomp.c.

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

Referenced by parseqatom().

495 {
496  struct subre **p;
497  size_t n;
498 
499  assert(wanted > 0 && (size_t) wanted >= v->nsubs);
500  n = (size_t) wanted * 3 / 2 + 1;
501 
502  if (v->subs == v->sub10)
503  {
504  p = (struct subre **) MALLOC(n * sizeof(struct subre *));
505  if (p != NULL)
506  memcpy(VS(p), VS(v->subs),
507  v->nsubs * sizeof(struct subre *));
508  }
509  else
510  p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
511  if (p == NULL)
512  {
513  ERR(REG_ESPACE);
514  return;
515  }
516  v->subs = p;
517  for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
518  *p = NULL;
519  assert(v->nsubs == n);
520  assert((size_t) wanted < v->nsubs);
521 }
#define REG_ESPACE
Definition: regex.h:149
#define MALLOC(n)
Definition: regcustom.h:62
#define ERR(e)
Definition: regcomp.c:265
#define REALLOC(p, n)
Definition: regcustom.h:64
struct subre ** subs
Definition: regcomp.c:238
#define assert(TEST)
Definition: imath.c:73
size_t nsubs
Definition: regcomp.c:239
#define VS(x)
Definition: regguts.h:61
Definition: regguts.h:408
struct subre * sub10[10]
Definition: regcomp.c:240

◆ 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 1926 of file regcomp.c.

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

Referenced by processlacon().

1930 {
1931  int n;
1932  struct subre *newlacons;
1933  struct subre *sub;
1934 
1935  if (v->nlacons == 0)
1936  {
1937  n = 1; /* skip 0th */
1938  newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
1939  }
1940  else
1941  {
1942  n = v->nlacons;
1943  newlacons = (struct subre *) REALLOC(v->lacons,
1944  (n + 1) * sizeof(struct subre));
1945  }
1946  if (newlacons == NULL)
1947  {
1948  ERR(REG_ESPACE);
1949  return 0;
1950  }
1951  v->lacons = newlacons;
1952  v->nlacons = n + 1;
1953  sub = &v->lacons[n];
1954  sub->begin = begin;
1955  sub->end = end;
1956  sub->subno = latype;
1957  ZAPCNFA(sub->cnfa);
1958  return n;
1959 }
#define REG_ESPACE
Definition: regex.h:149
int subno
Definition: regguts.h:427
struct state * begin
Definition: regguts.h:433
#define MALLOC(n)
Definition: regcustom.h:62
#define ERR(e)
Definition: regcomp.c:265
#define REALLOC(p, n)
Definition: regcustom.h:64
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
int nlacons
Definition: regcomp.c:252
struct state * end
Definition: regguts.h:434
Definition: regguts.h:408
struct cnfa cnfa
Definition: regguts.h:435
struct subre * lacons
Definition: regcomp.c:251

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

◆ nfanode()

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

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

1891 {
1892  struct nfa *nfa;
1893  long ret = 0;
1894 
1895  assert(t->begin != NULL);
1896 
1897 #ifdef REG_DEBUG
1898  if (f != NULL)
1899  {
1900  char idbuf[50];
1901 
1902  fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
1903  stid(t, idbuf, sizeof(idbuf)));
1904  }
1905 #endif
1906  nfa = newnfa(v, v->cm, v->nfa);
1907  NOERRZ();
1908  dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
1909  if (!ISERR())
1910  specialcolors(nfa);
1911  if (!ISERR())
1912  ret = optimize(nfa, f);
1913  if (converttosearch && !ISERR())
1914  makesearch(v, nfa);
1915  if (!ISERR())
1916  compact(nfa, &t->cnfa);
1917 
1918  freenfa(nfa);
1919  return ret;
1920 }
static long optimize(struct nfa *, FILE *)
struct state * final
Definition: regguts.h:319
static void specialcolors(struct nfa *)
struct state * begin
Definition: regguts.h:433
Definition: regguts.h:315
#define ISERR()
Definition: regcomp.c:262
#define fprintf
Definition: port.h:196
static void makesearch(struct vars *, struct nfa *)
Definition: regcomp.c:559
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
struct colormap * cm
Definition: regcomp.c:242
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:241
struct state * end
Definition: regguts.h:434
#define NOERRZ()
Definition: regcomp.c:268
struct cnfa cnfa
Definition: regguts.h:435
struct state * init
Definition: regguts.h:318
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 1867 of file regcomp.c.

References assert, subre::begin, DISCARD, subre::left, nfanode(), and subre::right.

Referenced by pg_regcomp().

1870 {
1871  assert(t != NULL && t->begin != NULL);
1872 
1873  if (t->left != NULL)
1874  (DISCARD) nfatree(v, t->left, f);
1875  if (t->right != NULL)
1876  (DISCARD) nfatree(v, t->right, f);
1877 
1878  return nfanode(v, t, 0, f);
1879 }
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:1867
struct subre * right
Definition: regguts.h:432
struct state * begin
Definition: regguts.h:433
#define assert(TEST)
Definition: imath.c:73
struct subre * left
Definition: regguts.h:431
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:1887
#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 1228 of file regcomp.c.

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

Referenced by parseqatom().

1232 {
1233  int anchor = (dir == AHEAD) ? '$' : '^';
1234 
1235  assert(dir == AHEAD || dir == BEHIND);
1236  newarc(v->nfa, anchor, 1, lp, rp);
1237  newarc(v->nfa, anchor, 0, lp, rp);
1238  colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
1239  /* (no need for special attention to \n) */
1240 }
#define BEHIND
Definition: regcomp.c:286
struct colormap * cm
Definition: regcomp.c:242
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:241
struct state * wordchrs
Definition: regcomp.c:244
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:285
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 1799 of file regcomp.c.

References assert, i, subre::id, subre::left, subre::right, and vars::start.

Referenced by pg_regcomp().

1801 {
1802  int i;
1803 
1804  assert(t != NULL);
1805 
1806  i = start;
1807  t->id = (short) i++;
1808  if (t->left != NULL)
1809  i = numst(t->left, i);
1810  if (t->right != NULL)
1811  i = numst(t->right, i);
1812  return i;
1813 }
static int numst(struct subre *, int)
Definition: regcomp.c:1799
struct subre * right
Definition: regguts.h:432
#define assert(TEST)
Definition: imath.c:73
struct subre * left
Definition: regguts.h:431
short id
Definition: regguts.h:426
int i

◆ okcolors()

static void okcolors ( struct nfa ,
struct colormap  
)
static

Referenced by bracket(), parseqatom(), and pg_regcomp().

◆ onechr()

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

Definition at line 1564 of file regcomp.c.

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

Referenced by brackpart(), and parseqatom().

1568 {
1569  if (!(v->cflags & REG_ICASE))
1570  {
1571  color lastsubcolor = COLORLESS;
1572 
1573  subcoloronechr(v, c, lp, rp, &lastsubcolor);
1574  return;
1575  }
1576 
1577  /* rats, need general case anyway... */
1578  subcolorcvec(v, allcases(v, c), lp, rp);
1579 }
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
short color
Definition: regguts.h:134
static void subcoloronechr(struct vars *, chr, struct state *, struct state *, color *)
#define REG_ICASE
Definition: regex.h:106
char * c
static struct cvec * allcases(struct vars *, chr)
int cflags
Definition: regcomp.c:232
#define COLORLESS
Definition: regguts.h:137

◆ optimize()

static long optimize ( struct nfa ,
FILE *   
)
static

Referenced by nfanode(), and pg_regcomp().

◆ optst()

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

Definition at line 1783 of file regcomp.c.

Referenced by pg_regcomp().

1785 {
1786  /*
1787  * DGP (2007-11-13): I assume it was the programmer's intent to eventually
1788  * come back and add code to optimize subRE trees, but the routine coded
1789  * just spends effort traversing the tree and doing nothing. We can do
1790  * nothing with less effort.
1791  */
1792  return;
1793 }

◆ parse()

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

Definition at line 648 of file regcomp.c.

References assert, EAT, EMPTYARC, EOS, ERR, subre::flags, freesubre(), subre::left, LONGER, MESSY, newstate(), vars::nfa, NOERRN, subre::op, parsebranch(), REG_EPAREN, subre::right, SEE, 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_partial_grouping_paths(), expand_single_inheritance_child(), flatten_simple_union_all(), get_number_of_groups(), grouping_planner(), inheritance_planner(), is_degenerate_grouping(), 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().

653 {
654  struct state *left; /* scaffolding for branch */
655  struct state *right;
656  struct subre *branches; /* top level */
657  struct subre *branch; /* current branch */
658  struct subre *t; /* temporary */
659  int firstbranch; /* is this the first branch? */
660 
661  assert(stopper == ')' || stopper == EOS);
662 
663  branches = subre(v, '|', LONGER, init, final);
664  NOERRN();
665  branch = branches;
666  firstbranch = 1;
667  do
668  { /* a branch */
669  if (!firstbranch)
670  {
671  /* need a place to hang it */
672  branch->right = subre(v, '|', LONGER, init, final);
673  NOERRN();
674  branch = branch->right;
675  }
676  firstbranch = 0;
677  left = newstate(v->nfa);
678  right = newstate(v->nfa);
679  NOERRN();
680  EMPTYARC(init, left);
681  EMPTYARC(right, final);
682  NOERRN();
683  branch->left = parsebranch(v, stopper, type, left, right, 0);
684  NOERRN();
685  branch->flags |= UP(branch->flags | branch->left->flags);
686  if ((branch->flags & ~branches->flags) != 0) /* new flags */
687  for (t = branches; t != branch; t = t->right)
688  t->flags |= branch->flags;
689  } while (EAT('|'));
690  assert(SEE(stopper) || SEE(EOS));
691 
692  if (!SEE(stopper))
693  {
694  assert(stopper == ')' && SEE(EOS));
695  ERR(REG_EPAREN);
696  }
697 
698  /* optimize out simple cases */
699  if (branch == branches)
700  { /* only one branch */
701  assert(branch->right == NULL);
702  t = branch->left;
703  branch->left = NULL;
704  freesubre(v, branches);
705  branches = t;
706  }
707  else if (!MESSY(branches->flags))
708  { /* no interesting innards */
709  freesubre(v, branches->left);
710  branches->left = NULL;
711  freesubre(v, branches->right);
712  branches->right = NULL;
713  branches->op = '=';
714  }
715 
716  return branches;
717 }
#define EAT(t)
Definition: regcomp.c:260
#define REG_EPAREN
Definition: regex.h:145
#define NOERRN()
Definition: regcomp.c:267
struct subre * right
Definition: regguts.h:432
#define MESSY(f)
Definition: regguts.h:422
char op
Definition: regguts.h:410
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:1689
#define ERR(e)
Definition: regcomp.c:265
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1741
char flags
Definition: regguts.h:411
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:259
struct subre * left
Definition: regguts.h:431
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:241
#define EMPTYARC(x, y)
Definition: regcomp.c:271
static struct subre * parsebranch(struct vars *, int, int, struct state *, struct state *, int)
Definition: regcomp.c:727
#define EOS
Definition: regcomp.c:275
Definition: regguts.h:298
Definition: regguts.h:408
#define LONGER
Definition: regguts.h:412
#define UP(f)
Definition: regguts.h:421

◆ parsebranch()

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

Definition at line 727 of file regcomp.c.

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

Referenced by parse(), and parseqatom().

733 {
734  struct state *lp; /* left end of current construct */
735  int seencontent; /* is there anything in this branch yet? */
736  struct subre *t;
737 
738  lp = left;
739  seencontent = 0;
740  t = subre(v, '=', 0, left, right); /* op '=' is tentative */
741  NOERRN();
742  while (!SEE('|') && !SEE(stopper) && !SEE(EOS))
743  {
744  if (seencontent)
745  { /* implicit concat operator */
746  lp = newstate(v->nfa);
747  NOERRN();
748  moveins(v->nfa, right, lp);
749  }
750  seencontent = 1;
751 
752  /* NB, recursion in parseqatom() may swallow rest of branch */
753  parseqatom(v, stopper, type, lp, right, t);
754  NOERRN();
755  }
756 
757  if (!seencontent)
758  { /* empty branch */
759  if (!partial)
760  NOTE(REG_UUNSPEC);
761  assert(lp == left);
762  EMPTYARC(left, right);
763  }
764 
765  return t;
766 }
#define NOERRN()
Definition: regcomp.c:267
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:1689
static void parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *)
Definition: regcomp.c:776
#define assert(TEST)
Definition: imath.c:73
static void moveins(struct nfa *, struct state *, struct state *)
#define SEE(t)
Definition: regcomp.c:259
struct subre * left
Definition: regguts.h:431
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:241
#define EMPTYARC(x, y)
Definition: regcomp.c:271
#define EOS
Definition: regcomp.c:275
#define REG_UUNSPEC
Definition: regex.h:68
Definition: regguts.h:298
Definition: regguts.h:408
#define NOTE(b)
Definition: regcomp.c:270

◆ parseqatom()

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

Definition at line 776 of file regcomp.c.

References AHEAD, ARCV, assert, BACKR, BACKREF, subre::begin, BEHIND, bracket(), CAP, cbracket(), vars::cflags, vars::cm, COLORLESS, COMBINE, delsub(), DIGIT, DUPINF, dupnfa(), EAT, EMPTYARC, subre::end, EOS, ERR, subre::flags, freesubre(), INSIST, ISERR, LACON, subre::left, 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, repeat(), subre::right, s2, SBEGIN, scannum(), SEE, SEND, SHORTER, subre::subno, subre(), vars::subs, UP, WBDRY, word(), and vars::wordchrs.

Referenced by parsebranch().

782 {
783  struct state *s; /* temporaries for new states */
784  struct state *s2;
785 
786 #define ARCV(t, val) newarc(v->nfa, t, val, lp, rp)
787  int m,
788  n;
789  struct subre *atom; /* atom's subtree */
790  struct subre *t;
791  int cap; /* capturing parens? */
792  int latype; /* lookaround constraint type */
793  int subno; /* capturing-parens or backref number */
794  int atomtype;
795  int qprefer; /* quantifier short/long preference */
796  int f;
797  struct subre **atomp; /* where the pointer to atom is */
798 
799  /* initial bookkeeping */
800  atom = NULL;
801  assert(lp->nouts == 0); /* must string new code */
802  assert(rp->nins == 0); /* between lp and rp */
803  subno = 0; /* just to shut lint up */
804 
805  /* an atom or constraint... */
806  atomtype = v->nexttype;
807  switch (atomtype)
808  {
809  /* first, constraints, which end by returning */
810  case '^':
811  ARCV('^', 1);
812  if (v->cflags & REG_NLANCH)
813  ARCV(BEHIND, v->nlcolor);
814  NEXT();
815  return;
816  break;
817  case '$':
818  ARCV('$', 1);
819  if (v->cflags & REG_NLANCH)
820  ARCV(AHEAD, v->nlcolor);
821  NEXT();
822  return;
823  break;
824  case SBEGIN:
825  ARCV('^', 1); /* BOL */
826  ARCV('^', 0); /* or BOS */
827  NEXT();
828  return;
829  break;
830  case SEND:
831  ARCV('$', 1); /* EOL */
832  ARCV('$', 0); /* or EOS */
833  NEXT();
834  return;
835  break;
836  case '<':
837  wordchrs(v); /* does NEXT() */
838  s = newstate(v->nfa);
839  NOERR();
840  nonword(v, BEHIND, lp, s);
841  word(v, AHEAD, s, rp);
842  return;
843  break;
844  case '>':
845  wordchrs(v); /* does NEXT() */
846  s = newstate(v->nfa);
847  NOERR();
848  word(v, BEHIND, lp, s);
849  nonword(v, AHEAD, s, rp);
850  return;
851  break;
852  case WBDRY:
853  wordchrs(v); /* does NEXT() */
854  s = newstate(v->nfa);
855  NOERR();
856  nonword(v, BEHIND, lp, s);
857  word(v, AHEAD, s, rp);
858  s = newstate(v->nfa);
859  NOERR();
860  word(v, BEHIND, lp, s);
861  nonword(v, AHEAD, s, rp);
862  return;
863  break;
864  case NWBDRY:
865  wordchrs(v); /* does NEXT() */
866  s = newstate(v->nfa);
867  NOERR();
868  word(v, BEHIND, lp, s);
869  word(v, AHEAD, s, rp);
870  s = newstate(v->nfa);
871  NOERR();
872  nonword(v, BEHIND, lp, s);
873  nonword(v, AHEAD, s, rp);
874  return;
875  break;
876  case LACON: /* lookaround constraint */
877  latype = v->nextvalue;
878  NEXT();
879  s = newstate(v->nfa);
880  s2 = newstate(v->nfa);
881  NOERR();
882  t = parse(v, ')', LACON, s, s2);
883  freesubre(v, t); /* internal structure irrelevant */
884  NOERR();
885  assert(SEE(')'));
886  NEXT();
887  processlacon(v, s, s2, latype, lp, rp);
888  return;
889  break;
890  /* then errors, to get them out of the way */
891  case '*':
892  case '+':
893  case '?':
894  case '{':
895  ERR(REG_BADRPT);
896  return;
897  break;
898  default:
899  ERR(REG_ASSERT);
900  return;
901  break;
902  /* then plain characters, and minor variants on that theme */
903  case ')': /* unbalanced paren */
904  if ((v->cflags & REG_ADVANCED) != REG_EXTENDED)
905  {
906  ERR(REG_EPAREN);
907  return;
908  }
909  /* legal in EREs due to specification botch */
910  NOTE(REG_UPBOTCH);
911  /* fall through into case PLAIN */
912  /* FALLTHROUGH */
913  case PLAIN:
914  onechr(v, v->nextvalue, lp, rp);
915  okcolors(v->nfa, v->cm);
916  NOERR();
917  NEXT();
918  break;
919  case '[':
920  if (v->nextvalue == 1)
921  bracket(v, lp, rp);
922  else
923  cbracket(v, lp, rp);
924  assert(SEE(']') || ISERR());
925  NEXT();
926  break;
927  case '.':
928  rainbow(v->nfa, v->cm, PLAIN,
929  (v->cflags & REG_NLSTOP) ? v->nlcolor : COLORLESS,
930  lp, rp);
931  NEXT();
932  break;
933  /* and finally the ugly stuff */
934  case '(': /* value flags as capturing or non */
935  cap = (type == LACON) ? 0 : v->nextvalue;
936  if (cap)
937  {
938  v->nsubexp++;
939  subno = v->nsubexp;
940  if ((size_t) subno >= v->nsubs)
941  moresubs(v, subno);
942  assert((size_t) subno < v->nsubs);
943  }
944  else
945  atomtype = PLAIN; /* something that's not '(' */
946  NEXT();
947  /* need new endpoints because tree will contain pointers */
948  s = newstate(v->nfa);
949  s2 = newstate(v->nfa);
950  NOERR();
951  EMPTYARC(lp, s);
952  EMPTYARC(s2, rp);
953  NOERR();
954  atom = parse(v, ')', type, s, s2);
955  assert(SEE(')') || ISERR());
956  NEXT();
957  NOERR();
958  if (cap)
959  {
960  v->subs[subno] = atom;
961  t = subre(v, '(', atom->flags | CAP, lp, rp);
962  NOERR();
963  t->subno = subno;
964  t->left = atom;
965  atom = t;
966  }
967  /* postpone everything else pending possible {0} */
968  break;
969  case BACKREF: /* the Feature From The Black Lagoon */
971  INSIST(v->nextvalue < v->nsubs, REG_ESUBREG);
972  INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG);
973  NOERR();
974  assert(v->nextvalue > 0);
975  atom = subre(v, 'b', BACKR, lp, rp);
976  NOERR();
977  subno = v->nextvalue;
978  atom->subno = subno;
979  EMPTYARC(lp, rp); /* temporarily, so there's something */
980  NEXT();
981  break;
982  }
983 
984  /* ...and an atom may be followed by a quantifier */
985  switch (v->nexttype)
986  {
987  case '*':
988  m = 0;
989  n = DUPINF;
990  qprefer = (v->nextvalue) ? LONGER : SHORTER;
991  NEXT();
992  break;
993  case '+':
994  m = 1;
995  n = DUPINF;
996  qprefer = (v->nextvalue) ? LONGER : SHORTER;
997  NEXT();
998  break;
999  case '?':
1000  m = 0;
1001  n = 1;
1002  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1003  NEXT();
1004  break;
1005  case '{':
1006  NEXT();
1007  m = scannum(v);
1008  if (EAT(','))
1009  {
1010  if (SEE(DIGIT))
1011  n = scannum(v);
1012  else
1013  n = DUPINF;
1014  if (m > n)
1015  {
1016  ERR(REG_BADBR);
1017  return;
1018  }
1019  /* {m,n} exercises preference, even if it's {m,m} */
1020  qprefer = (v->nextvalue) ? LONGER : SHORTER;
1021  }
1022  else
1023  {
1024  n = m;
1025  /* {m} passes operand's preference through */
1026  qprefer = 0;
1027  }
1028  if (!SEE('}'))
1029  { /* catches errors too */
1030  ERR(REG_BADBR);
1031  return;
1032  }
1033  NEXT();
1034  break;
1035  default: /* no quantifier */
1036  m = n = 1;
1037  qprefer = 0;
1038  break;
1039  }
1040 
1041  /* annoying special case: {0} or {0,0} cancels everything */
1042  if (m == 0 && n == 0)
1043  {
1044  if (atom != NULL)
1045  freesubre(v, atom);
1046  if (atomtype == '(')
1047  v->subs[subno] = NULL;
1048  delsub(v->nfa, lp, rp);
1049  EMPTYARC(lp, rp);
1050  return;
1051  }
1052 
1053  /* if not a messy case, avoid hard part */
1054  assert(!MESSY(top->flags));
1055  f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
1056  if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f)))
1057  {
1058  if (!(m == 1 && n == 1))
1059  repeat(v, lp, rp, m, n);
1060  if (atom != NULL)
1061  freesubre(v, atom);
1062  top->flags = f;
1063  return;
1064  }
1065 
1066  /*
1067  * hard part: something messy
1068  *
1069  * That is, capturing parens, back reference, short/long clash, or an atom
1070  * with substructure containing one of those.
1071  */
1072 
1073  /* now we'll need a subre for the contents even if they're boring */
1074  if (atom == NULL)
1075  {
1076  atom = subre(v, '=', 0, lp, rp);
1077  NOERR();
1078  }
1079 
1080  /*----------
1081  * Prepare a general-purpose state skeleton.
1082  *
1083  * In the no-backrefs case, we want this:
1084  *
1085  * [lp] ---> [s] ---prefix---> [begin] ---atom---> [end] ---rest---> [rp]
1086  *
1087  * where prefix is some repetitions of atom. In the general case we need
1088  *
1089  * [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp]
1090  *
1091  * where the iterator wraps around [begin] ---atom---> [end]
1092  *
1093  * We make the s state here for both cases; s2 is made below if needed
1094  *----------
1095  */
1096  s = newstate(v->nfa); /* first, new endpoints for the atom */
1097  s2 = newstate(v->nfa);
1098  NOERR();
1099  moveouts(v->nfa, lp, s);
1100  moveins(v->nfa, rp, s2);
1101  NOERR();
1102  atom->begin = s;
1103  atom->end = s2;
1104  s = newstate(v->nfa); /* set up starting state */
1105  NOERR();
1106  EMPTYARC(lp, s);
1107  NOERR();
1108 
1109  /* break remaining subRE into x{...} and what follows */
1110  t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
1111  NOERR();
1112  t->left = atom;
1113  atomp = &t->left;
1114 
1115  /* here we should recurse... but we must postpone that to the end */
1116 
1117  /* split top into prefix and remaining */
1118  assert(top->op == '=' && top->left == NULL && top->right == NULL);
1119  top->left = subre(v, '=', top->flags, top->begin, lp);
1120  NOERR();
1121  top->op = '.';
1122  top->right = t;
1123 
1124  /* if it's a backref, now is the time to replicate the subNFA */
1125  if (atomtype == BACKREF)
1126  {
1127  assert(atom->begin->nouts == 1); /* just the EMPTY */
1128  delsub(v->nfa, atom->begin, atom->end);
1129  assert(v->subs[subno] != NULL);
1130 
1131  /*
1132  * And here's why the recursion got postponed: it must wait until the
1133  * skeleton is filled in, because it may hit a backref that wants to
1134  * copy the filled-in skeleton.
1135  */
1136  dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
1137  atom->begin, atom->end);
1138  NOERR();
1139  }
1140 
1141  /*
1142  * It's quantifier time. If the atom is just a backref, we'll let it deal
1143  * with quantifiers internally.
1144  */
1145  if (atomtype == BACKREF)
1146  {
1147  /* special case: backrefs have internal quantifiers */
1148  EMPTYARC(s, atom->begin); /* empty prefix */
1149  /* just stuff everything into atom */
1150  repeat(v, atom->begin, atom->end, m, n);
1151  atom->min = (short) m;
1152  atom->max = (short) n;
1153  atom->flags |= COMBINE(qprefer, atom->flags);
1154  /* rest of branch can be strung starting from atom->end */
1155  s2 = atom->end;
1156  }
1157  else if (m == 1 && n == 1 &&
1158  (qprefer == 0 ||
1159  (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
1160  qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
1161  {
1162  /* no/vacuous quantifier: done */
1163  EMPTYARC(s, atom->begin); /* empty prefix */
1164  /* rest of branch can be strung starting from atom->end */
1165  s2 = atom->end;
1166  }
1167  else if (m > 0 && !(atom->flags & BACKR))
1168  {
1169  /*
1170  * If there's no backrefs involved, we can turn x{m,n} into
1171  * x{m-1,n-1}x, with capturing parens in only the second x. This is
1172  * valid because we only care about capturing matches from the final
1173  * iteration of the quantifier. It's a win because we can implement
1174  * the backref-free left side as a plain DFA node, since we don't
1175  * really care where its submatches are.
1176  */
1177  dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
1178  assert(m >= 1 && m != DUPINF && n >= 1);
1179  repeat(v, s, atom->begin, m - 1, (n == DUPINF) ? n : n - 1);
1180  f = COMBINE(qprefer, atom->flags);
1181  t = subre(v, '.', f, s, atom->end); /* prefix and atom */
1182  NOERR();
1183  t->left = subre(v, '=', PREF(f), s, atom->begin);
1184  NOERR();
1185  t->right = atom;
1186  *atomp = t;
1187  /* rest of branch can be strung starting from atom->end */
1188  s2 = atom->end;
1189  }
1190  else
1191  {
1192  /* general case: need an iteration node */
1193  s2 = newstate(v->nfa);
1194  NOERR();
1195  moveouts(v->nfa, atom->end, s2);
1196  NOERR();
1197  dupnfa(v->nfa, atom->begin, atom->end, s, s2);
1198  repeat(v, s, s2, m, n);
1199  f = COMBINE(qprefer, atom->flags);
1200  t = subre(v, '*', f, s, s2);
1201  NOERR();
1202  t->min = (short) m;
1203  t->max = (short) n;
1204  t->left = atom;
1205  *atomp = t;
1206  /* rest of branch is to be strung from iteration's end state */
1207  }
1208 
1209  /* and finally, look after that postponed recursion */
1210  t = top->right;
1211  if (!(SEE('|') || SEE(stopper) || SEE(EOS)))
1212  t->right = parsebranch(v, stopper, type, s2, rp, 1);
1213  else
1214  {
1215  EMPTYARC(s2, rp);
1216  t->right = subre(v, '=', 0, s2, rp);
1217  }
1218  NOERR();
1219  assert(SEE('|') || SEE(stopper) || SEE(EOS));
1220  t->flags |= COMBINE(t->flags, t->right->flags);
1221  top->flags |= COMBINE(top->flags, t->flags);
1222 }
#define EAT(t)
Definition: regcomp.c:260
static int scannum(struct vars *)
Definition: regcomp.c:1260
#define REG_ESUBREG
Definition: regex.h:143
int nexttype
Definition: regcomp.c:234
#define REG_NLSTOP
Definition: regex.h:109
static void okcolors(struct nfa *, struct colormap *)
chr nextvalue
Definition: regcomp.c:235
#define REG_EPAREN
Definition: regex.h:145
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1377
static void nonword(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1228
struct subre * right
Definition: regguts.h:432
int subno
Definition: regguts.h:427
#define NEXT()
Definition: regcomp.c:258
#define MESSY(f)
Definition: regguts.h:422
int nins
Definition: regguts.h:303
color nlcolor
Definition: regcomp.c:243
char op
Definition: regguts.h:410
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:1689
short min
Definition: regguts.h:429
static void onechr(struct vars *, chr, struct state *, struct state *)
Definition: regcomp.c:1564
static void wordchrs(struct vars *)
Definition: regcomp.c:1591
struct state * begin
Definition: regguts.h:433
#define COMBINE(f1, f2)
Definition: regguts.h:425
#define ISERR()
Definition: regcomp.c:262
#define BEHIND
Definition: regcomp.c:286
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
int nsubexp
Definition: regcomp.c:237
#define ERR(e)
Definition: regcomp.c:265
struct colormap * cm
Definition: regcomp.c:242
static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *)
#define LACON
Definition: regcomp.c:284
#define REG_BADBR
Definition: regex.h:147
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1741
#define REG_BADRPT
Definition: regex.h:150
static void processlacon(struct vars *, struct state *, struct state *, int, struct state *, struct state *)
Definition: regcomp.c:1623
char flags
Definition: regguts.h:411
struct subre ** subs
Definition: regcomp.c:238
#define assert(TEST)
Definition: imath.c:73
static void moveins(struct nfa *, struct state *, struct state *)
static void moveouts(struct nfa *, struct state *, struct state *)
#define REG_ADVANCED
Definition: regex.h:103
#define SEE(t)
Definition: regcomp.c:259
struct subre * left
Definition: regguts.h:431
size_t nsubs
Definition: regcomp.c:239
static struct state * newstate(struct nfa *)
#define SBEGIN
Definition: regcomp.c:289
static void delsub(struct nfa *, struct state *, struct state *)
#define BACKREF
Definition: regcomp.c:278
#define REG_EXTENDED
Definition: regex.h:101
struct nfa * nfa
Definition: regcomp.c:241
#define PREF(f)
Definition: regguts.h:423
char * s2
#define PLAIN
Definition: regcomp.c:276
#define EMPTYARC(x, y)
Definition: regcomp.c:271
#define BACKR
Definition: regguts.h:416
static struct subre * parsebranch(struct vars *, int, int, struct state *, struct state *, int)
Definition: regcomp.c:727
#define EOS
Definition: regcomp.c:275
static void cbracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1396
#define NWBDRY
Definition: regcomp.c:288
#define DUPINF
Definition: regguts.h:94
#define REG_ASSERT
Definition: regex.h:151
#define MIXED
Definition: regguts.h:414
Definition: regguts.h:298
short max
Definition: regguts.h:430
#define REG_NLANCH
Definition: regex.h:110
struct state * end
Definition: regguts.h:434
Definition: regguts.h:408
#define NOTE(b)
Definition: regcomp.c:270
#define LONGER
Definition: regguts.h:412
static void repeat(struct vars *, struct state *, struct state *, int, int)
Definition: regcomp.c:1291
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1246
int nouts
Definition: regguts.h:305
#define CAP
Definition: regguts.h:415
#define NOERR()
Definition: regcomp.c:266
#define UP(f)
Definition: regguts.h:421
#define SEND
Definition: regcomp.c:290
int cflags
Definition: regcomp.c:232
#define ARCV(t, val)
#define SHORTER
Definition: regguts.h:413
#define COLORLESS
Definition: regguts.h:137
#define WBDRY
Definition: regcomp.c:287
#define INSIST(c, e)
Definition: regcomp.c:269
static void moresubs(struct vars *, int)
Definition: regcomp.c:493
#define DIGIT
Definition: regcomp.c:277
#define AHEAD
Definition: regcomp.c:285
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648
#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 313 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, 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, vars::savenow, vars::savestop, guts::search, SEE, SHORTER, vars::spaceused, specialcolors(), generate_unaccent_rules::stdout, vars::stop, vars::sub10, subcolor(), subre::subno, vars::subs, vars::tree, guts::tree, vars::treechain, vars::treefree, VS, vars::wordchrs, and ZAPCNFA.

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

318 {
319  struct vars var;
320  struct vars *v = &var;
321  struct guts *g;
322  int i;
323  size_t j;
324 
325 #ifdef REG_DEBUG
326  FILE *debug = (flags & REG_PROGRESS) ? stdout : (FILE *) NULL;
327 #else
328  FILE *debug = (FILE *) NULL;
329 #endif
330 
331 #define CNOERR() { if (ISERR()) return freev(v, v->err); }
332 
333  /* sanity checks */
334 
335  if (re == NULL || string == NULL)
336  return REG_INVARG;
337  if ((flags & REG_QUOTE) &&
338  (flags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)))
339  return REG_INVARG;
340  if (!(flags & REG_EXTENDED) && (flags & REG_ADVF))
341  return REG_INVARG;
342 
343  /* Initialize locale-dependent support */
344  pg_set_regex_collation(collation);
345 
346  /* initial setup (after which freev() is callable) */
347  v->re = re;
348  v->now = string;
349  v->stop = v->now + len;
350  v->savenow = v->savestop = NULL;
351  v->err = 0;
352  v->cflags = flags;
353  v->nsubexp = 0;
354  v->subs = v->sub10;
355  v->nsubs = 10;
356  for (j = 0; j < v->nsubs; j++)
357  v->subs[j] = NULL;
358  v->nfa = NULL;
359  v->cm = NULL;
360  v->nlcolor = COLORLESS;
361  v->wordchrs = NULL;
362  v->tree = NULL;
363  v->treechain = NULL;
364  v->treefree = NULL;
365  v->cv = NULL;
366  v->cv2 = NULL;
367  v->lacons = NULL;
368  v->nlacons = 0;
369  v->spaceused = 0;
370  re->re_magic = REMAGIC;
371  re->re_info = 0; /* bits get set during parse */
372  re->re_csize = sizeof(chr);
373  re->re_collation = collation;
374  re->re_guts = NULL;
375  re->re_fns = VS(&functions);
376 
377  /* more complex setup, malloced things */
378  re->re_guts = VS(MALLOC(sizeof(struct guts)));
379  if (re->re_guts == NULL)
380  return freev(v, REG_ESPACE);
381  g = (struct guts *) re->re_guts;
382  g->tree = NULL;
383  initcm(v, &g->cmap);
384  v->cm = &g->cmap;
385  g->lacons = NULL;
386  g->nlacons = 0;
387  ZAPCNFA(g->search);
388  v->nfa = newnfa(v, v->cm, (struct nfa *) NULL);
389  CNOERR();
390  /* set up a reasonably-sized transient cvec for getcvec usage */
391  v->cv = newcvec(100, 20);
392  if (v->cv == NULL)
393  return freev(v, REG_ESPACE);
394 
395  /* parsing */
396  lexstart(v); /* also handles prefixes */
397  if ((v->cflags & REG_NLSTOP) || (v->cflags & REG_NLANCH))
398  {
399  /* assign newline a unique color */
400  v->nlcolor = subcolor(v->cm, newline());
401  okcolors(v->nfa, v->cm);
402  }
403  CNOERR();
404  v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
405  assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */
406  CNOERR();
407  assert(v->tree != NULL);
408 
409  /* finish setup of nfa and its subre tree */
410  specialcolors(v->nfa);
411  CNOERR();
412 #ifdef REG_DEBUG
413  if (debug != NULL)
414  {
415  fprintf(debug, "\n\n\n========= RAW ==========\n");
416  dumpnfa(v->nfa, debug);
417  dumpst(v->tree, debug, 1);
418  }
419 #endif
420  optst(v, v->tree);
421  v->ntree = numst(v->tree, 1);
422  markst(v->tree);
423  cleanst(v);
424 #ifdef REG_DEBUG
425  if (debug != NULL)
426  {
427  fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
428  dumpst(v->tree, debug, 1);
429  }
430 #endif
431 
432  /* build compacted NFAs for tree and lacons */
433  re->re_info |= nfatree(v, v->tree, debug);
434  CNOERR();
435  assert(v->nlacons == 0 || v->lacons != NULL);
436  for (i = 1; i < v->nlacons; i++)
437  {
438  struct subre *lasub = &v->lacons[i];
439 
440 #ifdef REG_DEBUG
441  if (debug != NULL)
442  fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
443 #endif
444 
445  /* Prepend .* to pattern if it's a lookbehind LACON */
446  nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->subno), debug);
447  }
448  CNOERR();
449  if (v->tree->flags & SHORTER)
451 
452  /* build compacted NFAs for tree, lacons, fast search */
453 #ifdef REG_DEBUG
454  if (debug != NULL)
455  fprintf(debug, "\n\n\n========= SEARCH ==========\n");
456 #endif
457  /* can sacrifice main NFA now, so use it as work area */
458  (DISCARD) optimize(v->nfa, debug);
459  CNOERR();
460  makesearch(v, v->nfa);
461  CNOERR();
462  compact(v->nfa, &g->search);
463  CNOERR();
464 
465  /* looks okay, package it up */
466  re->re_nsub = v->nsubexp;
467  v->re = NULL; /* freev no longer frees re */
468  g->magic = GUTSMAGIC;
469  g->cflags = v->cflags;
470  g->info = re->re_info;
471  g->nsub = re->re_nsub;
472  g->tree = v->tree;
473  v->tree = NULL;
474  g->ntree = v->ntree;
475  g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
476  g->lacons = v->lacons;
477  v->lacons = NULL;
478  g->nlacons = v->nlacons;
479 
480 #ifdef REG_DEBUG
481  if (flags & REG_DUMP)
482  dump(re, stdout);
483 #endif
484 
485  assert(v->err == 0);
486  return freev(v, 0);
487 }
#define REG_USHORTEST
Definition: regex.h:73
static chr newline(void)
struct subre * treefree
Definition: regcomp.c:247
static void optst(struct vars *, struct subre *)
Definition: regcomp.c:1783
#define REG_NLSTOP
Definition: regex.h:109
char * re_fns
Definition: regex.h:79
static void okcolors(struct nfa *, struct colormap *)
static int numst(struct subre *, int)
Definition: regcomp.c:1799
static long optimize(struct nfa *, FILE *)
int ntree
Definition: regcomp.c:248
struct subre * lacons
Definition: regguts.h:474
struct subre * tree
Definition: regguts.h:469
struct state * final
Definition: regguts.h:319
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:1867
struct subre * treechain
Definition: regcomp.c:246
#define REG_ESPACE
Definition: regex.h:149
int subno
Definition: regguts.h:427
static int freev(struct vars *, int)
Definition: regcomp.c:530
static void specialcolors(struct nfa *)
#define REG_QUOTE
Definition: regex.h:104
const chr * savenow
Definition: regcomp.c:229
static struct cvec * newcvec(int, int)
color nlcolor
Definition: regcomp.c:243
int nlacons
Definition: regguts.h:475
#define REMAGIC
Definition: regguts.h:96
static color subcolor(struct colormap *, chr)
Definition: regguts.h:315
#define fprintf
Definition: port.h:196
#define REG_ICASE
Definition: regex.h:106
int re_csize
Definition: regex.h:74
static void makesearch(struct vars *, struct nfa *)
Definition: regcomp.c:559
int nsubexp
Definition: regcomp.c:237
#define MALLOC(n)
Definition: regcustom.h:62
#define REG_PROGRESS
Definition: regex.h:117
int re_magic
Definition: regex.h:57
struct cnfa search
Definition: regguts.h:470
pg_wchar chr
Definition: regcustom.h:68
size_t re_nsub
Definition: regex.h:58
struct colormap * cm
Definition: regcomp.c:242
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
static void dumpnfa(struct nfa *, FILE *)
bool debug
Definition: pg_standby.c:48
regex_t * re
Definition: regcomp.c:226
int nlacons
Definition: regcomp.c:252
int magic
Definition: regguts.h:464
#define REG_INVARG
Definition: regex.h:152
static void markst(struct subre *)
Definition: regcomp.c:1833
char flags
Definition: regguts.h:411
struct subre ** subs
Definition: regcomp.c:238
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
char string[11]
Definition: preproc-type.c:46
struct colormap cmap
Definition: regguts.h:472
#define assert(TEST)
Definition: imath.c:73
#define LATYPE_IS_AHEAD(la)
Definition: regguts.h:104
#define REG_NEWLINE
Definition: regex.h:111
#define REG_ADVANCED
Definition: regex.h:103
#define SEE(t)
Definition: regcomp.c:259
size_t nsubs
Definition: regcomp.c:239
int err
Definition: regcomp.c:231
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:1887
struct nfa * nfa
Definition: regcomp.c:241
#define REG_EXTENDED
Definition: regex.h:101
struct cvec * cv
Definition: regcomp.c:249
struct cvec * cv2
Definition: regcomp.c:250
#define REG_ADVF
Definition: regex.h:102
const chr * savestop
Definition: regcomp.c:230
#define PLAIN
Definition: regcomp.c:276
struct state * wordchrs
Definition: regcomp.c:244
long re_info
Definition: regex.h:59
#define REG_DUMP
Definition: regex.h:115
Definition: regguts.h:462
int ntree
Definition: regguts.h:471
#define EOS
Definition: regcomp.c:275
#define GUTSMAGIC
Definition: regguts.h:465
void pg_set_regex_collation(Oid collation)
const chr * now
Definition: regcomp.c:227
#define REG_NLANCH
Definition: regex.h:110
static const struct fns functions
Definition: regcomp.c:298
#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:467
Definition: regguts.h:408
#define NOTE(b)
Definition: regcomp.c:270
#define CNOERR()
const chr * stop
Definition: regcomp.c:228
char * re_guts
Definition: regex.h:78
int cflags
Definition: regguts.h:466
struct state * init
Definition: regguts.h:318
int cflags
Definition: regcomp.c:232
size_t nsub
Definition: regguts.h:468
static void lexstart(struct vars *)
static int casecmp(const chr *, const chr *, size_t)
#define SHORTER
Definition: regguts.h:413
int i
struct subre * sub10[10]
Definition: regcomp.c:240
#define COLORLESS
Definition: regguts.h:137
#define REG_EXPANDED
Definition: regex.h:108
#define DISCARD
Definition: regguts.h:58
struct subre * tree
Definition: regcomp.c:245
static void cleanst(struct vars *)
Definition: regcomp.c:1848
size_t spaceused
Definition: regcomp.c:254
struct subre * lacons
Definition: regcomp.c:251
Definition: regcomp.c:224
Oid re_collation
Definition: regex.h:76
static void compact(struct nfa *, struct cnfa *)
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

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

1629 {
1630  struct state *s1;
1631  int n;
1632 
1633  /*
1634  * Check for lookaround RE consisting of a single plain color arc (or set
1635  * of arcs); this would typically be a simple chr or a bracket expression.
1636  */
1637  s1 = single_color_transition(begin, end);
1638  switch (latype)
1639  {
1640  case LATYPE_AHEAD_POS:
1641  /* If lookahead RE is just colorset C, convert to AHEAD(C) */
1642  if (s1 != NULL)
1643  {
1644  cloneouts(v->nfa, s1, lp, rp, AHEAD);
1645  return;
1646  }
1647  break;
1648  case LATYPE_AHEAD_NEG:
1649  /* If lookahead RE is just colorset C, convert to AHEAD(^C)|$ */
1650  if (s1 != NULL)
1651  {
1652  colorcomplement(v->nfa, v->cm, AHEAD, s1, lp, rp);
1653  newarc(v->nfa, '$', 1, lp, rp);
1654  newarc(v->nfa, '$', 0, lp, rp);
1655  return;
1656  }
1657  break;
1658  case LATYPE_BEHIND_POS:
1659  /* If lookbehind RE is just colorset C, convert to BEHIND(C) */
1660  if (s1 != NULL)
1661  {
1662  cloneouts(v->nfa, s1, lp, rp, BEHIND);
1663  return;
1664  }
1665  break;
1666  case LATYPE_BEHIND_NEG:
1667  /* If lookbehind RE is just colorset C, convert to BEHIND(^C)|^ */
1668  if (s1 != NULL)
1669  {
1670  colorcomplement(v->nfa, v->cm, BEHIND, s1, lp, rp);
1671  newarc(v->nfa, '^', 1, lp, rp);
1672  newarc(v->nfa, '^', 0, lp, rp);
1673  return;
1674  }
1675  break;
1676  default:
1677  assert(NOTREACHED);
1678  }
1679 
1680  /* General case: we need a LACON subre and arc */
1681  n = newlacon(v, begin, end, latype);
1682  newarc(v->nfa, LACON, n, lp, rp);
1683 }
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:286
#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:242
char * s1
#define LACON
Definition: regcomp.c:284
#define assert(TEST)
Definition: imath.c:73
#define LATYPE_BEHIND_NEG
Definition: regguts.h:102
struct nfa * nfa
Definition: regcomp.c:241
Definition: regguts.h:298
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:285
static int newlacon(struct vars *, struct state *, struct state *, int)
Definition: regcomp.c:1926
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 2018 of file regcomp.c.

References InterruptPending, ProcDiePending, and QueryCancelPending.

2019 {
2021 }
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

◆ repeat()

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

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

1296 {
1297 #define SOME 2
1298 #define INF 3
1299 #define PAIR(x, y) ((x)*4 + (y))
1300 #define REDUCE(x) ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
1301  const int rm = REDUCE(m);
1302  const int rn = REDUCE(n);
1303  struct state *s;
1304  struct state *s2;
1305 
1306  switch (PAIR(rm, rn))
1307  {
1308  case PAIR(0, 0): /* empty string */
1309  delsub(v->nfa, lp, rp);
1310  EMPTYARC(lp, rp);
1311  break;
1312  case PAIR(0, 1): /* do as x| */
1313  EMPTYARC(lp, rp);
1314  break;
1315  case PAIR(0, SOME): /* do as x{1,n}| */
1316  repeat(v, lp, rp, 1, n);
1317  NOERR();
1318  EMPTYARC(lp, rp);
1319  break;
1320  case PAIR(0, INF): /* loop x around */
1321  s = newstate(v->nfa);
1322  NOERR();
1323  moveouts(v->nfa, lp, s);
1324  moveins(v->nfa, rp, s);
1325  EMPTYARC(lp, s);
1326  EMPTYARC(s, rp);
1327  break;
1328  case PAIR(1, 1): /* no action required */
1329  break;
1330  case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */
1331  s = newstate(v->nfa);
1332  NOERR();
1333  moveouts(v->nfa, lp, s);
1334  dupnfa(v->nfa, s, rp, lp, s);
1335  NOERR();
1336  repeat(v, lp, s, 1, n - 1);
1337  NOERR();
1338  EMPTYARC(lp, s);
1339  break;
1340  case PAIR(1, INF): /* add loopback arc */
1341  s = newstate(v->nfa);
1342  s2 = newstate(v->nfa);
1343  NOERR();
1344  moveouts(v->nfa, lp, s);
1345  moveins(v->nfa, rp, s2);
1346  EMPTYARC(lp, s);
1347  EMPTYARC(s2, rp);
1348  EMPTYARC(s2, s);
1349  break;
1350  case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */
1351  s = newstate(v->nfa);
1352  NOERR();
1353  moveouts(v->nfa, lp, s);
1354  dupnfa(v->nfa, s, rp, lp, s);
1355  NOERR();
1356  repeat(v, lp, s, m - 1, n - 1);
1357  break;
1358  case PAIR(SOME, INF): /* do as x{m-1,}x */
1359  s = newstate(v->nfa);
1360  NOERR();
1361  moveouts(v->nfa, lp, s);
1362  dupnfa(v->nfa, s, rp, lp, s);
1363  NOERR();
1364  repeat(v, lp, s, m - 1, n);
1365  break;
1366  default:
1367  ERR(REG_ASSERT);
1368  break;
1369  }
1370 }
#define PAIR(x, y)
#define REDUCE(x)
Definition: regguts.h:315
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
#define SOME
#define ERR(e)
Definition: regcomp.c:265
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:241
char * s2
#define EMPTYARC(x, y)
Definition: regcomp.c:271
#define REG_ASSERT
Definition: regex.h:151
Definition: regguts.h:298
static void repeat(struct vars *, struct state *, struct state *, int, int)
Definition: regcomp.c:1291
#define NOERR()
Definition: regcomp.c:266
#define INF

◆ rfree()

static void rfree ( regex_t re)
static

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

1983 {
1984  struct guts *g;
1985 
1986  if (re == NULL || re->re_magic != REMAGIC)
1987  return;
1988 
1989  re->re_magic = 0; /* invalidate RE */
1990  g = (struct guts *) re->re_guts;
1991  re->re_guts = NULL;
1992  re->re_fns = NULL;
1993  if (g != NULL)
1994  {
1995  g->magic = 0;
1996  freecm(&g->cmap);
1997  if (g->tree != NULL)
1998  freesubre((struct vars *) NULL, g->tree);
1999  if (g->lacons != NULL)
2000  freelacons(g->lacons, g->nlacons);
2001  if (!NULLCNFA(g->search))
2002  freecnfa(&g->search);
2003  FREE(g);
2004  }
2005 }
char * re_fns
Definition: regex.h:79
static void freelacons(struct subre *, int)
Definition: regcomp.c:1965
struct subre * lacons
Definition: regguts.h:474
struct subre * tree
Definition: regguts.h:469
static void freecm(struct colormap *)
#define NULLCNFA(cnfa)
Definition: regguts.h:372
int nlacons
Definition: regguts.h:475
#define REMAGIC
Definition: regguts.h:96
int re_magic
Definition: regex.h:57
struct cnfa search
Definition: regguts.h:470
int magic
Definition: regguts.h:464
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1741
struct colormap cmap
Definition: regguts.h:472
Definition: regguts.h:462
char * re_guts
Definition: regex.h:78
static void freecnfa(struct cnfa *)
Definition: regcomp.c:224
#define FREE(size)
Definition: saslprep.c:51

◆ rstacktoodeep()

◆ scannum()

static int scannum ( struct vars v)
static

Definition at line 1260 of file regcomp.c.

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

Referenced by parseqatom().

1261 {
1262  int n = 0;
1263 
1264  while (SEE(DIGIT) && n < DUPMAX)
1265  {
1266  n = n * 10 + v->nextvalue;
1267  NEXT();
1268  }
1269  if (SEE(DIGIT) || n > DUPMAX)
1270  {
1271  ERR(REG_BADBR);
1272  return 0;
1273  }
1274  return n;
1275 }
chr nextvalue
Definition: regcomp.c:235
#define DUPMAX
Definition: regguts.h:93
#define NEXT()
Definition: regcomp.c:258
#define ERR(e)
Definition: regcomp.c:265
#define REG_BADBR
Definition: regex.h:147
#define SEE(t)
Definition: regcomp.c:259
#define DIGIT
Definition: regcomp.c:277

◆ scanplain()

static const chr * scanplain ( struct vars v)
static

Definition at line 1539 of file regcomp.c.

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

Referenced by brackpart().

1540 {
1541  const chr *endp;
1542 
1543  assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
1544  NEXT();
1545 
1546  endp = v->now;
1547  while (SEE(PLAIN))
1548  {
1549  endp = v->now;
1550  NEXT();
1551  }
1552 
1553  assert(SEE(END) || ISERR());
1554  NEXT();
1555 
1556  return endp;
1557 }
#define END
Definition: regcomp.c:282
#define CCLASS
Definition: regcomp.c:281
#define NEXT()
Definition: regcomp.c:258
#define ISERR()
Definition: regcomp.c:262
pg_wchar chr
Definition: regcustom.h:68
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:259
#define PLAIN
Definition: regcomp.c:276
const chr * now
Definition: regcomp.c:227
#define COLLEL
Definition: regcomp.c:279
#define ECLASS
Definition: regcomp.c:280

◆ 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

Referenced by brackpart(), and onechr().

◆ 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 1689 of file regcomp.c.

References assert, subre::begin, subre::chain, subre::cnfa, subre::end, ERR, subre::flags, subre::id, subre::left, MALLOC, subre::max, subre::min, subre::op, vars::re, REG_ESPACE, REG_ETOOBIG, subre::right, STACK_TOO_DEEP, subre::subno, vars::treechain, vars::treefree, and ZAPCNFA.

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

1694 {
1695  struct subre *ret = v->treefree;
1696 
1697  /*
1698  * Checking for stack overflow here is sufficient to protect parse() and
1699  * its recursive subroutines.
1700  */
1701  if (STACK_TOO_DEEP(v->re))
1702  {
1703  ERR(REG_ETOOBIG);
1704  return NULL;
1705  }
1706 
1707  if (ret != NULL)
1708  v->treefree = ret->left;
1709  else
1710  {
1711  ret = (struct subre *) MALLOC(sizeof(struct subre));
1712  if (ret == NULL)
1713  {
1714  ERR(REG_ESPACE);
1715  return NULL;
1716  }
1717  ret->chain = v->treechain;
1718  v->treechain = ret;
1719  }
1720 
1721  assert(strchr("=b|.*(", op) != NULL);
1722 
1723  ret->op = op;
1724  ret->flags = flags;
1725  ret->id = 0; /* will be assigned later */
1726  ret->subno = 0;
1727  ret->min = ret->max = 1;
1728  ret->left = NULL;
1729  ret->right = NULL;
1730  ret->begin = begin;
1731  ret->end = end;
1732  ZAPCNFA(ret->cnfa);
1733 
1734  return ret;
1735 }
struct subre * treefree
Definition: regcomp.c:247
struct subre * treechain
Definition: regcomp.c:246
struct subre * right
Definition: regguts.h:432
#define REG_ESPACE
Definition: regex.h:149
int subno
Definition: regguts.h:427
char op
Definition: regguts.h:410
short min
Definition: regguts.h:429
struct state * begin
Definition: regguts.h:433
#define MALLOC(n)
Definition: regcustom.h:62
#define ERR(e)
Definition: regcomp.c:265
struct subre * chain
Definition: regguts.h:436
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
regex_t * re
Definition: regcomp.c:226
char flags
Definition: regguts.h:411
#define assert(TEST)
Definition: imath.c:73
struct subre * left
Definition: regguts.h:431
short max
Definition: regguts.h:430
#define REG_ETOOBIG
Definition: regex.h:155
struct state * end
Definition: regguts.h:434
Definition: regguts.h:408
struct cnfa cnfa
Definition: regguts.h:435
short id
Definition: regguts.h:426
#define STACK_TOO_DEEP(re)
Definition: regguts.h:455

◆ 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 1246 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_rightmost_one_pos64(), and unicode_to_utf8word().

1250 {
1251  assert(dir == AHEAD || dir == BEHIND);
1252  cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
1253  /* (no need for special attention to \n) */
1254 }
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
#define BEHIND
Definition: regcomp.c:286
#define assert(TEST)
Definition: imath.c:73
struct nfa * nfa
Definition: regcomp.c:241
struct state * wordchrs
Definition: regcomp.c:244
#define AHEAD
Definition: regcomp.c:285

◆ wordchrs()

static void wordchrs ( struct vars v)
static

Definition at line 1591 of file regcomp.c.

References assert, bracket(), ISERR, lexword(), newstate(), NEXT, vars::nfa, NOERR, vars::savenow, SEE, and vars::wordchrs.

1592 {
1593  struct state *left;
1594  struct state *right;
1595 
1596  if (v->wordchrs != NULL)
1597  {
1598  NEXT(); /* for consistency */
1599  return;
1600  }
1601 
1602  left = newstate(v->nfa);
1603  right = newstate(v->nfa);
1604  NOERR();
1605  /* fine point: implemented with [::], and lexer will set REG_ULOCALE */
1606  lexword(v);
1607  NEXT();
1608  assert(v->savenow != NULL && SEE('['));
1609  bracket(v, left, right);
1610  assert((v->savenow != NULL && SEE(']')) || ISERR());
1611  NEXT();
1612  NOERR();
1613  v->wordchrs = left;
1614 }
static void lexword(struct vars *)
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1377
#define NEXT()
Definition: regcomp.c:258
const chr * savenow
Definition: regcomp.c:229
#define ISERR()
Definition: regcomp.c:262
#define assert(TEST)
Definition: imath.c:73
#define SEE(t)
Definition: regcomp.c:259
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:241
struct state * wordchrs
Definition: regcomp.c:244
Definition: regguts.h:298
#define NOERR()
Definition: regcomp.c:266

Variable Documentation

◆ functions

const struct fns functions
static