81 static void dump(
regex_t *, FILE *);
82 static void dumpst(
struct subre *, FILE *,
int);
83 static void stdump(
struct subre *, FILE *,
int);
84 static const char *stid(
struct subre *,
char *,
size_t);
120 static void dumpcolors(
struct colormap *, FILE *);
121 static void dumpchr(
chr, FILE *);
164 #define INCOMPATIBLE 1
179 char *,
char *,
int);
195 static void dumpstate(
struct state *, FILE *);
196 static void dumparcs(
struct state *, FILE *);
197 static void dumparc(
struct arc *,
struct state *, FILE *);
198 static void dumpcnfa(
struct cnfa *, FILE *);
199 static void dumpcstate(
int,
struct cnfa *, FILE *);
269 #define NEXT() (next(v))
270 #define SEE(t) (v->nexttype == (t))
271 #define EAT(t) (SEE(t) && next(v))
272 #define VISERR(vv) ((vv)->err != 0)
273 #define ISERR() VISERR(v)
274 #define VERR(vv,e) ((vv)->nexttype = EOS, \
275 (vv)->err = ((vv)->err ? (vv)->err : (e)))
276 #define ERR(e) VERR(v, e)
277 #define NOERR() {if (ISERR()) return;}
278 #define NOERRN() {if (ISERR()) return NULL;}
279 #define NOERRZ() {if (ISERR()) return 0;}
280 #define INSIST(c, e) do { if (!(c)) ERR(e); } while (0)
281 #define NOTE(b) (v->re->re_info |= (b))
282 #define EMPTYARC(x, y) newarc(v->nfa, EMPTY, 0, x, y)
309 ((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND))
335 struct vars *v = &var;
343 FILE *
debug = (FILE *) NULL;
346 #define CNOERR() { if (ISERR()) return freev(v, v->err); }
350 if (re == NULL ||
string == NULL)
442 fprintf(
debug,
"\n\n\n========= TREE FIXED ==========\n");
518 n = (size_t) wanted * 3 / 2 + 1;
538 assert((
size_t) wanted < v->nsubs);
588 for (
a = pre->
outs;
a != NULL;
a =
a->outchain)
622 for (
a = pre->
outs;
a != NULL;
a =
a->outchain)
625 for (
b = s->
ins;
b != NULL;
b =
b->inchain)
637 if (
b != NULL && s->
tmp == NULL)
639 s->
tmp = (slist != NULL) ? slist : s;
645 for (s = slist; s != NULL; s =
s2)
651 for (
a = s->
ins;
a != NULL;
a =
b)
672 static struct subre *
679 struct subre *branches;
680 struct subre *lastbranch;
682 assert(stopper ==
')' || stopper ==
EOS);
689 struct subre *branch;
704 branches->
child = branch;
717 if (lastbranch == branches->
child)
721 branches = lastbranch;
726 branches->
child = NULL;
740 static struct subre *
754 t =
subre(v,
'=', 0, left, right);
793 static struct subre *
804 #define ARCV(t, val) newarc(v->nfa, t, val, lp, rp)
815 struct subre **atomp;
972 if ((
size_t) subno >= v->
nsubs)
1002 if (atom->
capno == 0)
1006 atom->
capno = subno;
1018 v->
subs[subno] = atom;
1097 if (m == 0 && n == 0)
1104 if (atom != NULL && (atom->
flags &
CAP))
1122 f = top->
flags | qprefer | ((atom != NULL) ? atom->
flags : 0);
1125 if (!(m == 1 && n == 1))
1143 atom =
subre(v,
'=', 0, lp, rp);
1154 if (atom->
begin == lp || atom->
end == rp)
1251 atom->
min = (short) m;
1252 atom->
max = (short) n;
1257 else if (m == 1 && n == 1 &&
1298 t =
subre(v,
'.', f, s, atom->
end);
1370 else if (t->
child->
op ==
'=' &&
1419 int anchor = (dir ==
AHEAD) ?
'$' :
'^';
1479 struct state *cstate;
1550 #define PAIR(x, y) ((x)*4 + (y))
1551 #define REDUCE(x) ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
1552 const int rm =
REDUCE(m);
1553 const int rn =
REDUCE(n);
1557 switch (
PAIR(rm, rn))
1587 repeat(v, lp, s, 1, n - 1);
1607 repeat(v, lp, s, m - 1, n - 1);
1615 repeat(v, lp, s, m - 1, n);
1643 memset(have_cclassc,
false,
sizeof(have_cclassc));
1656 any_cclassc =
false;
1659 if (have_cclassc[
i])
1744 onechr(v, startc, lp, rp);
1754 startc =
element(v, startp, endp);
1762 startc =
element(v, startp, endp);
1812 endc =
element(v, startp, endp);
1907 for (
a = lp->
outs;
a != NULL;
a =
a->outchain)
1912 cd = &v->
cm->
cd[
a->co];
1919 for (cd = v->
cm->
cd; cd < end; cd++)
1932 while ((
a = lp->
outs) != NULL)
1951 struct state *cstate;
1986 struct state *begin,
2043 n =
newlacon(v, begin, end, latype);
2050 static struct subre *
2083 assert(strchr(
"=b|.*(",
op) != NULL);
2114 if (sr->
child != NULL)
2309 int converttosearch,
2322 fprintf(f,
"\n\n\n========= TREE NODE %s ==========\n",
2323 stid(t, idbuf,
sizeof(idbuf)));
2333 if (converttosearch && !
ISERR())
2347 struct state *begin,
2352 struct subre *newlacons;
2364 (n + 1) *
sizeof(
struct subre));
2366 if (newlacons == NULL)
2392 for (sub = subs + 1,
i = n - 1;
i > 0; sub++,
i--)
2417 if (g->
tree != NULL)
2481 fprintf(f,
"bad guts magic number (0x%x not 0x%x)\n", g->
magic,
2484 fprintf(f,
"\n\n\n========= DUMP ==========\n");
2485 fprintf(f,
"nsub %d, info 0%lo, csize %d, ntree %d\n",
2488 dumpcolors(&g->
cmap, f);
2502 latype =
"positive lookahead";
2505 latype =
"negative lookahead";
2508 latype =
"positive lookbehind";
2511 latype =
"negative lookbehind";
2518 dumpcnfa(&lasub->
cnfa, f);
2521 dumpst(g->
tree, f, 0);
2528 dumpst(
struct subre *t,
2535 stdump(t, f, nfapresent);
2543 stdump(
struct subre *t,
2550 fprintf(f,
"%s. `%c'", stid(t, idbuf,
sizeof(idbuf)), t->
op);
2565 if (t->
latype != (
char) -1)
2571 if (t->
min != 1 || t->
max != 1)
2580 if (t->
child != NULL)
2581 fprintf(f,
" C:%s", stid(t->
child, idbuf,
sizeof(idbuf)));
2590 dumpcnfa(&t->
cnfa, f);
2594 stdump(t2, f, nfapresent);
2601 stid(
struct subre *t,
2606 if (bufsize <
sizeof(
void *) * 2 + 3 || bufsize <
sizeof(t->
id) * 3 + 1)
volatile sig_atomic_t InterruptPending
volatile sig_atomic_t QueryCancelPending
volatile sig_atomic_t ProcDiePending
static void const char fflush(stdout)
bool stack_is_too_deep(void)
void pg_set_regex_collation(Oid collation)
static struct state * newstate(struct nfa *)
static struct subre * parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *)
static int pg_wc_islower(pg_wchar c)
static int pg_wc_isword(pg_wchar c)
static void delsub(struct nfa *, struct state *, struct state *)
static void pullback(struct nfa *, FILE *)
static struct cvec * eclass(struct vars *, chr, int)
static int lexescape(struct vars *)
static void colorchain(struct colormap *, struct arc *)
static struct cvec * getcvec(struct vars *, int, int)
static void freesubre(struct vars *, struct subre *)
static int pg_wc_isspace(pg_wchar c)
static pg_wchar pg_wc_tolower(pg_wchar c)
static int combine(struct nfa *nfa, struct arc *con, struct arc *a)
static void cleanup(struct nfa *)
static color subcolorhi(struct colormap *, color *)
static int newlacon(struct vars *, struct state *, struct state *, int)
static struct subre * subre(struct vars *, int, int, struct state *, struct state *)
static void moresubs(struct vars *, int)
static int pg_wc_ispunct(pg_wchar c)
static void removecaptures(struct vars *, struct subre *)
static void dropstate(struct nfa *, struct state *)
static int hasnonemptyout(struct state *)
static void markreachable(struct nfa *, struct state *, struct state *, struct state *)
static void lexstart(struct vars *)
static int pull(struct nfa *, struct arc *, struct state **)
static void freenfa(struct nfa *)
static void okcolors(struct nfa *, struct colormap *)
static struct cvec * cclasscvec(struct vars *, enum char_classes, int)
static void specialcolors(struct nfa *)
static int pg_wc_isgraph(pg_wchar c)
static void freesubreandsiblings(struct vars *, struct subre *)
static int scannum(struct vars *)
static void brackpart(struct vars *, struct state *, struct state *, bool *)
static void processlacon(struct vars *, struct state *, struct state *, int, struct state *, struct state *)
static pg_wchar pg_wc_toupper(pg_wchar c)
static long optimize(struct nfa *, FILE *)
static long nfatree(struct vars *, struct subre *, FILE *)
static struct state * newfstate(struct nfa *, int flag)
static void repeat(struct vars *, struct state *, struct state *, int, int)
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
static const struct fns functions
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
static void carcsort(struct carc *, size_t)
static void makesearch(struct vars *, struct nfa *)
static void copyouts(struct nfa *, struct state *, struct state *)
static color pseudocolor(struct colormap *)
static int before(chr, chr)
static int pg_wc_isprint(pg_wchar c)
static struct state * single_color_transition(struct state *, struct state *)
static void subcoloronerow(struct vars *, int, struct state *, struct state *, color *)
static int push(struct nfa *, struct arc *, struct state **)
static void pushfwd(struct nfa *, FILE *)
static chr lexdigits(struct vars *, int, int, int)
static int pg_wc_isalnum(pg_wchar c)
static void cleanst(struct vars *)
static void sortouts(struct nfa *, struct state *)
static int rstacktoodeep(void)
static int findconstraintloop(struct nfa *, struct state *)
static int pg_wc_isdigit(pg_wchar c)
static void prefixes(struct vars *)
static int freev(struct vars *, int)
int pg_regcomp(regex_t *re, const chr *string, size_t len, int flags, Oid collation)
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *)
static int rcancelrequested(void)
static void initcm(struct vars *, struct colormap *)
static long analyze(struct nfa *)
static void moveouts(struct nfa *, struct state *, struct state *)
static void cleartraverse(struct nfa *, struct state *)
static int casecmp(const chr *, const chr *, size_t)
static void wordchrs(struct vars *)
static struct cvec * range(struct vars *, chr, chr, int)
static struct cvec * allcases(struct vars *, chr)
static int isconstraintarc(struct arc *)
static void removetraverse(struct nfa *, struct state *)
static struct arc * findarc(struct state *, int, color)
static void uncolorchain(struct colormap *, struct arc *)
static void duptraverse(struct nfa *, struct state *, struct state *)
static void deltraverse(struct nfa *, struct state *, struct state *)
static chr element(struct vars *, const chr *, const chr *)
static void freesrnode(struct vars *, struct subre *)
static void freearc(struct nfa *, struct arc *)
static void moveins(struct nfa *, struct state *, struct state *)
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)
static void freestate(struct nfa *, struct state *)
static bool checkmatchall_recurse(struct nfa *, struct state *, bool **)
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int)
static void breakconstraintloop(struct nfa *, struct state *)
static void cbracket(struct vars *, struct state *, struct state *)
static struct subre * parsebranch(struct vars *, int, int, struct state *, struct state *, int)
static void fixempties(struct nfa *, FILE *)
static color subcolor(struct colormap *, chr)
static int carc_cmp(const void *, const void *)
static void nonword(struct vars *, int, struct state *, struct state *)
static void removeconstraints(struct nfa *, struct state *, struct state *)
static void newhicolorcols(struct colormap *)
static const chr * scanplain(struct vars *)
static struct cvec * newcvec(int, int)
static int newhicolorrow(struct colormap *, int)
static void mergeins(struct nfa *, struct state *, struct arc **, int)
static int cclass_column_index(struct colormap *, chr)
static color maxcolor(struct colormap *)
static int sortins_cmp(const void *, const void *)
static void changearctarget(struct arc *, struct state *)
static void onechr(struct vars *, chr, struct state *, struct state *)
static bool check_out_colors_match(struct state *, color, color)
static void fixconstraintloops(struct nfa *, FILE *)
static long nfanode(struct vars *, struct subre *, int, FILE *)
static void sortins(struct nfa *, struct state *)
static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *)
static void word(struct vars *, int, struct state *, struct state *)
static enum char_classes lookupcclass(struct vars *, const chr *, const chr *)
static int brenext(struct vars *, chr)
static int cmp(const chr *, const chr *, size_t)
static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *)
static int numst(struct subre *, int)
static struct arc * allocarc(struct nfa *)
static void subcoloronechr(struct vars *, chr, struct state *, struct state *, color *)
static void addrange(struct cvec *, chr, chr)
static void freecvec(struct cvec *)
static void markcanreach(struct nfa *, struct state *, struct state *, struct state *)
static void createarc(struct nfa *, int, color, struct state *, struct state *)
static void dumpnfa(struct nfa *, FILE *)
static void changearcsource(struct arc *, struct state *)
static void markst(struct subre *)
static void freecnfa(struct cnfa *)
static void freecolor(struct colormap *, color)
static void checkmatchall(struct nfa *)
static void clonesuccessorstates(struct nfa *, struct state *, struct state *, struct state *, struct arc *, char *, char *, int)
static void cparc(struct nfa *, struct arc *, struct state *, struct state *)
static int hasconstraintout(struct state *)
static void charclasscomplement(struct vars *, enum char_classes, struct state *, struct state *)
static void newarc(struct nfa *, int, color, struct state *, struct state *)
static void subcoloronerange(struct vars *, chr, chr, struct state *, struct state *, color *)
static struct cvec * clearcvec(struct cvec *)
static color newsub(struct colormap *, color)
static void copyins(struct nfa *, struct state *, struct state *)
static void charclass(struct vars *, enum char_classes, struct state *, struct state *)
static void freelacons(struct subre *, int)
static void skip(struct vars *)
static struct state * emptyreachable(struct nfa *, struct state *, struct state *, struct arc **)
static void rfree(regex_t *)
static void bracket(struct vars *, struct state *, struct state *)
static void compact(struct nfa *, struct cnfa *)
static int sortouts_cmp(const void *, const void *)
static int next(struct vars *)
static void addchr(struct cvec *, chr)
static int pg_wc_isupper(pg_wchar c)
static int pg_wc_isalpha(pg_wchar c)
static color newcolor(struct colormap *)
static bool check_in_colors_match(struct state *, color, color)
static chr chrnamed(struct vars *, const chr *, const chr *, chr)
static void optimizebracket(struct vars *, struct state *, struct state *)
static void freecm(struct colormap *)
#define LATYPE_BEHIND_POS
#define STACK_TOO_DEEP(re)
#define LATYPE_BEHIND_NEG
#define LATYPE_IS_AHEAD(la)