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