| 1 |
/* |
| 2 |
* re_*comp and friends - compile REs |
| 3 |
* This file #includes several others (see the bottom). |
| 4 |
* |
| 5 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. |
| 6 |
* |
| 7 |
* Development of this software was funded, in part, by Cray Research Inc., |
| 8 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics |
| 9 |
* Corporation, none of whom are responsible for the results. The author |
| 10 |
* thanks all of them. |
| 11 |
* |
| 12 |
* Redistribution and use in source and binary forms -- with or without |
| 13 |
* modification -- are permitted for any purpose, provided that |
| 14 |
* redistributions in source form retain this entire copyright notice and |
| 15 |
* indicate the origin and nature of any modifications. |
| 16 |
* |
| 17 |
* I'd appreciate being given credit for this package in the documentation of |
| 18 |
* software which uses it, but that is not a requirement. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 21 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 22 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 26 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 29 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
*/ |
| 32 |
|
| 33 |
#include "regguts.h" |
| 34 |
|
| 35 |
/* |
| 36 |
* forward declarations, up here so forward datatypes etc. are defined early |
| 37 |
*/ |
| 38 |
/* =====^!^===== begin forwards =====^!^===== */ |
| 39 |
/* automatically gathered by fwd; do not hand-edit */ |
| 40 |
/* === regcomp.c === */ |
| 41 |
int compile(regex_t *, const chr *, size_t, int); |
| 42 |
static void moresubs(struct vars *, int); |
| 43 |
static int freev(struct vars *, int); |
| 44 |
static void makesearch(struct vars *, struct nfa *); |
| 45 |
static struct subre *parse(struct vars *, int, int, struct state *, struct state *); |
| 46 |
static struct subre *parsebranch(struct vars *, int, int, struct state *, struct state *, int); |
| 47 |
static void parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *); |
| 48 |
static void nonword(struct vars *, int, struct state *, struct state *); |
| 49 |
static void word(struct vars *, int, struct state *, struct state *); |
| 50 |
static int scannum(struct vars *); |
| 51 |
static void repeat(struct vars *, struct state *, struct state *, int, int); |
| 52 |
static void bracket(struct vars *, struct state *, struct state *); |
| 53 |
static void cbracket(struct vars *, struct state *, struct state *); |
| 54 |
static void brackpart(struct vars *, struct state *, struct state *); |
| 55 |
static const chr *scanplain(struct vars *); |
| 56 |
static void onechr(struct vars *, pchr, struct state *, struct state *); |
| 57 |
static void dovec(struct vars *, struct cvec *, struct state *, struct state *); |
| 58 |
static void wordchrs(struct vars *); |
| 59 |
static struct subre *subre(struct vars *, int, int, struct state *, struct state *); |
| 60 |
static void freesubre(struct vars *, struct subre *); |
| 61 |
static void freesrnode(struct vars *, struct subre *); |
| 62 |
static int numst(struct subre *, int); |
| 63 |
static void markst(struct subre *); |
| 64 |
static void cleanst(struct vars *); |
| 65 |
static long nfatree(struct vars *, struct subre *, FILE *); |
| 66 |
static long nfanode(struct vars *, struct subre *, FILE *); |
| 67 |
static int newlacon(struct vars *, struct state *, struct state *, int); |
| 68 |
static void freelacons(struct subre *, int); |
| 69 |
static void rfree(regex_t *); |
| 70 |
static void dump(regex_t *, FILE *); |
| 71 |
static void dumpst(struct subre *, FILE *, int); |
| 72 |
static void stdump(struct subre *, FILE *, int); |
| 73 |
static const char *stid(struct subre *, char *, size_t); |
| 74 |
/* === regc_lex.c === */ |
| 75 |
static void lexstart(struct vars *); |
| 76 |
static void prefixes(struct vars *); |
| 77 |
static void lexnest(struct vars *, const chr *, const chr *); |
| 78 |
static void lexword(struct vars *); |
| 79 |
static int next(struct vars *); |
| 80 |
static int lexescape(struct vars *); |
| 81 |
static int lexdigits(struct vars *, int, int, int); |
| 82 |
static int brenext(struct vars *, pchr); |
| 83 |
static void skip(struct vars *); |
| 84 |
static chr newline(NOPARMS); |
| 85 |
static chr chrnamed(struct vars *, const chr *, const chr *, pchr); |
| 86 |
/* === regc_color.c === */ |
| 87 |
static void initcm(struct vars *, struct colormap *); |
| 88 |
static void freecm(struct colormap *); |
| 89 |
static void cmtreefree(struct colormap *, union tree *, int); |
| 90 |
static color setcolor(struct colormap *, pchr, pcolor); |
| 91 |
static color maxcolor(struct colormap *); |
| 92 |
static color newcolor(struct colormap *); |
| 93 |
static void freecolor(struct colormap *, pcolor); |
| 94 |
static color pseudocolor(struct colormap *); |
| 95 |
static color subcolor(struct colormap *, pchr c); |
| 96 |
static color newsub(struct colormap *, pcolor); |
| 97 |
static void subrange(struct vars *, pchr, pchr, struct state *, struct state *); |
| 98 |
static void subblock(struct vars *, pchr, struct state *, struct state *); |
| 99 |
static void okcolors(struct nfa *, struct colormap *); |
| 100 |
static void colorchain(struct colormap *, struct arc *); |
| 101 |
static void uncolorchain(struct colormap *, struct arc *); |
| 102 |
static void rainbow(struct nfa *, struct colormap *, int, pcolor, struct state *, struct state *); |
| 103 |
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *); |
| 104 |
#ifdef REG_DEBUG |
| 105 |
static void dumpcolors(struct colormap *, FILE *); |
| 106 |
static void fillcheck(struct colormap *, union tree *, int, FILE *); |
| 107 |
static void dumpchr(pchr, FILE *); |
| 108 |
#endif |
| 109 |
/* === regc_nfa.c === */ |
| 110 |
static struct nfa *newnfa(struct vars *, struct colormap *, struct nfa *); |
| 111 |
static void freenfa(struct nfa *); |
| 112 |
static struct state *newstate(struct nfa *); |
| 113 |
static struct state *newfstate(struct nfa *, int flag); |
| 114 |
static void dropstate(struct nfa *, struct state *); |
| 115 |
static void freestate(struct nfa *, struct state *); |
| 116 |
static void destroystate(struct nfa *, struct state *); |
| 117 |
static void newarc(struct nfa *, int, pcolor, struct state *, struct state *); |
| 118 |
static void createarc(struct nfa *, int, pcolor, struct state *, struct state *); |
| 119 |
static struct arc *allocarc(struct nfa *, struct state *); |
| 120 |
static void freearc(struct nfa *, struct arc *); |
| 121 |
static void changearctarget(struct arc *, struct state *); |
| 122 |
static int hasnonemptyout(struct state *); |
| 123 |
static struct arc *findarc(struct state *, int, pcolor); |
| 124 |
static void cparc(struct nfa *, struct arc *, struct state *, struct state *); |
| 125 |
static void sortins(struct nfa *, struct state *); |
| 126 |
static int sortins_cmp(const void *, const void *); |
| 127 |
static void sortouts(struct nfa *, struct state *); |
| 128 |
static int sortouts_cmp(const void *, const void *); |
| 129 |
static void moveins(struct nfa *, struct state *, struct state *); |
| 130 |
static void copyins(struct nfa *, struct state *, struct state *); |
| 131 |
static void mergeins(struct nfa *, struct state *, struct arc **, int); |
| 132 |
static void moveouts(struct nfa *, struct state *, struct state *); |
| 133 |
static void copyouts(struct nfa *, struct state *, struct state *); |
| 134 |
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int); |
| 135 |
static void delsub(struct nfa *, struct state *, struct state *); |
| 136 |
static void deltraverse(struct nfa *, struct state *, struct state *); |
| 137 |
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *); |
| 138 |
static void duptraverse(struct nfa *, struct state *, struct state *, int); |
| 139 |
static void cleartraverse(struct nfa *, struct state *); |
| 140 |
static void specialcolors(struct nfa *); |
| 141 |
static long optimize(struct nfa *, FILE *); |
| 142 |
static void pullback(struct nfa *, FILE *); |
| 143 |
static int pull(struct nfa *, struct arc *, struct state **); |
| 144 |
static void pushfwd(struct nfa *, FILE *); |
| 145 |
static int push(struct nfa *, struct arc *, struct state **); |
| 146 |
#define INCOMPATIBLE 1 /* destroys arc */ |
| 147 |
#define SATISFIED 2 /* constraint satisfied */ |
| 148 |
#define COMPATIBLE 3 /* compatible but not satisfied yet */ |
| 149 |
static int combine(struct arc *, struct arc *); |
| 150 |
static void fixempties(struct nfa *, FILE *); |
| 151 |
static struct state *emptyreachable(struct nfa *, struct state *, |
| 152 |
struct state *, struct arc **); |
| 153 |
static int isconstraintarc(struct arc *); |
| 154 |
static int hasconstraintout(struct state *); |
| 155 |
static void fixconstraintloops(struct nfa *, FILE *); |
| 156 |
static int findconstraintloop(struct nfa *, struct state *); |
| 157 |
static void breakconstraintloop(struct nfa *, struct state *); |
| 158 |
static void clonesuccessorstates(struct nfa *, struct state *, struct state *, |
| 159 |
struct state *, struct arc *, char *, char *, int); |
| 160 |
static void cleanup(struct nfa *); |
| 161 |
static void markreachable(struct nfa *, struct state *, struct state *, struct state *); |
| 162 |
static void markcanreach(struct nfa *, struct state *, struct state *, struct state *); |
| 163 |
static long analyze(struct nfa *); |
| 164 |
static void compact(struct nfa *, struct cnfa *); |
| 165 |
static void carcsort(struct carc *, size_t); |
| 166 |
static int carc_cmp(const void *, const void *); |
| 167 |
static void freecnfa(struct cnfa *); |
| 168 |
static void dumpnfa(struct nfa *, FILE *); |
| 169 |
#ifdef REG_DEBUG |
| 170 |
static void dumpstate(struct state *, FILE *); |
| 171 |
static void dumparcs(struct state *, FILE *); |
| 172 |
static void dumparc(struct arc *, struct state *, FILE *); |
| 173 |
#endif |
| 174 |
static void dumpcnfa(struct cnfa *, FILE *); |
| 175 |
#ifdef REG_DEBUG |
| 176 |
static void dumpcstate(int, struct cnfa *, FILE *); |
| 177 |
#endif |
| 178 |
/* === regc_cvec.c === */ |
| 179 |
static struct cvec *clearcvec(struct cvec *); |
| 180 |
static void addchr(struct cvec *, pchr); |
| 181 |
static void addrange(struct cvec *, pchr, pchr); |
| 182 |
static struct cvec *newcvec(int, int); |
| 183 |
static struct cvec *getcvec(struct vars *, int, int); |
| 184 |
static void freecvec(struct cvec *); |
| 185 |
/* === regc_locale.c === */ |
| 186 |
static celt element(struct vars *, const chr *, const chr *); |
| 187 |
static struct cvec *range(struct vars *, celt, celt, int); |
| 188 |
static int before(celt, celt); |
| 189 |
static struct cvec *eclass(struct vars *, celt, int); |
| 190 |
static struct cvec *cclass(struct vars *, const chr *, const chr *, int); |
| 191 |
static struct cvec *allcases(struct vars *, pchr); |
| 192 |
static int cmp(const chr *, const chr *, size_t); |
| 193 |
static int casecmp(const chr *, const chr *, size_t); |
| 194 |
/* automatically gathered by fwd; do not hand-edit */ |
| 195 |
/* =====^!^===== end forwards =====^!^===== */ |
| 196 |
|
| 197 |
/* internal variables, bundled for easy passing around */ |
| 198 |
struct vars { |
| 199 |
regex_t *re; |
| 200 |
const chr *now; /* scan pointer into string */ |
| 201 |
const chr *stop; /* end of string */ |
| 202 |
const chr *savenow; /* saved now and stop for "subroutine call" */ |
| 203 |
const chr *savestop; |
| 204 |
int err; /* error code (0 if none) */ |
| 205 |
int cflags; /* copy of compile flags */ |
| 206 |
int lasttype; /* type of previous token */ |
| 207 |
int nexttype; /* type of next token */ |
| 208 |
chr nextvalue; /* value (if any) of next token */ |
| 209 |
int lexcon; /* lexical context type (see lex.c) */ |
| 210 |
int nsubexp; /* subexpression count */ |
| 211 |
struct subre **subs; /* subRE pointer vector */ |
| 212 |
size_t nsubs; /* length of vector */ |
| 213 |
struct subre *sub10[10]; /* initial vector, enough for most */ |
| 214 |
struct nfa *nfa; /* the NFA */ |
| 215 |
struct colormap *cm; /* character color map */ |
| 216 |
color nlcolor; /* color of newline */ |
| 217 |
struct state *wordchrs; /* state in nfa holding word-char outarcs */ |
| 218 |
struct subre *tree; /* subexpression tree */ |
| 219 |
struct subre *treechain; /* all tree nodes allocated */ |
| 220 |
struct subre *treefree; /* any free tree nodes */ |
| 221 |
int ntree; /* number of tree nodes, plus one */ |
| 222 |
struct cvec *cv; /* interface cvec */ |
| 223 |
struct cvec *cv2; /* utility cvec */ |
| 224 |
struct subre *lacons; /* lookahead-constraint vector */ |
| 225 |
int nlacons; /* size of lacons */ |
| 226 |
size_t spaceused; /* approx. space used for compilation */ |
| 227 |
}; |
| 228 |
|
| 229 |
/* parsing macros; most know that `v' is the struct vars pointer */ |
| 230 |
#define NEXT() (next(v)) /* advance by one token */ |
| 231 |
#define SEE(t) (v->nexttype == (t)) /* is next token this? */ |
| 232 |
#define EAT(t) (SEE(t) && next(v)) /* if next is this, swallow it */ |
| 233 |
#define VISERR(vv) ((vv)->err != 0)/* have we seen an error yet? */ |
| 234 |
#define ISERR() VISERR(v) |
| 235 |
#define VERR(vv,e) ((vv)->nexttype = EOS, \ |
| 236 |
(vv)->err = ((vv)->err ? (vv)->err : (e))) |
| 237 |
#define ERR(e) VERR(v, e) /* record an error */ |
| 238 |
#define NOERR() {if (ISERR()) return;} /* if error seen, return */ |
| 239 |
#define NOERRN() {if (ISERR()) return NULL;} /* NOERR with retval */ |
| 240 |
#define NOERRZ() {if (ISERR()) return 0;} /* NOERR with retval */ |
| 241 |
#define INSIST(c, e) do { if (!(c)) ERR(e); } while (0) /* error if c false */ |
| 242 |
#define NOTE(b) (v->re->re_info |= (b)) /* note visible condition */ |
| 243 |
#define EMPTYARC(x, y) newarc(v->nfa, EMPTY, 0, x, y) |
| 244 |
|
| 245 |
/* token type codes, some also used as NFA arc types */ |
| 246 |
#undef DIGIT /* prevent conflict with libtommath */ |
| 247 |
#define EMPTY 'n' /* no token present */ |
| 248 |
#define EOS 'e' /* end of string */ |
| 249 |
#define PLAIN 'p' /* ordinary character */ |
| 250 |
#define DIGIT 'd' /* digit (in bound) */ |
| 251 |
#define BACKREF 'b' /* back reference */ |
| 252 |
#define COLLEL 'I' /* start of [. */ |
| 253 |
#define ECLASS 'E' /* start of [= */ |
| 254 |
#define CCLASS 'C' /* start of [: */ |
| 255 |
#define END 'X' /* end of [. [= [: */ |
| 256 |
#define RANGE 'R' /* - within [] which might be range delim. */ |
| 257 |
#define LACON 'L' /* lookahead constraint subRE */ |
| 258 |
#define AHEAD 'a' /* color-lookahead arc */ |
| 259 |
#define BEHIND 'r' /* color-lookbehind arc */ |
| 260 |
#define WBDRY 'w' /* word boundary constraint */ |
| 261 |
#define NWBDRY 'W' /* non-word-boundary constraint */ |
| 262 |
#define SBEGIN 'A' /* beginning of string (even if not BOL) */ |
| 263 |
#define SEND 'Z' /* end of string (even if not EOL) */ |
| 264 |
#define PREFER 'P' /* length preference */ |
| 265 |
|
| 266 |
/* is an arc colored, and hence on a color chain? */ |
| 267 |
#define COLORED(a) \ |
| 268 |
((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND) |
| 269 |
|
| 270 |
/* static function list */ |
| 271 |
static const struct fns functions = { |
| 272 |
rfree, /* regfree insides */ |
| 273 |
}; |
| 274 |
|
| 275 |
/* |
| 276 |
- compile - compile regular expression |
| 277 |
* Note: on failure, no resources remain allocated, so regfree() |
| 278 |
* need not be applied to re. |
| 279 |
^ int compile(regex_t *, const chr *, size_t, int); |
| 280 |
*/ |
| 281 |
int |
| 282 |
compile( |
| 283 |
regex_t *re, |
| 284 |
const chr *string, |
| 285 |
size_t len, |
| 286 |
int flags) |
| 287 |
{ |
| 288 |
AllocVars(v); |
| 289 |
struct guts *g; |
| 290 |
int i; |
| 291 |
size_t j; |
| 292 |
FILE *debug = (flags®_PROGRESS) ? stdout : NULL; |
| 293 |
#define CNOERR() { if (ISERR()) return freev(v, v->err); } |
| 294 |
|
| 295 |
/* |
| 296 |
* Sanity checks. |
| 297 |
*/ |
| 298 |
|
| 299 |
if (re == NULL || string == NULL) { |
| 300 |
FreeVars(v); |
| 301 |
return REG_INVARG; |
| 302 |
} |
| 303 |
if ((flags®_QUOTE) && (flags&(REG_ADVANCED|REG_EXPANDED|REG_NEWLINE))) { |
| 304 |
FreeVars(v); |
| 305 |
return REG_INVARG; |
| 306 |
} |
| 307 |
if (!(flags®_EXTENDED) && (flags®_ADVF)) { |
| 308 |
FreeVars(v); |
| 309 |
return REG_INVARG; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* Initial setup (after which freev() is callable). |
| 314 |
*/ |
| 315 |
|
| 316 |
v->re = re; |
| 317 |
v->now = string; |
| 318 |
v->stop = v->now + len; |
| 319 |
v->savenow = v->savestop = NULL; |
| 320 |
v->err = 0; |
| 321 |
v->cflags = flags; |
| 322 |
v->nsubexp = 0; |
| 323 |
v->subs = v->sub10; |
| 324 |
v->nsubs = 10; |
| 325 |
for (j = 0; j < v->nsubs; j++) { |
| 326 |
v->subs[j] = NULL; |
| 327 |
} |
| 328 |
v->nfa = NULL; |
| 329 |
v->cm = NULL; |
| 330 |
v->nlcolor = COLORLESS; |
| 331 |
v->wordchrs = NULL; |
| 332 |
v->tree = NULL; |
| 333 |
v->treechain = NULL; |
| 334 |
v->treefree = NULL; |
| 335 |
v->cv = NULL; |
| 336 |
v->cv2 = NULL; |
| 337 |
v->lacons = NULL; |
| 338 |
v->nlacons = 0; |
| 339 |
v->spaceused = 0; |
| 340 |
re->re_magic = REMAGIC; |
| 341 |
re->re_info = 0; /* bits get set during parse */ |
| 342 |
re->re_csize = sizeof(chr); |
| 343 |
re->re_guts = NULL; |
| 344 |
re->re_fns = (char *)&functions; |
| 345 |
|
| 346 |
/* |
| 347 |
* More complex setup, malloced things. |
| 348 |
*/ |
| 349 |
|
| 350 |
re->re_guts = (char *)(MALLOC(sizeof(struct guts))); |
| 351 |
if (re->re_guts == NULL) { |
| 352 |
return freev(v, REG_ESPACE); |
| 353 |
} |
| 354 |
g = (struct guts *) re->re_guts; |
| 355 |
g->tree = NULL; |
| 356 |
initcm(v, &g->cmap); |
| 357 |
v->cm = &g->cmap; |
| 358 |
g->lacons = NULL; |
| 359 |
g->nlacons = 0; |
| 360 |
ZAPCNFA(g->search); |
| 361 |
v->nfa = newnfa(v, v->cm, NULL); |
| 362 |
CNOERR(); |
| 363 |
v->cv = newcvec(100, 20); |
| 364 |
if (v->cv == NULL) { |
| 365 |
return freev(v, REG_ESPACE); |
| 366 |
} |
| 367 |
|
| 368 |
/* |
| 369 |
* Parsing. |
| 370 |
*/ |
| 371 |
|
| 372 |
lexstart(v); /* also handles prefixes */ |
| 373 |
if ((v->cflags®_NLSTOP) || (v->cflags®_NLANCH)) { |
| 374 |
/* |
| 375 |
* Assign newline a unique color. |
| 376 |
*/ |
| 377 |
|
| 378 |
v->nlcolor = subcolor(v->cm, newline()); |
| 379 |
okcolors(v->nfa, v->cm); |
| 380 |
} |
| 381 |
CNOERR(); |
| 382 |
v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final); |
| 383 |
assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */ |
| 384 |
CNOERR(); |
| 385 |
assert(v->tree != NULL); |
| 386 |
|
| 387 |
/* |
| 388 |
* Finish setup of nfa and its subre tree. |
| 389 |
*/ |
| 390 |
|
| 391 |
specialcolors(v->nfa); |
| 392 |
CNOERR(); |
| 393 |
if (debug != NULL) { |
| 394 |
fprintf(debug, "\n\n\n========= RAW ==========\n"); |
| 395 |
dumpnfa(v->nfa, debug); |
| 396 |
dumpst(v->tree, debug, 1); |
| 397 |
} |
| 398 |
v->ntree = numst(v->tree, 1); |
| 399 |
markst(v->tree); |
| 400 |
cleanst(v); |
| 401 |
if (debug != NULL) { |
| 402 |
fprintf(debug, "\n\n\n========= TREE FIXED ==========\n"); |
| 403 |
dumpst(v->tree, debug, 1); |
| 404 |
} |
| 405 |
|
| 406 |
/* |
| 407 |
* Build compacted NFAs for tree and lacons. |
| 408 |
*/ |
| 409 |
|
| 410 |
re->re_info |= nfatree(v, v->tree, debug); |
| 411 |
CNOERR(); |
| 412 |
assert(v->nlacons == 0 || v->lacons != NULL); |
| 413 |
for (i = 1; i < v->nlacons; i++) { |
| 414 |
if (debug != NULL) { |
| 415 |
fprintf(debug, "\n\n\n========= LA%d ==========\n", i); |
| 416 |
} |
| 417 |
nfanode(v, &v->lacons[i], debug); |
| 418 |
} |
| 419 |
CNOERR(); |
| 420 |
if (v->tree->flags&SHORTER) { |
| 421 |
NOTE(REG_USHORTEST); |
| 422 |
} |
| 423 |
|
| 424 |
/* |
| 425 |
* Build compacted NFAs for tree, lacons, fast search. |
| 426 |
*/ |
| 427 |
|
| 428 |
if (debug != NULL) { |
| 429 |
fprintf(debug, "\n\n\n========= SEARCH ==========\n"); |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Can sacrifice main NFA now, so use it as work area. |
| 434 |
*/ |
| 435 |
|
| 436 |
(DISCARD) optimize(v->nfa, debug); |
| 437 |
CNOERR(); |
| 438 |
makesearch(v, v->nfa); |
| 439 |
CNOERR(); |
| 440 |
compact(v->nfa, &g->search); |
| 441 |
CNOERR(); |
| 442 |
|
| 443 |
/* |
| 444 |
* Looks okay, package it up. |
| 445 |
*/ |
| 446 |
|
| 447 |
re->re_nsub = v->nsubexp; |
| 448 |
v->re = NULL; /* freev no longer frees re */ |
| 449 |
g->magic = GUTSMAGIC; |
| 450 |
g->cflags = v->cflags; |
| 451 |
g->info = re->re_info; |
| 452 |
g->nsub = re->re_nsub; |
| 453 |
g->tree = v->tree; |
| 454 |
v->tree = NULL; |
| 455 |
g->ntree = v->ntree; |
| 456 |
g->compare = (v->cflags®_ICASE) ? casecmp : cmp; |
| 457 |
g->lacons = v->lacons; |
| 458 |
v->lacons = NULL; |
| 459 |
g->nlacons = v->nlacons; |
| 460 |
|
| 461 |
if (flags®_DUMP) { |
| 462 |
dump(re, stdout); |
| 463 |
} |
| 464 |
|
| 465 |
assert(v->err == 0); |
| 466 |
return freev(v, 0); |
| 467 |
} |
| 468 |
|
| 469 |
/* |
| 470 |
- moresubs - enlarge subRE vector |
| 471 |
^ static void moresubs(struct vars *, int); |
| 472 |
*/ |
| 473 |
static void |
| 474 |
moresubs( |
| 475 |
struct vars *v, |
| 476 |
int wanted) /* want enough room for this one */ |
| 477 |
{ |
| 478 |
struct subre **p; |
| 479 |
size_t n; |
| 480 |
|
| 481 |
assert(wanted > 0 && (size_t)wanted >= v->nsubs); |
| 482 |
n = (size_t)wanted * 3 / 2 + 1; |
| 483 |
if (v->subs == v->sub10) { |
| 484 |
p = (struct subre **) MALLOC(n * sizeof(struct subre *)); |
| 485 |
if (p != NULL) { |
| 486 |
memcpy(p, v->subs, v->nsubs * sizeof(struct subre *)); |
| 487 |
} |
| 488 |
} else { |
| 489 |
p = (struct subre **) REALLOC(v->subs, n*sizeof(struct subre *)); |
| 490 |
} |
| 491 |
if (p == NULL) { |
| 492 |
ERR(REG_ESPACE); |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
v->subs = p; |
| 497 |
for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++) { |
| 498 |
*p = NULL; |
| 499 |
} |
| 500 |
assert(v->nsubs == n); |
| 501 |
assert((size_t)wanted < v->nsubs); |
| 502 |
} |
| 503 |
|
| 504 |
/* |
| 505 |
- freev - free vars struct's substructures where necessary |
| 506 |
* Optionally does error-number setting, and always returns error code (if |
| 507 |
* any), to make error-handling code terser. |
| 508 |
^ static int freev(struct vars *, int); |
| 509 |
*/ |
| 510 |
static int |
| 511 |
freev( |
| 512 |
struct vars *v, |
| 513 |
int err) |
| 514 |
{ |
| 515 |
int ret; |
| 516 |
|
| 517 |
if (v->re != NULL) { |
| 518 |
rfree(v->re); |
| 519 |
} |
| 520 |
if (v->subs != v->sub10) { |
| 521 |
FREE(v->subs); |
| 522 |
} |
| 523 |
if (v->nfa != NULL) { |
| 524 |
freenfa(v->nfa); |
| 525 |
} |
| 526 |
if (v->tree != NULL) { |
| 527 |
freesubre(v, v->tree); |
| 528 |
} |
| 529 |
if (v->treechain != NULL) { |
| 530 |
cleanst(v); |
| 531 |
} |
| 532 |
if (v->cv != NULL) { |
| 533 |
freecvec(v->cv); |
| 534 |
} |
| 535 |
if (v->cv2 != NULL) { |
| 536 |
freecvec(v->cv2); |
| 537 |
} |
| 538 |
if (v->lacons != NULL) { |
| 539 |
freelacons(v->lacons, v->nlacons); |
| 540 |
} |
| 541 |
ERR(err); /* nop if err==0 */ |
| 542 |
|
| 543 |
ret = v->err; |
| 544 |
FreeVars(v); |
| 545 |
return ret; |
| 546 |
} |
| 547 |
|
| 548 |
/* |
| 549 |
- makesearch - turn an NFA into a search NFA (implicit prepend of .*?) |
| 550 |
* NFA must have been optimize()d already. |
| 551 |
^ static void makesearch(struct vars *, struct nfa *); |
| 552 |
*/ |
| 553 |
static void |
| 554 |
makesearch( |
| 555 |
struct vars *v, |
| 556 |
struct nfa *nfa) |
| 557 |
{ |
| 558 |
struct arc *a, *b; |
| 559 |
struct state *pre = nfa->pre; |
| 560 |
struct state *s, *s2, *slist; |
| 561 |
|
| 562 |
/* |
| 563 |
* No loops are needed if it's anchored. |
| 564 |
*/ |
| 565 |
|
| 566 |
for (a = pre->outs; a != NULL; a = a->outchain) { |
| 567 |
assert(a->type == PLAIN); |
| 568 |
if (a->co != nfa->bos[0] && a->co != nfa->bos[1]) { |
| 569 |
break; |
| 570 |
} |
| 571 |
} |
| 572 |
if (a != NULL) { |
| 573 |
/* |
| 574 |
* Add implicit .* in front. |
| 575 |
*/ |
| 576 |
|
| 577 |
rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre); |
| 578 |
|
| 579 |
/* |
| 580 |
* And ^* and \A* too -- not always necessary, but harmless. |
| 581 |
*/ |
| 582 |
|
| 583 |
newarc(nfa, PLAIN, nfa->bos[0], pre, pre); |
| 584 |
newarc(nfa, PLAIN, nfa->bos[1], pre, pre); |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* Now here's the subtle part. Because many REs have no lookback |
| 589 |
* constraints, often knowing when you were in the pre state tells you |
| 590 |
* little; it's the next state(s) that are informative. But some of them |
| 591 |
* may have other inarcs, i.e. it may be possible to make actual progress |
| 592 |
* and then return to one of them. We must de-optimize such cases, |
| 593 |
* splitting each such state into progress and no-progress states. |
| 594 |
*/ |
| 595 |
|
| 596 |
/* |
| 597 |
* First, make a list of the states. |
| 598 |
*/ |
| 599 |
|
| 600 |
slist = NULL; |
| 601 |
for (a=pre->outs ; a!=NULL ; a=a->outchain) { |
| 602 |
s = a->to; |
| 603 |
for (b=s->ins ; b!=NULL ; b=b->inchain) { |
| 604 |
if (b->from != pre) { |
| 605 |
break; |
| 606 |
} |
| 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 |
s->tmp = (slist != NULL) ? slist : s; |
| 617 |
slist = s; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
/* |
| 622 |
* Do the splits. |
| 623 |
*/ |
| 624 |
|
| 625 |
for (s=slist ; s!=NULL ; s=s2) { |
| 626 |
s2 = newstate(nfa); |
| 627 |
NOERR(); |
| 628 |
copyouts(nfa, s, s2); |
| 629 |
NOERR(); |
| 630 |
for (a=s->ins ; a!=NULL ; a=b) { |
| 631 |
b = a->inchain; |
| 632 |
|
| 633 |
if (a->from != pre) { |
| 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 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
- parse - parse an RE |
| 645 |
* This is actually just the top level, which parses a bunch of branches tied |
| 646 |
* together with '|'. They appear in the tree as the left children of a chain |
| 647 |
* of '|' subres. |
| 648 |
^ static struct subre *parse(struct vars *, int, int, struct state *, |
| 649 |
^ struct state *); |
| 650 |
*/ |
| 651 |
static struct subre * |
| 652 |
parse( |
| 653 |
struct vars *v, |
| 654 |
int stopper, /* EOS or ')' */ |
| 655 |
int type, /* LACON (lookahead subRE) or PLAIN */ |
| 656 |
struct state *init, /* initial state */ |
| 657 |
struct state *final) /* final state */ |
| 658 |
{ |
| 659 |
struct state *left, *right; /* scaffolding for branch */ |
| 660 |
struct subre *branches; /* top level */ |
| 661 |
struct subre *branch; /* current branch */ |
| 662 |
struct subre *t; /* temporary */ |
| 663 |
int firstbranch; /* is this the first branch? */ |
| 664 |
|
| 665 |
assert(stopper == ')' || stopper == EOS); |
| 666 |
|
| 667 |
branches = subre(v, '|', LONGER, init, final); |
| 668 |
NOERRN(); |
| 669 |
branch = branches; |
| 670 |
firstbranch = 1; |
| 671 |
do { /* a branch */ |
| 672 |
if (!firstbranch) { |
| 673 |
/* |
| 674 |
* Need a place to hang the branch. |
| 675 |
*/ |
| 676 |
|
| 677 |
branch->right = subre(v, '|', LONGER, init, final); |
| 678 |
NOERRN(); |
| 679 |
branch = branch->right; |
| 680 |
} |
| 681 |
firstbranch = 0; |
| 682 |
left = newstate(v->nfa); |
| 683 |
right = newstate(v->nfa); |
| 684 |
NOERRN(); |
| 685 |
EMPTYARC(init, left); |
| 686 |
EMPTYARC(right, final); |
| 687 |
NOERRN(); |
| 688 |
branch->left = parsebranch(v, stopper, type, left, right, 0); |
| 689 |
NOERRN(); |
| 690 |
branch->flags |= UP(branch->flags | branch->left->flags); |
| 691 |
if ((branch->flags &~ branches->flags) != 0) { /* new flags */ |
| 692 |
for (t = branches; t != branch; t = t->right) { |
| 693 |
t->flags |= branch->flags; |
| 694 |
} |
| 695 |
} |
| 696 |
} while (EAT('|')); |
| 697 |
assert(SEE(stopper) || SEE(EOS)); |
| 698 |
|
| 699 |
if (!SEE(stopper)) { |
| 700 |
assert(stopper == ')' && SEE(EOS)); |
| 701 |
ERR(REG_EPAREN); |
| 702 |
} |
| 703 |
|
| 704 |
/* |
| 705 |
* Optimize out simple cases. |
| 706 |
*/ |
| 707 |
|
| 708 |
if (branch == branches) { /* only one branch */ |
| 709 |
assert(branch->right == NULL); |
| 710 |
t = branch->left; |
| 711 |
branch->left = NULL; |
| 712 |
freesubre(v, branches); |
| 713 |
branches = t; |
| 714 |
} else if (!MESSY(branches->flags)) { /* no interesting innards */ |
| 715 |
freesubre(v, branches->left); |
| 716 |
branches->left = NULL; |
| 717 |
freesubre(v, branches->right); |
| 718 |
branches->right = NULL; |
| 719 |
branches->op = '='; |
| 720 |
} |
| 721 |
|
| 722 |
return branches; |
| 723 |
} |
| 724 |
|
| 725 |
/* |
| 726 |
- parsebranch - parse one branch of an RE |
| 727 |
* This mostly manages concatenation, working closely with parseqatom(). |
| 728 |
* Concatenated things are bundled up as much as possible, with separate |
| 729 |
* ',' nodes introduced only when necessary due to substructure. |
| 730 |
^ static struct subre *parsebranch(struct vars *, int, int, struct state *, |
| 731 |
^ struct state *, int); |
| 732 |
*/ |
| 733 |
static struct subre * |
| 734 |
parsebranch( |
| 735 |
struct vars *v, |
| 736 |
int stopper, /* EOS or ')' */ |
| 737 |
int type, /* LACON (lookahead subRE) or PLAIN */ |
| 738 |
struct state *left, /* leftmost state */ |
| 739 |
struct state *right, /* rightmost state */ |
| 740 |
int partial) /* is this only part of a branch? */ |
| 741 |
{ |
| 742 |
struct state *lp; /* left end of current construct */ |
| 743 |
int seencontent; /* is there anything in this branch yet? */ |
| 744 |
struct subre *t; |
| 745 |
|
| 746 |
lp = left; |
| 747 |
seencontent = 0; |
| 748 |
t = subre(v, '=', 0, left, right); /* op '=' is tentative */ |
| 749 |
NOERRN(); |
| 750 |
while (!SEE('|') && !SEE(stopper) && !SEE(EOS)) { |
| 751 |
if (seencontent) { /* implicit concat operator */ |
| 752 |
lp = newstate(v->nfa); |
| 753 |
NOERRN(); |
| 754 |
moveins(v->nfa, right, lp); |
| 755 |
} |
| 756 |
seencontent = 1; |
| 757 |
|
| 758 |
/* NB, recursion in parseqatom() may swallow rest of branch */ |
| 759 |
parseqatom(v, stopper, type, lp, right, t); |
| 760 |
NOERRN(); |
| 761 |
} |
| 762 |
|
| 763 |
if (!seencontent) { /* empty branch */ |
| 764 |
if (!partial) { |
| 765 |
NOTE(REG_UUNSPEC); |
| 766 |
} |
| 767 |
assert(lp == left); |
| 768 |
EMPTYARC(left, right); |
| 769 |
} |
| 770 |
|
| 771 |
return t; |
| 772 |
} |
| 773 |
|
| 774 |
/* |
| 775 |
- parseqatom - parse one quantified atom or constraint of an RE |
| 776 |
* The bookkeeping near the end cooperates very closely with parsebranch(); in |
| 777 |
* particular, it contains a recursion that can involve parsing the rest of |
| 778 |
* the branch, making this function's name somewhat inaccurate. |
| 779 |
^ static void parseqatom(struct vars *, int, int, struct state *, |
| 780 |
^ struct state *, struct subre *); |
| 781 |
*/ |
| 782 |
static void |
| 783 |
parseqatom( |
| 784 |
struct vars *v, |
| 785 |
int stopper, /* EOS or ')' */ |
| 786 |
int type, /* LACON (lookahead subRE) or PLAIN */ |
| 787 |
struct state *lp, /* left state to hang it on */ |
| 788 |
struct state *rp, /* right state to hang it on */ |
| 789 |
struct subre *top) /* subtree top */ |
| 790 |
{ |
| 791 |
struct state *s; /* temporaries for new states */ |
| 792 |
struct state *s2; |
| 793 |
#define ARCV(t, val) newarc(v->nfa, t, val, lp, rp) |
| 794 |
int m, n; |
| 795 |
struct subre *atom; /* atom's subtree */ |
| 796 |
struct subre *t; |
| 797 |
int cap; /* capturing parens? */ |
| 798 |
int pos; /* positive lookahead? */ |
| 799 |
int subno; /* capturing-parens or backref number */ |
| 800 |
int atomtype; |
| 801 |
int qprefer; /* quantifier short/long preference */ |
| 802 |
int f; |
| 803 |
struct subre **atomp; /* where the pointer to atom is */ |
| 804 |
|
| 805 |
/* |
| 806 |
* Initial bookkeeping. |
| 807 |
*/ |
| 808 |
|
| 809 |
atom = NULL; |
| 810 |
assert(lp->nouts == 0); /* must string new code */ |
| 811 |
assert(rp->nins == 0); /* between lp and rp */ |
| 812 |
subno = 0; /* just to shut lint up */ |
| 813 |
|
| 814 |
/* |
| 815 |
* An atom or constraint... |
| 816 |
*/ |
| 817 |
|
| 818 |
atomtype = v->nexttype; |
| 819 |
switch (atomtype) { |
| 820 |
/* first, constraints, which end by returning */ |
| 821 |
case '^': |
| 822 |
ARCV('^', 1); |
| 823 |
if (v->cflags®_NLANCH) { |
| 824 |
ARCV(BEHIND, v->nlcolor); |
| 825 |
} |
| 826 |
NEXT(); |
| 827 |
return; |
| 828 |
case '$': |
| 829 |
ARCV('$', 1); |
| 830 |
if (v->cflags®_NLANCH) { |
| 831 |
ARCV(AHEAD, v->nlcolor); |
| 832 |
} |
| 833 |
NEXT(); |
| 834 |
return; |
| 835 |
case SBEGIN: |
| 836 |
ARCV('^', 1); /* BOL */ |
| 837 |
ARCV('^', 0); /* or BOS */ |
| 838 |
NEXT(); |
| 839 |
return; |
| 840 |
case SEND: |
| 841 |
ARCV('$', 1); /* EOL */ |
| 842 |
ARCV('$', 0); /* or EOS */ |
| 843 |
NEXT(); |
| 844 |
return; |
| 845 |
case '<': |
| 846 |
wordchrs(v); /* does NEXT() */ |
| 847 |
s = newstate(v->nfa); |
| 848 |
NOERR(); |
| 849 |
nonword(v, BEHIND, lp, s); |
| 850 |
word(v, AHEAD, s, rp); |
| 851 |
return; |
| 852 |
case '>': |
| 853 |
wordchrs(v); /* does NEXT() */ |
| 854 |
s = newstate(v->nfa); |
| 855 |
NOERR(); |
| 856 |
word(v, BEHIND, lp, s); |
| 857 |
nonword(v, AHEAD, s, rp); |
| 858 |
return; |
| 859 |
case WBDRY: |
| 860 |
wordchrs(v); /* does NEXT() */ |
| 861 |
s = newstate(v->nfa); |
| 862 |
NOERR(); |
| 863 |
nonword(v, BEHIND, lp, s); |
| 864 |
word(v, AHEAD, s, rp); |
| 865 |
s = newstate(v->nfa); |
| 866 |
NOERR(); |
| 867 |
word(v, BEHIND, lp, s); |
| 868 |
nonword(v, AHEAD, s, rp); |
| 869 |
return; |
| 870 |
case NWBDRY: |
| 871 |
wordchrs(v); /* does NEXT() */ |
| 872 |
s = newstate(v->nfa); |
| 873 |
NOERR(); |
| 874 |
word(v, BEHIND, lp, s); |
| 875 |
word(v, AHEAD, s, rp); |
| 876 |
s = newstate(v->nfa); |
| 877 |
NOERR(); |
| 878 |
nonword(v, BEHIND, lp, s); |
| 879 |
nonword(v, AHEAD, s, rp); |
| 880 |
return; |
| 881 |
case LACON: /* lookahead constraint */ |
| 882 |
pos = v->nextvalue; |
| 883 |
NEXT(); |
| 884 |
s = newstate(v->nfa); |
| 885 |
s2 = newstate(v->nfa); |
| 886 |
NOERR(); |
| 887 |
t = parse(v, ')', LACON, s, s2); |
| 888 |
freesubre(v, t); /* internal structure irrelevant */ |
| 889 |
assert(SEE(')') || ISERR()); |
| 890 |
NEXT(); |
| 891 |
n = newlacon(v, s, s2, pos); |
| 892 |
NOERR(); |
| 893 |
ARCV(LACON, n); |
| 894 |
return; |
| 895 |
|
| 896 |
/* |
| 897 |
* Then errors, to get them out of the way. |
| 898 |
*/ |
| 899 |
|
| 900 |
case '*': |
| 901 |
case '+': |
| 902 |
case '?': |
| 903 |
case '{': |
| 904 |
ERR(REG_BADRPT); |
| 905 |
return; |
| 906 |
default: |
| 907 |
ERR(REG_ASSERT); |
| 908 |
return; |
| 909 |
|
| 910 |
/* |
| 911 |
* Then plain characters, and minor variants on that theme. |
| 912 |
*/ |
| 913 |
|
| 914 |
case ')': /* unbalanced paren */ |
| 915 |
if ((v->cflags®_ADVANCED) != REG_EXTENDED) { |
| 916 |
ERR(REG_EPAREN); |
| 917 |
return; |
| 918 |
} |
| 919 |
|
| 920 |
/* |
| 921 |
* Legal in EREs due to specification botch. |
| 922 |
*/ |
| 923 |
|
| 924 |
NOTE(REG_UPBOTCH); |
| 925 |
/* FALLTHRU */ |
| 926 |
case PLAIN: |
| 927 |
onechr(v, v->nextvalue, lp, rp); |
| 928 |
okcolors(v->nfa, v->cm); |
| 929 |
NOERR(); |
| 930 |
NEXT(); |
| 931 |
break; |
| 932 |
case '[': |
| 933 |
if (v->nextvalue == 1) { |
| 934 |
bracket(v, lp, rp); |
| 935 |
} else { |
| 936 |
cbracket(v, lp, rp); |
| 937 |
} |
| 938 |
assert(SEE(']') || ISERR()); |
| 939 |
NEXT(); |
| 940 |
break; |
| 941 |
case '.': |
| 942 |
rainbow(v->nfa, v->cm, PLAIN, |
| 943 |
(v->cflags®_NLSTOP) ? v->nlcolor : COLORLESS, lp, rp); |
| 944 |
NEXT(); |
| 945 |
break; |
| 946 |
|
| 947 |
/* |
| 948 |
* And finally the ugly stuff. |
| 949 |
*/ |
| 950 |
|
| 951 |
case '(': /* value flags as capturing or non */ |
| 952 |
cap = (type == LACON) ? 0 : v->nextvalue; |
| 953 |
if (cap) { |
| 954 |
v->nsubexp++; |
| 955 |
subno = v->nsubexp; |
| 956 |
if ((size_t)subno >= v->nsubs) { |
| 957 |
moresubs(v, subno); |
| 958 |
} |
| 959 |
assert((size_t)subno < v->nsubs); |
| 960 |
} else { |
| 961 |
atomtype = PLAIN; /* something that's not '(' */ |
| 962 |
} |
| 963 |
NEXT(); |
| 964 |
|
| 965 |
/* |
| 966 |
* Need new endpoints because tree will contain pointers. |
| 967 |
*/ |
| 968 |
|
| 969 |
s = newstate(v->nfa); |
| 970 |
s2 = newstate(v->nfa); |
| 971 |
NOERR(); |
| 972 |
EMPTYARC(lp, s); |
| 973 |
EMPTYARC(s2, rp); |
| 974 |
NOERR(); |
| 975 |
atom = parse(v, ')', PLAIN, s, s2); |
| 976 |
assert(SEE(')') || ISERR()); |
| 977 |
NEXT(); |
| 978 |
NOERR(); |
| 979 |
if (cap) { |
| 980 |
v->subs[subno] = atom; |
| 981 |
t = subre(v, '(', atom->flags|CAP, lp, rp); |
| 982 |
NOERR(); |
| 983 |
t->subno = subno; |
| 984 |
t->left = atom; |
| 985 |
atom = t; |
| 986 |
} |
| 987 |
|
| 988 |
/* |
| 989 |
* Postpone everything else pending possible {0}. |
| 990 |
*/ |
| 991 |
|
| 992 |
break; |
| 993 |
case BACKREF: /* the Feature From The Black Lagoon */ |
| 994 |
INSIST(type != LACON, REG_ESUBREG); |
| 995 |
INSIST(v->nextvalue < v->nsubs, REG_ESUBREG); |
| 996 |
INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG); |
| 997 |
NOERR(); |
| 998 |
assert(v->nextvalue > 0); |
| 999 |
atom = subre(v, 'b', BACKR, lp, rp); |
| 1000 |
NOERR(); |
| 1001 |
subno = v->nextvalue; |
| 1002 |
atom->subno = subno; |
| 1003 |
EMPTYARC(lp, rp); /* temporarily, so there's something */ |
| 1004 |
NEXT(); |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* ...and an atom may be followed by a quantifier. |
| 1010 |
*/ |
| 1011 |
|
| 1012 |
switch (v->nexttype) { |
| 1013 |
case '*': |
| 1014 |
m = 0; |
| 1015 |
n = DUPINF; |
| 1016 |
qprefer = (v->nextvalue) ? LONGER : SHORTER; |
| 1017 |
NEXT(); |
| 1018 |
break; |
| 1019 |
case '+': |
| 1020 |
m = 1; |
| 1021 |
n = DUPINF; |
| 1022 |
qprefer = (v->nextvalue) ? LONGER : SHORTER; |
| 1023 |
NEXT(); |
| 1024 |
break; |
| 1025 |
case '?': |
| 1026 |
m = 0; |
| 1027 |
n = 1; |
| 1028 |
qprefer = (v->nextvalue) ? LONGER : SHORTER; |
| 1029 |
NEXT(); |
| 1030 |
break; |
| 1031 |
case '{': |
| 1032 |
NEXT(); |
| 1033 |
m = scannum(v); |
| 1034 |
if (EAT(',')) { |
| 1035 |
if (SEE(DIGIT)) { |
| 1036 |
n = scannum(v); |
| 1037 |
} else { |
| 1038 |
n = DUPINF; |
| 1039 |
} |
| 1040 |
if (m > n) { |
| 1041 |
ERR(REG_BADBR); |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* {m,n} exercises preference, even if it's {m,m} |
| 1047 |
*/ |
| 1048 |
|
| 1049 |
qprefer = (v->nextvalue) ? LONGER : SHORTER; |
| 1050 |
} else { |
| 1051 |
n = m; |
| 1052 |
/* |
| 1053 |
* {m} passes operand's preference through. |
| 1054 |
*/ |
| 1055 |
|
| 1056 |
qprefer = 0; |
| 1057 |
} |
| 1058 |
if (!SEE('}')) { /* catches errors too */ |
| 1059 |
ERR(REG_BADBR); |
| 1060 |
return; |
| 1061 |
} |
| 1062 |
NEXT(); |
| 1063 |
break; |
| 1064 |
default: /* no quantifier */ |
| 1065 |
m = n = 1; |
| 1066 |
qprefer = 0; |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* |
| 1071 |
* Annoying special case: {0} or {0,0} cancels everything. |
| 1072 |
*/ |
| 1073 |
|
| 1074 |
if (m == 0 && n == 0) { |
| 1075 |
if (atom != NULL) { |
| 1076 |
freesubre(v, atom); |
| 1077 |
} |
| 1078 |
if (atomtype == '(') { |
| 1079 |
v->subs[subno] = NULL; |
| 1080 |
} |
| 1081 |
delsub(v->nfa, lp, rp); |
| 1082 |
EMPTYARC(lp, rp); |
| 1083 |
return; |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* |
| 1087 |
* If not a messy case, avoid hard part. |
| 1088 |
*/ |
| 1089 |
|
| 1090 |
assert(!MESSY(top->flags)); |
| 1091 |
f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0); |
| 1092 |
if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f))) { |
| 1093 |
if (!(m == 1 && n == 1)) { |
| 1094 |
repeat(v, lp, rp, m, n); |
| 1095 |
} |
| 1096 |
if (atom != NULL) { |
| 1097 |
freesubre(v, atom); |
| 1098 |
} |
| 1099 |
top->flags = f; |
| 1100 |
return; |
| 1101 |
} |
| 1102 |
|
| 1103 |
/* |
| 1104 |
* hard part: something messy |
| 1105 |
* That is, capturing parens, back reference, short/long clash, or an atom |
| 1106 |
* with substructure containing one of those. |
| 1107 |
*/ |
| 1108 |
|
| 1109 |
/* |
| 1110 |
* Now we'll need a subre for the contents even if they're boring. |
| 1111 |
*/ |
| 1112 |
|
| 1113 |
if (atom == NULL) { |
| 1114 |
atom = subre(v, '=', 0, lp, rp); |
| 1115 |
NOERR(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* Prepare a general-purpose state skeleton. |
| 1120 |
* |
| 1121 |
* In the no-backrefs case, we want this: |
| 1122 |
* |
| 1123 |
* [lp] ---> [s] ---prefix---> [begin] ---atom---> [end] ---rest---> [rp] |
| 1124 |
* |
| 1125 |
* where prefix is some repetitions of atom. In the general case we need |
| 1126 |
* |
| 1127 |
* [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp] |
| 1128 |
* |
| 1129 |
* where the iterator wraps around [begin] ---atom---> [end] |
| 1130 |
* |
| 1131 |
* We make the s state here for both cases; s2 is made below if needed |
| 1132 |
*/ |
| 1133 |
|
| 1134 |
s = newstate(v->nfa); /* first, new endpoints for the atom */ |
| 1135 |
s2 = newstate(v->nfa); |
| 1136 |
NOERR(); |
| 1137 |
moveouts(v->nfa, lp, s); |
| 1138 |
moveins(v->nfa, rp, s2); |
| 1139 |
NOERR(); |
| 1140 |
atom->begin = s; |
| 1141 |
atom->end = s2; |
| 1142 |
s = newstate(v->nfa); /* set up starting state */ |
| 1143 |
NOERR(); |
| 1144 |
EMPTYARC(lp, s); |
| 1145 |
NOERR(); |
| 1146 |
|
| 1147 |
/* |
| 1148 |
* Break remaining subRE into x{...} and what follows. |
| 1149 |
*/ |
| 1150 |
|
| 1151 |
t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp); |
| 1152 |
NOERR(); |
| 1153 |
t->left = atom; |
| 1154 |
atomp = &t->left; |
| 1155 |
|
| 1156 |
/* |
| 1157 |
* Here we should recurse... but we must postpone that to the end. |
| 1158 |
*/ |
| 1159 |
|
| 1160 |
/* |
| 1161 |
* Split top into prefix and remaining. |
| 1162 |
*/ |
| 1163 |
|
| 1164 |
assert(top->op == '=' && top->left == NULL && top->right == NULL); |
| 1165 |
top->left = subre(v, '=', top->flags, top->begin, lp); |
| 1166 |
NOERR(); |
| 1167 |
top->op = '.'; |
| 1168 |
top->right = t; |
| 1169 |
|
| 1170 |
/* |
| 1171 |
* If it's a backref, now is the time to replicate the subNFA. |
| 1172 |
*/ |
| 1173 |
|
| 1174 |
if (atomtype == BACKREF) { |
| 1175 |
assert(atom->begin->nouts == 1); /* just the EMPTY */ |
| 1176 |
delsub(v->nfa, atom->begin, atom->end); |
| 1177 |
assert(v->subs[subno] != NULL); |
| 1178 |
|
| 1179 |
/* |
| 1180 |
* And here's why the recursion got postponed: it must wait until the |
| 1181 |
* skeleton is filled in, because it may hit a backref that wants to |
| 1182 |
* copy the filled-in skeleton. |
| 1183 |
*/ |
| 1184 |
|
| 1185 |
dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end, |
| 1186 |
atom->begin, atom->end); |
| 1187 |
NOERR(); |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* |
| 1191 |
* It's quantifier time. If the atom is just a backref, we'll let it deal |
| 1192 |
* with quantifiers internally. |
| 1193 |
*/ |
| 1194 |
|
| 1195 |
if (atomtype == BACKREF) { |
| 1196 |
/* |
| 1197 |
* Special case: backrefs have internal quantifiers. |
| 1198 |
*/ |
| 1199 |
|
| 1200 |
EMPTYARC(s, atom->begin); /* empty prefix */ |
| 1201 |
|
| 1202 |
/* |
| 1203 |
* Just stuff everything into atom. |
| 1204 |
*/ |
| 1205 |
|
| 1206 |
repeat(v, atom->begin, atom->end, m, n); |
| 1207 |
atom->min = (short) m; |
| 1208 |
atom->max = (short) n; |
| 1209 |
atom->flags |= COMBINE(qprefer, atom->flags); |
| 1210 |
/* rest of branch can be strung starting from atom->end */ |
| 1211 |
s2 = atom->end; |
| 1212 |
} else if (m == 1 && n == 1) { |
| 1213 |
/* |
| 1214 |
* No/vacuous quantifier: done. |
| 1215 |
*/ |
| 1216 |
|
| 1217 |
EMPTYARC(s, atom->begin); /* empty prefix */ |
| 1218 |
/* rest of branch can be strung starting from atom->end */ |
| 1219 |
s2 = atom->end; |
| 1220 |
} else if (m > 0 && !(atom->flags & BACKR)) { |
| 1221 |
/* |
| 1222 |
* If there's no backrefs involved, we can turn x{m,n} into |
| 1223 |
* x{m-1,n-1}x, with capturing parens in only the second x. This |
| 1224 |
* is valid because we only care about capturing matches from the |
| 1225 |
* final iteration of the quantifier. It's a win because we can |
| 1226 |
* implement the backref-free left side as a plain DFA node, since |
| 1227 |
* we don't really care where its submatches are. |
| 1228 |
*/ |
| 1229 |
|
| 1230 |
dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin); |
| 1231 |
assert(m >= 1 && m != DUPINF && n >= 1); |
| 1232 |
repeat(v, s, atom->begin, m-1, (n == DUPINF) ? n : n-1); |
| 1233 |
f = COMBINE(qprefer, atom->flags); |
| 1234 |
t = subre(v, '.', f, s, atom->end); /* prefix and atom */ |
| 1235 |
NOERR(); |
| 1236 |
t->left = subre(v, '=', PREF(f), s, atom->begin); |
| 1237 |
NOERR(); |
| 1238 |
t->right = atom; |
| 1239 |
*atomp = t; |
| 1240 |
/* rest of branch can be strung starting from atom->end */ |
| 1241 |
s2 = atom->end; |
| 1242 |
} else { |
| 1243 |
/* general case: need an iteration node */ |
| 1244 |
s2 = newstate(v->nfa); |
| 1245 |
NOERR(); |
| 1246 |
moveouts(v->nfa, atom->end, s2); |
| 1247 |
NOERR(); |
| 1248 |
dupnfa(v->nfa, atom->begin, atom->end, s, s2); |
| 1249 |
repeat(v, s, s2, m, n); |
| 1250 |
f = COMBINE(qprefer, atom->flags); |
| 1251 |
t = subre(v, '*', f, s, s2); |
| 1252 |
NOERR(); |
| 1253 |
t->min = (short) m; |
| 1254 |
t->max = (short) n; |
| 1255 |
t->left = atom; |
| 1256 |
*atomp = t; |
| 1257 |
/* rest of branch is to be strung from iteration's end state */ |
| 1258 |
} |
| 1259 |
|
| 1260 |
/* |
| 1261 |
* And finally, look after that postponed recursion. |
| 1262 |
*/ |
| 1263 |
|
| 1264 |
t = top->right; |
| 1265 |
if (!(SEE('|') || SEE(stopper) || SEE(EOS))) { |
| 1266 |
t->right = parsebranch(v, stopper, type, s2, rp, 1); |
| 1267 |
} else { |
| 1268 |
EMPTYARC(s2, rp); |
| 1269 |
t->right = subre(v, '=', 0, s2, rp); |
| 1270 |
} |
| 1271 |
NOERR(); |
| 1272 |
assert(SEE('|') || SEE(stopper) || SEE(EOS)); |
| 1273 |
t->flags |= COMBINE(t->flags, t->right->flags); |
| 1274 |
top->flags |= COMBINE(top->flags, t->flags); |
| 1275 |
} |
| 1276 |
|
| 1277 |
/* |
| 1278 |
- nonword - generate arcs for non-word-character ahead or behind |
| 1279 |
^ static void nonword(struct vars *, int, struct state *, struct state *); |
| 1280 |
*/ |
| 1281 |
static void |
| 1282 |
nonword( |
| 1283 |
struct vars *v, |
| 1284 |
int dir, /* AHEAD or BEHIND */ |
| 1285 |
struct state *lp, |
| 1286 |
struct state *rp) |
| 1287 |
{ |
| 1288 |
int anchor = (dir == AHEAD) ? '$' : '^'; |
| 1289 |
|
| 1290 |
assert(dir == AHEAD || dir == BEHIND); |
| 1291 |
newarc(v->nfa, anchor, 1, lp, rp); |
| 1292 |
newarc(v->nfa, anchor, 0, lp, rp); |
| 1293 |
colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp); |
| 1294 |
/* (no need for special attention to \n) */ |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* |
| 1298 |
- word - generate arcs for word character ahead or behind |
| 1299 |
^ static void word(struct vars *, int, struct state *, struct state *); |
| 1300 |
*/ |
| 1301 |
static void |
| 1302 |
word( |
| 1303 |
struct vars *v, |
| 1304 |
int dir, /* AHEAD or BEHIND */ |
| 1305 |
struct state *lp, |
| 1306 |
struct state *rp) |
| 1307 |
{ |
| 1308 |
assert(dir == AHEAD || dir == BEHIND); |
| 1309 |
cloneouts(v->nfa, v->wordchrs, lp, rp, dir); |
| 1310 |
/* (no need for special attention to \n) */ |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* |
| 1314 |
- scannum - scan a number |
| 1315 |
^ static int scannum(struct vars *); |
| 1316 |
*/ |
| 1317 |
static int /* value, <= DUPMAX */ |
| 1318 |
scannum( |
| 1319 |
struct vars *v) |
| 1320 |
{ |
| 1321 |
int n = 0; |
| 1322 |
|
| 1323 |
while (SEE(DIGIT) && n < DUPMAX) { |
| 1324 |
n = n*10 + v->nextvalue; |
| 1325 |
NEXT(); |
| 1326 |
} |
| 1327 |
if (SEE(DIGIT) || n > DUPMAX) { |
| 1328 |
ERR(REG_BADBR); |
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
return n; |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* |
| 1335 |
- repeat - replicate subNFA for quantifiers |
| 1336 |
* The sub-NFA strung from lp to rp is modified to represent m to n |
| 1337 |
* repetitions of its initial contents. |
| 1338 |
* The duplication sequences used here are chosen carefully so that any |
| 1339 |
* pointers starting out pointing into the subexpression end up pointing into |
| 1340 |
* the last occurrence. (Note that it may not be strung between the same left |
| 1341 |
* and right end states, however!) This used to be important for the subRE |
| 1342 |
* tree, although the important bits are now handled by the in-line code in |
| 1343 |
* parse(), and when this is called, it doesn't matter any more. |
| 1344 |
^ static void repeat(struct vars *, struct state *, struct state *, int, int); |
| 1345 |
*/ |
| 1346 |
static void |
| 1347 |
repeat( |
| 1348 |
struct vars *v, |
| 1349 |
struct state *lp, |
| 1350 |
struct state *rp, |
| 1351 |
int m, |
| 1352 |
int n) |
| 1353 |
{ |
| 1354 |
#define SOME 2 |
| 1355 |
#define INF 3 |
| 1356 |
#define PAIR(x, y) ((x)*4 + (y)) |
| 1357 |
#define REDUCE(x) ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) ) |
| 1358 |
const int rm = REDUCE(m); |
| 1359 |
const int rn = REDUCE(n); |
| 1360 |
struct state *s, *s2; |
| 1361 |
|
| 1362 |
switch (PAIR(rm, rn)) { |
| 1363 |
case PAIR(0, 0): /* empty string */ |
| 1364 |
delsub(v->nfa, lp, rp); |
| 1365 |
EMPTYARC(lp, rp); |
| 1366 |
break; |
| 1367 |
case PAIR(0, 1): /* do as x| */ |
| 1368 |
EMPTYARC(lp, rp); |
| 1369 |
break; |
| 1370 |
case PAIR(0, SOME): /* do as x{1,n}| */ |
| 1371 |
repeat(v, lp, rp, 1, n); |
| 1372 |
NOERR(); |
| 1373 |
EMPTYARC(lp, rp); |
| 1374 |
break; |
| 1375 |
case PAIR(0, INF): /* loop x around */ |
| 1376 |
s = newstate(v->nfa); |
| 1377 |
NOERR(); |
| 1378 |
moveouts(v->nfa, lp, s); |
| 1379 |
moveins(v->nfa, rp, s); |
| 1380 |
EMPTYARC(lp, s); |
| 1381 |
EMPTYARC(s, rp); |
| 1382 |
break; |
| 1383 |
case PAIR(1, 1): /* no action required */ |
| 1384 |
break; |
| 1385 |
case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */ |
| 1386 |
s = newstate(v->nfa); |
| 1387 |
NOERR(); |
| 1388 |
moveouts(v->nfa, lp, s); |
| 1389 |
dupnfa(v->nfa, s, rp, lp, s); |
| 1390 |
NOERR(); |
| 1391 |
repeat(v, lp, s, 1, n-1); |
| 1392 |
NOERR(); |
| 1393 |
EMPTYARC(lp, s); |
| 1394 |
break; |
| 1395 |
case PAIR(1, INF): /* add loopback arc */ |
| 1396 |
s = newstate(v->nfa); |
| 1397 |
s2 = newstate(v->nfa); |
| 1398 |
NOERR(); |
| 1399 |
moveouts(v->nfa, lp, s); |
| 1400 |
moveins(v->nfa, rp, s2); |
| 1401 |
EMPTYARC(lp, s); |
| 1402 |
EMPTYARC(s2, rp); |
| 1403 |
EMPTYARC(s2, s); |
| 1404 |
break; |
| 1405 |
case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */ |
| 1406 |
s = newstate(v->nfa); |
| 1407 |
NOERR(); |
| 1408 |
moveouts(v->nfa, lp, s); |
| 1409 |
dupnfa(v->nfa, s, rp, lp, s); |
| 1410 |
NOERR(); |
| 1411 |
repeat(v, lp, s, m-1, n-1); |
| 1412 |
break; |
| 1413 |
case PAIR(SOME, INF): /* do as x{m-1,}x */ |
| 1414 |
s = newstate(v->nfa); |
| 1415 |
NOERR(); |
| 1416 |
moveouts(v->nfa, lp, s); |
| 1417 |
dupnfa(v->nfa, s, rp, lp, s); |
| 1418 |
NOERR(); |
| 1419 |
repeat(v, lp, s, m-1, n); |
| 1420 |
break; |
| 1421 |
default: |
| 1422 |
ERR(REG_ASSERT); |
| 1423 |
break; |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* |
| 1428 |
- bracket - handle non-complemented bracket expression |
| 1429 |
* Also called from cbracket for complemented bracket expressions. |
| 1430 |
^ static void bracket(struct vars *, struct state *, struct state *); |
| 1431 |
*/ |
| 1432 |
static void |
| 1433 |
bracket( |
| 1434 |
struct vars *v, |
| 1435 |
struct state *lp, |
| 1436 |
struct state *rp) |
| 1437 |
{ |
| 1438 |
assert(SEE('[')); |
| 1439 |
NEXT(); |
| 1440 |
while (!SEE(']') && !SEE(EOS)) { |
| 1441 |
brackpart(v, lp, rp); |
| 1442 |
} |
| 1443 |
assert(SEE(']') || ISERR()); |
| 1444 |
okcolors(v->nfa, v->cm); |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* |
| 1448 |
- cbracket - handle complemented bracket expression |
| 1449 |
* We do it by calling bracket() with dummy endpoints, and then complementing |
| 1450 |
* the result. The alternative would be to invoke rainbow(), and then delete |
| 1451 |
* arcs as the b.e. is seen... but that gets messy. |
| 1452 |
^ static void cbracket(struct vars *, struct state *, struct state *); |
| 1453 |
*/ |
| 1454 |
static void |
| 1455 |
cbracket( |
| 1456 |
struct vars *v, |
| 1457 |
struct state *lp, |
| 1458 |
struct state *rp) |
| 1459 |
{ |
| 1460 |
struct state *left = newstate(v->nfa); |
| 1461 |
struct state *right = newstate(v->nfa); |
| 1462 |
|
| 1463 |
NOERR(); |
| 1464 |
bracket(v, left, right); |
| 1465 |
if (v->cflags®_NLSTOP) { |
| 1466 |
newarc(v->nfa, PLAIN, v->nlcolor, left, right); |
| 1467 |
} |
| 1468 |
NOERR(); |
| 1469 |
|
| 1470 |
assert(lp->nouts == 0); /* all outarcs will be ours */ |
| 1471 |
|
| 1472 |
/* |
| 1473 |
* Easy part of complementing, and all there is to do since the MCCE code |
| 1474 |
* was removed. |
| 1475 |
*/ |
| 1476 |
|
| 1477 |
colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp); |
| 1478 |
NOERR(); |
| 1479 |
dropstate(v->nfa, left); |
| 1480 |
assert(right->nins == 0); |
| 1481 |
freestate(v->nfa, right); |
| 1482 |
return; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/* |
| 1486 |
- brackpart - handle one item (or range) within a bracket expression |
| 1487 |
^ static void brackpart(struct vars *, struct state *, struct state *); |
| 1488 |
*/ |
| 1489 |
static void |
| 1490 |
brackpart( |
| 1491 |
struct vars *v, |
| 1492 |
struct state *lp, |
| 1493 |
struct state *rp) |
| 1494 |
{ |
| 1495 |
celt startc, endc; |
| 1496 |
struct cvec *cv; |
| 1497 |
const chr *startp, *endp; |
| 1498 |
chr c; |
| 1499 |
|
| 1500 |
/* |
| 1501 |
* Parse something, get rid of special cases, take shortcuts. |
| 1502 |
*/ |
| 1503 |
|
| 1504 |
switch (v->nexttype) { |
| 1505 |
case RANGE: /* a-b-c or other botch */ |
| 1506 |
ERR(REG_ERANGE); |
| 1507 |
return; |
| 1508 |
break; |
| 1509 |
case PLAIN: |
| 1510 |
c = v->nextvalue; |
| 1511 |
NEXT(); |
| 1512 |
|
| 1513 |
/* |
| 1514 |
* Shortcut for ordinary chr (not range). |
| 1515 |
*/ |
| 1516 |
|
| 1517 |
if (!SEE(RANGE)) { |
| 1518 |
onechr(v, c, lp, rp); |
| 1519 |
return; |
| 1520 |
} |
| 1521 |
startc = element(v, &c, &c+1); |
| 1522 |
NOERR(); |
| 1523 |
break; |
| 1524 |
case COLLEL: |
| 1525 |
startp = v->now; |
| 1526 |
endp = scanplain(v); |
| 1527 |
INSIST(startp < endp, REG_ECOLLATE); |
| 1528 |
NOERR(); |
| 1529 |
startc = element(v, startp, endp); |
| 1530 |
NOERR(); |
| 1531 |
break; |
| 1532 |
case ECLASS: |
| 1533 |
startp = v->now; |
| 1534 |
endp = scanplain(v); |
| 1535 |
INSIST(startp < endp, REG_ECOLLATE); |
| 1536 |
NOERR(); |
| 1537 |
startc = element(v, startp, endp); |
| 1538 |
NOERR(); |
| 1539 |
cv = eclass(v, startc, (v->cflags®_ICASE)); |
| 1540 |
NOERR(); |
| 1541 |
dovec(v, cv, lp, rp); |
| 1542 |
return; |
| 1543 |
break; |
| 1544 |
case CCLASS: |
| 1545 |
startp = v->now; |
| 1546 |
endp = scanplain(v); |
| 1547 |
INSIST(startp < endp, REG_ECTYPE); |
| 1548 |
NOERR(); |
| 1549 |
cv = cclass(v, startp, endp, (v->cflags®_ICASE)); |
| 1550 |
NOERR(); |
| 1551 |
dovec(v, cv, lp, rp); |
| 1552 |
return; |
| 1553 |
break; |
| 1554 |
default: |
| 1555 |
ERR(REG_ASSERT); |
| 1556 |
return; |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (SEE(RANGE)) { |
| 1561 |
NEXT(); |
| 1562 |
switch (v->nexttype) { |
| 1563 |
case PLAIN: |
| 1564 |
case RANGE: |
| 1565 |
c = v->nextvalue; |
| 1566 |
NEXT(); |
| 1567 |
endc = element(v, &c, &c+1); |
| 1568 |
NOERR(); |
| 1569 |
break; |
| 1570 |
case COLLEL: |
| 1571 |
startp = v->now; |
| 1572 |
endp = scanplain(v); |
| 1573 |
INSIST(startp < endp, REG_ECOLLATE); |
| 1574 |
NOERR(); |
| 1575 |
endc = element(v, startp, endp); |
| 1576 |
NOERR(); |
| 1577 |
break; |
| 1578 |
default: |
| 1579 |
ERR(REG_ERANGE); |
| 1580 |
return; |
| 1581 |
break; |
| 1582 |
} |
| 1583 |
} else { |
| 1584 |
endc = startc; |
| 1585 |
} |
| 1586 |
|
| 1587 |
/* |
| 1588 |
* Ranges are unportable. Actually, standard C does guarantee that digits |
| 1589 |
* are contiguous, but making that an exception is just too complicated. |
| 1590 |
*/ |
| 1591 |
|
| 1592 |
if (startc != endc) { |
| 1593 |
NOTE(REG_UUNPORT); |
| 1594 |
} |
| 1595 |
cv = range(v, startc, endc, (v->cflags®_ICASE)); |
| 1596 |
NOERR(); |
| 1597 |
dovec(v, cv, lp, rp); |
| 1598 |
} |
| 1599 |
|
| 1600 |
/* |
| 1601 |
- scanplain - scan PLAIN contents of [. etc. |
| 1602 |
* Certain bits of trickery in lex.c know that this code does not try to look |
| 1603 |
* past the final bracket of the [. etc. |
| 1604 |
^ static const chr *scanplain(struct vars *); |
| 1605 |
*/ |
| 1606 |
static const chr * /* just after end of sequence */ |
| 1607 |
scanplain( |
| 1608 |
struct vars *v) |
| 1609 |
{ |
| 1610 |
const chr *endp; |
| 1611 |
|
| 1612 |
assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS)); |
| 1613 |
NEXT(); |
| 1614 |
|
| 1615 |
endp = v->now; |
| 1616 |
while (SEE(PLAIN)) { |
| 1617 |
endp = v->now; |
| 1618 |
NEXT(); |
| 1619 |
} |
| 1620 |
|
| 1621 |
assert(SEE(END) || ISERR()); |
| 1622 |
NEXT(); |
| 1623 |
|
| 1624 |
return endp; |
| 1625 |
} |
| 1626 |
|
| 1627 |
/* |
| 1628 |
- onechr - fill in arcs for a plain character, and possible case complements |
| 1629 |
* This is mostly a shortcut for efficient handling of the common case. |
| 1630 |
^ static void onechr(struct vars *, pchr, struct state *, struct state *); |
| 1631 |
*/ |
| 1632 |
static void |
| 1633 |
onechr( |
| 1634 |
struct vars *v, |
| 1635 |
pchr c, |
| 1636 |
struct state *lp, |
| 1637 |
struct state *rp) |
| 1638 |
{ |
| 1639 |
if (!(v->cflags®_ICASE)) { |
| 1640 |
newarc(v->nfa, PLAIN, subcolor(v->cm, c), lp, rp); |
| 1641 |
return; |
| 1642 |
} |
| 1643 |
|
| 1644 |
/* |
| 1645 |
* Rats, need general case anyway... |
| 1646 |
*/ |
| 1647 |
|
| 1648 |
dovec(v, allcases(v, c), lp, rp); |
| 1649 |
} |
| 1650 |
|
| 1651 |
/* |
| 1652 |
- dovec - fill in arcs for each element of a cvec |
| 1653 |
^ static void dovec(struct vars *, struct cvec *, struct state *, |
| 1654 |
^ struct state *); |
| 1655 |
*/ |
| 1656 |
static void |
| 1657 |
dovec( |
| 1658 |
struct vars *v, |
| 1659 |
struct cvec *cv, |
| 1660 |
struct state *lp, |
| 1661 |
struct state *rp) |
| 1662 |
{ |
| 1663 |
chr ch, from, to; |
| 1664 |
const chr *p; |
| 1665 |
int i; |
| 1666 |
|
| 1667 |
for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--) { |
| 1668 |
ch = *p; |
| 1669 |
newarc(v->nfa, PLAIN, subcolor(v->cm, ch), lp, rp); |
| 1670 |
} |
| 1671 |
|
| 1672 |
for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--) { |
| 1673 |
from = *p; |
| 1674 |
to = *(p+1); |
| 1675 |
if (from <= to) { |
| 1676 |
subrange(v, from, to, lp, rp); |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
} |
| 1681 |
|
| 1682 |
/* |
| 1683 |
- wordchrs - set up word-chr list for word-boundary stuff, if needed |
| 1684 |
* The list is kept as a bunch of arcs between two dummy states; it's disposed |
| 1685 |
* of by the unreachable-states sweep in NFA optimization. Does NEXT(). Must |
| 1686 |
* not be called from any unusual lexical context. This should be reconciled |
| 1687 |
* with the \w etc. handling in lex.c, and should be cleaned up to reduce |
| 1688 |
* dependencies on input scanning. |
| 1689 |
^ static void wordchrs(struct vars *); |
| 1690 |
*/ |
| 1691 |
static void |
| 1692 |
wordchrs( |
| 1693 |
struct vars *v) |
| 1694 |
{ |
| 1695 |
struct state *left, *right; |
| 1696 |
|
| 1697 |
if (v->wordchrs != NULL) { |
| 1698 |
NEXT(); /* for consistency */ |
| 1699 |
return; |
| 1700 |
} |
| 1701 |
|
| 1702 |
left = newstate(v->nfa); |
| 1703 |
right = newstate(v->nfa); |
| 1704 |
NOERR(); |
| 1705 |
|
| 1706 |
/* |
| 1707 |
* Fine point: implemented with [::], and lexer will set REG_ULOCALE. |
| 1708 |
*/ |
| 1709 |
|
| 1710 |
lexword(v); |
| 1711 |
NEXT(); |
| 1712 |
assert(v->savenow != NULL && SEE('[')); |
| 1713 |
bracket(v, left, right); |
| 1714 |
assert((v->savenow != NULL && SEE(']')) || ISERR()); |
| 1715 |
NEXT(); |
| 1716 |
NOERR(); |
| 1717 |
v->wordchrs = left; |
| 1718 |
} |
| 1719 |
|
| 1720 |
/* |
| 1721 |
- subre - allocate a subre |
| 1722 |
^ static struct subre *subre(struct vars *, int, int, struct state *, |
| 1723 |
^ struct state *); |
| 1724 |
*/ |
| 1725 |
static struct subre * |
| 1726 |
subre( |
| 1727 |
struct vars *v, |
| 1728 |
int op, |
| 1729 |
int flags, |
| 1730 |
struct state *begin, |
| 1731 |
struct state *end) |
| 1732 |
{ |
| 1733 |
struct subre *ret = v->treefree; |
| 1734 |
|
| 1735 |
if (ret != NULL) { |
| 1736 |
v->treefree = ret->left; |
| 1737 |
} else { |
| 1738 |
ret = (struct subre *) MALLOC(sizeof(struct subre)); |
| 1739 |
if (ret == NULL) { |
| 1740 |
ERR(REG_ESPACE); |
| 1741 |
return NULL; |
| 1742 |
} |
| 1743 |
ret->chain = v->treechain; |
| 1744 |
v->treechain = ret; |
| 1745 |
} |
| 1746 |
|
| 1747 |
assert(strchr("=b|.*(", op) != NULL); |
| 1748 |
|
| 1749 |
ret->op = op; |
| 1750 |
ret->flags = flags; |
| 1751 |
ret->id = 0; /* will be assigned later */ |
| 1752 |
ret->subno = 0; |
| 1753 |
ret->min = ret->max = 1; |
| 1754 |
ret->left = NULL; |
| 1755 |
ret->right = NULL; |
| 1756 |
ret->begin = begin; |
| 1757 |
ret->end = end; |
| 1758 |
ZAPCNFA(ret->cnfa); |
| 1759 |
|
| 1760 |
return ret; |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* |
| 1764 |
- freesubre - free a subRE subtree |
| 1765 |
^ static void freesubre(struct vars *, struct subre *); |
| 1766 |
*/ |
| 1767 |
static void |
| 1768 |
freesubre( |
| 1769 |
struct vars *v, /* might be NULL */ |
| 1770 |
struct subre *sr) |
| 1771 |
{ |
| 1772 |
if (sr == NULL) { |
| 1773 |
return; |
| 1774 |
} |
| 1775 |
|
| 1776 |
if (sr->left != NULL) { |
| 1777 |
freesubre(v, sr->left); |
| 1778 |
} |
| 1779 |
if (sr->right != NULL) { |
| 1780 |
freesubre(v, sr->right); |
| 1781 |
} |
| 1782 |
|
| 1783 |
freesrnode(v, sr); |
| 1784 |
} |
| 1785 |
|
| 1786 |
/* |
| 1787 |
- freesrnode - free one node in a subRE subtree |
| 1788 |
^ static void freesrnode(struct vars *, struct subre *); |
| 1789 |
*/ |
| 1790 |
static void |
| 1791 |
freesrnode( |
| 1792 |
struct vars *v, /* might be NULL */ |
| 1793 |
struct subre *sr) |
| 1794 |
{ |
| 1795 |
if (sr == NULL) { |
| 1796 |
return; |
| 1797 |
} |
| 1798 |
|
| 1799 |
if (!NULLCNFA(sr->cnfa)) { |
| 1800 |
freecnfa(&sr->cnfa); |
| 1801 |
} |
| 1802 |
sr->flags = 0; |
| 1803 |
|
| 1804 |
if (v != NULL && v->treechain != NULL) { |
| 1805 |
/* we're still parsing, maybe we can reuse the subre */ |
| 1806 |
sr->left = v->treefree; |
| 1807 |
v->treefree = sr; |
| 1808 |
} else { |
| 1809 |
FREE(sr); |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
/* |
| 1814 |
- numst - number tree nodes (assigning "id" indexes) |
| 1815 |
^ static int numst(struct subre *, int); |
| 1816 |
*/ |
| 1817 |
static int /* next number */ |
| 1818 |
numst( |
| 1819 |
struct subre *t, |
| 1820 |
int start) /* starting point for subtree numbers */ |
| 1821 |
{ |
| 1822 |
int i; |
| 1823 |
|
| 1824 |
assert(t != NULL); |
| 1825 |
|
| 1826 |
i = start; |
| 1827 |
t->id = (short) i++; |
| 1828 |
if (t->left != NULL) { |
| 1829 |
i = numst(t->left, i); |
| 1830 |
} |
| 1831 |
if (t->right != NULL) { |
| 1832 |
i = numst(t->right, i); |
| 1833 |
} |
| 1834 |
return i; |
| 1835 |
} |
| 1836 |
|
| 1837 |
/* |
| 1838 |
- markst - mark tree nodes as INUSE |
| 1839 |
* Note: this is a great deal more subtle than it looks. During initial |
| 1840 |
* parsing of a regex, all subres are linked into the treechain list; |
| 1841 |
* discarded ones are also linked into the treefree list for possible reuse. |
| 1842 |
* After we are done creating all subres required for a regex, we run markst() |
| 1843 |
* then cleanst(), which results in discarding all subres not reachable from |
| 1844 |
* v->tree. We then clear v->treechain, indicating that subres must be found |
| 1845 |
* by descending from v->tree. This changes the behavior of freesubre(): it |
| 1846 |
* will henceforth FREE() unwanted subres rather than sticking them into the |
| 1847 |
* treefree list. (Doing that any earlier would result in dangling links in |
| 1848 |
* the treechain list.) This all means that freev() will clean up correctly |
| 1849 |
* if invoked before or after markst()+cleanst(); but it would not work if |
| 1850 |
* called partway through this state conversion, so we mustn't error out |
| 1851 |
* in or between these two functions. |
| 1852 |
^ static void markst(struct subre *); |
| 1853 |
*/ |
| 1854 |
static void |
| 1855 |
markst( |
| 1856 |
struct subre *t) |
| 1857 |
{ |
| 1858 |
assert(t != NULL); |
| 1859 |
|
| 1860 |
t->flags |= INUSE; |
| 1861 |
if (t->left != NULL) { |
| 1862 |
markst(t->left); |
| 1863 |
} |
| 1864 |
if (t->right != NULL) { |
| 1865 |
markst(t->right); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
/* |
| 1870 |
- cleanst - free any tree nodes not marked INUSE |
| 1871 |
^ static void cleanst(struct vars *); |
| 1872 |
*/ |
| 1873 |
static void |
| 1874 |
cleanst( |
| 1875 |
struct vars *v) |
| 1876 |
{ |
| 1877 |
struct subre *t; |
| 1878 |
struct subre *next; |
| 1879 |
|
| 1880 |
for (t = v->treechain; t != NULL; t = next) { |
| 1881 |
next = t->chain; |
| 1882 |
if (!(t->flags&INUSE)) { |
| 1883 |
FREE(t); |
| 1884 |
} |
| 1885 |
} |
| 1886 |
v->treechain = NULL; |
| 1887 |
v->treefree = NULL; /* just on general principles */ |
| 1888 |
} |
| 1889 |
|
| 1890 |
/* |
| 1891 |
- nfatree - turn a subRE subtree into a tree of compacted NFAs |
| 1892 |
^ static long nfatree(struct vars *, struct subre *, FILE *); |
| 1893 |
*/ |
| 1894 |
static long /* optimize results from top node */ |
| 1895 |
nfatree( |
| 1896 |
struct vars *v, |
| 1897 |
struct subre *t, |
| 1898 |
FILE *f) /* for debug output */ |
| 1899 |
{ |
| 1900 |
assert(t != NULL && t->begin != NULL); |
| 1901 |
|
| 1902 |
if (t->left != NULL) { |
| 1903 |
(DISCARD) nfatree(v, t->left, f); |
| 1904 |
} |
| 1905 |
if (t->right != NULL) { |
| 1906 |
(DISCARD) nfatree(v, t->right, f); |
| 1907 |
} |
| 1908 |
|
| 1909 |
return nfanode(v, t, f); |
| 1910 |
} |
| 1911 |
|
| 1912 |
/* |
| 1913 |
- nfanode - do one NFA for nfatree |
| 1914 |
^ static long nfanode(struct vars *, struct subre *, FILE *); |
| 1915 |
*/ |
| 1916 |
static long /* optimize results */ |
| 1917 |
nfanode( |
| 1918 |
struct vars *v, |
| 1919 |
struct subre *t, |
| 1920 |
FILE *f) /* for debug output */ |
| 1921 |
{ |
| 1922 |
struct nfa *nfa; |
| 1923 |
long ret = 0; |
| 1924 |
char idbuf[50]; |
| 1925 |
|
| 1926 |
assert(t->begin != NULL); |
| 1927 |
|
| 1928 |
if (f != NULL) { |
| 1929 |
fprintf(f, "\n\n\n========= TREE NODE %s ==========\n", |
| 1930 |
stid(t, idbuf, sizeof(idbuf))); |
| 1931 |
} |
| 1932 |
nfa = newnfa(v, v->cm, v->nfa); |
| 1933 |
NOERRZ(); |
| 1934 |
dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final); |
| 1935 |
if (!ISERR()) { |
| 1936 |
specialcolors(nfa); |
| 1937 |
ret = optimize(nfa, f); |
| 1938 |
} |
| 1939 |
if (!ISERR()) { |
| 1940 |
compact(nfa, &t->cnfa); |
| 1941 |
} |
| 1942 |
|
| 1943 |
freenfa(nfa); |
| 1944 |
return ret; |
| 1945 |
} |
| 1946 |
|
| 1947 |
/* |
| 1948 |
- newlacon - allocate a lookahead-constraint subRE |
| 1949 |
^ static int newlacon(struct vars *, struct state *, struct state *, int); |
| 1950 |
*/ |
| 1951 |
static int /* lacon number */ |
| 1952 |
newlacon( |
| 1953 |
struct vars *v, |
| 1954 |
struct state *begin, |
| 1955 |
struct state *end, |
| 1956 |
int pos) |
| 1957 |
{ |
| 1958 |
int n; |
| 1959 |
struct subre *newlacons; |
| 1960 |
struct subre *sub; |
| 1961 |
|
| 1962 |
if (v->nlacons == 0) { |
| 1963 |
n = 1; /* skip 0th */ |
| 1964 |
newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre)); |
| 1965 |
} else { |
| 1966 |
n = v->nlacons; |
| 1967 |
newlacons = (struct subre *) REALLOC(v->lacons, |
| 1968 |
(n + 1) * sizeof(struct subre)); |
| 1969 |
} |
| 1970 |
|
| 1971 |
if (newlacons == NULL) { |
| 1972 |
ERR(REG_ESPACE); |
| 1973 |
return 0; |
| 1974 |
} |
| 1975 |
|
| 1976 |
v->lacons = newlacons; |
| 1977 |
v->nlacons = n + 1; |
| 1978 |
sub = &v->lacons[n]; |
| 1979 |
sub->begin = begin; |
| 1980 |
sub->end = end; |
| 1981 |
sub->subno = pos; |
| 1982 |
ZAPCNFA(sub->cnfa); |
| 1983 |
return n; |
| 1984 |
} |
| 1985 |
|
| 1986 |
/* |
| 1987 |
- freelacons - free lookahead-constraint subRE vector |
| 1988 |
^ static void freelacons(struct subre *, int); |
| 1989 |
*/ |
| 1990 |
static void |
| 1991 |
freelacons( |
| 1992 |
struct subre *subs, |
| 1993 |
int n) |
| 1994 |
{ |
| 1995 |
struct subre *sub; |
| 1996 |
int i; |
| 1997 |
|
| 1998 |
assert(n > 0); |
| 1999 |
for (sub=subs+1, i=n-1; i>0; sub++, i--) { /* no 0th */ |
| 2000 |
if (!NULLCNFA(sub->cnfa)) { |
| 2001 |
freecnfa(&sub->cnfa); |
| 2002 |
} |
| 2003 |
} |
| 2004 |
FREE(subs); |
| 2005 |
} |
| 2006 |
|
| 2007 |
/* |
| 2008 |
- rfree - free a whole RE (insides of regfree) |
| 2009 |
^ static void rfree(regex_t *); |
| 2010 |
*/ |
| 2011 |
static void |
| 2012 |
rfree( |
| 2013 |
regex_t *re) |
| 2014 |
{ |
| 2015 |
struct guts *g; |
| 2016 |
|
| 2017 |
if (re == NULL || re->re_magic != REMAGIC) { |
| 2018 |
return; |
| 2019 |
} |
| 2020 |
|
| 2021 |
re->re_magic = 0; /* invalidate RE */ |
| 2022 |
g = (struct guts *) re->re_guts; |
| 2023 |
re->re_guts = NULL; |
| 2024 |
re->re_fns = NULL; |
| 2025 |
if (g != NULL) { |
| 2026 |
g->magic = 0; |
| 2027 |
freecm(&g->cmap); |
| 2028 |
if (g->tree != NULL) { |
| 2029 |
freesubre(NULL, g->tree); |
| 2030 |
} |
| 2031 |
if (g->lacons != NULL) { |
| 2032 |
freelacons(g->lacons, g->nlacons); |
| 2033 |
} |
| 2034 |
if (!NULLCNFA(g->search)) { |
| 2035 |
freecnfa(&g->search); |
| 2036 |
} |
| 2037 |
FREE(g); |
| 2038 |
} |
| 2039 |
} |
| 2040 |
|
| 2041 |
/* |
| 2042 |
- dump - dump an RE in human-readable form |
| 2043 |
^ static void dump(regex_t *, FILE *); |
| 2044 |
*/ |
| 2045 |
static void |
| 2046 |
dump( |
| 2047 |
regex_t *re, |
| 2048 |
FILE *f) |
| 2049 |
{ |
| 2050 |
#ifdef REG_DEBUG |
| 2051 |
struct guts *g; |
| 2052 |
int i; |
| 2053 |
|
| 2054 |
if (re->re_magic != REMAGIC) { |
| 2055 |
fprintf(f, "bad magic number (0x%x not 0x%x)\n", |
| 2056 |
re->re_magic, REMAGIC); |
| 2057 |
} |
| 2058 |
if (re->re_guts == NULL) { |
| 2059 |
fprintf(f, "NULL guts!!!\n"); |
| 2060 |
return; |
| 2061 |
} |
| 2062 |
g = (struct guts *) re->re_guts; |
| 2063 |
if (g->magic != GUTSMAGIC) { |
| 2064 |
fprintf(f, "bad guts magic number (0x%x not 0x%x)\n", |
| 2065 |
g->magic, GUTSMAGIC); |
| 2066 |
} |
| 2067 |
|
| 2068 |
fprintf(f, "\n\n\n========= DUMP ==========\n"); |
| 2069 |
fprintf(f, "nsub %d, info 0%lo, csize %d, ntree %d\n", |
| 2070 |
(int) re->re_nsub, re->re_info, re->re_csize, g->ntree); |
| 2071 |
|
| 2072 |
dumpcolors(&g->cmap, f); |
| 2073 |
if (!NULLCNFA(g->search)) { |
| 2074 |
fprintf(f, "\nsearch:\n"); |
| 2075 |
dumpcnfa(&g->search, f); |
| 2076 |
} |
| 2077 |
for (i = 1; i < g->nlacons; i++) { |
| 2078 |
fprintf(f, "\nla%d (%s):\n", i, |
| 2079 |
(g->lacons[i].subno) ? "positive" : "negative"); |
| 2080 |
dumpcnfa(&g->lacons[i].cnfa, f); |
| 2081 |
} |
| 2082 |
fprintf(f, "\n"); |
| 2083 |
dumpst(g->tree, f, 0); |
| 2084 |
#else |
| 2085 |
(void)re; |
| 2086 |
(void)f; |
| 2087 |
#endif |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
- dumpst - dump a subRE tree |
| 2092 |
^ static void dumpst(struct subre *, FILE *, int); |
| 2093 |
*/ |
| 2094 |
static void |
| 2095 |
dumpst( |
| 2096 |
struct subre *t, |
| 2097 |
FILE *f, |
| 2098 |
int nfapresent) /* is the original NFA still around? */ |
| 2099 |
{ |
| 2100 |
if (t == NULL) { |
| 2101 |
fprintf(f, "null tree\n"); |
| 2102 |
} else { |
| 2103 |
stdump(t, f, nfapresent); |
| 2104 |
} |
| 2105 |
fflush(f); |
| 2106 |
} |
| 2107 |
|
| 2108 |
/* |
| 2109 |
- stdump - recursive guts of dumpst |
| 2110 |
^ static void stdump(struct subre *, FILE *, int); |
| 2111 |
*/ |
| 2112 |
static void |
| 2113 |
stdump( |
| 2114 |
struct subre *t, |
| 2115 |
FILE *f, |
| 2116 |
int nfapresent) /* is the original NFA still around? */ |
| 2117 |
{ |
| 2118 |
char idbuf[50]; |
| 2119 |
|
| 2120 |
fprintf(f, "%s. `%c'", stid(t, idbuf, sizeof(idbuf)), t->op); |
| 2121 |
if (t->flags&LONGER) { |
| 2122 |
fprintf(f, " longest"); |
| 2123 |
} |
| 2124 |
if (t->flags&SHORTER) { |
| 2125 |
fprintf(f, " shortest"); |
| 2126 |
} |
| 2127 |
if (t->flags&MIXED) { |
| 2128 |
fprintf(f, " hasmixed"); |
| 2129 |
} |
| 2130 |
if (t->flags&CAP) { |
| 2131 |
fprintf(f, " hascapture"); |
| 2132 |
} |
| 2133 |
if (t->flags&BACKR) { |
| 2134 |
fprintf(f, " hasbackref"); |
| 2135 |
} |
| 2136 |
if (!(t->flags&INUSE)) { |
| 2137 |
fprintf(f, " UNUSED"); |
| 2138 |
} |
| 2139 |
if (t->subno != 0) { |
| 2140 |
fprintf(f, " (#%d)", t->subno); |
| 2141 |
} |
| 2142 |
if (t->min != 1 || t->max != 1) { |
| 2143 |
fprintf(f, " {%d,", t->min); |
| 2144 |
if (t->max != DUPINF) { |
| 2145 |
fprintf(f, "%d", t->max); |
| 2146 |
} |
| 2147 |
fprintf(f, "}"); |
| 2148 |
} |
| 2149 |
if (nfapresent) { |
| 2150 |
fprintf(f, " %ld-%ld", (long)t->begin->no, (long)t->end->no); |
| 2151 |
} |
| 2152 |
if (t->left != NULL) { |
| 2153 |
fprintf(f, " L:%s", stid(t->left, idbuf, sizeof(idbuf))); |
| 2154 |
} |
| 2155 |
if (t->right != NULL) { |
| 2156 |
fprintf(f, " R:%s", stid(t->right, idbuf, sizeof(idbuf))); |
| 2157 |
} |
| 2158 |
if (!NULLCNFA(t->cnfa)) { |
| 2159 |
fprintf(f, "\n"); |
| 2160 |
dumpcnfa(&t->cnfa, f); |
| 2161 |
} |
| 2162 |
fprintf(f, "\n"); |
| 2163 |
if (t->left != NULL) { |
| 2164 |
stdump(t->left, f, nfapresent); |
| 2165 |
} |
| 2166 |
if (t->right != NULL) { |
| 2167 |
stdump(t->right, f, nfapresent); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
/* |
| 2172 |
- stid - identify a subtree node for dumping |
| 2173 |
^ static const char *stid(struct subre *, char *, size_t); |
| 2174 |
*/ |
| 2175 |
static const char * /* points to buf or constant string */ |
| 2176 |
stid( |
| 2177 |
struct subre *t, |
| 2178 |
char *buf, |
| 2179 |
size_t bufsize) |
| 2180 |
{ |
| 2181 |
/* |
| 2182 |
* Big enough for hex int or decimal t->id? |
| 2183 |
*/ |
| 2184 |
|
| 2185 |
if (bufsize < sizeof(void*)*2 + 3 || bufsize < sizeof(t->id)*3 + 1) { |
| 2186 |
return "unable"; |
| 2187 |
} |
| 2188 |
if (t->id != 0) { |
| 2189 |
snprintf(buf, bufsize, "%d", t->id); |
| 2190 |
} else { |
| 2191 |
snprintf(buf, bufsize, "%p", t); |
| 2192 |
} |
| 2193 |
return buf; |
| 2194 |
} |
| 2195 |
|
| 2196 |
#include "regc_lex.c" |
| 2197 |
#include "regc_color.c" |
| 2198 |
#include "regc_nfa.c" |
| 2199 |
#include "regc_cvec.c" |
| 2200 |
#include "regc_locale.c" |
| 2201 |
|
| 2202 |
/* |
| 2203 |
* Local Variables: |
| 2204 |
* mode: c |
| 2205 |
* c-basic-offset: 4 |
| 2206 |
* fill-column: 78 |
| 2207 |
* End: |
| 2208 |
*/ |