PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regcomp.c File Reference
#include "regex/regguts.h"
#include "miscadmin.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 PREFER   'P' /* length preference */
 
#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

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

Definition at line 287 of file regcomp.c.

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

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

Referenced by parseqatom().

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

Definition at line 280 of file regcomp.c.

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

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

Definition at line 288 of file regcomp.c.

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

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

Definition at line 283 of file regcomp.c.

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

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

Referenced by pg_regcomp().

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

Definition at line 281 of file regcomp.c.

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

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

Definition at line 296 of file regcomp.c.

Referenced by createarc(), and freearc().

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

Definition at line 162 of file regcomp.c.

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

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

Definition at line 279 of file regcomp.c.

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

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

Definition at line 262 of file regcomp.c.

Referenced by parse(), and parseqatom().

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

Definition at line 282 of file regcomp.c.

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

#define EMPTY   'n' /* no token present */
#define EMPTYARC (   x,
 
)    newarc(v->nfa, EMPTY, 0, x, y)

Definition at line 273 of file regcomp.c.

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

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

Definition at line 284 of file regcomp.c.

Referenced by scanplain().

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

Definition at line 277 of file regcomp.c.

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

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

Definition at line 267 of file regcomp.c.

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

#define INCOMPATIBLE   1 /* destroys arc */

Definition at line 160 of file regcomp.c.

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

#define INF   3

Referenced by repeat().

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

Definition at line 271 of file regcomp.c.

Referenced by brackpart(), and parseqatom().

#define ISERR ( )    VISERR(v)

Definition at line 264 of file regcomp.c.

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

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

Definition at line 286 of file regcomp.c.

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

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

Definition at line 260 of file regcomp.c.

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

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

Definition at line 269 of file regcomp.c.

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

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

Definition at line 270 of file regcomp.c.

Referenced by nfanode().

#define NOTE (   b)    (v->re->re_info |= (b)) /* note visible condition */
#define NWBDRY   'W' /* non-word-boundary constraint */

Definition at line 290 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

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

Referenced by repeat().

#define PREFER   'P' /* length preference */

Definition at line 293 of file regcomp.c.

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

Definition at line 285 of file regcomp.c.

Referenced by brackpart(), and next().

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

Referenced by repeat().

#define SATISFIED   2 /* constraint satisfied */

Definition at line 161 of file regcomp.c.

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

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

Definition at line 291 of file regcomp.c.

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

#define SEE (   t)    (v->nexttype == (t)) /* is next token this? */
#define SEND   'Z' /* end of string (even if not EOL) */

Definition at line 292 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

#define SOME   2

Referenced by repeat().

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

Definition at line 265 of file regcomp.c.

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

Definition at line 263 of file regcomp.c.

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

Definition at line 289 of file regcomp.c.

Referenced by lexescape(), and parseqatom().

Function Documentation

static void addchr ( struct cvec ,
chr   
)
static
static void addrange ( struct cvec ,
chr  ,
chr   
)
static
static struct cvec* allcases ( struct vars ,
chr   
)
static

Referenced by onechr().

static struct arc* allocarc ( struct nfa ,
struct state  
)
static
static long analyze ( struct nfa )
static
static int before ( chr  ,
chr   
)
static
static void bracket ( struct vars v,
struct state lp,
struct state rp 
)
static

Definition at line 1376 of file regcomp.c.

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

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

1379 {
1380  assert(SEE('['));
1381  NEXT();
1382  while (!SEE(']') && !SEE(EOS))
1383  brackpart(v, lp, rp);
1384  assert(SEE(']') || ISERR());
1385  okcolors(v->nfa, v->cm);
1386 }
static void okcolors(struct nfa *, struct colormap *)
#define NEXT()
Definition: regcomp.c:260
#define ISERR()
Definition: regcomp.c:264
struct colormap * cm
Definition: regcomp.c:244
#define assert(TEST)
Definition: imath.c:37
#define SEE(t)
Definition: regcomp.c:261
struct nfa * nfa
Definition: regcomp.c:243
static void brackpart(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1425
#define EOS
Definition: regcomp.c:277
static void brackpart ( struct vars v,
struct state lp,
struct state rp 
)
static

Definition at line 1425 of file regcomp.c.

References cclass(), CCLASS, vars::cflags, COLLEL, 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().

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

Referenced by pg_regcomp().

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

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

1398 {
1399  struct state *left = newstate(v->nfa);
1400  struct state *right = newstate(v->nfa);
1401 
1402  NOERR();
1403  bracket(v, left, right);
1404  if (v->cflags & REG_NLSTOP)
1405  newarc(v->nfa, PLAIN, v->nlcolor, left, right);
1406  NOERR();
1407 
1408  assert(lp->nouts == 0); /* all outarcs will be ours */
1409 
1410  /*
1411  * Easy part of complementing, and all there is to do since the MCCE code
1412  * was removed.
1413  */
1414  colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
1415  NOERR();
1416  dropstate(v->nfa, left);
1417  assert(right->nins == 0);
1418  freestate(v->nfa, right);
1419 }
#define REG_NLSTOP
Definition: regex.h:109
static void bracket(struct vars *, struct state *, struct state *)
Definition: regcomp.c:1376
int nins
Definition: regguts.h:303
color nlcolor
Definition: regcomp.c:245
struct colormap * cm
Definition: regcomp.c:244
#define assert(TEST)
Definition: imath.c:37
static struct state * newstate(struct nfa *)
struct nfa * nfa
Definition: regcomp.c:243
static void freestate(struct nfa *, struct state *)
#define PLAIN
Definition: regcomp.c:278
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:268
int cflags
Definition: regcomp.c:234
static void dropstate(struct nfa *, struct state *)
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)
static struct cvec* cclass ( struct vars ,
const chr ,
const chr ,
int   
)
static

Referenced by brackpart().

static int cclass_column_index ( struct colormap ,
chr   
)
static
static void changearctarget ( struct arc ,
struct state  
)
static
static chr chrnamed ( struct vars ,
const chr ,
const chr ,
chr   
)
static
static void cleanst ( struct vars v)
static

Definition at line 1847 of file regcomp.c.

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

Referenced by freev(), and pg_regcomp().

1848 {
1849  struct subre *t;
1850  struct subre *next;
1851 
1852  for (t = v->treechain; t != NULL; t = next)
1853  {
1854  next = t->chain;
1855  if (!(t->flags & INUSE))
1856  FREE(t);
1857  }
1858  v->treechain = NULL;
1859  v->treefree = NULL; /* just on general principles */
1860 }
struct subre * treefree
Definition: regcomp.c:249
struct subre * treechain
Definition: regcomp.c:248
#define FREE(p)
Definition: regcustom.h:54
struct subre * chain
Definition: regguts.h:436
static int next(struct vars *)
char flags
Definition: regguts.h:411
#define NULL
Definition: c.h:226
#define INUSE
Definition: regguts.h:417
Definition: regguts.h:408
static void cleanup ( struct nfa )
static
static struct cvec* clearcvec ( struct cvec )
static
static void cleartraverse ( struct nfa ,
struct state  
)
static
static void cloneouts ( struct nfa ,
struct state ,
struct state ,
struct state ,
int   
)
static

Referenced by processlacon(), and word().

static void clonesuccessorstates ( struct nfa ,
struct state ,
struct state ,
struct state ,
struct arc ,
char *  ,
char *  ,
int   
)
static
static int cmp ( const chr ,
const chr ,
size_t   
)
static

Referenced by pg_regcomp().

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

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

static int combine ( struct arc ,
struct arc  
)
static
static void compact ( struct nfa ,
struct cnfa  
)
static

Referenced by nfanode(), and pg_regcomp().

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

Referenced by makesearch().

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

Referenced by makesearch().

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

Referenced by parseqatom(), and repeat().

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

Referenced by cbracket().

static void dumpnfa ( struct nfa ,
FILE *   
)
static

Referenced by pg_regcomp().

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

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

static void duptraverse ( struct nfa ,
struct state ,
struct state  
)
static
static struct cvec* eclass ( struct vars ,
chr  ,
int   
)
static

Referenced by brackpart().

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

Referenced by brackpart().

static struct state* emptyreachable ( struct nfa ,
struct state ,
struct state ,
struct arc **   
)
static
static struct arc* findarc ( struct state ,
int  ,
color   
)
static
static int findconstraintloop ( struct nfa ,
struct state  
)
static
static void fixconstraintloops ( struct nfa ,
FILE *   
)
static
static void fixempties ( struct nfa ,
FILE *   
)
static
static void freearc ( struct nfa ,
struct arc  
)
static

Referenced by makesearch().

static void freecm ( struct colormap )
static

Referenced by rfree().

static void freecnfa ( struct cnfa )
static

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

static void freecolor ( struct colormap ,
color   
)
static
static void freecvec ( struct cvec )
static

Referenced by freev().

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

Definition at line 1964 of file regcomp.c.

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

Referenced by freev(), and rfree().

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

Referenced by freev(), and nfanode().

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

Definition at line 1758 of file regcomp.c.

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

Referenced by freesubre().

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

Referenced by cbracket().

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

Definition at line 1740 of file regcomp.c.

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

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

1742 {
1743  if (sr == NULL)
1744  return;
1745 
1746  if (sr->left != NULL)
1747  freesubre(v, sr->left);
1748  if (sr->right != NULL)
1749  freesubre(v, sr->right);
1750 
1751  freesrnode(v, sr);
1752 }
struct subre * right
Definition: regguts.h:432
static void freesrnode(struct vars *, struct subre *)
Definition: regcomp.c:1758
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1740
struct subre * left
Definition: regguts.h:431
#define NULL
Definition: c.h:226
static int freev ( struct vars v,
int  err 
)
static

Definition at line 533 of file regcomp.c.

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

Referenced by pg_regcomp().

535 {
536  if (v->re != NULL)
537  rfree(v->re);
538  if (v->subs != v->sub10)
539  FREE(v->subs);
540  if (v->nfa != NULL)
541  freenfa(v->nfa);
542  if (v->tree != NULL)
543  freesubre(v, v->tree);
544  if (v->treechain != NULL)
545  cleanst(v);
546  if (v->cv != NULL)
547  freecvec(v->cv);
548  if (v->cv2 != NULL)
549  freecvec(v->cv2);
550  if (v->lacons != NULL)
551  freelacons(v->lacons, v->nlacons);
552  ERR(err); /* nop if err==0 */
553 
554  return v->err;
555 }
static void freelacons(struct subre *, int)
Definition: regcomp.c:1964
static void rfree(regex_t *)
Definition: regcomp.c:1981
struct subre * treechain
Definition: regcomp.c:248
#define FREE(p)
Definition: regcustom.h:54
#define ERR(e)
Definition: regcomp.c:267
regex_t * re
Definition: regcomp.c:228
int nlacons
Definition: regcomp.c:254
static void freesubre(struct vars *, struct subre *)
Definition: regcomp.c:1740
struct subre ** subs
Definition: regcomp.c:240
int err
Definition: regcomp.c:233
struct nfa * nfa
Definition: regcomp.c:243
struct cvec * cv
Definition: regcomp.c:251
struct cvec * cv2
Definition: regcomp.c:252
#define NULL
Definition: c.h:226
static void freecvec(struct cvec *)
struct subre * sub10[10]
Definition: regcomp.c:242
static void freenfa(struct nfa *)
struct subre * tree
Definition: regcomp.c:247
static void cleanst(struct vars *)
Definition: regcomp.c:1847
struct subre * lacons
Definition: regcomp.c:253
static struct cvec* getcvec ( struct vars ,
int  ,
int   
)
static
static int hasconstraintout ( struct state )
static
static int hasnonemptyout ( struct state )
static
static void initcm ( struct vars ,
struct colormap  
)
static

Referenced by pg_regcomp().

static int isconstraintarc ( struct arc )
static
static chr lexdigits ( struct vars ,
int  ,
int  ,
int   
)
static
static int lexescape ( struct vars )
static
static void lexnest ( struct vars ,
const chr ,
const chr  
)
static
static void lexstart ( struct vars )
static

Referenced by pg_regcomp().

static void lexword ( struct vars )
static

Referenced by wordchrs().

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

Definition at line 562 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, NULL, arc::outchain, state::outs, PLAIN, nfa::pre, rainbow(), s2, state::tmp, arc::to, and arc::type.

Referenced by nfanode(), and pg_regcomp().

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

Definition at line 1832 of file regcomp.c.

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

Referenced by pg_regcomp().

1833 {
1834  assert(t != NULL);
1835 
1836  t->flags |= INUSE;
1837  if (t->left != NULL)
1838  markst(t->left);
1839  if (t->right != NULL)
1840  markst(t->right);
1841 }
struct subre * right
Definition: regguts.h:432
static void markst(struct subre *)
Definition: regcomp.c:1832
char flags
Definition: regguts.h:411
#define assert(TEST)
Definition: imath.c:37
struct subre * left
Definition: regguts.h:431
#define NULL
Definition: c.h:226
#define INUSE
Definition: regguts.h:417
static color maxcolor ( struct colormap )
static
static void mergeins ( struct nfa ,
struct state ,
struct arc **  ,
int   
)
static
static void moresubs ( struct vars v,
int  wanted 
)
static

Definition at line 496 of file regcomp.c.

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

Referenced by parseqatom().

498 {
499  struct subre **p;
500  size_t n;
501 
502  assert(wanted > 0 && (size_t) wanted >= v->nsubs);
503  n = (size_t) wanted *3 / 2 + 1;
504 
505  if (v->subs == v->sub10)
506  {
507  p = (struct subre **) MALLOC(n * sizeof(struct subre *));
508  if (p != NULL)
509  memcpy(VS(p), VS(v->subs),
510  v->nsubs * sizeof(struct subre *));
511  }
512  else
513  p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
514  if (p == NULL)
515  {
516  ERR(REG_ESPACE);
517  return;
518  }
519  v->subs = p;
520  for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
521  *p = NULL;
522  assert(v->nsubs == n);
523  assert((size_t) wanted < v->nsubs);
524 }
#define REG_ESPACE
Definition: regex.h:149
#define MALLOC(n)
Definition: regcustom.h:53
#define ERR(e)
Definition: regcomp.c:267
#define REALLOC(p, n)
Definition: regcustom.h:55
struct subre ** subs
Definition: regcomp.c:240
#define assert(TEST)
Definition: imath.c:37
size_t nsubs
Definition: regcomp.c:241
#define NULL
Definition: c.h:226
#define VS(x)
Definition: regguts.h:61
Definition: regguts.h:408
struct subre * sub10[10]
Definition: regcomp.c:242
static void moveins ( struct nfa ,
struct state ,
struct state  
)
static

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

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

Referenced by parseqatom(), and repeat().

static void newarc ( struct nfa ,
int  ,
color  ,
struct state ,
struct state  
)
static
static color newcolor ( struct colormap )
static
static struct cvec* newcvec ( int  ,
int   
)
static

Referenced by pg_regcomp().

static struct state* newfstate ( struct nfa ,
int  flag 
)
static
static void newhicolorcols ( struct colormap )
static
static int newhicolorrow ( struct colormap ,
int   
)
static
static int newlacon ( struct vars v,
struct state begin,
struct state end,
int  latype 
)
static

Definition at line 1925 of file regcomp.c.

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

Referenced by processlacon().

1929 {
1930  int n;
1931  struct subre *newlacons;
1932  struct subre *sub;
1933 
1934  if (v->nlacons == 0)
1935  {
1936  n = 1; /* skip 0th */
1937  newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
1938  }
1939  else
1940  {
1941  n = v->nlacons;
1942  newlacons = (struct subre *) REALLOC(v->lacons,
1943  (n + 1) * sizeof(struct subre));
1944  }
1945  if (newlacons == NULL)
1946  {
1947  ERR(REG_ESPACE);
1948  return 0;
1949  }
1950  v->lacons = newlacons;
1951  v->nlacons = n + 1;
1952  sub = &v->lacons[n];
1953  sub->begin = begin;
1954  sub->end = end;
1955  sub->subno = latype;
1956  ZAPCNFA(sub->cnfa);
1957  return n;
1958 }
#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:53
#define ERR(e)
Definition: regcomp.c:267
#define REALLOC(p, n)
Definition: regcustom.h:55
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
int nlacons
Definition: regcomp.c:254
#define NULL
Definition: c.h:226
struct state * end
Definition: regguts.h:434
Definition: regguts.h:408
struct cnfa cnfa
Definition: regguts.h:435
struct subre * lacons
Definition: regcomp.c:253
static chr newline ( void  )
static

Referenced by pg_regcomp().

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

Referenced by nfanode(), and pg_regcomp().

static struct state* newstate ( struct nfa )
static
static color newsub ( struct colormap ,
color   
)
static
static int next ( struct vars )
static

Referenced by cleanst().

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

Definition at line 1886 of file regcomp.c.

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

Referenced by nfatree(), and pg_regcomp().

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

Definition at line 1866 of file regcomp.c.

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

Referenced by pg_regcomp().

1869 {
1870  assert(t != NULL && t->begin != NULL);
1871 
1872  if (t->left != NULL)
1873  (DISCARD) nfatree(v, t->left, f);
1874  if (t->right != NULL)
1875  (DISCARD) nfatree(v, t->right, f);
1876 
1877  return nfanode(v, t, 0, f);
1878 }
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:1866
struct subre * right
Definition: regguts.h:432
struct state * begin
Definition: regguts.h:433
#define assert(TEST)
Definition: imath.c:37
struct subre * left
Definition: regguts.h:431
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:1886
#define NULL
Definition: c.h:226
#define DISCARD
Definition: regguts.h:58
static void nonword ( struct vars v,
int  dir,
struct state lp,
struct state rp 
)
static

Definition at line 1227 of file regcomp.c.

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

Referenced by parseqatom().

1231 {
1232  int anchor = (dir == AHEAD) ? '$' : '^';
1233 
1234  assert(dir == AHEAD || dir == BEHIND);
1235  newarc(v->nfa, anchor, 1, lp, rp);
1236  newarc(v->nfa, anchor, 0, lp, rp);
1237  colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
1238  /* (no need for special attention to \n) */
1239 }
#define BEHIND
Definition: regcomp.c:288
struct colormap * cm
Definition: regcomp.c:244
#define assert(TEST)
Definition: imath.c:37
struct nfa * nfa
Definition: regcomp.c:243
struct state * wordchrs
Definition: regcomp.c:246
static void newarc(struct nfa *, int, color, struct state *, struct state *)
#define AHEAD
Definition: regcomp.c:287
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)
static int numst ( struct subre t,
int  start 
)
static

Definition at line 1798 of file regcomp.c.

References assert, i, subre::id, subre::left, NULL, and subre::right.

Referenced by pg_regcomp().

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

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

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

Definition at line 1563 of file regcomp.c.

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

Referenced by brackpart(), and parseqatom().

1567 {
1568  if (!(v->cflags & REG_ICASE))
1569  {
1570  color lastsubcolor = COLORLESS;
1571 
1572  subcoloronechr(v, c, lp, rp, &lastsubcolor);
1573  return;
1574  }
1575 
1576  /* rats, need general case anyway... */
1577  subcolorcvec(v, allcases(v, c), lp, rp);
1578 }
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:234
#define COLORLESS
Definition: regguts.h:137
static long optimize ( struct nfa ,
FILE *   
)
static

Referenced by nfanode(), and pg_regcomp().

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

Definition at line 1782 of file regcomp.c.

Referenced by pg_regcomp().

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

Definition at line 651 of file regcomp.c.

References assert, EAT, EMPTYARC, EOS, ERR, subre::flags, freesubre(), subre::left, LONGER, MESSY, newstate(), vars::nfa, NOERRN, NULL, subre::op, parsebranch(), REG_EPAREN, subre::right, SEE, subre(), and UP.

Referenced by _outPlannerInfo(), add_foreign_grouping_paths(), build_minmax_path(), check_sql_fn_retval(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), create_distinct_paths(), create_grouping_paths(), expand_inherited_rtentry(), flatten_simple_union_all(), get_number_of_groups(), grouping_planner(), inheritance_planner(), make_group_input_target(), make_partial_grouping_target(), make_sort_input_target(), make_window_input_target(), max_parallel_hazard(), parseqatom(), pg_regcomp(), plan_set_operations(), planner(), preprocess_groupclause(), preprocess_limit(), preprocess_minmax_aggregates(), preprocess_rowmarks(), preprocess_targetlist(), 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().

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

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

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

Definition at line 779 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, moresubs(), moveins(), moveouts(), newstate(), NEXT, vars::nexttype, vars::nextvalue, vars::nfa, state::nins, vars::nlcolor, NOERR, nonword(), NOTE, state::nouts, vars::nsubexp, vars::nsubs, NULL, 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 wordchrs().

Referenced by parsebranch().

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

Definition at line 316 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, freev(), 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, NULL, 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(), 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().

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

Definition at line 1622 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, NULL, s1, and single_color_transition().

Referenced by parseqatom().

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

Referenced by makesearch(), and parseqatom().

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

Referenced by brackpart().

static int rcancelrequested ( void  )
static

Definition at line 2017 of file regcomp.c.

References InterruptPending, ProcDiePending, and QueryCancelPending.

2018 {
2020 }
volatile bool QueryCancelPending
Definition: globals.c:30
volatile bool InterruptPending
Definition: globals.c:29
volatile bool ProcDiePending
Definition: globals.c:31
static void repeat ( struct vars v,
struct state lp,
struct state rp,
int  m,
int  n 
)
static

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

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

Definition at line 1981 of file regcomp.c.

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

Referenced by freev().

1982 {
1983  struct guts *g;
1984 
1985  if (re == NULL || re->re_magic != REMAGIC)
1986  return;
1987 
1988  re->re_magic = 0; /* invalidate RE */
1989  g = (struct guts *) re->re_guts;
1990  re->re_guts = NULL;
1991  re->re_fns = NULL;
1992  if (g != NULL)
1993  {
1994  g->magic = 0;
1995  freecm(&g->cmap);
1996  if (g->tree != NULL)
1997  freesubre((struct vars *) NULL, g->tree);
1998  if (g->lacons != NULL)
1999  freelacons(g->lacons, g->nlacons);
2000  if (!NULLCNFA(g->search))
2001  freecnfa(&g->search);
2002  FREE(g);
2003  }
2004 }
char * re_fns
Definition: regex.h:79
static void freelacons(struct subre *, int)
Definition: regcomp.c:1964
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 FREE(p)
Definition: regcustom.h:54
#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:1740
struct colormap cmap
Definition: regguts.h:472
Definition: regguts.h:462
#define NULL
Definition: c.h:226
char * re_guts
Definition: regex.h:78
static void freecnfa(struct cnfa *)
Definition: regcomp.c:226
static int rstacktoodeep ( void  )
static

Definition at line 2033 of file regcomp.c.

References stack_is_too_deep().

2034 {
2035  return stack_is_too_deep();
2036 }
bool stack_is_too_deep(void)
Definition: postgres.c:3110
static int scannum ( struct vars v)
static

Definition at line 1259 of file regcomp.c.

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

Referenced by parseqatom().

1260 {
1261  int n = 0;
1262 
1263  while (SEE(DIGIT) && n < DUPMAX)
1264  {
1265  n = n * 10 + v->nextvalue;
1266  NEXT();
1267  }
1268  if (SEE(DIGIT) || n > DUPMAX)
1269  {
1270  ERR(REG_BADBR);
1271  return 0;
1272  }
1273  return n;
1274 }
chr nextvalue
Definition: regcomp.c:237
#define DUPMAX
Definition: regguts.h:93
#define NEXT()
Definition: regcomp.c:260
#define ERR(e)
Definition: regcomp.c:267
#define REG_BADBR
Definition: regex.h:147
#define SEE(t)
Definition: regcomp.c:261
#define DIGIT
Definition: regcomp.c:279
static const chr * scanplain ( struct vars v)
static

Definition at line 1538 of file regcomp.c.

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

Referenced by brackpart().

1539 {
1540  const chr *endp;
1541 
1542  assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
1543  NEXT();
1544 
1545  endp = v->now;
1546  while (SEE(PLAIN))
1547  {
1548  endp = v->now;
1549  NEXT();
1550  }
1551 
1552  assert(SEE(END) || ISERR());
1553  NEXT();
1554 
1555  return endp;
1556 }
#define END
Definition: regcomp.c:284
#define CCLASS
Definition: regcomp.c:283
#define NEXT()
Definition: regcomp.c:260
#define ISERR()
Definition: regcomp.c:264
pg_wchar chr
Definition: regcustom.h:59
#define assert(TEST)
Definition: imath.c:37
#define SEE(t)
Definition: regcomp.c:261
#define PLAIN
Definition: regcomp.c:278
const chr * now
Definition: regcomp.c:229
#define COLLEL
Definition: regcomp.c:281
#define ECLASS
Definition: regcomp.c:282
static struct state* single_color_transition ( struct state ,
struct state  
)
static

Referenced by processlacon().

static void skip ( struct vars )
static
static void sortins ( struct nfa ,
struct state  
)
static
static int sortins_cmp ( const void *  ,
const void *   
)
static
static void sortouts ( struct nfa ,
struct state  
)
static
static int sortouts_cmp ( const void *  ,
const void *   
)
static
static void specialcolors ( struct nfa )
static

Referenced by nfanode(), and pg_regcomp().

static color subcolor ( struct colormap ,
chr   
)
static

Referenced by pg_regcomp().

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

Referenced by brackpart(), and onechr().

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

Referenced by onechr().

static void subcoloronerange ( struct vars ,
chr  ,
chr  ,
struct state ,
struct state ,
color  
)
static
static void subcoloronerow ( struct vars ,
int  ,
struct state ,
struct state ,
color  
)
static
static struct subre * subre ( struct vars v,
int  op,
int  flags,
struct state begin,
struct state end 
)
static

Definition at line 1688 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, NULL, 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().

1693 {
1694  struct subre *ret = v->treefree;
1695 
1696  /*
1697  * Checking for stack overflow here is sufficient to protect parse() and
1698  * its recursive subroutines.
1699  */
1700  if (STACK_TOO_DEEP(v->re))
1701  {
1702  ERR(REG_ETOOBIG);
1703  return NULL;
1704  }
1705 
1706  if (ret != NULL)
1707  v->treefree = ret->left;
1708  else
1709  {
1710  ret = (struct subre *) MALLOC(sizeof(struct subre));
1711  if (ret == NULL)
1712  {
1713  ERR(REG_ESPACE);
1714  return NULL;
1715  }
1716  ret->chain = v->treechain;
1717  v->treechain = ret;
1718  }
1719 
1720  assert(strchr("=b|.*(", op) != NULL);
1721 
1722  ret->op = op;
1723  ret->flags = flags;
1724  ret->id = 0; /* will be assigned later */
1725  ret->subno = 0;
1726  ret->min = ret->max = 1;
1727  ret->left = NULL;
1728  ret->right = NULL;
1729  ret->begin = begin;
1730  ret->end = end;
1731  ZAPCNFA(ret->cnfa);
1732 
1733  return ret;
1734 }
struct subre * treefree
Definition: regcomp.c:249
struct subre * treechain
Definition: regcomp.c:248
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:53
#define ERR(e)
Definition: regcomp.c:267
struct subre * chain
Definition: regguts.h:436
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
regex_t * re
Definition: regcomp.c:228
char flags
Definition: regguts.h:411
#define assert(TEST)
Definition: imath.c:37
struct subre * left
Definition: regguts.h:431
#define NULL
Definition: c.h:226
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
static void uncolorchain ( struct colormap ,
struct arc  
)
static
static void word ( struct vars v,
int  dir,
struct state lp,
struct state rp 
)
static

Definition at line 1245 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(), ScanKeywordLookup(), and unicode_to_utf8word().

1249 {
1250  assert(dir == AHEAD || dir == BEHIND);
1251  cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
1252  /* (no need for special attention to \n) */
1253 }
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
#define BEHIND
Definition: regcomp.c:288
#define assert(TEST)
Definition: imath.c:37
struct nfa * nfa
Definition: regcomp.c:243
struct state * wordchrs
Definition: regcomp.c:246
#define AHEAD
Definition: regcomp.c:287
static void wordchrs ( struct vars v)
static

Definition at line 1590 of file regcomp.c.

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

Referenced by parseqatom().

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

Variable Documentation

const struct fns functions
static