sl@0: /* sl@0: * re_*comp and friends - compile REs sl@0: * This file #includes several others (see the bottom). sl@0: * sl@0: * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. sl@0: * sl@0: * Development of this software was funded, in part, by Cray Research Inc., sl@0: * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics sl@0: * Corporation, none of whom are responsible for the results. The author sl@0: * thanks all of them. sl@0: * sl@0: * Redistribution and use in source and binary forms -- with or without sl@0: * modification -- are permitted for any purpose, provided that sl@0: * redistributions in source form retain this entire copyright notice and sl@0: * indicate the origin and nature of any modifications. sl@0: * sl@0: * I'd appreciate being given credit for this package in the documentation sl@0: * of software which uses it, but that is not a requirement. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, sl@0: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY sl@0: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL sl@0: * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sl@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sl@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; sl@0: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, sl@0: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR sl@0: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF sl@0: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * sl@0: */ sl@0: sl@0: #include "regguts.h" sl@0: sl@0: /* sl@0: * forward declarations, up here so forward datatypes etc. are defined early sl@0: */ sl@0: /* =====^!^===== begin forwards =====^!^===== */ sl@0: /* automatically gathered by fwd; do not hand-edit */ sl@0: /* === regcomp.c === */ sl@0: int compile _ANSI_ARGS_((regex_t *, CONST chr *, size_t, int)); sl@0: static VOID moresubs _ANSI_ARGS_((struct vars *, int)); sl@0: static int freev _ANSI_ARGS_((struct vars *, int)); sl@0: static VOID makesearch _ANSI_ARGS_((struct vars *, struct nfa *)); sl@0: static struct subre *parse _ANSI_ARGS_((struct vars *, int, int, struct state *, struct state *)); sl@0: static struct subre *parsebranch _ANSI_ARGS_((struct vars *, int, int, struct state *, struct state *, int)); sl@0: static VOID parseqatom _ANSI_ARGS_((struct vars *, int, int, struct state *, struct state *, struct subre *)); sl@0: static VOID nonword _ANSI_ARGS_((struct vars *, int, struct state *, struct state *)); sl@0: static VOID word _ANSI_ARGS_((struct vars *, int, struct state *, struct state *)); sl@0: static int scannum _ANSI_ARGS_((struct vars *)); sl@0: static VOID repeat _ANSI_ARGS_((struct vars *, struct state *, struct state *, int, int)); sl@0: static VOID bracket _ANSI_ARGS_((struct vars *, struct state *, struct state *)); sl@0: static VOID cbracket _ANSI_ARGS_((struct vars *, struct state *, struct state *)); sl@0: static VOID brackpart _ANSI_ARGS_((struct vars *, struct state *, struct state *)); sl@0: static chr *scanplain _ANSI_ARGS_((struct vars *)); sl@0: static VOID leaders _ANSI_ARGS_((struct vars *, struct cvec *)); sl@0: static VOID onechr _ANSI_ARGS_((struct vars *, pchr, struct state *, struct state *)); sl@0: static VOID dovec _ANSI_ARGS_((struct vars *, struct cvec *, struct state *, struct state *)); sl@0: static celt nextleader _ANSI_ARGS_((struct vars *, pchr, pchr)); sl@0: static VOID wordchrs _ANSI_ARGS_((struct vars *)); sl@0: static struct subre *subre _ANSI_ARGS_((struct vars *, int, int, struct state *, struct state *)); sl@0: static VOID freesubre _ANSI_ARGS_((struct vars *, struct subre *)); sl@0: static VOID freesrnode _ANSI_ARGS_((struct vars *, struct subre *)); sl@0: static VOID optst _ANSI_ARGS_((struct vars *, struct subre *)); sl@0: static int numst _ANSI_ARGS_((struct subre *, int)); sl@0: static VOID markst _ANSI_ARGS_((struct subre *)); sl@0: static VOID cleanst _ANSI_ARGS_((struct vars *)); sl@0: static long nfatree _ANSI_ARGS_((struct vars *, struct subre *, FILE *)); sl@0: static long nfanode _ANSI_ARGS_((struct vars *, struct subre *, FILE *)); sl@0: static int newlacon _ANSI_ARGS_((struct vars *, struct state *, struct state *, int)); sl@0: static VOID freelacons _ANSI_ARGS_((struct subre *, int)); sl@0: static VOID rfree _ANSI_ARGS_((regex_t *)); sl@0: static VOID dump _ANSI_ARGS_((regex_t *, FILE *)); sl@0: static VOID dumpst _ANSI_ARGS_((struct subre *, FILE *, int)); sl@0: static VOID stdump _ANSI_ARGS_((struct subre *, FILE *, int)); sl@0: static char *stid _ANSI_ARGS_((struct subre *, char *, size_t)); sl@0: /* === regc_lex.c === */ sl@0: static VOID lexstart _ANSI_ARGS_((struct vars *)); sl@0: static VOID prefixes _ANSI_ARGS_((struct vars *)); sl@0: static VOID lexnest _ANSI_ARGS_((struct vars *, chr *, chr *)); sl@0: static VOID lexword _ANSI_ARGS_((struct vars *)); sl@0: static int next _ANSI_ARGS_((struct vars *)); sl@0: static int lexescape _ANSI_ARGS_((struct vars *)); sl@0: static chr lexdigits _ANSI_ARGS_((struct vars *, int, int, int)); sl@0: static int brenext _ANSI_ARGS_((struct vars *, pchr)); sl@0: static VOID skip _ANSI_ARGS_((struct vars *)); sl@0: static chr newline _ANSI_ARGS_((NOPARMS)); sl@0: #ifdef REG_DEBUG sl@0: static chr *ch _ANSI_ARGS_((NOPARMS)); sl@0: #endif sl@0: static chr chrnamed _ANSI_ARGS_((struct vars *, chr *, chr *, pchr)); sl@0: /* === regc_color.c === */ sl@0: static VOID initcm _ANSI_ARGS_((struct vars *, struct colormap *)); sl@0: static VOID freecm _ANSI_ARGS_((struct colormap *)); sl@0: static VOID cmtreefree _ANSI_ARGS_((struct colormap *, union tree *, int)); sl@0: static color setcolor _ANSI_ARGS_((struct colormap *, pchr, pcolor)); sl@0: static color maxcolor _ANSI_ARGS_((struct colormap *)); sl@0: static color newcolor _ANSI_ARGS_((struct colormap *)); sl@0: static VOID freecolor _ANSI_ARGS_((struct colormap *, pcolor)); sl@0: static color pseudocolor _ANSI_ARGS_((struct colormap *)); sl@0: static color subcolor _ANSI_ARGS_((struct colormap *, pchr c)); sl@0: static color newsub _ANSI_ARGS_((struct colormap *, pcolor)); sl@0: static VOID subrange _ANSI_ARGS_((struct vars *, pchr, pchr, struct state *, struct state *)); sl@0: static VOID subblock _ANSI_ARGS_((struct vars *, pchr, struct state *, struct state *)); sl@0: static VOID okcolors _ANSI_ARGS_((struct nfa *, struct colormap *)); sl@0: static VOID colorchain _ANSI_ARGS_((struct colormap *, struct arc *)); sl@0: static VOID uncolorchain _ANSI_ARGS_((struct colormap *, struct arc *)); sl@0: static int singleton _ANSI_ARGS_((struct colormap *, pchr c)); sl@0: static VOID rainbow _ANSI_ARGS_((struct nfa *, struct colormap *, int, pcolor, struct state *, struct state *)); sl@0: static VOID colorcomplement _ANSI_ARGS_((struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *)); sl@0: #ifdef REG_DEBUG sl@0: static VOID dumpcolors _ANSI_ARGS_((struct colormap *, FILE *)); sl@0: static VOID fillcheck _ANSI_ARGS_((struct colormap *, union tree *, int, FILE *)); sl@0: static VOID dumpchr _ANSI_ARGS_((pchr, FILE *)); sl@0: #endif sl@0: /* === regc_nfa.c === */ sl@0: static struct nfa *newnfa _ANSI_ARGS_((struct vars *, struct colormap *, struct nfa *)); sl@0: static VOID freenfa _ANSI_ARGS_((struct nfa *)); sl@0: static struct state *newstate _ANSI_ARGS_((struct nfa *)); sl@0: static struct state *newfstate _ANSI_ARGS_((struct nfa *, int flag)); sl@0: static VOID dropstate _ANSI_ARGS_((struct nfa *, struct state *)); sl@0: static VOID freestate _ANSI_ARGS_((struct nfa *, struct state *)); sl@0: static VOID destroystate _ANSI_ARGS_((struct nfa *, struct state *)); sl@0: static VOID newarc _ANSI_ARGS_((struct nfa *, int, pcolor, struct state *, struct state *)); sl@0: static struct arc *allocarc _ANSI_ARGS_((struct nfa *, struct state *)); sl@0: static VOID freearc _ANSI_ARGS_((struct nfa *, struct arc *)); sl@0: static struct arc *findarc _ANSI_ARGS_((struct state *, int, pcolor)); sl@0: static VOID cparc _ANSI_ARGS_((struct nfa *, struct arc *, struct state *, struct state *)); sl@0: static VOID moveins _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID copyins _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID moveouts _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID copyouts _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID cloneouts _ANSI_ARGS_((struct nfa *, struct state *, struct state *, struct state *, int)); sl@0: static VOID delsub _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID deltraverse _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID dupnfa _ANSI_ARGS_((struct nfa *, struct state *, struct state *, struct state *, struct state *)); sl@0: static VOID duptraverse _ANSI_ARGS_((struct nfa *, struct state *, struct state *)); sl@0: static VOID cleartraverse _ANSI_ARGS_((struct nfa *, struct state *)); sl@0: static VOID specialcolors _ANSI_ARGS_((struct nfa *)); sl@0: static long optimize _ANSI_ARGS_((struct nfa *, FILE *)); sl@0: static VOID pullback _ANSI_ARGS_((struct nfa *, FILE *)); sl@0: static int pull _ANSI_ARGS_((struct nfa *, struct arc *)); sl@0: static VOID pushfwd _ANSI_ARGS_((struct nfa *, FILE *)); sl@0: static int push _ANSI_ARGS_((struct nfa *, struct arc *)); sl@0: #define INCOMPATIBLE 1 /* destroys arc */ sl@0: #define SATISFIED 2 /* constraint satisfied */ sl@0: #define COMPATIBLE 3 /* compatible but not satisfied yet */ sl@0: static int combine _ANSI_ARGS_((struct arc *, struct arc *)); sl@0: static VOID fixempties _ANSI_ARGS_((struct nfa *, FILE *)); sl@0: static int unempty _ANSI_ARGS_((struct nfa *, struct arc *)); sl@0: static VOID cleanup _ANSI_ARGS_((struct nfa *)); sl@0: static VOID markreachable _ANSI_ARGS_((struct nfa *, struct state *, struct state *, struct state *)); sl@0: static VOID markcanreach _ANSI_ARGS_((struct nfa *, struct state *, struct state *, struct state *)); sl@0: static long analyze _ANSI_ARGS_((struct nfa *)); sl@0: static VOID compact _ANSI_ARGS_((struct nfa *, struct cnfa *)); sl@0: static VOID carcsort _ANSI_ARGS_((struct carc *, struct carc *)); sl@0: static VOID freecnfa _ANSI_ARGS_((struct cnfa *)); sl@0: static VOID dumpnfa _ANSI_ARGS_((struct nfa *, FILE *)); sl@0: #ifdef REG_DEBUG sl@0: static VOID dumpstate _ANSI_ARGS_((struct state *, FILE *)); sl@0: static VOID dumparcs _ANSI_ARGS_((struct state *, FILE *)); sl@0: static int dumprarcs _ANSI_ARGS_((struct arc *, struct state *, FILE *, int)); sl@0: static VOID dumparc _ANSI_ARGS_((struct arc *, struct state *, FILE *)); sl@0: #endif sl@0: static VOID dumpcnfa _ANSI_ARGS_((struct cnfa *, FILE *)); sl@0: #ifdef REG_DEBUG sl@0: static VOID dumpcstate _ANSI_ARGS_((int, struct carc *, struct cnfa *, FILE *)); sl@0: #endif sl@0: /* === regc_cvec.c === */ sl@0: static struct cvec *newcvec _ANSI_ARGS_((int, int, int)); sl@0: static struct cvec *clearcvec _ANSI_ARGS_((struct cvec *)); sl@0: static VOID addchr _ANSI_ARGS_((struct cvec *, pchr)); sl@0: static VOID addrange _ANSI_ARGS_((struct cvec *, pchr, pchr)); sl@0: static VOID addmcce _ANSI_ARGS_((struct cvec *, chr *, chr *)); sl@0: static int haschr _ANSI_ARGS_((struct cvec *, pchr)); sl@0: static struct cvec *getcvec _ANSI_ARGS_((struct vars *, int, int, int)); sl@0: static VOID freecvec _ANSI_ARGS_((struct cvec *)); sl@0: /* === regc_locale.c === */ sl@0: static int nmcces _ANSI_ARGS_((struct vars *)); sl@0: static int nleaders _ANSI_ARGS_((struct vars *)); sl@0: static struct cvec *allmcces _ANSI_ARGS_((struct vars *, struct cvec *)); sl@0: static celt element _ANSI_ARGS_((struct vars *, chr *, chr *)); sl@0: static struct cvec *range _ANSI_ARGS_((struct vars *, celt, celt, int)); sl@0: static int before _ANSI_ARGS_((celt, celt)); sl@0: static struct cvec *eclass _ANSI_ARGS_((struct vars *, celt, int)); sl@0: static struct cvec *cclass _ANSI_ARGS_((struct vars *, chr *, chr *, int)); sl@0: static struct cvec *allcases _ANSI_ARGS_((struct vars *, pchr)); sl@0: static int cmp _ANSI_ARGS_((CONST chr *, CONST chr *, size_t)); sl@0: static int casecmp _ANSI_ARGS_((CONST chr *, CONST chr *, size_t)); sl@0: /* automatically gathered by fwd; do not hand-edit */ sl@0: /* =====^!^===== end forwards =====^!^===== */ sl@0: sl@0: sl@0: sl@0: /* internal variables, bundled for easy passing around */ sl@0: struct vars { sl@0: regex_t *re; sl@0: chr *now; /* scan pointer into string */ sl@0: chr *stop; /* end of string */ sl@0: chr *savenow; /* saved now and stop for "subroutine call" */ sl@0: chr *savestop; sl@0: int err; /* error code (0 if none) */ sl@0: int cflags; /* copy of compile flags */ sl@0: int lasttype; /* type of previous token */ sl@0: int nexttype; /* type of next token */ sl@0: chr nextvalue; /* value (if any) of next token */ sl@0: int lexcon; /* lexical context type (see lex.c) */ sl@0: int nsubexp; /* subexpression count */ sl@0: struct subre **subs; /* subRE pointer vector */ sl@0: size_t nsubs; /* length of vector */ sl@0: struct subre *sub10[10]; /* initial vector, enough for most */ sl@0: struct nfa *nfa; /* the NFA */ sl@0: struct colormap *cm; /* character color map */ sl@0: color nlcolor; /* color of newline */ sl@0: struct state *wordchrs; /* state in nfa holding word-char outarcs */ sl@0: struct subre *tree; /* subexpression tree */ sl@0: struct subre *treechain; /* all tree nodes allocated */ sl@0: struct subre *treefree; /* any free tree nodes */ sl@0: int ntree; /* number of tree nodes */ sl@0: struct cvec *cv; /* interface cvec */ sl@0: struct cvec *cv2; /* utility cvec */ sl@0: struct cvec *mcces; /* collating-element information */ sl@0: # define ISCELEADER(v,c) (v->mcces != NULL && haschr(v->mcces, (c))) sl@0: struct state *mccepbegin; /* in nfa, start of MCCE prototypes */ sl@0: struct state *mccepend; /* in nfa, end of MCCE prototypes */ sl@0: struct subre *lacons; /* lookahead-constraint vector */ sl@0: int nlacons; /* size of lacons */ sl@0: }; sl@0: sl@0: /* parsing macros; most know that `v' is the struct vars pointer */ sl@0: #define NEXT() (next(v)) /* advance by one token */ sl@0: #define SEE(t) (v->nexttype == (t)) /* is next token this? */ sl@0: #define EAT(t) (SEE(t) && next(v)) /* if next is this, swallow it */ sl@0: #define VISERR(vv) ((vv)->err != 0) /* have we seen an error yet? */ sl@0: #define ISERR() VISERR(v) sl@0: #define VERR(vv,e) ((vv)->nexttype = EOS, ((vv)->err) ? (vv)->err :\ sl@0: ((vv)->err = (e))) sl@0: #define ERR(e) VERR(v, e) /* record an error */ sl@0: #define NOERR() {if (ISERR()) return;} /* if error seen, return */ sl@0: #define NOERRN() {if (ISERR()) return NULL;} /* NOERR with retval */ sl@0: #define NOERRZ() {if (ISERR()) return 0;} /* NOERR with retval */ sl@0: #define INSIST(c, e) ((c) ? 0 : ERR(e)) /* if condition false, error */ sl@0: #define NOTE(b) (v->re->re_info |= (b)) /* note visible condition */ sl@0: #define EMPTYARC(x, y) newarc(v->nfa, EMPTY, 0, x, y) sl@0: sl@0: /* token type codes, some also used as NFA arc types */ sl@0: #define EMPTY 'n' /* no token present */ sl@0: #define EOS 'e' /* end of string */ sl@0: #define PLAIN 'p' /* ordinary character */ sl@0: #define DIGIT 'd' /* digit (in bound) */ sl@0: #define BACKREF 'b' /* back reference */ sl@0: #define COLLEL 'I' /* start of [. */ sl@0: #define ECLASS 'E' /* start of [= */ sl@0: #define CCLASS 'C' /* start of [: */ sl@0: #define END 'X' /* end of [. [= [: */ sl@0: #define RANGE 'R' /* - within [] which might be range delim. */ sl@0: #define LACON 'L' /* lookahead constraint subRE */ sl@0: #define AHEAD 'a' /* color-lookahead arc */ sl@0: #define BEHIND 'r' /* color-lookbehind arc */ sl@0: #define WBDRY 'w' /* word boundary constraint */ sl@0: #define NWBDRY 'W' /* non-word-boundary constraint */ sl@0: #define SBEGIN 'A' /* beginning of string (even if not BOL) */ sl@0: #define SEND 'Z' /* end of string (even if not EOL) */ sl@0: #define PREFER 'P' /* length preference */ sl@0: sl@0: /* is an arc colored, and hence on a color chain? */ sl@0: #define COLORED(a) ((a)->type == PLAIN || (a)->type == AHEAD || \ sl@0: (a)->type == BEHIND) sl@0: sl@0: sl@0: sl@0: /* static function list */ sl@0: static struct fns functions = { sl@0: rfree, /* regfree insides */ sl@0: }; sl@0: sl@0: sl@0: sl@0: /* sl@0: - compile - compile regular expression sl@0: ^ int compile(regex_t *, CONST chr *, size_t, int); sl@0: */ sl@0: int sl@0: compile(re, string, len, flags) sl@0: regex_t *re; sl@0: CONST chr *string; sl@0: size_t len; sl@0: int flags; sl@0: { sl@0: struct vars var; sl@0: struct vars *v = &var; sl@0: struct guts *g; sl@0: int i; sl@0: size_t j; sl@0: FILE *debug = (flags®_PROGRESS) ? stdout : (FILE *)NULL; sl@0: # define CNOERR() { if (ISERR()) return freev(v, v->err); } sl@0: sl@0: /* sanity checks */ sl@0: sl@0: if (re == NULL || string == NULL) sl@0: return REG_INVARG; sl@0: if ((flags®_QUOTE) && sl@0: (flags&(REG_ADVANCED|REG_EXPANDED|REG_NEWLINE))) sl@0: return REG_INVARG; sl@0: if (!(flags®_EXTENDED) && (flags®_ADVF)) sl@0: return REG_INVARG; sl@0: sl@0: /* initial setup (after which freev() is callable) */ sl@0: v->re = re; sl@0: v->now = (chr *)string; sl@0: v->stop = v->now + len; sl@0: v->savenow = v->savestop = NULL; sl@0: v->err = 0; sl@0: v->cflags = flags; sl@0: v->nsubexp = 0; sl@0: v->subs = v->sub10; sl@0: v->nsubs = 10; sl@0: for (j = 0; j < v->nsubs; j++) sl@0: v->subs[j] = NULL; sl@0: v->nfa = NULL; sl@0: v->cm = NULL; sl@0: v->nlcolor = COLORLESS; sl@0: v->wordchrs = NULL; sl@0: v->tree = NULL; sl@0: v->treechain = NULL; sl@0: v->treefree = NULL; sl@0: v->cv = NULL; sl@0: v->cv2 = NULL; sl@0: v->mcces = NULL; sl@0: v->lacons = NULL; sl@0: v->nlacons = 0; sl@0: re->re_magic = REMAGIC; sl@0: re->re_info = 0; /* bits get set during parse */ sl@0: re->re_csize = sizeof(chr); sl@0: re->re_guts = NULL; sl@0: re->re_fns = VS(&functions); sl@0: sl@0: /* more complex setup, malloced things */ sl@0: re->re_guts = VS(MALLOC(sizeof(struct guts))); sl@0: if (re->re_guts == NULL) sl@0: return freev(v, REG_ESPACE); sl@0: g = (struct guts *)re->re_guts; sl@0: g->tree = NULL; sl@0: initcm(v, &g->cmap); sl@0: v->cm = &g->cmap; sl@0: g->lacons = NULL; sl@0: g->nlacons = 0; sl@0: ZAPCNFA(g->search); sl@0: v->nfa = newnfa(v, v->cm, (struct nfa *)NULL); sl@0: CNOERR(); sl@0: v->cv = newcvec(100, 20, 10); sl@0: if (v->cv == NULL) sl@0: return freev(v, REG_ESPACE); sl@0: i = nmcces(v); sl@0: if (i > 0) { sl@0: v->mcces = newcvec(nleaders(v), 0, i); sl@0: CNOERR(); sl@0: v->mcces = allmcces(v, v->mcces); sl@0: leaders(v, v->mcces); sl@0: addmcce(v->mcces, (chr *)NULL, (chr *)NULL); /* dummy */ sl@0: } sl@0: CNOERR(); sl@0: sl@0: /* parsing */ sl@0: lexstart(v); /* also handles prefixes */ sl@0: if ((v->cflags®_NLSTOP) || (v->cflags®_NLANCH)) { sl@0: /* assign newline a unique color */ sl@0: v->nlcolor = subcolor(v->cm, newline()); sl@0: okcolors(v->nfa, v->cm); sl@0: } sl@0: CNOERR(); sl@0: v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final); sl@0: assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */ sl@0: CNOERR(); sl@0: assert(v->tree != NULL); sl@0: sl@0: /* finish setup of nfa and its subre tree */ sl@0: specialcolors(v->nfa); sl@0: CNOERR(); sl@0: if (debug != NULL) { sl@0: fprintf(debug, "\n\n\n========= RAW ==========\n"); sl@0: dumpnfa(v->nfa, debug); sl@0: dumpst(v->tree, debug, 1); sl@0: } sl@0: optst(v, v->tree); sl@0: v->ntree = numst(v->tree, 1); sl@0: markst(v->tree); sl@0: cleanst(v); sl@0: if (debug != NULL) { sl@0: fprintf(debug, "\n\n\n========= TREE FIXED ==========\n"); sl@0: dumpst(v->tree, debug, 1); sl@0: } sl@0: sl@0: /* build compacted NFAs for tree and lacons */ sl@0: re->re_info |= nfatree(v, v->tree, debug); sl@0: CNOERR(); sl@0: assert(v->nlacons == 0 || v->lacons != NULL); sl@0: for (i = 1; i < v->nlacons; i++) { sl@0: if (debug != NULL) sl@0: fprintf(debug, "\n\n\n========= LA%d ==========\n", i); sl@0: nfanode(v, &v->lacons[i], debug); sl@0: } sl@0: CNOERR(); sl@0: if (v->tree->flags&SHORTER) sl@0: NOTE(REG_USHORTEST); sl@0: sl@0: /* build compacted NFAs for tree, lacons, fast search */ sl@0: if (debug != NULL) sl@0: fprintf(debug, "\n\n\n========= SEARCH ==========\n"); sl@0: /* can sacrifice main NFA now, so use it as work area */ sl@0: (DISCARD)optimize(v->nfa, debug); sl@0: CNOERR(); sl@0: makesearch(v, v->nfa); sl@0: CNOERR(); sl@0: compact(v->nfa, &g->search); sl@0: CNOERR(); sl@0: sl@0: /* looks okay, package it up */ sl@0: re->re_nsub = v->nsubexp; sl@0: v->re = NULL; /* freev no longer frees re */ sl@0: g->magic = GUTSMAGIC; sl@0: g->cflags = v->cflags; sl@0: g->info = re->re_info; sl@0: g->nsub = re->re_nsub; sl@0: g->tree = v->tree; sl@0: v->tree = NULL; sl@0: g->ntree = v->ntree; sl@0: g->compare = (v->cflags®_ICASE) ? casecmp : cmp; sl@0: g->lacons = v->lacons; sl@0: v->lacons = NULL; sl@0: g->nlacons = v->nlacons; sl@0: sl@0: if (flags®_DUMP) sl@0: dump(re, stdout); sl@0: sl@0: assert(v->err == 0); sl@0: return freev(v, 0); sl@0: } sl@0: sl@0: /* sl@0: - moresubs - enlarge subRE vector sl@0: ^ static VOID moresubs(struct vars *, int); sl@0: */ sl@0: static VOID sl@0: moresubs(v, wanted) sl@0: struct vars *v; sl@0: int wanted; /* want enough room for this one */ sl@0: { sl@0: struct subre **p; sl@0: size_t n; sl@0: sl@0: assert(wanted > 0 && (size_t)wanted >= v->nsubs); sl@0: n = (size_t)wanted * 3 / 2 + 1; sl@0: if (v->subs == v->sub10) { sl@0: p = (struct subre **)MALLOC(n * sizeof(struct subre *)); sl@0: if (p != NULL) sl@0: memcpy(VS(p), VS(v->subs), sl@0: v->nsubs * sizeof(struct subre *)); sl@0: } else sl@0: p = (struct subre **)REALLOC(v->subs, n*sizeof(struct subre *)); sl@0: if (p == NULL) { sl@0: ERR(REG_ESPACE); sl@0: return; sl@0: } sl@0: v->subs = p; sl@0: for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++) sl@0: *p = NULL; sl@0: assert(v->nsubs == n); sl@0: assert((size_t)wanted < v->nsubs); sl@0: } sl@0: sl@0: /* sl@0: - freev - free vars struct's substructures where necessary sl@0: * Optionally does error-number setting, and always returns error code sl@0: * (if any), to make error-handling code terser. sl@0: ^ static int freev(struct vars *, int); sl@0: */ sl@0: static int sl@0: freev(v, err) sl@0: struct vars *v; sl@0: int err; sl@0: { sl@0: if (v->re != NULL) sl@0: rfree(v->re); sl@0: if (v->subs != v->sub10) sl@0: FREE(v->subs); sl@0: if (v->nfa != NULL) sl@0: freenfa(v->nfa); sl@0: if (v->tree != NULL) sl@0: freesubre(v, v->tree); sl@0: if (v->treechain != NULL) sl@0: cleanst(v); sl@0: if (v->cv != NULL) sl@0: freecvec(v->cv); sl@0: if (v->cv2 != NULL) sl@0: freecvec(v->cv2); sl@0: if (v->mcces != NULL) sl@0: freecvec(v->mcces); sl@0: if (v->lacons != NULL) sl@0: freelacons(v->lacons, v->nlacons); sl@0: ERR(err); /* nop if err==0 */ sl@0: sl@0: return v->err; sl@0: } sl@0: sl@0: /* sl@0: - makesearch - turn an NFA into a search NFA (implicit prepend of .*?) sl@0: * NFA must have been optimize()d already. sl@0: ^ static VOID makesearch(struct vars *, struct nfa *); sl@0: */ sl@0: static VOID sl@0: makesearch(v, nfa) sl@0: struct vars *v; sl@0: struct nfa *nfa; sl@0: { sl@0: struct arc *a; sl@0: struct arc *b; sl@0: struct state *pre = nfa->pre; sl@0: struct state *s; sl@0: struct state *s2; sl@0: struct state *slist; sl@0: sl@0: /* no loops are needed if it's anchored */ sl@0: for (a = pre->outs; a != NULL; a = a->outchain) { sl@0: assert(a->type == PLAIN); sl@0: if (a->co != nfa->bos[0] && a->co != nfa->bos[1]) sl@0: break; sl@0: } sl@0: if (a != NULL) { sl@0: /* add implicit .* in front */ sl@0: rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre); sl@0: sl@0: /* and ^* and \A* too -- not always necessary, but harmless */ sl@0: newarc(nfa, PLAIN, nfa->bos[0], pre, pre); sl@0: newarc(nfa, PLAIN, nfa->bos[1], pre, pre); sl@0: } sl@0: sl@0: /* sl@0: * Now here's the subtle part. Because many REs have no lookback sl@0: * constraints, often knowing when you were in the pre state tells sl@0: * you little; it's the next state(s) that are informative. But sl@0: * some of them may have other inarcs, i.e. it may be possible to sl@0: * make actual progress and then return to one of them. We must sl@0: * de-optimize such cases, splitting each such state into progress sl@0: * and no-progress states. sl@0: */ sl@0: sl@0: /* first, make a list of the states */ sl@0: slist = NULL; sl@0: for (a = pre->outs; a != NULL; a = a->outchain) { sl@0: s = a->to; sl@0: for (b = s->ins; b != NULL; b = b->inchain) sl@0: if (b->from != pre) sl@0: break; sl@0: if (b != NULL) { /* must be split */ sl@0: if (s->tmp == NULL) { /* if not already in the list */ sl@0: /* (fixes bugs 505048, 230589, */ sl@0: /* 840258, 504785) */ sl@0: s->tmp = slist; sl@0: slist = s; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* do the splits */ sl@0: for (s = slist; s != NULL; s = s2) { sl@0: s2 = newstate(nfa); sl@0: copyouts(nfa, s, s2); sl@0: for (a = s->ins; a != NULL; a = b) { sl@0: b = a->inchain; sl@0: if (a->from != pre) { sl@0: cparc(nfa, a, a->from, s2); sl@0: freearc(nfa, a); sl@0: } sl@0: } sl@0: s2 = s->tmp; sl@0: s->tmp = NULL; /* clean up while we're at it */ sl@0: } sl@0: } sl@0: sl@0: /* sl@0: - parse - parse an RE sl@0: * This is actually just the top level, which parses a bunch of branches sl@0: * tied together with '|'. They appear in the tree as the left children sl@0: * of a chain of '|' subres. sl@0: ^ static struct subre *parse(struct vars *, int, int, struct state *, sl@0: ^ struct state *); sl@0: */ sl@0: static struct subre * sl@0: parse(v, stopper, type, init, final) sl@0: struct vars *v; sl@0: int stopper; /* EOS or ')' */ sl@0: int type; /* LACON (lookahead subRE) or PLAIN */ sl@0: struct state *init; /* initial state */ sl@0: struct state *final; /* final state */ sl@0: { sl@0: struct state *left; /* scaffolding for branch */ sl@0: struct state *right; sl@0: struct subre *branches; /* top level */ sl@0: struct subre *branch; /* current branch */ sl@0: struct subre *t; /* temporary */ sl@0: int firstbranch; /* is this the first branch? */ sl@0: sl@0: assert(stopper == ')' || stopper == EOS); sl@0: sl@0: branches = subre(v, '|', LONGER, init, final); sl@0: NOERRN(); sl@0: branch = branches; sl@0: firstbranch = 1; sl@0: do { /* a branch */ sl@0: if (!firstbranch) { sl@0: /* need a place to hang it */ sl@0: branch->right = subre(v, '|', LONGER, init, final); sl@0: NOERRN(); sl@0: branch = branch->right; sl@0: } sl@0: firstbranch = 0; sl@0: left = newstate(v->nfa); sl@0: right = newstate(v->nfa); sl@0: NOERRN(); sl@0: EMPTYARC(init, left); sl@0: EMPTYARC(right, final); sl@0: NOERRN(); sl@0: branch->left = parsebranch(v, stopper, type, left, right, 0); sl@0: NOERRN(); sl@0: branch->flags |= UP(branch->flags | branch->left->flags); sl@0: if ((branch->flags &~ branches->flags) != 0) /* new flags */ sl@0: for (t = branches; t != branch; t = t->right) sl@0: t->flags |= branch->flags; sl@0: } while (EAT('|')); sl@0: assert(SEE(stopper) || SEE(EOS)); sl@0: sl@0: if (!SEE(stopper)) { sl@0: assert(stopper == ')' && SEE(EOS)); sl@0: ERR(REG_EPAREN); sl@0: } sl@0: sl@0: /* optimize out simple cases */ sl@0: if (branch == branches) { /* only one branch */ sl@0: assert(branch->right == NULL); sl@0: t = branch->left; sl@0: branch->left = NULL; sl@0: freesubre(v, branches); sl@0: branches = t; sl@0: } else if (!MESSY(branches->flags)) { /* no interesting innards */ sl@0: freesubre(v, branches->left); sl@0: branches->left = NULL; sl@0: freesubre(v, branches->right); sl@0: branches->right = NULL; sl@0: branches->op = '='; sl@0: } sl@0: sl@0: return branches; sl@0: } sl@0: sl@0: /* sl@0: - parsebranch - parse one branch of an RE sl@0: * This mostly manages concatenation, working closely with parseqatom(). sl@0: * Concatenated things are bundled up as much as possible, with separate sl@0: * ',' nodes introduced only when necessary due to substructure. sl@0: ^ static struct subre *parsebranch(struct vars *, int, int, struct state *, sl@0: ^ struct state *, int); sl@0: */ sl@0: static struct subre * sl@0: parsebranch(v, stopper, type, left, right, partial) sl@0: struct vars *v; sl@0: int stopper; /* EOS or ')' */ sl@0: int type; /* LACON (lookahead subRE) or PLAIN */ sl@0: struct state *left; /* leftmost state */ sl@0: struct state *right; /* rightmost state */ sl@0: int partial; /* is this only part of a branch? */ sl@0: { sl@0: struct state *lp; /* left end of current construct */ sl@0: int seencontent; /* is there anything in this branch yet? */ sl@0: struct subre *t; sl@0: sl@0: lp = left; sl@0: seencontent = 0; sl@0: t = subre(v, '=', 0, left, right); /* op '=' is tentative */ sl@0: NOERRN(); sl@0: while (!SEE('|') && !SEE(stopper) && !SEE(EOS)) { sl@0: if (seencontent) { /* implicit concat operator */ sl@0: lp = newstate(v->nfa); sl@0: NOERRN(); sl@0: moveins(v->nfa, right, lp); sl@0: } sl@0: seencontent = 1; sl@0: sl@0: /* NB, recursion in parseqatom() may swallow rest of branch */ sl@0: parseqatom(v, stopper, type, lp, right, t); sl@0: } sl@0: sl@0: if (!seencontent) { /* empty branch */ sl@0: if (!partial) sl@0: NOTE(REG_UUNSPEC); sl@0: assert(lp == left); sl@0: EMPTYARC(left, right); sl@0: } sl@0: sl@0: return t; sl@0: } sl@0: sl@0: /* sl@0: - parseqatom - parse one quantified atom or constraint of an RE sl@0: * The bookkeeping near the end cooperates very closely with parsebranch(); sl@0: * in particular, it contains a recursion that can involve parsing the rest sl@0: * of the branch, making this function's name somewhat inaccurate. sl@0: ^ static VOID parseqatom(struct vars *, int, int, struct state *, sl@0: ^ struct state *, struct subre *); sl@0: */ sl@0: static VOID sl@0: parseqatom(v, stopper, type, lp, rp, top) sl@0: struct vars *v; sl@0: int stopper; /* EOS or ')' */ sl@0: int type; /* LACON (lookahead subRE) or PLAIN */ sl@0: struct state *lp; /* left state to hang it on */ sl@0: struct state *rp; /* right state to hang it on */ sl@0: struct subre *top; /* subtree top */ sl@0: { sl@0: struct state *s; /* temporaries for new states */ sl@0: struct state *s2; sl@0: # define ARCV(t, val) newarc(v->nfa, t, val, lp, rp) sl@0: int m, n; sl@0: struct subre *atom; /* atom's subtree */ sl@0: struct subre *t; sl@0: int cap; /* capturing parens? */ sl@0: int pos; /* positive lookahead? */ sl@0: int subno; /* capturing-parens or backref number */ sl@0: int atomtype; sl@0: int qprefer; /* quantifier short/long preference */ sl@0: int f; sl@0: struct subre **atomp; /* where the pointer to atom is */ sl@0: sl@0: /* initial bookkeeping */ sl@0: atom = NULL; sl@0: assert(lp->nouts == 0); /* must string new code */ sl@0: assert(rp->nins == 0); /* between lp and rp */ sl@0: subno = 0; /* just to shut lint up */ sl@0: sl@0: /* an atom or constraint... */ sl@0: atomtype = v->nexttype; sl@0: switch (atomtype) { sl@0: /* first, constraints, which end by returning */ sl@0: case '^': sl@0: ARCV('^', 1); sl@0: if (v->cflags®_NLANCH) sl@0: ARCV(BEHIND, v->nlcolor); sl@0: NEXT(); sl@0: return; sl@0: break; sl@0: case '$': sl@0: ARCV('$', 1); sl@0: if (v->cflags®_NLANCH) sl@0: ARCV(AHEAD, v->nlcolor); sl@0: NEXT(); sl@0: return; sl@0: break; sl@0: case SBEGIN: sl@0: ARCV('^', 1); /* BOL */ sl@0: ARCV('^', 0); /* or BOS */ sl@0: NEXT(); sl@0: return; sl@0: break; sl@0: case SEND: sl@0: ARCV('$', 1); /* EOL */ sl@0: ARCV('$', 0); /* or EOS */ sl@0: NEXT(); sl@0: return; sl@0: break; sl@0: case '<': sl@0: wordchrs(v); /* does NEXT() */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: nonword(v, BEHIND, lp, s); sl@0: word(v, AHEAD, s, rp); sl@0: return; sl@0: break; sl@0: case '>': sl@0: wordchrs(v); /* does NEXT() */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: word(v, BEHIND, lp, s); sl@0: nonword(v, AHEAD, s, rp); sl@0: return; sl@0: break; sl@0: case WBDRY: sl@0: wordchrs(v); /* does NEXT() */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: nonword(v, BEHIND, lp, s); sl@0: word(v, AHEAD, s, rp); sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: word(v, BEHIND, lp, s); sl@0: nonword(v, AHEAD, s, rp); sl@0: return; sl@0: break; sl@0: case NWBDRY: sl@0: wordchrs(v); /* does NEXT() */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: word(v, BEHIND, lp, s); sl@0: word(v, AHEAD, s, rp); sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: nonword(v, BEHIND, lp, s); sl@0: nonword(v, AHEAD, s, rp); sl@0: return; sl@0: break; sl@0: case LACON: /* lookahead constraint */ sl@0: pos = v->nextvalue; sl@0: NEXT(); sl@0: s = newstate(v->nfa); sl@0: s2 = newstate(v->nfa); sl@0: NOERR(); sl@0: t = parse(v, ')', LACON, s, s2); sl@0: freesubre(v, t); /* internal structure irrelevant */ sl@0: assert(SEE(')') || ISERR()); sl@0: NEXT(); sl@0: n = newlacon(v, s, s2, pos); sl@0: NOERR(); sl@0: ARCV(LACON, n); sl@0: return; sl@0: break; sl@0: /* then errors, to get them out of the way */ sl@0: case '*': sl@0: case '+': sl@0: case '?': sl@0: case '{': sl@0: ERR(REG_BADRPT); sl@0: return; sl@0: break; sl@0: default: sl@0: ERR(REG_ASSERT); sl@0: return; sl@0: break; sl@0: /* then plain characters, and minor variants on that theme */ sl@0: case ')': /* unbalanced paren */ sl@0: if ((v->cflags®_ADVANCED) != REG_EXTENDED) { sl@0: ERR(REG_EPAREN); sl@0: return; sl@0: } sl@0: /* legal in EREs due to specification botch */ sl@0: NOTE(REG_UPBOTCH); sl@0: /* fallthrough into case PLAIN */ sl@0: case PLAIN: sl@0: onechr(v, v->nextvalue, lp, rp); sl@0: okcolors(v->nfa, v->cm); sl@0: NOERR(); sl@0: NEXT(); sl@0: break; sl@0: case '[': sl@0: if (v->nextvalue == 1) sl@0: bracket(v, lp, rp); sl@0: else sl@0: cbracket(v, lp, rp); sl@0: assert(SEE(']') || ISERR()); sl@0: NEXT(); sl@0: break; sl@0: case '.': sl@0: rainbow(v->nfa, v->cm, PLAIN, sl@0: (v->cflags®_NLSTOP) ? v->nlcolor : COLORLESS, sl@0: lp, rp); sl@0: NEXT(); sl@0: break; sl@0: /* and finally the ugly stuff */ sl@0: case '(': /* value flags as capturing or non */ sl@0: cap = (type == LACON) ? 0 : v->nextvalue; sl@0: if (cap) { sl@0: v->nsubexp++; sl@0: subno = v->nsubexp; sl@0: if ((size_t)subno >= v->nsubs) sl@0: moresubs(v, subno); sl@0: assert((size_t)subno < v->nsubs); sl@0: } else sl@0: atomtype = PLAIN; /* something that's not '(' */ sl@0: NEXT(); sl@0: /* need new endpoints because tree will contain pointers */ sl@0: s = newstate(v->nfa); sl@0: s2 = newstate(v->nfa); sl@0: NOERR(); sl@0: EMPTYARC(lp, s); sl@0: EMPTYARC(s2, rp); sl@0: NOERR(); sl@0: atom = parse(v, ')', PLAIN, s, s2); sl@0: assert(SEE(')') || ISERR()); sl@0: NEXT(); sl@0: NOERR(); sl@0: if (cap) { sl@0: v->subs[subno] = atom; sl@0: t = subre(v, '(', atom->flags|CAP, lp, rp); sl@0: NOERR(); sl@0: t->subno = subno; sl@0: t->left = atom; sl@0: atom = t; sl@0: } sl@0: /* postpone everything else pending possible {0} */ sl@0: break; sl@0: case BACKREF: /* the Feature From The Black Lagoon */ sl@0: INSIST(type != LACON, REG_ESUBREG); sl@0: INSIST(v->nextvalue < v->nsubs, REG_ESUBREG); sl@0: INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG); sl@0: NOERR(); sl@0: assert(v->nextvalue > 0); sl@0: atom = subre(v, 'b', BACKR, lp, rp); sl@0: subno = v->nextvalue; sl@0: atom->subno = subno; sl@0: EMPTYARC(lp, rp); /* temporarily, so there's something */ sl@0: NEXT(); sl@0: break; sl@0: } sl@0: sl@0: /* ...and an atom may be followed by a quantifier */ sl@0: switch (v->nexttype) { sl@0: case '*': sl@0: m = 0; sl@0: n = INFINITY; sl@0: qprefer = (v->nextvalue) ? LONGER : SHORTER; sl@0: NEXT(); sl@0: break; sl@0: case '+': sl@0: m = 1; sl@0: n = INFINITY; sl@0: qprefer = (v->nextvalue) ? LONGER : SHORTER; sl@0: NEXT(); sl@0: break; sl@0: case '?': sl@0: m = 0; sl@0: n = 1; sl@0: qprefer = (v->nextvalue) ? LONGER : SHORTER; sl@0: NEXT(); sl@0: break; sl@0: case '{': sl@0: NEXT(); sl@0: m = scannum(v); sl@0: if (EAT(',')) { sl@0: if (SEE(DIGIT)) sl@0: n = scannum(v); sl@0: else sl@0: n = INFINITY; sl@0: if (m > n) { sl@0: ERR(REG_BADBR); sl@0: return; sl@0: } sl@0: /* {m,n} exercises preference, even if it's {m,m} */ sl@0: qprefer = (v->nextvalue) ? LONGER : SHORTER; sl@0: } else { sl@0: n = m; sl@0: /* {m} passes operand's preference through */ sl@0: qprefer = 0; sl@0: } sl@0: if (!SEE('}')) { /* catches errors too */ sl@0: ERR(REG_BADBR); sl@0: return; sl@0: } sl@0: NEXT(); sl@0: break; sl@0: default: /* no quantifier */ sl@0: m = n = 1; sl@0: qprefer = 0; sl@0: break; sl@0: } sl@0: sl@0: /* annoying special case: {0} or {0,0} cancels everything */ sl@0: if (m == 0 && n == 0) { sl@0: if (atom != NULL) sl@0: freesubre(v, atom); sl@0: if (atomtype == '(') sl@0: v->subs[subno] = NULL; sl@0: delsub(v->nfa, lp, rp); sl@0: EMPTYARC(lp, rp); sl@0: return; sl@0: } sl@0: sl@0: /* if not a messy case, avoid hard part */ sl@0: assert(!MESSY(top->flags)); sl@0: f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0); sl@0: if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f))) { sl@0: if (!(m == 1 && n == 1)) sl@0: repeat(v, lp, rp, m, n); sl@0: if (atom != NULL) sl@0: freesubre(v, atom); sl@0: top->flags = f; sl@0: return; sl@0: } sl@0: sl@0: /* sl@0: * hard part: something messy sl@0: * That is, capturing parens, back reference, short/long clash, or sl@0: * an atom with substructure containing one of those. sl@0: */ sl@0: sl@0: /* now we'll need a subre for the contents even if they're boring */ sl@0: if (atom == NULL) { sl@0: atom = subre(v, '=', 0, lp, rp); sl@0: NOERR(); sl@0: } sl@0: sl@0: /* sl@0: * prepare a general-purpose state skeleton sl@0: * sl@0: * ---> [s] ---prefix---> [begin] ---atom---> [end] ----rest---> [rp] sl@0: * / / sl@0: * [lp] ----> [s2] ----bypass--------------------- sl@0: * sl@0: * where bypass is an empty, and prefix is some repetitions of atom sl@0: */ sl@0: s = newstate(v->nfa); /* first, new endpoints for the atom */ sl@0: s2 = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: moveins(v->nfa, rp, s2); sl@0: NOERR(); sl@0: atom->begin = s; sl@0: atom->end = s2; sl@0: s = newstate(v->nfa); /* and spots for prefix and bypass */ sl@0: s2 = newstate(v->nfa); sl@0: NOERR(); sl@0: EMPTYARC(lp, s); sl@0: EMPTYARC(lp, s2); sl@0: NOERR(); sl@0: sl@0: /* break remaining subRE into x{...} and what follows */ sl@0: t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp); sl@0: t->left = atom; sl@0: atomp = &t->left; sl@0: /* here we should recurse... but we must postpone that to the end */ sl@0: sl@0: /* split top into prefix and remaining */ sl@0: assert(top->op == '=' && top->left == NULL && top->right == NULL); sl@0: top->left = subre(v, '=', top->flags, top->begin, lp); sl@0: top->op = '.'; sl@0: top->right = t; sl@0: sl@0: /* if it's a backref, now is the time to replicate the subNFA */ sl@0: if (atomtype == BACKREF) { sl@0: assert(atom->begin->nouts == 1); /* just the EMPTY */ sl@0: delsub(v->nfa, atom->begin, atom->end); sl@0: assert(v->subs[subno] != NULL); sl@0: /* and here's why the recursion got postponed: it must */ sl@0: /* wait until the skeleton is filled in, because it may */ sl@0: /* hit a backref that wants to copy the filled-in skeleton */ sl@0: dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end, sl@0: atom->begin, atom->end); sl@0: NOERR(); sl@0: } sl@0: sl@0: /* it's quantifier time; first, turn x{0,...} into x{1,...}|empty */ sl@0: if (m == 0) { sl@0: EMPTYARC(s2, atom->end); /* the bypass */ sl@0: assert(PREF(qprefer) != 0); sl@0: f = COMBINE(qprefer, atom->flags); sl@0: t = subre(v, '|', f, lp, atom->end); sl@0: NOERR(); sl@0: t->left = atom; sl@0: t->right = subre(v, '|', PREF(f), s2, atom->end); sl@0: NOERR(); sl@0: t->right->left = subre(v, '=', 0, s2, atom->end); sl@0: NOERR(); sl@0: *atomp = t; sl@0: atomp = &t->left; sl@0: m = 1; sl@0: } sl@0: sl@0: /* deal with the rest of the quantifier */ sl@0: if (atomtype == BACKREF) { sl@0: /* special case: backrefs have internal quantifiers */ sl@0: EMPTYARC(s, atom->begin); /* empty prefix */ sl@0: /* just stuff everything into atom */ sl@0: repeat(v, atom->begin, atom->end, m, n); sl@0: atom->min = (short)m; sl@0: atom->max = (short)n; sl@0: atom->flags |= COMBINE(qprefer, atom->flags); sl@0: } else if (m == 1 && n == 1) { sl@0: /* no/vacuous quantifier: done */ sl@0: EMPTYARC(s, atom->begin); /* empty prefix */ sl@0: } else { sl@0: /* turn x{m,n} into x{m-1,n-1}x, with capturing */ sl@0: /* parens in only second x */ sl@0: dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin); sl@0: assert(m >= 1 && m != INFINITY && n >= 1); sl@0: repeat(v, s, atom->begin, m-1, (n == INFINITY) ? n : n-1); sl@0: f = COMBINE(qprefer, atom->flags); sl@0: t = subre(v, '.', f, s, atom->end); /* prefix and atom */ sl@0: NOERR(); sl@0: t->left = subre(v, '=', PREF(f), s, atom->begin); sl@0: NOERR(); sl@0: t->right = atom; sl@0: *atomp = t; sl@0: } sl@0: sl@0: /* and finally, look after that postponed recursion */ sl@0: t = top->right; sl@0: if (!(SEE('|') || SEE(stopper) || SEE(EOS))) sl@0: t->right = parsebranch(v, stopper, type, atom->end, rp, 1); sl@0: else { sl@0: EMPTYARC(atom->end, rp); sl@0: t->right = subre(v, '=', 0, atom->end, rp); sl@0: } sl@0: assert(SEE('|') || SEE(stopper) || SEE(EOS)); sl@0: t->flags |= COMBINE(t->flags, t->right->flags); sl@0: top->flags |= COMBINE(top->flags, t->flags); sl@0: } sl@0: sl@0: /* sl@0: - nonword - generate arcs for non-word-character ahead or behind sl@0: ^ static VOID nonword(struct vars *, int, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: nonword(v, dir, lp, rp) sl@0: struct vars *v; sl@0: int dir; /* AHEAD or BEHIND */ sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: int anchor = (dir == AHEAD) ? '$' : '^'; sl@0: sl@0: assert(dir == AHEAD || dir == BEHIND); sl@0: newarc(v->nfa, anchor, 1, lp, rp); sl@0: newarc(v->nfa, anchor, 0, lp, rp); sl@0: colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp); sl@0: /* (no need for special attention to \n) */ sl@0: } sl@0: sl@0: /* sl@0: - word - generate arcs for word character ahead or behind sl@0: ^ static VOID word(struct vars *, int, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: word(v, dir, lp, rp) sl@0: struct vars *v; sl@0: int dir; /* AHEAD or BEHIND */ sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: assert(dir == AHEAD || dir == BEHIND); sl@0: cloneouts(v->nfa, v->wordchrs, lp, rp, dir); sl@0: /* (no need for special attention to \n) */ sl@0: } sl@0: sl@0: /* sl@0: - scannum - scan a number sl@0: ^ static int scannum(struct vars *); sl@0: */ sl@0: static int /* value, <= DUPMAX */ sl@0: scannum(v) sl@0: struct vars *v; sl@0: { sl@0: int n = 0; sl@0: sl@0: while (SEE(DIGIT) && n < DUPMAX) { sl@0: n = n*10 + v->nextvalue; sl@0: NEXT(); sl@0: } sl@0: if (SEE(DIGIT) || n > DUPMAX) { sl@0: ERR(REG_BADBR); sl@0: return 0; sl@0: } sl@0: return n; sl@0: } sl@0: sl@0: /* sl@0: - repeat - replicate subNFA for quantifiers sl@0: * The duplication sequences used here are chosen carefully so that any sl@0: * pointers starting out pointing into the subexpression end up pointing into sl@0: * the last occurrence. (Note that it may not be strung between the same sl@0: * left and right end states, however!) This used to be important for the sl@0: * subRE tree, although the important bits are now handled by the in-line sl@0: * code in parse(), and when this is called, it doesn't matter any more. sl@0: ^ static VOID repeat(struct vars *, struct state *, struct state *, int, int); sl@0: */ sl@0: static VOID sl@0: repeat(v, lp, rp, m, n) sl@0: struct vars *v; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: int m; sl@0: int n; sl@0: { sl@0: # define SOME 2 sl@0: # define INF 3 sl@0: # define PAIR(x, y) ((x)*4 + (y)) sl@0: # define REDUCE(x) ( ((x) == INFINITY) ? INF : (((x) > 1) ? SOME : (x)) ) sl@0: CONST int rm = REDUCE(m); sl@0: CONST int rn = REDUCE(n); sl@0: struct state *s; sl@0: struct state *s2; sl@0: sl@0: switch (PAIR(rm, rn)) { sl@0: case PAIR(0, 0): /* empty string */ sl@0: delsub(v->nfa, lp, rp); sl@0: EMPTYARC(lp, rp); sl@0: break; sl@0: case PAIR(0, 1): /* do as x| */ sl@0: EMPTYARC(lp, rp); sl@0: break; sl@0: case PAIR(0, SOME): /* do as x{1,n}| */ sl@0: repeat(v, lp, rp, 1, n); sl@0: NOERR(); sl@0: EMPTYARC(lp, rp); sl@0: break; sl@0: case PAIR(0, INF): /* loop x around */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: moveins(v->nfa, rp, s); sl@0: EMPTYARC(lp, s); sl@0: EMPTYARC(s, rp); sl@0: break; sl@0: case PAIR(1, 1): /* no action required */ sl@0: break; sl@0: case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: dupnfa(v->nfa, s, rp, lp, s); sl@0: NOERR(); sl@0: repeat(v, lp, s, 1, n-1); sl@0: NOERR(); sl@0: EMPTYARC(lp, s); sl@0: break; sl@0: case PAIR(1, INF): /* add loopback arc */ sl@0: s = newstate(v->nfa); sl@0: s2 = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: moveins(v->nfa, rp, s2); sl@0: EMPTYARC(lp, s); sl@0: EMPTYARC(s2, rp); sl@0: EMPTYARC(s2, s); sl@0: break; sl@0: case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: dupnfa(v->nfa, s, rp, lp, s); sl@0: NOERR(); sl@0: repeat(v, lp, s, m-1, n-1); sl@0: break; sl@0: case PAIR(SOME, INF): /* do as x{m-1,}x */ sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: moveouts(v->nfa, lp, s); sl@0: dupnfa(v->nfa, s, rp, lp, s); sl@0: NOERR(); sl@0: repeat(v, lp, s, m-1, n); sl@0: break; sl@0: default: sl@0: ERR(REG_ASSERT); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: - bracket - handle non-complemented bracket expression sl@0: * Also called from cbracket for complemented bracket expressions. sl@0: ^ static VOID bracket(struct vars *, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: bracket(v, lp, rp) sl@0: struct vars *v; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: assert(SEE('[')); sl@0: NEXT(); sl@0: while (!SEE(']') && !SEE(EOS)) sl@0: brackpart(v, lp, rp); sl@0: assert(SEE(']') || ISERR()); sl@0: okcolors(v->nfa, v->cm); sl@0: } sl@0: sl@0: /* sl@0: - cbracket - handle complemented bracket expression sl@0: * We do it by calling bracket() with dummy endpoints, and then complementing sl@0: * the result. The alternative would be to invoke rainbow(), and then delete sl@0: * arcs as the b.e. is seen... but that gets messy. sl@0: ^ static VOID cbracket(struct vars *, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: cbracket(v, lp, rp) sl@0: struct vars *v; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: struct state *left = newstate(v->nfa); sl@0: struct state *right = newstate(v->nfa); sl@0: struct state *s; sl@0: struct arc *a; /* arc from lp */ sl@0: struct arc *ba; /* arc from left, from bracket() */ sl@0: struct arc *pa; /* MCCE-prototype arc */ sl@0: color co; sl@0: chr *p; sl@0: int i; sl@0: sl@0: NOERR(); sl@0: bracket(v, left, right); sl@0: if (v->cflags®_NLSTOP) sl@0: newarc(v->nfa, PLAIN, v->nlcolor, left, right); sl@0: NOERR(); sl@0: sl@0: assert(lp->nouts == 0); /* all outarcs will be ours */ sl@0: sl@0: /* easy part of complementing */ sl@0: colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp); sl@0: NOERR(); sl@0: if (v->mcces == NULL) { /* no MCCEs -- we're done */ sl@0: dropstate(v->nfa, left); sl@0: assert(right->nins == 0); sl@0: freestate(v->nfa, right); sl@0: return; sl@0: } sl@0: sl@0: /* but complementing gets messy in the presence of MCCEs... */ sl@0: NOTE(REG_ULOCALE); sl@0: for (p = v->mcces->chrs, i = v->mcces->nchrs; i > 0; p++, i--) { sl@0: co = GETCOLOR(v->cm, *p); sl@0: a = findarc(lp, PLAIN, co); sl@0: ba = findarc(left, PLAIN, co); sl@0: if (ba == NULL) { sl@0: assert(a != NULL); sl@0: freearc(v->nfa, a); sl@0: } else { sl@0: assert(a == NULL); sl@0: } sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: newarc(v->nfa, PLAIN, co, lp, s); sl@0: NOERR(); sl@0: pa = findarc(v->mccepbegin, PLAIN, co); sl@0: assert(pa != NULL); sl@0: if (ba == NULL) { /* easy case, need all of them */ sl@0: cloneouts(v->nfa, pa->to, s, rp, PLAIN); sl@0: newarc(v->nfa, '$', 1, s, rp); sl@0: newarc(v->nfa, '$', 0, s, rp); sl@0: colorcomplement(v->nfa, v->cm, AHEAD, pa->to, s, rp); sl@0: } else { /* must be selective */ sl@0: if (findarc(ba->to, '$', 1) == NULL) { sl@0: newarc(v->nfa, '$', 1, s, rp); sl@0: newarc(v->nfa, '$', 0, s, rp); sl@0: colorcomplement(v->nfa, v->cm, AHEAD, pa->to, sl@0: s, rp); sl@0: } sl@0: for (pa = pa->to->outs; pa != NULL; pa = pa->outchain) sl@0: if (findarc(ba->to, PLAIN, pa->co) == NULL) sl@0: newarc(v->nfa, PLAIN, pa->co, s, rp); sl@0: if (s->nouts == 0) /* limit of selectivity: none */ sl@0: dropstate(v->nfa, s); /* frees arc too */ sl@0: } sl@0: NOERR(); sl@0: } sl@0: sl@0: delsub(v->nfa, left, right); sl@0: assert(left->nouts == 0); sl@0: freestate(v->nfa, left); sl@0: assert(right->nins == 0); sl@0: freestate(v->nfa, right); sl@0: } sl@0: sl@0: /* sl@0: - brackpart - handle one item (or range) within a bracket expression sl@0: ^ static VOID brackpart(struct vars *, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: brackpart(v, lp, rp) sl@0: struct vars *v; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: celt startc; sl@0: celt endc; sl@0: struct cvec *cv; sl@0: chr *startp; sl@0: chr *endp; sl@0: chr c[1]; sl@0: sl@0: /* parse something, get rid of special cases, take shortcuts */ sl@0: switch (v->nexttype) { sl@0: case RANGE: /* a-b-c or other botch */ sl@0: ERR(REG_ERANGE); sl@0: return; sl@0: break; sl@0: case PLAIN: sl@0: c[0] = v->nextvalue; sl@0: NEXT(); sl@0: /* shortcut for ordinary chr (not range, not MCCE leader) */ sl@0: if (!SEE(RANGE) && !ISCELEADER(v, c[0])) { sl@0: onechr(v, c[0], lp, rp); sl@0: return; sl@0: } sl@0: startc = element(v, c, c+1); sl@0: NOERR(); sl@0: break; sl@0: case COLLEL: sl@0: startp = v->now; sl@0: endp = scanplain(v); sl@0: INSIST(startp < endp, REG_ECOLLATE); sl@0: NOERR(); sl@0: startc = element(v, startp, endp); sl@0: NOERR(); sl@0: break; sl@0: case ECLASS: sl@0: startp = v->now; sl@0: endp = scanplain(v); sl@0: INSIST(startp < endp, REG_ECOLLATE); sl@0: NOERR(); sl@0: startc = element(v, startp, endp); sl@0: NOERR(); sl@0: cv = eclass(v, startc, (v->cflags®_ICASE)); sl@0: NOERR(); sl@0: dovec(v, cv, lp, rp); sl@0: return; sl@0: break; sl@0: case CCLASS: sl@0: startp = v->now; sl@0: endp = scanplain(v); sl@0: INSIST(startp < endp, REG_ECTYPE); sl@0: NOERR(); sl@0: cv = cclass(v, startp, endp, (v->cflags®_ICASE)); sl@0: NOERR(); sl@0: dovec(v, cv, lp, rp); sl@0: return; sl@0: break; sl@0: default: sl@0: ERR(REG_ASSERT); sl@0: return; sl@0: break; sl@0: } sl@0: sl@0: if (SEE(RANGE)) { sl@0: NEXT(); sl@0: switch (v->nexttype) { sl@0: case PLAIN: sl@0: case RANGE: sl@0: c[0] = v->nextvalue; sl@0: NEXT(); sl@0: endc = element(v, c, c+1); sl@0: NOERR(); sl@0: break; sl@0: case COLLEL: sl@0: startp = v->now; sl@0: endp = scanplain(v); sl@0: INSIST(startp < endp, REG_ECOLLATE); sl@0: NOERR(); sl@0: endc = element(v, startp, endp); sl@0: NOERR(); sl@0: break; sl@0: default: sl@0: ERR(REG_ERANGE); sl@0: return; sl@0: break; sl@0: } sl@0: } else sl@0: endc = startc; sl@0: sl@0: /* sl@0: * Ranges are unportable. Actually, standard C does sl@0: * guarantee that digits are contiguous, but making sl@0: * that an exception is just too complicated. sl@0: */ sl@0: if (startc != endc) sl@0: NOTE(REG_UUNPORT); sl@0: cv = range(v, startc, endc, (v->cflags®_ICASE)); sl@0: NOERR(); sl@0: dovec(v, cv, lp, rp); sl@0: } sl@0: sl@0: /* sl@0: - scanplain - scan PLAIN contents of [. etc. sl@0: * Certain bits of trickery in lex.c know that this code does not try sl@0: * to look past the final bracket of the [. etc. sl@0: ^ static chr *scanplain(struct vars *); sl@0: */ sl@0: static chr * /* just after end of sequence */ sl@0: scanplain(v) sl@0: struct vars *v; sl@0: { sl@0: chr *endp; sl@0: sl@0: assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS)); sl@0: NEXT(); sl@0: sl@0: endp = v->now; sl@0: while (SEE(PLAIN)) { sl@0: endp = v->now; sl@0: NEXT(); sl@0: } sl@0: sl@0: assert(SEE(END) || ISERR()); sl@0: NEXT(); sl@0: sl@0: return endp; sl@0: } sl@0: sl@0: /* sl@0: - leaders - process a cvec of collating elements to also include leaders sl@0: * Also gives all characters involved their own colors, which is almost sl@0: * certainly necessary, and sets up little disconnected subNFA. sl@0: ^ static VOID leaders(struct vars *, struct cvec *); sl@0: */ sl@0: static VOID sl@0: leaders(v, cv) sl@0: struct vars *v; sl@0: struct cvec *cv; sl@0: { sl@0: int mcce; sl@0: chr *p; sl@0: chr leader; sl@0: struct state *s; sl@0: struct arc *a; sl@0: sl@0: v->mccepbegin = newstate(v->nfa); sl@0: v->mccepend = newstate(v->nfa); sl@0: NOERR(); sl@0: sl@0: for (mcce = 0; mcce < cv->nmcces; mcce++) { sl@0: p = cv->mcces[mcce]; sl@0: leader = *p; sl@0: if (!haschr(cv, leader)) { sl@0: addchr(cv, leader); sl@0: s = newstate(v->nfa); sl@0: newarc(v->nfa, PLAIN, subcolor(v->cm, leader), sl@0: v->mccepbegin, s); sl@0: okcolors(v->nfa, v->cm); sl@0: } else { sl@0: a = findarc(v->mccepbegin, PLAIN, sl@0: GETCOLOR(v->cm, leader)); sl@0: assert(a != NULL); sl@0: s = a->to; sl@0: assert(s != v->mccepend); sl@0: } sl@0: p++; sl@0: assert(*p != 0 && *(p+1) == 0); /* only 2-char MCCEs for now */ sl@0: newarc(v->nfa, PLAIN, subcolor(v->cm, *p), s, v->mccepend); sl@0: okcolors(v->nfa, v->cm); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: - onechr - fill in arcs for a plain character, and possible case complements sl@0: * This is mostly a shortcut for efficient handling of the common case. sl@0: ^ static VOID onechr(struct vars *, pchr, struct state *, struct state *); sl@0: */ sl@0: static VOID sl@0: onechr(v, c, lp, rp) sl@0: struct vars *v; sl@0: pchr c; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: if (!(v->cflags®_ICASE)) { sl@0: newarc(v->nfa, PLAIN, subcolor(v->cm, c), lp, rp); sl@0: return; sl@0: } sl@0: sl@0: /* rats, need general case anyway... */ sl@0: dovec(v, allcases(v, c), lp, rp); sl@0: } sl@0: sl@0: /* sl@0: - dovec - fill in arcs for each element of a cvec sl@0: * This one has to handle the messy cases, like MCCEs and MCCE leaders. sl@0: ^ static VOID dovec(struct vars *, struct cvec *, struct state *, sl@0: ^ struct state *); sl@0: */ sl@0: static VOID sl@0: dovec(v, cv, lp, rp) sl@0: struct vars *v; sl@0: struct cvec *cv; sl@0: struct state *lp; sl@0: struct state *rp; sl@0: { sl@0: chr ch, from, to; sl@0: celt ce; sl@0: chr *p; sl@0: int i; sl@0: color co; sl@0: struct cvec *leads; sl@0: struct arc *a; sl@0: struct arc *pa; /* arc in prototype */ sl@0: struct state *s; sl@0: struct state *ps; /* state in prototype */ sl@0: sl@0: /* need a place to store leaders, if any */ sl@0: if (nmcces(v) > 0) { sl@0: assert(v->mcces != NULL); sl@0: if (v->cv2 == NULL || v->cv2->nchrs < v->mcces->nchrs) { sl@0: if (v->cv2 != NULL) sl@0: free(v->cv2); sl@0: v->cv2 = newcvec(v->mcces->nchrs, 0, v->mcces->nmcces); sl@0: NOERR(); sl@0: leads = v->cv2; sl@0: } else sl@0: leads = clearcvec(v->cv2); sl@0: } else sl@0: leads = NULL; sl@0: sl@0: /* first, get the ordinary characters out of the way */ sl@0: for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--) { sl@0: ch = *p; sl@0: if (!ISCELEADER(v, ch)) sl@0: newarc(v->nfa, PLAIN, subcolor(v->cm, ch), lp, rp); sl@0: else { sl@0: assert(singleton(v->cm, ch)); sl@0: assert(leads != NULL); sl@0: if (!haschr(leads, ch)) sl@0: addchr(leads, ch); sl@0: } sl@0: } sl@0: sl@0: /* and the ranges */ sl@0: for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--) { sl@0: from = *p; sl@0: to = *(p+1); sl@0: while (from <= to && (ce = nextleader(v, from, to)) != NOCELT) { sl@0: if (from < ce) sl@0: subrange(v, from, ce - 1, lp, rp); sl@0: assert(singleton(v->cm, ce)); sl@0: assert(leads != NULL); sl@0: if (!haschr(leads, ce)) sl@0: addchr(leads, ce); sl@0: from = ce + 1; sl@0: } sl@0: if (from <= to) sl@0: subrange(v, from, to, lp, rp); sl@0: } sl@0: sl@0: if ((leads == NULL || leads->nchrs == 0) && cv->nmcces == 0) sl@0: return; sl@0: sl@0: /* deal with the MCCE leaders */ sl@0: NOTE(REG_ULOCALE); sl@0: for (p = leads->chrs, i = leads->nchrs; i > 0; p++, i--) { sl@0: co = GETCOLOR(v->cm, *p); sl@0: a = findarc(lp, PLAIN, co); sl@0: if (a != NULL) sl@0: s = a->to; sl@0: else { sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: newarc(v->nfa, PLAIN, co, lp, s); sl@0: NOERR(); sl@0: } sl@0: pa = findarc(v->mccepbegin, PLAIN, co); sl@0: assert(pa != NULL); sl@0: ps = pa->to; sl@0: newarc(v->nfa, '$', 1, s, rp); sl@0: newarc(v->nfa, '$', 0, s, rp); sl@0: colorcomplement(v->nfa, v->cm, AHEAD, ps, s, rp); sl@0: NOERR(); sl@0: } sl@0: sl@0: /* and the MCCEs */ sl@0: for (i = 0; i < cv->nmcces; i++) { sl@0: p = cv->mcces[i]; sl@0: assert(singleton(v->cm, *p)); sl@0: if (!singleton(v->cm, *p)) { sl@0: ERR(REG_ASSERT); sl@0: return; sl@0: } sl@0: ch = *p++; sl@0: co = GETCOLOR(v->cm, ch); sl@0: a = findarc(lp, PLAIN, co); sl@0: if (a != NULL) sl@0: s = a->to; sl@0: else { sl@0: s = newstate(v->nfa); sl@0: NOERR(); sl@0: newarc(v->nfa, PLAIN, co, lp, s); sl@0: NOERR(); sl@0: } sl@0: assert(*p != 0); /* at least two chars */ sl@0: assert(singleton(v->cm, *p)); sl@0: ch = *p++; sl@0: co = GETCOLOR(v->cm, ch); sl@0: assert(*p == 0); /* and only two, for now */ sl@0: newarc(v->nfa, PLAIN, co, s, rp); sl@0: NOERR(); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: - nextleader - find next MCCE leader within range sl@0: ^ static celt nextleader(struct vars *, pchr, pchr); sl@0: */ sl@0: static celt /* NOCELT means none */ sl@0: nextleader(v, from, to) sl@0: struct vars *v; sl@0: pchr from; sl@0: pchr to; sl@0: { sl@0: int i; sl@0: chr *p; sl@0: chr ch; sl@0: celt it = NOCELT; sl@0: sl@0: if (v->mcces == NULL) sl@0: return it; sl@0: sl@0: for (i = v->mcces->nchrs, p = v->mcces->chrs; i > 0; i--, p++) { sl@0: ch = *p; sl@0: if (from <= ch && ch <= to) sl@0: if (it == NOCELT || ch < it) sl@0: it = ch; sl@0: } sl@0: return it; sl@0: } sl@0: sl@0: /* sl@0: - wordchrs - set up word-chr list for word-boundary stuff, if needed sl@0: * The list is kept as a bunch of arcs between two dummy states; it's sl@0: * disposed of by the unreachable-states sweep in NFA optimization. sl@0: * Does NEXT(). Must not be called from any unusual lexical context. sl@0: * This should be reconciled with the \w etc. handling in lex.c, and sl@0: * should be cleaned up to reduce dependencies on input scanning. sl@0: ^ static VOID wordchrs(struct vars *); sl@0: */ sl@0: static VOID sl@0: wordchrs(v) sl@0: struct vars *v; sl@0: { sl@0: struct state *left; sl@0: struct state *right; sl@0: sl@0: if (v->wordchrs != NULL) { sl@0: NEXT(); /* for consistency */ sl@0: return; sl@0: } sl@0: sl@0: left = newstate(v->nfa); sl@0: right = newstate(v->nfa); sl@0: NOERR(); sl@0: /* fine point: implemented with [::], and lexer will set REG_ULOCALE */ sl@0: lexword(v); sl@0: NEXT(); sl@0: assert(v->savenow != NULL && SEE('[')); sl@0: bracket(v, left, right); sl@0: assert((v->savenow != NULL && SEE(']')) || ISERR()); sl@0: NEXT(); sl@0: NOERR(); sl@0: v->wordchrs = left; sl@0: } sl@0: sl@0: /* sl@0: - subre - allocate a subre sl@0: ^ static struct subre *subre(struct vars *, int, int, struct state *, sl@0: ^ struct state *); sl@0: */ sl@0: static struct subre * sl@0: subre(v, op, flags, begin, end) sl@0: struct vars *v; sl@0: int op; sl@0: int flags; sl@0: struct state *begin; sl@0: struct state *end; sl@0: { sl@0: struct subre *ret; sl@0: sl@0: ret = v->treefree; sl@0: if (ret != NULL) sl@0: v->treefree = ret->left; sl@0: else { sl@0: ret = (struct subre *)MALLOC(sizeof(struct subre)); sl@0: if (ret == NULL) { sl@0: ERR(REG_ESPACE); sl@0: return NULL; sl@0: } sl@0: ret->chain = v->treechain; sl@0: v->treechain = ret; sl@0: } sl@0: sl@0: assert(strchr("|.b(=", op) != NULL); sl@0: sl@0: ret->op = op; sl@0: ret->flags = flags; sl@0: ret->retry = 0; sl@0: ret->subno = 0; sl@0: ret->min = ret->max = 1; sl@0: ret->left = NULL; sl@0: ret->right = NULL; sl@0: ret->begin = begin; sl@0: ret->end = end; sl@0: ZAPCNFA(ret->cnfa); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: /* sl@0: - freesubre - free a subRE subtree sl@0: ^ static VOID freesubre(struct vars *, struct subre *); sl@0: */ sl@0: static VOID sl@0: freesubre(v, sr) sl@0: struct vars *v; /* might be NULL */ sl@0: struct subre *sr; sl@0: { sl@0: if (sr == NULL) sl@0: return; sl@0: sl@0: if (sr->left != NULL) sl@0: freesubre(v, sr->left); sl@0: if (sr->right != NULL) sl@0: freesubre(v, sr->right); sl@0: sl@0: freesrnode(v, sr); sl@0: } sl@0: sl@0: /* sl@0: - freesrnode - free one node in a subRE subtree sl@0: ^ static VOID freesrnode(struct vars *, struct subre *); sl@0: */ sl@0: static VOID sl@0: freesrnode(v, sr) sl@0: struct vars *v; /* might be NULL */ sl@0: struct subre *sr; sl@0: { sl@0: if (sr == NULL) sl@0: return; sl@0: sl@0: if (!NULLCNFA(sr->cnfa)) sl@0: freecnfa(&sr->cnfa); sl@0: sr->flags = 0; sl@0: sl@0: if (v != NULL) { sl@0: sr->left = v->treefree; sl@0: v->treefree = sr; sl@0: } else sl@0: FREE(sr); sl@0: } sl@0: sl@0: /* sl@0: - optst - optimize a subRE subtree sl@0: ^ static VOID optst(struct vars *, struct subre *); sl@0: */ sl@0: static VOID sl@0: optst(v, t) sl@0: struct vars *v; sl@0: struct subre *t; sl@0: { sl@0: if (t == NULL) sl@0: return; sl@0: sl@0: /* recurse through children */ sl@0: if (t->left != NULL) sl@0: optst(v, t->left); sl@0: if (t->right != NULL) sl@0: optst(v, t->right); sl@0: } sl@0: sl@0: /* sl@0: - numst - number tree nodes (assigning retry indexes) sl@0: ^ static int numst(struct subre *, int); sl@0: */ sl@0: static int /* next number */ sl@0: numst(t, start) sl@0: struct subre *t; sl@0: int start; /* starting point for subtree numbers */ sl@0: { sl@0: int i; sl@0: sl@0: assert(t != NULL); sl@0: sl@0: i = start; sl@0: t->retry = (short)i++; sl@0: if (t->left != NULL) sl@0: i = numst(t->left, i); sl@0: if (t->right != NULL) sl@0: i = numst(t->right, i); sl@0: return i; sl@0: } sl@0: sl@0: /* sl@0: - markst - mark tree nodes as INUSE sl@0: ^ static VOID markst(struct subre *); sl@0: */ sl@0: static VOID sl@0: markst(t) sl@0: struct subre *t; sl@0: { sl@0: assert(t != NULL); sl@0: sl@0: t->flags |= INUSE; sl@0: if (t->left != NULL) sl@0: markst(t->left); sl@0: if (t->right != NULL) sl@0: markst(t->right); sl@0: } sl@0: sl@0: /* sl@0: - cleanst - free any tree nodes not marked INUSE sl@0: ^ static VOID cleanst(struct vars *); sl@0: */ sl@0: static VOID sl@0: cleanst(v) sl@0: struct vars *v; sl@0: { sl@0: struct subre *t; sl@0: struct subre *next; sl@0: sl@0: for (t = v->treechain; t != NULL; t = next) { sl@0: next = t->chain; sl@0: if (!(t->flags&INUSE)) sl@0: FREE(t); sl@0: } sl@0: v->treechain = NULL; sl@0: v->treefree = NULL; /* just on general principles */ sl@0: } sl@0: sl@0: /* sl@0: - nfatree - turn a subRE subtree into a tree of compacted NFAs sl@0: ^ static long nfatree(struct vars *, struct subre *, FILE *); sl@0: */ sl@0: static long /* optimize results from top node */ sl@0: nfatree(v, t, f) sl@0: struct vars *v; sl@0: struct subre *t; sl@0: FILE *f; /* for debug output */ sl@0: { sl@0: assert(t != NULL && t->begin != NULL); sl@0: sl@0: if (t->left != NULL) sl@0: (DISCARD)nfatree(v, t->left, f); sl@0: if (t->right != NULL) sl@0: (DISCARD)nfatree(v, t->right, f); sl@0: sl@0: return nfanode(v, t, f); sl@0: } sl@0: sl@0: /* sl@0: - nfanode - do one NFA for nfatree sl@0: ^ static long nfanode(struct vars *, struct subre *, FILE *); sl@0: */ sl@0: static long /* optimize results */ sl@0: nfanode(v, t, f) sl@0: struct vars *v; sl@0: struct subre *t; sl@0: FILE *f; /* for debug output */ sl@0: { sl@0: struct nfa *nfa; sl@0: long ret = 0; sl@0: char idbuf[50]; sl@0: sl@0: assert(t->begin != NULL); sl@0: sl@0: if (f != NULL) sl@0: fprintf(f, "\n\n\n========= TREE NODE %s ==========\n", sl@0: stid(t, idbuf, sizeof(idbuf))); sl@0: nfa = newnfa(v, v->cm, v->nfa); sl@0: NOERRZ(); sl@0: dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final); sl@0: if (!ISERR()) { sl@0: specialcolors(nfa); sl@0: ret = optimize(nfa, f); sl@0: } sl@0: if (!ISERR()) sl@0: compact(nfa, &t->cnfa); sl@0: sl@0: freenfa(nfa); sl@0: return ret; sl@0: } sl@0: sl@0: /* sl@0: - newlacon - allocate a lookahead-constraint subRE sl@0: ^ static int newlacon(struct vars *, struct state *, struct state *, int); sl@0: */ sl@0: static int /* lacon number */ sl@0: newlacon(v, begin, end, pos) sl@0: struct vars *v; sl@0: struct state *begin; sl@0: struct state *end; sl@0: int pos; sl@0: { sl@0: int n; sl@0: struct subre *sub; sl@0: sl@0: if (v->nlacons == 0) { sl@0: v->lacons = (struct subre *)MALLOC(2 * sizeof(struct subre)); sl@0: n = 1; /* skip 0th */ sl@0: v->nlacons = 2; sl@0: } else { sl@0: v->lacons = (struct subre *)REALLOC(v->lacons, sl@0: (v->nlacons+1)*sizeof(struct subre)); sl@0: n = v->nlacons++; sl@0: } sl@0: if (v->lacons == NULL) { sl@0: ERR(REG_ESPACE); sl@0: return 0; sl@0: } sl@0: sub = &v->lacons[n]; sl@0: sub->begin = begin; sl@0: sub->end = end; sl@0: sub->subno = pos; sl@0: ZAPCNFA(sub->cnfa); sl@0: return n; sl@0: } sl@0: sl@0: /* sl@0: - freelacons - free lookahead-constraint subRE vector sl@0: ^ static VOID freelacons(struct subre *, int); sl@0: */ sl@0: static VOID sl@0: freelacons(subs, n) sl@0: struct subre *subs; sl@0: int n; sl@0: { sl@0: struct subre *sub; sl@0: int i; sl@0: sl@0: assert(n > 0); sl@0: for (sub = subs + 1, i = n - 1; i > 0; sub++, i--) /* no 0th */ sl@0: if (!NULLCNFA(sub->cnfa)) sl@0: freecnfa(&sub->cnfa); sl@0: FREE(subs); sl@0: } sl@0: sl@0: /* sl@0: - rfree - free a whole RE (insides of regfree) sl@0: ^ static VOID rfree(regex_t *); sl@0: */ sl@0: static VOID sl@0: rfree(re) sl@0: regex_t *re; sl@0: { sl@0: struct guts *g; sl@0: sl@0: if (re == NULL || re->re_magic != REMAGIC) sl@0: return; sl@0: sl@0: re->re_magic = 0; /* invalidate RE */ sl@0: g = (struct guts *)re->re_guts; sl@0: re->re_guts = NULL; sl@0: re->re_fns = NULL; sl@0: g->magic = 0; sl@0: freecm(&g->cmap); sl@0: if (g->tree != NULL) sl@0: freesubre((struct vars *)NULL, g->tree); sl@0: if (g->lacons != NULL) sl@0: freelacons(g->lacons, g->nlacons); sl@0: if (!NULLCNFA(g->search)) sl@0: freecnfa(&g->search); sl@0: FREE(g); sl@0: } sl@0: sl@0: /* sl@0: - dump - dump an RE in human-readable form sl@0: ^ static VOID dump(regex_t *, FILE *); sl@0: */ sl@0: static VOID sl@0: dump(re, f) sl@0: regex_t *re; sl@0: FILE *f; sl@0: { sl@0: #ifdef REG_DEBUG sl@0: struct guts *g; sl@0: int i; sl@0: sl@0: if (re->re_magic != REMAGIC) sl@0: fprintf(f, "bad magic number (0x%x not 0x%x)\n", re->re_magic, sl@0: REMAGIC); sl@0: if (re->re_guts == NULL) { sl@0: fprintf(f, "NULL guts!!!\n"); sl@0: return; sl@0: } sl@0: g = (struct guts *)re->re_guts; sl@0: if (g->magic != GUTSMAGIC) sl@0: fprintf(f, "bad guts magic number (0x%x not 0x%x)\n", g->magic, sl@0: GUTSMAGIC); sl@0: sl@0: fprintf(f, "\n\n\n========= DUMP ==========\n"); sl@0: fprintf(f, "nsub %d, info 0%lo, csize %d, ntree %d\n", sl@0: re->re_nsub, re->re_info, re->re_csize, g->ntree); sl@0: sl@0: dumpcolors(&g->cmap, f); sl@0: if (!NULLCNFA(g->search)) { sl@0: printf("\nsearch:\n"); sl@0: dumpcnfa(&g->search, f); sl@0: } sl@0: for (i = 1; i < g->nlacons; i++) { sl@0: fprintf(f, "\nla%d (%s):\n", i, sl@0: (g->lacons[i].subno) ? "positive" : "negative"); sl@0: dumpcnfa(&g->lacons[i].cnfa, f); sl@0: } sl@0: fprintf(f, "\n"); sl@0: dumpst(g->tree, f, 0); sl@0: #endif sl@0: } sl@0: sl@0: /* sl@0: - dumpst - dump a subRE tree sl@0: ^ static VOID dumpst(struct subre *, FILE *, int); sl@0: */ sl@0: static VOID sl@0: dumpst(t, f, nfapresent) sl@0: struct subre *t; sl@0: FILE *f; sl@0: int nfapresent; /* is the original NFA still around? */ sl@0: { sl@0: if (t == NULL) sl@0: fprintf(f, "null tree\n"); sl@0: else sl@0: stdump(t, f, nfapresent); sl@0: fflush(f); sl@0: } sl@0: sl@0: /* sl@0: - stdump - recursive guts of dumpst sl@0: ^ static VOID stdump(struct subre *, FILE *, int); sl@0: */ sl@0: static VOID sl@0: stdump(t, f, nfapresent) sl@0: struct subre *t; sl@0: FILE *f; sl@0: int nfapresent; /* is the original NFA still around? */ sl@0: { sl@0: char idbuf[50]; sl@0: sl@0: fprintf(f, "%s. `%c'", stid(t, idbuf, sizeof(idbuf)), t->op); sl@0: if (t->flags&LONGER) sl@0: fprintf(f, " longest"); sl@0: if (t->flags&SHORTER) sl@0: fprintf(f, " shortest"); sl@0: if (t->flags&MIXED) sl@0: fprintf(f, " hasmixed"); sl@0: if (t->flags&CAP) sl@0: fprintf(f, " hascapture"); sl@0: if (t->flags&BACKR) sl@0: fprintf(f, " hasbackref"); sl@0: if (!(t->flags&INUSE)) sl@0: fprintf(f, " UNUSED"); sl@0: if (t->subno != 0) sl@0: fprintf(f, " (#%d)", t->subno); sl@0: if (t->min != 1 || t->max != 1) { sl@0: fprintf(f, " {%d,", t->min); sl@0: if (t->max != INFINITY) sl@0: fprintf(f, "%d", t->max); sl@0: fprintf(f, "}"); sl@0: } sl@0: if (nfapresent) sl@0: fprintf(f, " %ld-%ld", (long)t->begin->no, (long)t->end->no); sl@0: if (t->left != NULL) sl@0: fprintf(f, " L:%s", stid(t->left, idbuf, sizeof(idbuf))); sl@0: if (t->right != NULL) sl@0: fprintf(f, " R:%s", stid(t->right, idbuf, sizeof(idbuf))); sl@0: if (!NULLCNFA(t->cnfa)) { sl@0: fprintf(f, "\n"); sl@0: dumpcnfa(&t->cnfa, f); sl@0: fprintf(f, "\n"); sl@0: } sl@0: if (t->left != NULL) sl@0: stdump(t->left, f, nfapresent); sl@0: if (t->right != NULL) sl@0: stdump(t->right, f, nfapresent); sl@0: } sl@0: sl@0: /* sl@0: - stid - identify a subtree node for dumping sl@0: ^ static char *stid(struct subre *, char *, size_t); sl@0: */ sl@0: static char * /* points to buf or constant string */ sl@0: stid(t, buf, bufsize) sl@0: struct subre *t; sl@0: char *buf; sl@0: size_t bufsize; sl@0: { sl@0: /* big enough for hex int or decimal t->retry? */ sl@0: if (bufsize < sizeof(void*)*2 + 3 || bufsize < sizeof(t->retry)*3 + 1) sl@0: return "unable"; sl@0: if (t->retry != 0) sl@0: sprintf(buf, "%d", t->retry); sl@0: else sl@0: sprintf(buf, "%p", t); sl@0: return buf; sl@0: } sl@0: sl@0: #include "regc_lex.c" sl@0: #include "regc_color.c" sl@0: #include "regc_nfa.c" sl@0: #include "regc_cvec.c" sl@0: #include "regc_locale.c"