Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regcomp.c |
2 | */ | |
3 | ||
4 | /* | |
5 | * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee | |
6 | */ | |
7 | ||
a687059c LW |
8 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
9 | * confused with the original package (see point 3 below). Thanks, Henry! | |
10 | */ | |
11 | ||
12 | /* Additional note: this code is very heavily munged from Henry's version | |
13 | * in places. In some spots I've traded clarity for efficiency, so don't | |
14 | * blame Henry for some of the lack of readability. | |
15 | */ | |
16 | ||
e50aee73 AD |
17 | /* The names of the functions have been changed from regcomp and |
18 | * regexec to pregcomp and pregexec in order to avoid conflicts | |
19 | * with the POSIX routines of the same names. | |
20 | */ | |
21 | ||
f0fcb552 | 22 | /*SUPPRESS 112*/ |
a687059c | 23 | /* |
e50aee73 | 24 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
25 | * |
26 | * Copyright (c) 1986 by University of Toronto. | |
27 | * Written by Henry Spencer. Not derived from licensed software. | |
28 | * | |
29 | * Permission is granted to anyone to use this software for any | |
30 | * purpose on any computer system, and to redistribute it freely, | |
31 | * subject to the following restrictions: | |
32 | * | |
33 | * 1. The author is not responsible for the consequences of use of | |
34 | * this software, no matter how awful, even if they arise | |
35 | * from defects in it. | |
36 | * | |
37 | * 2. The origin of this software must not be misrepresented, either | |
38 | * by explicit claim or by omission. | |
39 | * | |
40 | * 3. Altered versions must be plainly marked as such, and must not | |
41 | * be misrepresented as being the original software. | |
42 | * | |
43 | * | |
44 | **** Alterations to Henry's code are... | |
45 | **** | |
9607fc9c | 46 | **** Copyright (c) 1991-1997, Larry Wall |
a687059c | 47 | **** |
9ef589d8 LW |
48 | **** You may distribute under the terms of either the GNU General Public |
49 | **** License or the Artistic License, as specified in the README file. | |
50 | ||
a687059c LW |
51 | * |
52 | * Beware that some of this code is subtly aware of the way operator | |
53 | * precedence is structured in regular expressions. Serious changes in | |
54 | * regular-expression syntax might require a total rethink. | |
55 | */ | |
56 | #include "EXTERN.h" | |
57 | #include "perl.h" | |
58 | #include "INTERN.h" | |
c277df42 IZ |
59 | |
60 | #define REG_COMP_C | |
a687059c LW |
61 | #include "regcomp.h" |
62 | ||
d4cce5f1 | 63 | #ifdef op |
11343788 | 64 | #undef op |
d4cce5f1 | 65 | #endif /* op */ |
11343788 | 66 | |
fe14fcc3 LW |
67 | #ifdef MSDOS |
68 | # if defined(BUGGY_MSC6) | |
69 | /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */ | |
70 | # pragma optimize("a",off) | |
71 | /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/ | |
72 | # pragma optimize("w",on ) | |
73 | # endif /* BUGGY_MSC6 */ | |
74 | #endif /* MSDOS */ | |
75 | ||
a687059c LW |
76 | #ifndef STATIC |
77 | #define STATIC static | |
78 | #endif | |
79 | ||
80 | #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?') | |
81 | #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \ | |
82 | ((*s) == '{' && regcurly(s))) | |
2b69d0c2 LW |
83 | #ifdef atarist |
84 | #define PERL_META "^$.[()|?+*\\" | |
85 | #else | |
a687059c | 86 | #define META "^$.[()|?+*\\" |
2b69d0c2 | 87 | #endif |
a687059c | 88 | |
35c8bce7 LW |
89 | #ifdef SPSTART |
90 | #undef SPSTART /* dratted cpp namespace... */ | |
91 | #endif | |
a687059c LW |
92 | /* |
93 | * Flags to be passed up and down. | |
94 | */ | |
a687059c | 95 | #define WORST 0 /* Worst case. */ |
a0d0e21e LW |
96 | #define HASWIDTH 0x1 /* Known never to match null string. */ |
97 | #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */ | |
98 | #define SPSTART 0x4 /* Starts with * or +. */ | |
99 | #define TRYAGAIN 0x8 /* Weeded out a declaration. */ | |
a687059c LW |
100 | |
101 | /* | |
e50aee73 | 102 | * Forward declarations for pregcomp()'s friends. |
a687059c | 103 | */ |
a0d0e21e | 104 | |
76e3520e | 105 | #ifndef PERL_OBJECT |
c277df42 IZ |
106 | static regnode *reg _((I32, I32 *)); |
107 | static regnode *reganode _((U8, U32)); | |
108 | static regnode *regatom _((I32 *)); | |
109 | static regnode *regbranch _((I32 *, I32)); | |
110 | static void regc _((U8, char *)); | |
111 | static regnode *regclass _((void)); | |
a0d0e21e | 112 | STATIC I32 regcurly _((char *)); |
c277df42 IZ |
113 | static regnode *reg_node _((U8)); |
114 | static regnode *regpiece _((I32 *)); | |
115 | static void reginsert _((U8, regnode *)); | |
116 | static void regoptail _((regnode *, regnode *)); | |
c277df42 | 117 | static void regtail _((regnode *, regnode *)); |
873ef191 | 118 | static char* regwhite _((char *, char *)); |
a0d0e21e | 119 | static char* nextchar _((void)); |
3bd495df | 120 | static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn)); |
76e3520e | 121 | #endif |
a687059c | 122 | |
c277df42 IZ |
123 | /* Length of a variant. */ |
124 | ||
76e3520e | 125 | #ifndef PERL_OBJECT |
c277df42 IZ |
126 | typedef struct { |
127 | I32 len_min; | |
128 | I32 len_delta; | |
129 | I32 pos_min; | |
130 | I32 pos_delta; | |
131 | SV *last_found; | |
132 | I32 last_end; /* min value, <0 unless valid. */ | |
133 | I32 last_start_min; | |
134 | I32 last_start_max; | |
135 | SV **longest; /* Either &l_fixed, or &l_float. */ | |
136 | SV *longest_fixed; | |
137 | I32 offset_fixed; | |
138 | SV *longest_float; | |
139 | I32 offset_float_min; | |
140 | I32 offset_float_max; | |
141 | I32 flags; | |
142 | } scan_data_t; | |
76e3520e | 143 | #endif |
c277df42 IZ |
144 | |
145 | static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
146 | ||
147 | #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL) | |
148 | #define SF_BEFORE_SEOL 0x1 | |
149 | #define SF_BEFORE_MEOL 0x2 | |
150 | #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL) | |
151 | #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL) | |
152 | ||
09b7f37c CB |
153 | #ifdef NO_UNARY_PLUS |
154 | # define SF_FIX_SHIFT_EOL (0+2) | |
155 | # define SF_FL_SHIFT_EOL (0+4) | |
156 | #else | |
157 | # define SF_FIX_SHIFT_EOL (+2) | |
158 | # define SF_FL_SHIFT_EOL (+4) | |
159 | #endif | |
c277df42 IZ |
160 | |
161 | #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL) | |
162 | #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL) | |
163 | ||
164 | #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL) | |
165 | #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */ | |
166 | #define SF_IS_INF 0x40 | |
167 | #define SF_HAS_PAR 0x80 | |
168 | #define SF_IN_PAR 0x100 | |
169 | #define SF_HAS_EVAL 0x200 | |
4bfe0158 | 170 | #define SCF_DO_SUBSTR 0x400 |
c277df42 | 171 | |
76e3520e | 172 | STATIC void |
c277df42 IZ |
173 | scan_commit(scan_data_t *data) |
174 | { | |
175 | STRLEN l = SvCUR(data->last_found); | |
176 | STRLEN old_l = SvCUR(*data->longest); | |
177 | ||
178 | if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) { | |
179 | sv_setsv(*data->longest, data->last_found); | |
180 | if (*data->longest == data->longest_fixed) { | |
181 | data->offset_fixed = l ? data->last_start_min : data->pos_min; | |
182 | if (data->flags & SF_BEFORE_EOL) | |
183 | data->flags | |
184 | |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL); | |
185 | else | |
186 | data->flags &= ~SF_FIX_BEFORE_EOL; | |
187 | } else { | |
188 | data->offset_float_min = l ? data->last_start_min : data->pos_min; | |
189 | data->offset_float_max = (l | |
190 | ? data->last_start_max | |
191 | : data->pos_min + data->pos_delta); | |
192 | if (data->flags & SF_BEFORE_EOL) | |
193 | data->flags | |
194 | |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL); | |
195 | else | |
196 | data->flags &= ~SF_FL_BEFORE_EOL; | |
197 | } | |
198 | } | |
199 | SvCUR_set(data->last_found, 0); | |
200 | data->last_end = -1; | |
201 | data->flags &= ~SF_BEFORE_EOL; | |
202 | } | |
203 | ||
c277df42 IZ |
204 | /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set |
205 | to the position after last scanned or to NULL. */ | |
206 | ||
76e3520e | 207 | STATIC I32 |
c277df42 IZ |
208 | study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags) |
209 | /* scanp: Start here (read-write). */ | |
210 | /* deltap: Write maxlen-minlen here. */ | |
211 | /* last: Stop before this one. */ | |
212 | { | |
213 | I32 min = 0, pars = 0, code; | |
214 | regnode *scan = *scanp, *next; | |
215 | I32 delta = 0; | |
216 | int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF); | |
217 | I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0; | |
218 | scan_data_t data_fake; | |
219 | ||
220 | while (scan && OP(scan) != END && scan < last) { | |
221 | /* Peephole optimizer: */ | |
222 | ||
223 | if (regkind[(U8)OP(scan)] == EXACT) { | |
224 | regnode *n = regnext(scan); | |
225 | U32 stringok = 1; | |
226 | #ifdef DEBUGGING | |
227 | regnode *stop = scan; | |
228 | #endif | |
229 | ||
230 | next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2; | |
231 | /* Skip NOTHING, merge EXACT*. */ | |
232 | while (n && | |
233 | ( regkind[(U8)OP(n)] == NOTHING || | |
234 | (stringok && (OP(n) == OP(scan)))) | |
235 | && NEXT_OFF(n) | |
236 | && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) { | |
237 | if (OP(n) == TAIL || n > next) | |
238 | stringok = 0; | |
239 | if (regkind[(U8)OP(n)] == NOTHING) { | |
240 | NEXT_OFF(scan) += NEXT_OFF(n); | |
241 | next = n + NODE_STEP_REGNODE; | |
242 | #ifdef DEBUGGING | |
243 | if (stringok) | |
244 | stop = n; | |
245 | #endif | |
246 | n = regnext(n); | |
247 | } else { | |
248 | int oldl = *OPERAND(scan); | |
249 | regnode *nnext = regnext(n); | |
250 | ||
251 | if (oldl + *OPERAND(n) > U8_MAX) | |
252 | break; | |
253 | NEXT_OFF(scan) += NEXT_OFF(n); | |
254 | *OPERAND(scan) += *OPERAND(n); | |
255 | next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2; | |
256 | /* Now we can overwrite *n : */ | |
257 | Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1, | |
258 | *OPERAND(n) + 1, char); | |
259 | #ifdef DEBUGGING | |
260 | if (stringok) | |
261 | stop = next - 1; | |
262 | #endif | |
263 | n = nnext; | |
264 | } | |
265 | } | |
266 | #ifdef DEBUGGING | |
267 | /* Allow dumping */ | |
268 | n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2; | |
269 | while (n <= stop) { | |
270 | if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) { | |
271 | OP(n) = OPTIMIZED; | |
272 | NEXT_OFF(n) = 0; | |
273 | } | |
274 | n++; | |
275 | } | |
276 | #endif | |
277 | ||
278 | } | |
279 | if (OP(scan) != CURLYX) { | |
280 | int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX); | |
281 | int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan)); | |
282 | int noff; | |
283 | regnode *n = scan; | |
284 | ||
285 | /* Skip NOTHING and LONGJMP. */ | |
286 | while ((n = regnext(n)) | |
287 | && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n))) | |
288 | || ((OP(n) == LONGJMP) && (noff = ARG(n)))) | |
289 | && off + noff < max) | |
290 | off += noff; | |
291 | if (reg_off_by_arg[OP(scan)]) | |
292 | ARG(scan) = off; | |
293 | else | |
294 | NEXT_OFF(scan) = off; | |
295 | } | |
296 | if (OP(scan) == BRANCH || OP(scan) == BRANCHJ | |
297 | || OP(scan) == IFTHEN || OP(scan) == SUSPEND) { | |
298 | next = regnext(scan); | |
299 | code = OP(scan); | |
300 | ||
301 | if (OP(next) == code || code == IFTHEN || code == SUSPEND) { | |
302 | I32 max1 = 0, min1 = I32_MAX, num = 0; | |
303 | ||
304 | if (flags & SCF_DO_SUBSTR) | |
305 | scan_commit(data); | |
306 | while (OP(scan) == code) { | |
307 | I32 deltanext, minnext; | |
308 | ||
309 | num++; | |
310 | data_fake.flags = 0; | |
311 | next = regnext(scan); | |
312 | scan = NEXTOPER(scan); | |
313 | if (code != BRANCH) | |
314 | scan = NEXTOPER(scan); | |
315 | /* We suppose the run is continuous, last=next...*/ | |
316 | minnext = study_chunk(&scan, &deltanext, next, | |
317 | &data_fake, 0); | |
318 | if (min1 > minnext) | |
319 | min1 = minnext; | |
320 | if (max1 < minnext + deltanext) | |
321 | max1 = minnext + deltanext; | |
322 | if (deltanext == I32_MAX) | |
323 | is_inf = 1; | |
324 | scan = next; | |
325 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
326 | pars++; | |
405ff068 | 327 | if (data && (data_fake.flags & SF_HAS_EVAL)) |
c277df42 IZ |
328 | data->flags |= SF_HAS_EVAL; |
329 | if (code == SUSPEND) | |
330 | break; | |
331 | } | |
332 | if (code == IFTHEN && num < 2) /* Empty ELSE branch */ | |
333 | min1 = 0; | |
334 | if (flags & SCF_DO_SUBSTR) { | |
335 | data->pos_min += min1; | |
336 | data->pos_delta += max1 - min1; | |
337 | if (max1 != min1 || is_inf) | |
338 | data->longest = &(data->longest_float); | |
339 | } | |
340 | min += min1; | |
341 | delta += max1 - min1; | |
342 | } else if (code == BRANCHJ) /* single branch is optimized. */ | |
343 | scan = NEXTOPER(NEXTOPER(scan)); | |
344 | else /* single branch is optimized. */ | |
345 | scan = NEXTOPER(scan); | |
346 | continue; | |
347 | } else if (OP(scan) == EXACT) { | |
348 | min += *OPERAND(scan); | |
349 | if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */ | |
350 | I32 l = *OPERAND(scan); | |
351 | ||
352 | /* The code below prefers earlier match for fixed | |
353 | offset, later match for variable offset. */ | |
354 | if (data->last_end == -1) { /* Update the start info. */ | |
355 | data->last_start_min = data->pos_min; | |
356 | data->last_start_max = is_inf | |
357 | ? I32_MAX : data->pos_min + data->pos_delta; | |
358 | } | |
161b471a | 359 | sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l); |
c277df42 IZ |
360 | data->last_end = data->pos_min + l; |
361 | data->pos_min += l; /* As in the first entry. */ | |
362 | data->flags &= ~SF_BEFORE_EOL; | |
363 | } | |
364 | } else if (regkind[(U8)OP(scan)] == EXACT) { | |
365 | if (flags & SCF_DO_SUBSTR) | |
366 | scan_commit(data); | |
367 | min += *OPERAND(scan); | |
368 | if (data && (flags & SCF_DO_SUBSTR)) | |
369 | data->pos_min += *OPERAND(scan); | |
370 | } else if (strchr(varies,OP(scan))) { | |
371 | I32 mincount, maxcount, minnext, deltanext, pos_before, fl; | |
372 | regnode *oscan = scan; | |
373 | ||
374 | switch (regkind[(U8)OP(scan)]) { | |
375 | case WHILEM: | |
376 | scan = NEXTOPER(scan); | |
377 | goto finish; | |
378 | case PLUS: | |
379 | if (flags & SCF_DO_SUBSTR) { | |
380 | next = NEXTOPER(scan); | |
381 | if (OP(next) == EXACT) { | |
382 | mincount = 1; | |
383 | maxcount = REG_INFTY; | |
384 | next = regnext(scan); | |
385 | scan = NEXTOPER(scan); | |
386 | goto do_curly; | |
387 | } | |
388 | } | |
389 | if (flags & SCF_DO_SUBSTR) | |
390 | data->pos_min++; | |
391 | min++; | |
392 | /* Fall through. */ | |
393 | case STAR: | |
394 | is_inf = 1; | |
395 | scan = regnext(scan); | |
396 | if (flags & SCF_DO_SUBSTR) { | |
397 | scan_commit(data); | |
398 | data->longest = &(data->longest_float); | |
399 | } | |
400 | goto optimize_curly_tail; | |
401 | case CURLY: | |
402 | mincount = ARG1(scan); | |
403 | maxcount = ARG2(scan); | |
404 | next = regnext(scan); | |
405 | scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; | |
406 | do_curly: | |
407 | if (flags & SCF_DO_SUBSTR) { | |
408 | if (mincount == 0) scan_commit(data); | |
409 | pos_before = data->pos_min; | |
410 | } | |
411 | if (data) { | |
412 | fl = data->flags; | |
413 | data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL); | |
414 | if (is_inf) | |
415 | data->flags |= SF_IS_INF; | |
416 | } | |
417 | /* This will finish on WHILEM, setting scan, or on NULL: */ | |
418 | minnext = study_chunk(&scan, &deltanext, last, data, | |
419 | mincount == 0 | |
420 | ? (flags & ~SCF_DO_SUBSTR) : flags); | |
421 | if (!scan) /* It was not CURLYX, but CURLY. */ | |
422 | scan = next; | |
423 | if (dowarn && (minnext + deltanext == 0) | |
424 | && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))) | |
425 | warn("Strange *+?{} on zero-length expression"); | |
426 | min += minnext * mincount; | |
427 | is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0 | |
428 | || deltanext == I32_MAX); | |
429 | delta += (minnext + deltanext) * maxcount - minnext * mincount; | |
430 | ||
431 | /* Try powerful optimization CURLYX => CURLYN. */ | |
432 | #ifdef REGALIGN_STRUCT | |
433 | if ( OP(oscan) == CURLYX && data | |
434 | && data->flags & SF_IN_PAR | |
435 | && !(data->flags & SF_HAS_EVAL) | |
436 | && !deltanext && minnext == 1 ) { | |
437 | /* Try to optimize to CURLYN. */ | |
438 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; | |
439 | regnode *nxt1 = nxt, *nxt2; | |
440 | ||
441 | /* Skip open. */ | |
442 | nxt = regnext(nxt); | |
443 | if (!strchr(simple,OP(nxt)) | |
444 | && !(regkind[(U8)OP(nxt)] == EXACT | |
445 | && *OPERAND(nxt) == 1)) | |
446 | goto nogo; | |
447 | nxt2 = nxt; | |
448 | nxt = regnext(nxt); | |
449 | if (OP(nxt) != CLOSE) | |
450 | goto nogo; | |
451 | /* Now we know that nxt2 is the only contents: */ | |
452 | oscan->flags = ARG(nxt); | |
453 | OP(oscan) = CURLYN; | |
454 | OP(nxt1) = NOTHING; /* was OPEN. */ | |
455 | #ifdef DEBUGGING | |
456 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ | |
457 | NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */ | |
458 | NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */ | |
459 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ | |
460 | OP(nxt + 1) = OPTIMIZED; /* was count. */ | |
461 | NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */ | |
462 | #endif | |
463 | } | |
464 | #endif | |
465 | nogo: | |
466 | ||
467 | /* Try optimization CURLYX => CURLYM. */ | |
468 | if ( OP(oscan) == CURLYX && data | |
469 | #ifdef REGALIGN_STRUCT | |
470 | && !(data->flags & SF_HAS_PAR) | |
471 | #else | |
472 | && !(data->flags & (SF_HAS_PAR|SF_IN_PAR)) | |
473 | #endif | |
474 | && !(data->flags & SF_HAS_EVAL) | |
475 | && !deltanext ) { | |
476 | /* XXXX How to optimize if data == 0? */ | |
477 | /* Optimize to a simpler form. */ | |
478 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */ | |
479 | regnode *nxt2; | |
480 | ||
481 | OP(oscan) = CURLYM; | |
482 | while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/ | |
483 | && (OP(nxt2) != WHILEM)) | |
484 | nxt = nxt2; | |
485 | OP(nxt2) = SUCCEED; /* Whas WHILEM */ | |
486 | #ifdef REGALIGN_STRUCT | |
487 | /* Need to optimize away parenths. */ | |
488 | if (data->flags & SF_IN_PAR) { | |
489 | /* Set the parenth number. */ | |
490 | regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/ | |
491 | ||
492 | if (OP(nxt) != CLOSE) | |
493 | FAIL("panic opt close"); | |
494 | oscan->flags = ARG(nxt); | |
495 | OP(nxt1) = OPTIMIZED; /* was OPEN. */ | |
496 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ | |
497 | #ifdef DEBUGGING | |
498 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ | |
499 | OP(nxt + 1) = OPTIMIZED; /* was count. */ | |
500 | NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */ | |
501 | NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */ | |
502 | #endif | |
503 | #if 0 | |
504 | while ( nxt1 && (OP(nxt1) != WHILEM)) { | |
505 | regnode *nnxt = regnext(nxt1); | |
506 | ||
507 | if (nnxt == nxt) { | |
508 | if (reg_off_by_arg[OP(nxt1)]) | |
509 | ARG_SET(nxt1, nxt2 - nxt1); | |
510 | else if (nxt2 - nxt1 < U16_MAX) | |
511 | NEXT_OFF(nxt1) = nxt2 - nxt1; | |
512 | else | |
513 | OP(nxt) = NOTHING; /* Cannot beautify */ | |
514 | } | |
515 | nxt1 = nnxt; | |
516 | } | |
517 | #endif | |
518 | /* Optimize again: */ | |
519 | study_chunk(&nxt1, &deltanext, nxt, NULL, 0); | |
520 | } else | |
521 | oscan->flags = 0; | |
522 | #endif | |
523 | } | |
524 | if (data && fl & (SF_HAS_PAR|SF_IN_PAR)) | |
525 | pars++; | |
526 | if (flags & SCF_DO_SUBSTR) { | |
527 | SV *last_str = Nullsv; | |
528 | int counted = mincount != 0; | |
529 | ||
530 | if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */ | |
531 | I32 b = pos_before >= data->last_start_min | |
532 | ? pos_before : data->last_start_min; | |
533 | STRLEN l; | |
534 | char *s = SvPV(data->last_found, l); | |
535 | ||
536 | l -= b - data->last_start_min; | |
537 | /* Get the added string: */ | |
538 | last_str = newSVpv(s + b - data->last_start_min, l); | |
539 | if (deltanext == 0 && pos_before == b) { | |
540 | /* What was added is a constant string */ | |
541 | if (mincount > 1) { | |
542 | SvGROW(last_str, (mincount * l) + 1); | |
543 | repeatcpy(SvPVX(last_str) + l, | |
544 | SvPVX(last_str), l, mincount - 1); | |
545 | SvCUR(last_str) *= mincount; | |
546 | /* Add additional parts. */ | |
547 | SvCUR_set(data->last_found, | |
548 | SvCUR(data->last_found) - l); | |
549 | sv_catsv(data->last_found, last_str); | |
550 | data->last_end += l * (mincount - 1); | |
551 | } | |
552 | } | |
553 | } | |
554 | /* It is counted once already... */ | |
555 | data->pos_min += minnext * (mincount - counted); | |
556 | data->pos_delta += - counted * deltanext + | |
557 | (minnext + deltanext) * maxcount - minnext * mincount; | |
558 | if (mincount != maxcount) { | |
559 | scan_commit(data); | |
560 | if (mincount && last_str) { | |
561 | sv_setsv(data->last_found, last_str); | |
562 | data->last_end = data->pos_min; | |
563 | data->last_start_min = | |
564 | data->pos_min - SvCUR(last_str); | |
565 | data->last_start_max = is_inf | |
566 | ? I32_MAX | |
567 | : data->pos_min + data->pos_delta | |
568 | - SvCUR(last_str); | |
569 | } | |
570 | data->longest = &(data->longest_float); | |
571 | } | |
572 | } | |
405ff068 | 573 | if (data && (fl & SF_HAS_EVAL)) |
c277df42 IZ |
574 | data->flags |= SF_HAS_EVAL; |
575 | optimize_curly_tail: | |
576 | #ifdef REGALIGN | |
577 | if (OP(oscan) != CURLYX) { | |
578 | while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING | |
579 | && NEXT_OFF(next)) | |
580 | NEXT_OFF(oscan) += NEXT_OFF(next); | |
581 | } | |
582 | #endif | |
583 | continue; | |
584 | default: /* REF only? */ | |
585 | if (flags & SCF_DO_SUBSTR) { | |
586 | scan_commit(data); | |
587 | data->longest = &(data->longest_float); | |
588 | } | |
589 | is_inf = 1; | |
590 | break; | |
591 | } | |
592 | } else if (strchr(simple,OP(scan))) { | |
593 | if (flags & SCF_DO_SUBSTR) { | |
594 | scan_commit(data); | |
595 | data->pos_min++; | |
596 | } | |
597 | min++; | |
598 | } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) { | |
599 | data->flags |= (OP(scan) == MEOL | |
600 | ? SF_BEFORE_MEOL | |
601 | : SF_BEFORE_SEOL); | |
602 | } else if (regkind[(U8)OP(scan)] == BRANCHJ | |
603 | && (scan->flags || data) | |
604 | && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) { | |
605 | I32 deltanext, minnext; | |
606 | regnode *nscan; | |
607 | ||
608 | data_fake.flags = 0; | |
609 | next = regnext(scan); | |
610 | nscan = NEXTOPER(NEXTOPER(scan)); | |
611 | minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0); | |
612 | if (scan->flags) { | |
613 | if (deltanext) { | |
614 | FAIL("variable length lookbehind not implemented"); | |
615 | } else if (minnext > U8_MAX) { | |
616 | FAIL2("lookbehind longer than %d not implemented", U8_MAX); | |
617 | } | |
618 | scan->flags = minnext; | |
619 | } | |
620 | if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) | |
621 | pars++; | |
405ff068 | 622 | if (data && (data_fake.flags & SF_HAS_EVAL)) |
c277df42 IZ |
623 | data->flags |= SF_HAS_EVAL; |
624 | } else if (OP(scan) == OPEN) { | |
625 | pars++; | |
626 | } else if (OP(scan) == CLOSE && ARG(scan) == is_par) { | |
627 | #ifdef REGALIGN_STRUCT | |
628 | next = regnext(scan); | |
629 | ||
630 | if ( next && (OP(next) != WHILEM) && next < last) | |
631 | #endif | |
632 | is_par = 0; /* Disable optimization */ | |
633 | } else if (OP(scan) == EVAL) { | |
634 | if (data) | |
635 | data->flags |= SF_HAS_EVAL; | |
636 | } | |
637 | /* Else: zero-length, ignore. */ | |
638 | scan = regnext(scan); | |
639 | } | |
640 | ||
641 | finish: | |
642 | *scanp = scan; | |
643 | *deltap = is_inf ? I32_MAX : delta; | |
644 | if (flags & SCF_DO_SUBSTR && is_inf) | |
645 | data->pos_delta = I32_MAX - data->pos_min; | |
646 | if (is_par > U8_MAX) | |
647 | is_par = 0; | |
648 | if (is_par && pars==1 && data) { | |
649 | data->flags |= SF_IN_PAR; | |
650 | data->flags &= ~SF_HAS_PAR; | |
651 | } else if (pars && data) { | |
652 | data->flags |= SF_HAS_PAR; | |
653 | data->flags &= ~SF_IN_PAR; | |
654 | } | |
655 | return min; | |
656 | } | |
657 | ||
76e3520e | 658 | STATIC I32 |
c277df42 IZ |
659 | add_data(I32 n, char *s) |
660 | { | |
77d41b28 GS |
661 | if (regcomp_rx->data) { |
662 | Renewc(regcomp_rx->data, | |
663 | sizeof(*regcomp_rx->data) + sizeof(void*) * (regcomp_rx->data->count + n - 1), | |
c277df42 | 664 | char, struct reg_data); |
77d41b28 GS |
665 | Renew(regcomp_rx->data->what, regcomp_rx->data->count + n, U8); |
666 | regcomp_rx->data->count += n; | |
c277df42 | 667 | } else { |
77d41b28 | 668 | Newc(1207, regcomp_rx->data, sizeof(*regcomp_rx->data) + sizeof(void*) * (n - 1), |
c277df42 | 669 | char, struct reg_data); |
77d41b28 GS |
670 | New(1208, regcomp_rx->data->what, n, U8); |
671 | regcomp_rx->data->count = n; | |
c277df42 | 672 | } |
77d41b28 GS |
673 | Copy(s, regcomp_rx->data->what + regcomp_rx->data->count - n, n, U8); |
674 | return regcomp_rx->data->count - n; | |
c277df42 IZ |
675 | } |
676 | ||
a687059c | 677 | /* |
e50aee73 | 678 | - pregcomp - compile a regular expression into internal code |
a687059c LW |
679 | * |
680 | * We can't allocate space until we know how big the compiled form will be, | |
681 | * but we can't compile it (and thus know how big it is) until we've got a | |
682 | * place to put the code. So we cheat: we compile it twice, once with code | |
683 | * generation turned off and size counting turned on, and once "for real". | |
684 | * This also means that we don't allocate space until we are sure that the | |
685 | * thing really will compile successfully, and we never have to move the | |
686 | * code and thus invalidate pointers into it. (Note that it has to be in | |
687 | * one piece because free() must be able to free it all.) [NB: not true in perl] | |
688 | * | |
689 | * Beware that the optimization-preparation code in here knows about some | |
690 | * of the structure of the compiled regexp. [I'll say.] | |
691 | */ | |
692 | regexp * | |
8ac85365 | 693 | pregcomp(char *exp, char *xend, PMOP *pm) |
a687059c | 694 | { |
a0d0e21e | 695 | register regexp *r; |
c277df42 IZ |
696 | regnode *scan; |
697 | SV **longest; | |
698 | SV *longest_fixed; | |
699 | SV *longest_float; | |
700 | regnode *first; | |
a0d0e21e | 701 | I32 flags; |
a0d0e21e LW |
702 | I32 minlen = 0; |
703 | I32 sawplus = 0; | |
704 | I32 sawopen = 0; | |
705 | ||
706 | if (exp == NULL) | |
c277df42 | 707 | FAIL("NULL regexp argument"); |
a0d0e21e | 708 | |
bbce6d69 | 709 | regprecomp = savepvn(exp, xend - exp); |
c277df42 IZ |
710 | DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n", |
711 | xend - exp, regprecomp)); | |
a0d0e21e | 712 | regflags = pm->op_pmflags; |
bbce6d69 | 713 | regsawback = 0; |
714 | ||
c277df42 IZ |
715 | regseen = 0; |
716 | seen_zerolen = *exp == '^' ? -1 : 0; | |
717 | extralen = 0; | |
718 | ||
bbce6d69 | 719 | /* First pass: determine size, legality. */ |
77d41b28 | 720 | regcomp_parse = exp; |
a0d0e21e | 721 | regxend = xend; |
a0d0e21e | 722 | regnaughty = 0; |
a0d0e21e LW |
723 | regnpar = 1; |
724 | regsize = 0L; | |
725 | regcode = ®dummy; | |
c277df42 | 726 | regc((U8)MAGIC, (char*)regcode); |
a0d0e21e LW |
727 | if (reg(0, &flags) == NULL) { |
728 | Safefree(regprecomp); | |
729 | regprecomp = Nullch; | |
730 | return(NULL); | |
731 | } | |
c277df42 IZ |
732 | DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize)); |
733 | ||
734 | DEBUG_r( | |
735 | if (!colorset) { | |
736 | int i = 0; | |
76e3520e | 737 | char *s = PerlEnv_getenv("TERMCAP_COLORS"); |
c277df42 IZ |
738 | |
739 | colorset = 1; | |
740 | if (s) { | |
741 | colors[0] = s = savepv(s); | |
742 | while (++i < 4) { | |
743 | s = strchr(s, '\t'); | |
744 | if (!s) | |
745 | FAIL("Not enough TABs in TERMCAP_COLORS"); | |
746 | *s = '\0'; | |
747 | colors[i] = ++s; | |
748 | } | |
749 | } else { | |
750 | while (i < 4) | |
751 | colors[i++] = ""; | |
752 | } | |
753 | /* Reset colors: */ | |
754 | PerlIO_printf(Perl_debug_log, "%s%s%s%s", | |
755 | colors[0],colors[1],colors[2],colors[3]); | |
756 | } | |
757 | ); | |
a0d0e21e | 758 | |
c277df42 IZ |
759 | /* Small enough for pointer-storage convention? |
760 | If extralen==0, this means that we will not need long jumps. */ | |
761 | #ifndef REGALIGN_STRUCT | |
762 | if (regsize >= 0x10000L && extralen) | |
a0d0e21e | 763 | FAIL("regexp too big"); |
c277df42 IZ |
764 | #else |
765 | if (regsize >= 0x10000L && extralen) | |
766 | regsize += extralen; | |
767 | else | |
768 | extralen = 0; | |
769 | #endif | |
a0d0e21e | 770 | |
bbce6d69 | 771 | /* Allocate space and initialize. */ |
c277df42 IZ |
772 | Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode), |
773 | char, regexp); | |
a0d0e21e LW |
774 | if (r == NULL) |
775 | FAIL("regexp out of space"); | |
c277df42 | 776 | r->refcnt = 1; |
bbce6d69 | 777 | r->prelen = xend - exp; |
a0d0e21e LW |
778 | r->precomp = regprecomp; |
779 | r->subbeg = r->subbase = NULL; | |
77d41b28 | 780 | regcomp_rx = r; |
bbce6d69 | 781 | |
782 | /* Second pass: emit code. */ | |
77d41b28 | 783 | regcomp_parse = exp; |
bbce6d69 | 784 | regxend = xend; |
785 | regnaughty = 0; | |
a0d0e21e LW |
786 | regnpar = 1; |
787 | regcode = r->program; | |
c277df42 IZ |
788 | regc((U8)MAGIC, (char*) regcode++); |
789 | r->data = 0; | |
a0d0e21e LW |
790 | if (reg(0, &flags) == NULL) |
791 | return(NULL); | |
792 | ||
793 | /* Dig out information for optimizations. */ | |
794 | pm->op_pmflags = regflags; | |
a0d0e21e | 795 | r->reganch = 0; |
c277df42 | 796 | r->regstclass = NULL; |
a0d0e21e | 797 | r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */ |
c277df42 | 798 | scan = r->program + 1; /* First BRANCH. */ |
2779dcf1 IZ |
799 | |
800 | /* XXXX To minimize changes to RE engine we always allocate | |
801 | 3-units-long substrs field. */ | |
802 | Newz(1004, r->substrs, 1, struct reg_substr_data); | |
803 | ||
c277df42 IZ |
804 | if (OP(scan) != BRANCH) { /* Only one top-level choice. */ |
805 | scan_data_t data; | |
806 | I32 fake; | |
c5254dd6 | 807 | STRLEN longest_float_length, longest_fixed_length; |
a0d0e21e | 808 | |
c277df42 | 809 | StructCopy(&zero_scan_data, &data, scan_data_t); |
a0d0e21e | 810 | first = scan; |
c277df42 | 811 | /* Skip introductions and multiplicators >= 1. */ |
a0d0e21e LW |
812 | while ((OP(first) == OPEN && (sawopen = 1)) || |
813 | (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) || | |
814 | (OP(first) == PLUS) || | |
815 | (OP(first) == MINMOD) || | |
816 | (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) { | |
817 | if (OP(first) == PLUS) | |
818 | sawplus = 1; | |
819 | else | |
820 | first += regarglen[(U8)OP(first)]; | |
821 | first = NEXTOPER(first); | |
a687059c LW |
822 | } |
823 | ||
a0d0e21e LW |
824 | /* Starting-point info. */ |
825 | again: | |
c277df42 | 826 | if (OP(first) == EXACT); /* Empty, get anchored substr later. */ |
a0d0e21e LW |
827 | else if (strchr(simple+2,OP(first))) |
828 | r->regstclass = first; | |
bbce6d69 | 829 | else if (regkind[(U8)OP(first)] == BOUND || |
830 | regkind[(U8)OP(first)] == NBOUND) | |
a0d0e21e LW |
831 | r->regstclass = first; |
832 | else if (regkind[(U8)OP(first)] == BOL) { | |
c277df42 | 833 | r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL); |
a0d0e21e | 834 | first = NEXTOPER(first); |
774d564b | 835 | goto again; |
836 | } | |
837 | else if (OP(first) == GPOS) { | |
838 | r->reganch |= ROPT_ANCH_GPOS; | |
839 | first = NEXTOPER(first); | |
840 | goto again; | |
a0d0e21e LW |
841 | } |
842 | else if ((OP(first) == STAR && | |
843 | regkind[(U8)OP(NEXTOPER(first))] == ANY) && | |
844 | !(r->reganch & ROPT_ANCH) ) | |
845 | { | |
846 | /* turn .* into ^.* with an implied $*=1 */ | |
774d564b | 847 | r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT; |
a0d0e21e | 848 | first = NEXTOPER(first); |
774d564b | 849 | goto again; |
a0d0e21e LW |
850 | } |
851 | if (sawplus && (!sawopen || !regsawback)) | |
852 | r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */ | |
853 | ||
c277df42 IZ |
854 | /* Scan is after the zeroth branch, first is atomic matcher. */ |
855 | DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n", | |
856 | first - scan + 1)); | |
a0d0e21e LW |
857 | /* |
858 | * If there's something expensive in the r.e., find the | |
859 | * longest literal string that must appear and make it the | |
860 | * regmust. Resolve ties in favor of later strings, since | |
861 | * the regstart check works with the beginning of the r.e. | |
862 | * and avoiding duplication strengthens checking. Not a | |
863 | * strong reason, but sufficient in the absence of others. | |
864 | * [Now we resolve ties in favor of the earlier string if | |
c277df42 | 865 | * it happens that c_offset_min has been invalidated, since the |
a0d0e21e LW |
866 | * earlier string may buy us something the later one won't.] |
867 | */ | |
a0d0e21e | 868 | minlen = 0; |
a687059c | 869 | |
c277df42 IZ |
870 | data.longest_fixed = newSVpv("",0); |
871 | data.longest_float = newSVpv("",0); | |
872 | data.last_found = newSVpv("",0); | |
873 | data.longest = &(data.longest_fixed); | |
874 | first = scan; | |
875 | ||
876 | minlen = study_chunk(&first, &fake, scan + regsize, /* Up to end */ | |
877 | &data, SCF_DO_SUBSTR); | |
878 | if ( regnpar == 1 && data.longest == &(data.longest_fixed) | |
879 | && data.last_start_min == 0 && data.last_end > 0 | |
880 | && !seen_zerolen | |
881 | && (!(regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS))) | |
882 | r->reganch |= ROPT_CHECK_ALL; | |
883 | scan_commit(&data); | |
884 | SvREFCNT_dec(data.last_found); | |
885 | ||
c5254dd6 MB |
886 | longest_float_length = SvCUR(data.longest_float); |
887 | if (longest_float_length | |
c277df42 IZ |
888 | || (data.flags & SF_FL_BEFORE_EOL |
889 | && (!(data.flags & SF_FL_BEFORE_MEOL) | |
890 | || (regflags & PMf_MULTILINE)))) { | |
891 | if (SvCUR(data.longest_fixed) | |
892 | && data.offset_fixed == data.offset_float_min) | |
893 | goto remove; /* Like in (a)+. */ | |
894 | ||
895 | r->float_substr = data.longest_float; | |
896 | r->float_min_offset = data.offset_float_min; | |
897 | r->float_max_offset = data.offset_float_max; | |
2779dcf1 | 898 | fbm_compile(r->float_substr, 0); |
c277df42 IZ |
899 | BmUSEFUL(r->float_substr) = 100; |
900 | if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */ | |
901 | && (!(data.flags & SF_FL_BEFORE_MEOL) | |
902 | || (regflags & PMf_MULTILINE))) | |
903 | SvTAIL_on(r->float_substr); | |
904 | } else { | |
905 | remove: | |
906 | r->float_substr = Nullsv; | |
907 | SvREFCNT_dec(data.longest_float); | |
c5254dd6 | 908 | longest_float_length = 0; |
a0d0e21e | 909 | } |
c277df42 | 910 | |
c5254dd6 MB |
911 | longest_fixed_length = SvCUR(data.longest_fixed); |
912 | if (longest_fixed_length | |
c277df42 IZ |
913 | || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
914 | && (!(data.flags & SF_FIX_BEFORE_MEOL) | |
915 | || (regflags & PMf_MULTILINE)))) { | |
916 | r->anchored_substr = data.longest_fixed; | |
917 | r->anchored_offset = data.offset_fixed; | |
2779dcf1 | 918 | fbm_compile(r->anchored_substr, 0); |
c277df42 IZ |
919 | BmUSEFUL(r->anchored_substr) = 100; |
920 | if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ | |
921 | && (!(data.flags & SF_FIX_BEFORE_MEOL) | |
922 | || (regflags & PMf_MULTILINE))) | |
923 | SvTAIL_on(r->anchored_substr); | |
924 | } else { | |
925 | r->anchored_substr = Nullsv; | |
926 | SvREFCNT_dec(data.longest_fixed); | |
c5254dd6 | 927 | longest_fixed_length = 0; |
a0d0e21e | 928 | } |
c277df42 IZ |
929 | |
930 | /* A temporary algorithm prefers floated substr to fixed one to dig more info. */ | |
c5254dd6 | 931 | if (longest_fixed_length > longest_float_length) { |
c277df42 IZ |
932 | r->check_substr = r->anchored_substr; |
933 | r->check_offset_min = r->check_offset_max = r->anchored_offset; | |
934 | if (r->reganch & ROPT_ANCH_SINGLE) | |
935 | r->reganch |= ROPT_NOSCAN; | |
936 | } else { | |
937 | r->check_substr = r->float_substr; | |
938 | r->check_offset_min = data.offset_float_min; | |
939 | r->check_offset_max = data.offset_float_max; | |
a0d0e21e | 940 | } |
c277df42 IZ |
941 | } else { |
942 | /* Several toplevels. Best we can is to set minlen. */ | |
943 | I32 fake; | |
944 | ||
945 | DEBUG_r(PerlIO_printf(Perl_debug_log, "\n")); | |
946 | scan = r->program + 1; | |
947 | minlen = study_chunk(&scan, &fake, scan + regsize, NULL, 0); | |
948 | r->check_substr = r->anchored_substr = r->float_substr = Nullsv; | |
a0d0e21e LW |
949 | } |
950 | ||
a0d0e21e LW |
951 | r->nparens = regnpar - 1; |
952 | r->minlen = minlen; | |
c277df42 IZ |
953 | if (regseen & REG_SEEN_GPOS) |
954 | r->reganch |= ROPT_GPOS_SEEN; | |
955 | if (regseen & REG_SEEN_LOOKBEHIND) | |
956 | r->reganch |= ROPT_LOOKBEHIND_SEEN; | |
a0d0e21e LW |
957 | Newz(1002, r->startp, regnpar, char*); |
958 | Newz(1002, r->endp, regnpar, char*); | |
959 | DEBUG_r(regdump(r)); | |
960 | return(r); | |
a687059c LW |
961 | } |
962 | ||
963 | /* | |
964 | - reg - regular expression, i.e. main body or parenthesized thing | |
965 | * | |
966 | * Caller must absorb opening parenthesis. | |
967 | * | |
968 | * Combining parenthesis handling with the base level of regular expression | |
969 | * is a trifle forced, but the need to tie the tails of the branches to what | |
970 | * follows makes it hard to avoid. | |
971 | */ | |
76e3520e | 972 | STATIC regnode * |
8ac85365 | 973 | reg(I32 paren, I32 *flagp) |
c277df42 | 974 | /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */ |
a687059c | 975 | { |
c277df42 IZ |
976 | register regnode *ret; /* Will be the head of the group. */ |
977 | register regnode *br; | |
978 | register regnode *lastbr; | |
979 | register regnode *ender = 0; | |
a0d0e21e | 980 | register I32 parno = 0; |
c277df42 IZ |
981 | I32 flags, oregflags = regflags, have_branch = 0, open = 0; |
982 | char c; | |
a0d0e21e LW |
983 | |
984 | *flagp = HASWIDTH; /* Tentatively. */ | |
985 | ||
986 | /* Make an OPEN node, if parenthesized. */ | |
987 | if (paren) { | |
77d41b28 GS |
988 | if (*regcomp_parse == '?') { |
989 | regcomp_parse++; | |
990 | paren = *regcomp_parse++; | |
c277df42 | 991 | ret = NULL; /* For look-ahead/behind. */ |
a0d0e21e | 992 | switch (paren) { |
c277df42 IZ |
993 | case '<': |
994 | #ifndef REGALIGN_STRUCT | |
995 | FAIL("lookbehind non-implemented without REGALIGN_STRUCT"); | |
996 | #endif | |
997 | regseen |= REG_SEEN_LOOKBEHIND; | |
77d41b28 | 998 | if (*regcomp_parse == '!') |
c277df42 | 999 | paren = ','; |
77d41b28 | 1000 | if (*regcomp_parse != '=' && *regcomp_parse != '!') |
c277df42 | 1001 | goto unknown; |
77d41b28 | 1002 | regcomp_parse++; |
a0d0e21e LW |
1003 | case '=': |
1004 | case '!': | |
c277df42 IZ |
1005 | seen_zerolen++; |
1006 | case ':': | |
1007 | case '>': | |
a0d0e21e LW |
1008 | break; |
1009 | case '$': | |
1010 | case '@': | |
c277df42 | 1011 | FAIL2("Sequence (?%c...) not implemented", (int)paren); |
a0d0e21e LW |
1012 | break; |
1013 | case '#': | |
77d41b28 GS |
1014 | while (*regcomp_parse && *regcomp_parse != ')') |
1015 | regcomp_parse++; | |
1016 | if (*regcomp_parse != ')') | |
c277df42 | 1017 | FAIL("Sequence (?#... not terminated"); |
a0d0e21e LW |
1018 | nextchar(); |
1019 | *flagp = TRYAGAIN; | |
1020 | return NULL; | |
c277df42 IZ |
1021 | case '{': |
1022 | { | |
1023 | dTHR; | |
1024 | I32 count = 1, n = 0; | |
1025 | char c; | |
77d41b28 | 1026 | char *s = regcomp_parse; |
c277df42 IZ |
1027 | SV *sv; |
1028 | OP_4tree *sop, *rop; | |
1029 | ||
1030 | seen_zerolen++; | |
77d41b28 GS |
1031 | while (count && (c = *regcomp_parse)) { |
1032 | if (c == '\\' && regcomp_parse[1]) | |
1033 | regcomp_parse++; | |
c277df42 IZ |
1034 | else if (c == '{') |
1035 | count++; | |
1036 | else if (c == '}') | |
1037 | count--; | |
77d41b28 | 1038 | regcomp_parse++; |
c277df42 | 1039 | } |
77d41b28 | 1040 | if (*regcomp_parse != ')') |
c277df42 IZ |
1041 | FAIL("Sequence (?{...}) not terminated or not {}-balanced"); |
1042 | if (!SIZE_ONLY) { | |
1043 | AV *av; | |
1044 | ||
77d41b28 GS |
1045 | if (regcomp_parse - 1 - s) |
1046 | sv = newSVpv(s, regcomp_parse - 1 - s); | |
c277df42 IZ |
1047 | else |
1048 | sv = newSVpv("", 0); | |
1049 | ||
1050 | rop = sv_compile_2op(sv, &sop, "re", &av); | |
1051 | ||
1052 | n = add_data(3, "nso"); | |
77d41b28 GS |
1053 | regcomp_rx->data->data[n] = (void*)rop; |
1054 | regcomp_rx->data->data[n+1] = (void*)av; | |
1055 | regcomp_rx->data->data[n+2] = (void*)sop; | |
c277df42 | 1056 | SvREFCNT_dec(sv); |
cc6b7395 IZ |
1057 | } else { /* First pass */ |
1058 | if (tainted) | |
1059 | FAIL("Eval-group in insecure regular expression"); | |
c277df42 IZ |
1060 | } |
1061 | ||
1062 | nextchar(); | |
c277df42 IZ |
1063 | return reganode(EVAL, n); |
1064 | } | |
1065 | case '(': | |
1066 | { | |
77d41b28 GS |
1067 | if (regcomp_parse[0] == '?') { |
1068 | if (regcomp_parse[1] == '=' || regcomp_parse[1] == '!' | |
1069 | || regcomp_parse[1] == '<' | |
1070 | || regcomp_parse[1] == '{') { /* Lookahead or eval. */ | |
c277df42 IZ |
1071 | I32 flag; |
1072 | ||
1073 | ret = reg_node(LOGICAL); | |
1074 | regtail(ret, reg(1, &flag)); | |
1075 | goto insert_if; | |
1076 | } | |
77d41b28 GS |
1077 | } else if (regcomp_parse[0] >= '1' && regcomp_parse[0] <= '9' ) { |
1078 | parno = atoi(regcomp_parse++); | |
c277df42 | 1079 | |
77d41b28 GS |
1080 | while (isDIGIT(*regcomp_parse)) |
1081 | regcomp_parse++; | |
c277df42 IZ |
1082 | ret = reganode(GROUPP, parno); |
1083 | if ((c = *nextchar()) != ')') | |
1084 | FAIL2("Switch (?(number%c not recognized", c); | |
1085 | insert_if: | |
1086 | regtail(ret, reganode(IFTHEN, 0)); | |
1087 | br = regbranch(&flags, 1); | |
1088 | if (br == NULL) | |
1089 | br = reganode(LONGJMP, 0); | |
1090 | else | |
1091 | regtail(br, reganode(LONGJMP, 0)); | |
1092 | c = *nextchar(); | |
1093 | if (c == '|') { | |
1094 | lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */ | |
1095 | regbranch(&flags, 1); | |
1096 | regtail(ret, lastbr); | |
1097 | c = *nextchar(); | |
1098 | } else | |
1099 | lastbr = NULL; | |
1100 | if (c != ')') | |
1101 | FAIL("Switch (?(condition)... contains too many branches"); | |
1102 | ender = reg_node(TAIL); | |
1103 | regtail(br, ender); | |
1104 | if (lastbr) { | |
1105 | regtail(lastbr, ender); | |
1106 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); | |
1107 | } else | |
1108 | regtail(ret, ender); | |
1109 | return ret; | |
1110 | } else { | |
77d41b28 | 1111 | FAIL2("Unknown condition for (?(%.2s", regcomp_parse); |
c277df42 IZ |
1112 | } |
1113 | } | |
1b1626e4 | 1114 | case 0: |
c277df42 | 1115 | FAIL("Sequence (? incomplete"); |
1b1626e4 | 1116 | break; |
a0d0e21e | 1117 | default: |
77d41b28 GS |
1118 | --regcomp_parse; |
1119 | while (*regcomp_parse && strchr("iogcmsx", *regcomp_parse)) { | |
1120 | if (*regcomp_parse != 'o') | |
1121 | pmflag(®flags, *regcomp_parse); | |
1122 | ++regcomp_parse; | |
48c036b1 | 1123 | } |
c277df42 | 1124 | unknown: |
77d41b28 GS |
1125 | if (*regcomp_parse != ')') |
1126 | FAIL2("Sequence (?%c...) not recognized", *regcomp_parse); | |
a0d0e21e LW |
1127 | nextchar(); |
1128 | *flagp = TRYAGAIN; | |
1129 | return NULL; | |
1130 | } | |
1131 | } | |
1132 | else { | |
1133 | parno = regnpar; | |
1134 | regnpar++; | |
1135 | ret = reganode(OPEN, parno); | |
c277df42 | 1136 | open = 1; |
a0d0e21e LW |
1137 | } |
1138 | } else | |
1139 | ret = NULL; | |
1140 | ||
1141 | /* Pick up the branches, linking them together. */ | |
c277df42 | 1142 | br = regbranch(&flags, 1); |
a0d0e21e LW |
1143 | if (br == NULL) |
1144 | return(NULL); | |
77d41b28 | 1145 | if (*regcomp_parse == '|') { |
c277df42 IZ |
1146 | if (!SIZE_ONLY && extralen) { |
1147 | reginsert(BRANCHJ, br); | |
1148 | } else | |
1149 | reginsert(BRANCH, br); | |
1150 | have_branch = 1; | |
1151 | if (SIZE_ONLY) | |
1152 | extralen += 1; /* For BRANCHJ-BRANCH. */ | |
1153 | } else if (paren == ':') { | |
1154 | *flagp |= flags&SIMPLE; | |
1155 | } | |
1156 | if (open) { /* Starts with OPEN. */ | |
1157 | regtail(ret, br); /* OPEN -> first. */ | |
1158 | } else if (paren != '?') /* Not Conditional */ | |
a0d0e21e LW |
1159 | ret = br; |
1160 | if (!(flags&HASWIDTH)) | |
1161 | *flagp &= ~HASWIDTH; | |
1162 | *flagp |= flags&SPSTART; | |
c277df42 | 1163 | lastbr = br; |
77d41b28 | 1164 | while (*regcomp_parse == '|') { |
c277df42 IZ |
1165 | if (!SIZE_ONLY && extralen) { |
1166 | ender = reganode(LONGJMP,0); | |
1167 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */ | |
1168 | } | |
1169 | if (SIZE_ONLY) | |
1170 | extralen += 2; /* Account for LONGJMP. */ | |
a0d0e21e | 1171 | nextchar(); |
c277df42 | 1172 | br = regbranch(&flags, 0); |
a687059c | 1173 | if (br == NULL) |
a0d0e21e | 1174 | return(NULL); |
c277df42 IZ |
1175 | regtail(lastbr, br); /* BRANCH -> BRANCH. */ |
1176 | lastbr = br; | |
a687059c | 1177 | if (!(flags&HASWIDTH)) |
a0d0e21e | 1178 | *flagp &= ~HASWIDTH; |
a687059c | 1179 | *flagp |= flags&SPSTART; |
a0d0e21e LW |
1180 | } |
1181 | ||
c277df42 IZ |
1182 | if (have_branch || paren != ':') { |
1183 | /* Make a closing node, and hook it on the end. */ | |
1184 | switch (paren) { | |
1185 | case ':': | |
1186 | ender = reg_node(TAIL); | |
1187 | break; | |
1188 | case 1: | |
1189 | ender = reganode(CLOSE, parno); | |
1190 | break; | |
1191 | case '<': | |
1192 | case '>': | |
1193 | case ',': | |
1194 | case '=': | |
1195 | case '!': | |
1196 | ender = reg_node(SUCCEED); | |
1197 | *flagp &= ~HASWIDTH; | |
1198 | break; | |
1199 | case 0: | |
1200 | ender = reg_node(END); | |
1201 | break; | |
1202 | } | |
1203 | regtail(lastbr, ender); | |
a0d0e21e | 1204 | |
c277df42 IZ |
1205 | if (have_branch) { |
1206 | /* Hook the tails of the branches to the closing node. */ | |
1207 | for (br = ret; br != NULL; br = regnext(br)) { | |
1208 | regoptail(br, ender); | |
1209 | } | |
1210 | } | |
a0d0e21e | 1211 | } |
c277df42 IZ |
1212 | |
1213 | { | |
1214 | char *p; | |
1215 | static char parens[] = "=!<,>"; | |
1216 | ||
1217 | if (paren && (p = strchr(parens, paren))) { | |
1218 | int node = ((p - parens) % 2) ? UNLESSM : IFMATCH; | |
1219 | int flag = (p - parens) > 1; | |
1220 | ||
1221 | if (paren == '>') | |
1222 | node = SUSPEND, flag = 0; | |
1223 | reginsert(node,ret); | |
1224 | #ifdef REGALIGN_STRUCT | |
1225 | ret->flags = flag; | |
1226 | #endif | |
1227 | regtail(ret, reg_node(TAIL)); | |
1228 | } | |
a0d0e21e LW |
1229 | } |
1230 | ||
1231 | /* Check for proper termination. */ | |
77d41b28 | 1232 | if (paren && (regcomp_parse >= regxend || *nextchar() != ')')) { |
a0d0e21e | 1233 | FAIL("unmatched () in regexp"); |
77d41b28 GS |
1234 | } else if (!paren && regcomp_parse < regxend) { |
1235 | if (*regcomp_parse == ')') { | |
a0d0e21e LW |
1236 | FAIL("unmatched () in regexp"); |
1237 | } else | |
1238 | FAIL("junk on end of regexp"); /* "Can't happen". */ | |
1239 | /* NOTREACHED */ | |
1240 | } | |
c277df42 IZ |
1241 | if (paren != 0) { |
1242 | regflags = oregflags; | |
1243 | } | |
a687059c | 1244 | |
a0d0e21e | 1245 | return(ret); |
a687059c LW |
1246 | } |
1247 | ||
1248 | /* | |
1249 | - regbranch - one alternative of an | operator | |
1250 | * | |
1251 | * Implements the concatenation operator. | |
1252 | */ | |
76e3520e | 1253 | STATIC regnode * |
c277df42 | 1254 | regbranch(I32 *flagp, I32 first) |
a687059c | 1255 | { |
c277df42 IZ |
1256 | register regnode *ret; |
1257 | register regnode *chain = NULL; | |
1258 | register regnode *latest; | |
1259 | I32 flags = 0, c = 0; | |
a0d0e21e | 1260 | |
c277df42 IZ |
1261 | if (first) |
1262 | ret = NULL; | |
1263 | else { | |
1264 | if (!SIZE_ONLY && extralen) | |
1265 | ret = reganode(BRANCHJ,0); | |
1266 | else | |
1267 | ret = reg_node(BRANCH); | |
1268 | } | |
1269 | ||
1270 | if (!first && SIZE_ONLY) | |
1271 | extralen += 1; /* BRANCHJ */ | |
1272 | ||
1273 | *flagp = WORST; /* Tentatively. */ | |
a0d0e21e | 1274 | |
77d41b28 | 1275 | regcomp_parse--; |
a0d0e21e | 1276 | nextchar(); |
77d41b28 | 1277 | while (regcomp_parse < regxend && *regcomp_parse != '|' && *regcomp_parse != ')') { |
a0d0e21e LW |
1278 | flags &= ~TRYAGAIN; |
1279 | latest = regpiece(&flags); | |
1280 | if (latest == NULL) { | |
1281 | if (flags & TRYAGAIN) | |
1282 | continue; | |
1283 | return(NULL); | |
c277df42 IZ |
1284 | } else if (ret == NULL) |
1285 | ret = latest; | |
a0d0e21e | 1286 | *flagp |= flags&HASWIDTH; |
c277df42 | 1287 | if (chain == NULL) /* First piece. */ |
a0d0e21e LW |
1288 | *flagp |= flags&SPSTART; |
1289 | else { | |
1290 | regnaughty++; | |
1291 | regtail(chain, latest); | |
a687059c | 1292 | } |
a0d0e21e | 1293 | chain = latest; |
c277df42 IZ |
1294 | c++; |
1295 | } | |
1296 | if (chain == NULL) { /* Loop ran zero times. */ | |
1297 | chain = reg_node(NOTHING); | |
1298 | if (ret == NULL) | |
1299 | ret = chain; | |
1300 | } | |
1301 | if (c == 1) { | |
1302 | *flagp |= flags&SIMPLE; | |
a0d0e21e | 1303 | } |
a687059c | 1304 | |
a0d0e21e | 1305 | return(ret); |
a687059c LW |
1306 | } |
1307 | ||
1308 | /* | |
1309 | - regpiece - something followed by possible [*+?] | |
1310 | * | |
1311 | * Note that the branching code sequences used for ? and the general cases | |
1312 | * of * and + are somewhat optimized: they use the same NOTHING node as | |
1313 | * both the endmarker for their branch list and the body of the last branch. | |
1314 | * It might seem that this node could be dispensed with entirely, but the | |
1315 | * endmarker role is not redundant. | |
1316 | */ | |
76e3520e | 1317 | STATIC regnode * |
8ac85365 | 1318 | regpiece(I32 *flagp) |
a687059c | 1319 | { |
c277df42 | 1320 | register regnode *ret; |
a0d0e21e LW |
1321 | register char op; |
1322 | register char *next; | |
1323 | I32 flags; | |
77d41b28 | 1324 | char *origparse = regcomp_parse; |
a0d0e21e LW |
1325 | char *maxpos; |
1326 | I32 min; | |
c277df42 | 1327 | I32 max = REG_INFTY; |
a0d0e21e LW |
1328 | |
1329 | ret = regatom(&flags); | |
1330 | if (ret == NULL) { | |
1331 | if (flags & TRYAGAIN) | |
1332 | *flagp |= TRYAGAIN; | |
1333 | return(NULL); | |
1334 | } | |
1335 | ||
77d41b28 | 1336 | op = *regcomp_parse; |
a0d0e21e | 1337 | |
77d41b28 GS |
1338 | if (op == '{' && regcurly(regcomp_parse)) { |
1339 | next = regcomp_parse + 1; | |
a0d0e21e LW |
1340 | maxpos = Nullch; |
1341 | while (isDIGIT(*next) || *next == ',') { | |
1342 | if (*next == ',') { | |
1343 | if (maxpos) | |
1344 | break; | |
1345 | else | |
1346 | maxpos = next; | |
a687059c | 1347 | } |
a0d0e21e LW |
1348 | next++; |
1349 | } | |
1350 | if (*next == '}') { /* got one */ | |
1351 | if (!maxpos) | |
1352 | maxpos = next; | |
77d41b28 GS |
1353 | regcomp_parse++; |
1354 | min = atoi(regcomp_parse); | |
a0d0e21e LW |
1355 | if (*maxpos == ',') |
1356 | maxpos++; | |
1357 | else | |
77d41b28 | 1358 | maxpos = regcomp_parse; |
a0d0e21e LW |
1359 | max = atoi(maxpos); |
1360 | if (!max && *maxpos != '0') | |
c277df42 IZ |
1361 | max = REG_INFTY; /* meaning "infinity" */ |
1362 | else if (max >= REG_INFTY) | |
1363 | FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1); | |
77d41b28 | 1364 | regcomp_parse = next; |
a0d0e21e LW |
1365 | nextchar(); |
1366 | ||
1367 | do_curly: | |
1368 | if ((flags&SIMPLE)) { | |
1369 | regnaughty += 2 + regnaughty / 2; | |
1370 | reginsert(CURLY, ret); | |
1371 | } | |
1372 | else { | |
1373 | regnaughty += 4 + regnaughty; /* compound interest */ | |
c277df42 IZ |
1374 | regtail(ret, reg_node(WHILEM)); |
1375 | if (!SIZE_ONLY && extralen) { | |
1376 | reginsert(LONGJMP,ret); | |
1377 | reginsert(NOTHING,ret); | |
1378 | NEXT_OFF(ret) = 3; /* Go over LONGJMP. */ | |
1379 | } | |
a0d0e21e | 1380 | reginsert(CURLYX,ret); |
c277df42 IZ |
1381 | if (!SIZE_ONLY && extralen) |
1382 | NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */ | |
1383 | regtail(ret, reg_node(NOTHING)); | |
1384 | if (SIZE_ONLY) | |
1385 | extralen += 3; | |
a0d0e21e | 1386 | } |
c277df42 IZ |
1387 | #ifdef REGALIGN_STRUCT |
1388 | ret->flags = 0; | |
1389 | #endif | |
a0d0e21e LW |
1390 | |
1391 | if (min > 0) | |
1392 | *flagp = (WORST|HASWIDTH); | |
1393 | if (max && max < min) | |
c277df42 IZ |
1394 | FAIL("Can't do {n,m} with n > m"); |
1395 | if (!SIZE_ONLY) { | |
1396 | ARG1_SET(ret, min); | |
1397 | ARG2_SET(ret, max); | |
a687059c | 1398 | } |
a687059c | 1399 | |
a0d0e21e | 1400 | goto nest_check; |
a687059c | 1401 | } |
a0d0e21e | 1402 | } |
a687059c | 1403 | |
a0d0e21e LW |
1404 | if (!ISMULT1(op)) { |
1405 | *flagp = flags; | |
a687059c | 1406 | return(ret); |
a0d0e21e | 1407 | } |
bb20fd44 | 1408 | |
c277df42 | 1409 | #if 0 /* Now runtime fix should be reliable. */ |
bb20fd44 | 1410 | if (!(flags&HASWIDTH) && op != '?') |
c277df42 IZ |
1411 | FAIL("regexp *+ operand could be empty"); |
1412 | #endif | |
bb20fd44 | 1413 | |
a0d0e21e LW |
1414 | nextchar(); |
1415 | ||
1416 | *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); | |
1417 | ||
1418 | if (op == '*' && (flags&SIMPLE)) { | |
1419 | reginsert(STAR, ret); | |
c277df42 IZ |
1420 | #ifdef REGALIGN_STRUCT |
1421 | ret->flags = 0; | |
1422 | #endif | |
a0d0e21e LW |
1423 | regnaughty += 4; |
1424 | } | |
1425 | else if (op == '*') { | |
1426 | min = 0; | |
1427 | goto do_curly; | |
1428 | } else if (op == '+' && (flags&SIMPLE)) { | |
1429 | reginsert(PLUS, ret); | |
c277df42 IZ |
1430 | #ifdef REGALIGN_STRUCT |
1431 | ret->flags = 0; | |
1432 | #endif | |
a0d0e21e LW |
1433 | regnaughty += 3; |
1434 | } | |
1435 | else if (op == '+') { | |
1436 | min = 1; | |
1437 | goto do_curly; | |
1438 | } else if (op == '?') { | |
1439 | min = 0; max = 1; | |
1440 | goto do_curly; | |
1441 | } | |
1442 | nest_check: | |
c277df42 | 1443 | if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) { |
a0d0e21e | 1444 | warn("%.*s matches null string many times", |
77d41b28 | 1445 | regcomp_parse - origparse, origparse); |
a0d0e21e LW |
1446 | } |
1447 | ||
77d41b28 | 1448 | if (*regcomp_parse == '?') { |
a0d0e21e LW |
1449 | nextchar(); |
1450 | reginsert(MINMOD, ret); | |
1451 | #ifdef REGALIGN | |
c277df42 | 1452 | regtail(ret, ret + NODE_STEP_REGNODE); |
a0d0e21e LW |
1453 | #else |
1454 | regtail(ret, ret + 3); | |
1455 | #endif | |
1456 | } | |
77d41b28 | 1457 | if (ISMULT2(regcomp_parse)) |
a0d0e21e LW |
1458 | FAIL("nested *?+ in regexp"); |
1459 | ||
1460 | return(ret); | |
a687059c LW |
1461 | } |
1462 | ||
1463 | /* | |
1464 | - regatom - the lowest level | |
1465 | * | |
1466 | * Optimization: gobbles an entire sequence of ordinary characters so that | |
1467 | * it can turn them into a single node, which is smaller to store and | |
1468 | * faster to run. Backslashed characters are exceptions, each becoming a | |
1469 | * separate node; the code is simpler that way and it's not worth fixing. | |
1470 | * | |
1471 | * [Yes, it is worth fixing, some scripts can run twice the speed.] | |
1472 | */ | |
76e3520e | 1473 | STATIC regnode * |
8ac85365 | 1474 | regatom(I32 *flagp) |
a687059c | 1475 | { |
c277df42 | 1476 | register regnode *ret = 0; |
a0d0e21e LW |
1477 | I32 flags; |
1478 | ||
1479 | *flagp = WORST; /* Tentatively. */ | |
1480 | ||
1481 | tryagain: | |
77d41b28 | 1482 | switch (*regcomp_parse) { |
a0d0e21e | 1483 | case '^': |
c277df42 | 1484 | seen_zerolen++; |
a0d0e21e LW |
1485 | nextchar(); |
1486 | if (regflags & PMf_MULTILINE) | |
c277df42 | 1487 | ret = reg_node(MBOL); |
a0d0e21e | 1488 | else if (regflags & PMf_SINGLELINE) |
c277df42 | 1489 | ret = reg_node(SBOL); |
a0d0e21e | 1490 | else |
c277df42 | 1491 | ret = reg_node(BOL); |
a0d0e21e LW |
1492 | break; |
1493 | case '$': | |
77d41b28 | 1494 | if (regcomp_parse[1]) |
c277df42 | 1495 | seen_zerolen++; |
a0d0e21e LW |
1496 | nextchar(); |
1497 | if (regflags & PMf_MULTILINE) | |
c277df42 | 1498 | ret = reg_node(MEOL); |
a0d0e21e | 1499 | else if (regflags & PMf_SINGLELINE) |
c277df42 | 1500 | ret = reg_node(SEOL); |
a0d0e21e | 1501 | else |
c277df42 | 1502 | ret = reg_node(EOL); |
a0d0e21e LW |
1503 | break; |
1504 | case '.': | |
1505 | nextchar(); | |
1506 | if (regflags & PMf_SINGLELINE) | |
c277df42 | 1507 | ret = reg_node(SANY); |
a0d0e21e | 1508 | else |
c277df42 | 1509 | ret = reg_node(ANY); |
a0d0e21e LW |
1510 | regnaughty++; |
1511 | *flagp |= HASWIDTH|SIMPLE; | |
1512 | break; | |
1513 | case '[': | |
77d41b28 | 1514 | regcomp_parse++; |
a0d0e21e LW |
1515 | ret = regclass(); |
1516 | *flagp |= HASWIDTH|SIMPLE; | |
1517 | break; | |
1518 | case '(': | |
1519 | nextchar(); | |
1520 | ret = reg(1, &flags); | |
1521 | if (ret == NULL) { | |
1522 | if (flags & TRYAGAIN) | |
1523 | goto tryagain; | |
1524 | return(NULL); | |
1525 | } | |
c277df42 | 1526 | *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE); |
a0d0e21e LW |
1527 | break; |
1528 | case '|': | |
1529 | case ')': | |
1530 | if (flags & TRYAGAIN) { | |
1531 | *flagp |= TRYAGAIN; | |
1532 | return NULL; | |
1533 | } | |
77d41b28 | 1534 | FAIL2("internal urp in regexp at /%s/", regcomp_parse); |
a0d0e21e LW |
1535 | /* Supposed to be caught earlier. */ |
1536 | break; | |
85afd4ae | 1537 | case '{': |
77d41b28 GS |
1538 | if (!regcurly(regcomp_parse)) { |
1539 | regcomp_parse++; | |
85afd4ae CS |
1540 | goto defchar; |
1541 | } | |
1542 | /* FALL THROUGH */ | |
a0d0e21e LW |
1543 | case '?': |
1544 | case '+': | |
1545 | case '*': | |
3115e423 | 1546 | FAIL("?+*{} follows nothing in regexp"); |
a0d0e21e LW |
1547 | break; |
1548 | case '\\': | |
77d41b28 | 1549 | switch (*++regcomp_parse) { |
a0d0e21e | 1550 | case 'A': |
c277df42 IZ |
1551 | seen_zerolen++; |
1552 | ret = reg_node(SBOL); | |
a0d0e21e LW |
1553 | *flagp |= SIMPLE; |
1554 | nextchar(); | |
1555 | break; | |
1556 | case 'G': | |
c277df42 IZ |
1557 | ret = reg_node(GPOS); |
1558 | regseen |= REG_SEEN_GPOS; | |
a0d0e21e LW |
1559 | *flagp |= SIMPLE; |
1560 | nextchar(); | |
1561 | break; | |
1562 | case 'Z': | |
c277df42 | 1563 | ret = reg_node(SEOL); |
a0d0e21e LW |
1564 | *flagp |= SIMPLE; |
1565 | nextchar(); | |
1566 | break; | |
1567 | case 'w': | |
c277df42 | 1568 | ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM); |
a0d0e21e LW |
1569 | *flagp |= HASWIDTH|SIMPLE; |
1570 | nextchar(); | |
1571 | break; | |
1572 | case 'W': | |
c277df42 | 1573 | ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM); |
a0d0e21e LW |
1574 | *flagp |= HASWIDTH|SIMPLE; |
1575 | nextchar(); | |
1576 | break; | |
1577 | case 'b': | |
c277df42 IZ |
1578 | seen_zerolen++; |
1579 | ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND); | |
a0d0e21e LW |
1580 | *flagp |= SIMPLE; |
1581 | nextchar(); | |
1582 | break; | |
1583 | case 'B': | |
c277df42 IZ |
1584 | seen_zerolen++; |
1585 | ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND); | |
a0d0e21e LW |
1586 | *flagp |= SIMPLE; |
1587 | nextchar(); | |
1588 | break; | |
1589 | case 's': | |
c277df42 | 1590 | ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE); |
a0d0e21e LW |
1591 | *flagp |= HASWIDTH|SIMPLE; |
1592 | nextchar(); | |
1593 | break; | |
1594 | case 'S': | |
c277df42 | 1595 | ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE); |
a0d0e21e LW |
1596 | *flagp |= HASWIDTH|SIMPLE; |
1597 | nextchar(); | |
1598 | break; | |
1599 | case 'd': | |
c277df42 | 1600 | ret = reg_node(DIGIT); |
a0d0e21e LW |
1601 | *flagp |= HASWIDTH|SIMPLE; |
1602 | nextchar(); | |
1603 | break; | |
1604 | case 'D': | |
c277df42 | 1605 | ret = reg_node(NDIGIT); |
a0d0e21e LW |
1606 | *flagp |= HASWIDTH|SIMPLE; |
1607 | nextchar(); | |
1608 | break; | |
1609 | case 'n': | |
1610 | case 'r': | |
1611 | case 't': | |
1612 | case 'f': | |
1613 | case 'e': | |
1614 | case 'a': | |
1615 | case 'x': | |
1616 | case 'c': | |
1617 | case '0': | |
1618 | goto defchar; | |
1619 | case '1': case '2': case '3': case '4': | |
1620 | case '5': case '6': case '7': case '8': case '9': | |
1621 | { | |
77d41b28 | 1622 | I32 num = atoi(regcomp_parse); |
a0d0e21e LW |
1623 | |
1624 | if (num > 9 && num >= regnpar) | |
1625 | goto defchar; | |
1626 | else { | |
1627 | regsawback = 1; | |
c8756f30 AK |
1628 | ret = reganode((regflags & PMf_FOLD) |
1629 | ? ((regflags & PMf_LOCALE) ? REFFL : REFF) | |
1630 | : REF, num); | |
a0d0e21e | 1631 | *flagp |= HASWIDTH; |
77d41b28 GS |
1632 | while (isDIGIT(*regcomp_parse)) |
1633 | regcomp_parse++; | |
1634 | regcomp_parse--; | |
a0d0e21e LW |
1635 | nextchar(); |
1636 | } | |
1637 | } | |
1638 | break; | |
1639 | case '\0': | |
77d41b28 | 1640 | if (regcomp_parse >= regxend) |
a0d0e21e LW |
1641 | FAIL("trailing \\ in regexp"); |
1642 | /* FALL THROUGH */ | |
1643 | default: | |
1644 | goto defchar; | |
1645 | } | |
1646 | break; | |
4633a7c4 LW |
1647 | |
1648 | case '#': | |
1649 | if (regflags & PMf_EXTENDED) { | |
77d41b28 GS |
1650 | while (regcomp_parse < regxend && *regcomp_parse != '\n') regcomp_parse++; |
1651 | if (regcomp_parse < regxend) | |
4633a7c4 LW |
1652 | goto tryagain; |
1653 | } | |
1654 | /* FALL THROUGH */ | |
1655 | ||
a0d0e21e LW |
1656 | default: { |
1657 | register I32 len; | |
c277df42 | 1658 | register U8 ender; |
a0d0e21e | 1659 | register char *p; |
c277df42 | 1660 | char *oldp, *s; |
a0d0e21e LW |
1661 | I32 numlen; |
1662 | ||
77d41b28 | 1663 | regcomp_parse++; |
a0d0e21e LW |
1664 | |
1665 | defchar: | |
c277df42 | 1666 | ret = reg_node((regflags & PMf_FOLD) |
bbce6d69 | 1667 | ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF) |
1668 | : EXACT); | |
161b471a | 1669 | s = (char *) OPERAND(ret); |
c277df42 | 1670 | regc(0, s++); /* save spot for len */ |
77d41b28 | 1671 | for (len = 0, p = regcomp_parse - 1; |
a0d0e21e LW |
1672 | len < 127 && p < regxend; |
1673 | len++) | |
1674 | { | |
1675 | oldp = p; | |
5b5a24f7 CS |
1676 | |
1677 | if (regflags & PMf_EXTENDED) | |
1678 | p = regwhite(p, regxend); | |
a0d0e21e LW |
1679 | switch (*p) { |
1680 | case '^': | |
1681 | case '$': | |
1682 | case '.': | |
1683 | case '[': | |
1684 | case '(': | |
1685 | case ')': | |
1686 | case '|': | |
1687 | goto loopdone; | |
1688 | case '\\': | |
1689 | switch (*++p) { | |
1690 | case 'A': | |
1691 | case 'G': | |
1692 | case 'Z': | |
1693 | case 'w': | |
1694 | case 'W': | |
1695 | case 'b': | |
1696 | case 'B': | |
1697 | case 's': | |
1698 | case 'S': | |
1699 | case 'd': | |
1700 | case 'D': | |
1701 | --p; | |
1702 | goto loopdone; | |
1703 | case 'n': | |
1704 | ender = '\n'; | |
1705 | p++; | |
a687059c | 1706 | break; |
a0d0e21e LW |
1707 | case 'r': |
1708 | ender = '\r'; | |
1709 | p++; | |
a687059c | 1710 | break; |
a0d0e21e LW |
1711 | case 't': |
1712 | ender = '\t'; | |
1713 | p++; | |
a687059c | 1714 | break; |
a0d0e21e LW |
1715 | case 'f': |
1716 | ender = '\f'; | |
1717 | p++; | |
a687059c | 1718 | break; |
a0d0e21e LW |
1719 | case 'e': |
1720 | ender = '\033'; | |
1721 | p++; | |
a687059c | 1722 | break; |
a0d0e21e LW |
1723 | case 'a': |
1724 | ender = '\007'; | |
1725 | p++; | |
a687059c | 1726 | break; |
a0d0e21e LW |
1727 | case 'x': |
1728 | ender = scan_hex(++p, 2, &numlen); | |
1729 | p += numlen; | |
a687059c | 1730 | break; |
a0d0e21e LW |
1731 | case 'c': |
1732 | p++; | |
bbce6d69 | 1733 | ender = UCHARAT(p++); |
1734 | ender = toCTRL(ender); | |
a687059c | 1735 | break; |
a0d0e21e LW |
1736 | case '0': case '1': case '2': case '3':case '4': |
1737 | case '5': case '6': case '7': case '8':case '9': | |
1738 | if (*p == '0' || | |
1739 | (isDIGIT(p[1]) && atoi(p) >= regnpar) ) { | |
1740 | ender = scan_oct(p, 3, &numlen); | |
1741 | p += numlen; | |
1742 | } | |
1743 | else { | |
1744 | --p; | |
1745 | goto loopdone; | |
a687059c LW |
1746 | } |
1747 | break; | |
a0d0e21e LW |
1748 | case '\0': |
1749 | if (p >= regxend) | |
a687059c LW |
1750 | FAIL("trailing \\ in regexp"); |
1751 | /* FALL THROUGH */ | |
a0d0e21e LW |
1752 | default: |
1753 | ender = *p++; | |
1754 | break; | |
1755 | } | |
1756 | break; | |
a687059c | 1757 | default: |
a0d0e21e LW |
1758 | ender = *p++; |
1759 | break; | |
a687059c | 1760 | } |
5b5a24f7 CS |
1761 | if (regflags & PMf_EXTENDED) |
1762 | p = regwhite(p, regxend); | |
a0d0e21e LW |
1763 | if (ISMULT2(p)) { /* Back off on ?+*. */ |
1764 | if (len) | |
1765 | p = oldp; | |
1766 | else { | |
1767 | len++; | |
c277df42 | 1768 | regc(ender, s++); |
a0d0e21e LW |
1769 | } |
1770 | break; | |
a687059c | 1771 | } |
c277df42 | 1772 | regc(ender, s++); |
a0d0e21e LW |
1773 | } |
1774 | loopdone: | |
77d41b28 | 1775 | regcomp_parse = p - 1; |
a0d0e21e LW |
1776 | nextchar(); |
1777 | if (len < 0) | |
1778 | FAIL("internal disaster in regexp"); | |
1779 | if (len > 0) | |
1780 | *flagp |= HASWIDTH; | |
1781 | if (len == 1) | |
1782 | *flagp |= SIMPLE; | |
c277df42 | 1783 | if (!SIZE_ONLY) |
a0d0e21e | 1784 | *OPERAND(ret) = len; |
c277df42 IZ |
1785 | regc('\0', s++); |
1786 | if (SIZE_ONLY) { | |
1787 | #ifdef REGALIGN_STRUCT | |
1788 | regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
1789 | #endif | |
1790 | } else { | |
1791 | regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
1792 | } | |
a687059c | 1793 | } |
a0d0e21e LW |
1794 | break; |
1795 | } | |
a687059c | 1796 | |
a0d0e21e | 1797 | return(ret); |
a687059c LW |
1798 | } |
1799 | ||
873ef191 | 1800 | STATIC char * |
8ac85365 | 1801 | regwhite(char *p, char *e) |
5b5a24f7 CS |
1802 | { |
1803 | while (p < e) { | |
1804 | if (isSPACE(*p)) | |
1805 | ++p; | |
1806 | else if (*p == '#') { | |
1807 | do { | |
1808 | p++; | |
1809 | } while (p < e && *p != '\n'); | |
1810 | } | |
1811 | else | |
1812 | break; | |
1813 | } | |
1814 | return p; | |
1815 | } | |
1816 | ||
76e3520e | 1817 | STATIC regnode * |
8ac85365 | 1818 | regclass(void) |
a687059c | 1819 | { |
c277df42 | 1820 | register char *opnd, *s; |
8ac85365 | 1821 | register I32 Class; |
a0d0e21e LW |
1822 | register I32 lastclass = 1234; |
1823 | register I32 range = 0; | |
c277df42 | 1824 | register regnode *ret; |
a0d0e21e LW |
1825 | register I32 def; |
1826 | I32 numlen; | |
1827 | ||
161b471a | 1828 | s = opnd = (char *) OPERAND(regcode); |
c277df42 | 1829 | ret = reg_node(ANYOF); |
8ac85365 | 1830 | for (Class = 0; Class < 33; Class++) |
c277df42 | 1831 | regc(0, s++); |
77d41b28 | 1832 | if (*regcomp_parse == '^') { /* Complement of range. */ |
a0d0e21e | 1833 | regnaughty++; |
77d41b28 | 1834 | regcomp_parse++; |
c277df42 | 1835 | if (!SIZE_ONLY) |
bbce6d69 | 1836 | *opnd |= ANYOF_INVERT; |
1837 | } | |
c277df42 IZ |
1838 | if (!SIZE_ONLY) { |
1839 | regcode += ANY_SKIP; | |
bbce6d69 | 1840 | if (regflags & PMf_FOLD) |
1841 | *opnd |= ANYOF_FOLD; | |
1842 | if (regflags & PMf_LOCALE) | |
1843 | *opnd |= ANYOF_LOCALE; | |
c277df42 IZ |
1844 | } else { |
1845 | regsize += ANY_SKIP; | |
a0d0e21e | 1846 | } |
77d41b28 | 1847 | if (*regcomp_parse == ']' || *regcomp_parse == '-') |
a0d0e21e | 1848 | goto skipcond; /* allow 1st char to be ] or - */ |
77d41b28 | 1849 | while (regcomp_parse < regxend && *regcomp_parse != ']') { |
a0d0e21e | 1850 | skipcond: |
77d41b28 GS |
1851 | Class = UCHARAT(regcomp_parse++); |
1852 | if (Class == '[' && regcomp_parse + 1 < regxend && | |
4599a1de | 1853 | /* I smell either [: or [= or [. -- POSIX has been here, right? */ |
77d41b28 GS |
1854 | (*regcomp_parse == ':' || *regcomp_parse == '=' || *regcomp_parse == '.')) { |
1855 | char posixccc = *regcomp_parse; | |
1856 | char* posixccs = regcomp_parse++; | |
4599a1de | 1857 | |
77d41b28 GS |
1858 | while (regcomp_parse < regxend && *regcomp_parse != posixccc) |
1859 | regcomp_parse++; | |
1860 | if (regcomp_parse == regxend) | |
4599a1de | 1861 | /* Grandfather lone [:, [=, [. */ |
77d41b28 | 1862 | regcomp_parse = posixccs; |
4599a1de | 1863 | else { |
77d41b28 GS |
1864 | regcomp_parse++; /* skip over the posixccc */ |
1865 | if (*regcomp_parse == ']') { | |
4599a1de JH |
1866 | /* Not Implemented Yet. |
1867 | * (POSIX Extended Character Classes, that is) | |
1868 | * The text between e.g. [: and :] would start | |
77d41b28 | 1869 | * at posixccs + 1 and stop at regcomp_parse - 2. */ |
4599a1de JH |
1870 | if (dowarn && !SIZE_ONLY) |
1871 | warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc); | |
77d41b28 | 1872 | regcomp_parse++; /* skip over the ending ] */ |
4599a1de JH |
1873 | } |
1874 | } | |
1875 | } | |
8ac85365 | 1876 | if (Class == '\\') { |
77d41b28 | 1877 | Class = UCHARAT(regcomp_parse++); |
8ac85365 | 1878 | switch (Class) { |
a0d0e21e | 1879 | case 'w': |
ae5c130c GS |
1880 | if (!SIZE_ONLY) { |
1881 | if (regflags & PMf_LOCALE) | |
bbce6d69 | 1882 | *opnd |= ANYOF_ALNUML; |
ae5c130c GS |
1883 | else { |
1884 | for (Class = 0; Class < 256; Class++) | |
1885 | if (isALNUM(Class)) | |
1886 | ANYOF_SET(opnd, Class); | |
1887 | } | |
bbce6d69 | 1888 | } |
a0d0e21e LW |
1889 | lastclass = 1234; |
1890 | continue; | |
1891 | case 'W': | |
ae5c130c GS |
1892 | if (!SIZE_ONLY) { |
1893 | if (regflags & PMf_LOCALE) | |
bbce6d69 | 1894 | *opnd |= ANYOF_NALNUML; |
ae5c130c GS |
1895 | else { |
1896 | for (Class = 0; Class < 256; Class++) | |
1897 | if (!isALNUM(Class)) | |
1898 | ANYOF_SET(opnd, Class); | |
1899 | } | |
bbce6d69 | 1900 | } |
a0d0e21e LW |
1901 | lastclass = 1234; |
1902 | continue; | |
1903 | case 's': | |
ae5c130c GS |
1904 | if (!SIZE_ONLY) { |
1905 | if (regflags & PMf_LOCALE) | |
bbce6d69 | 1906 | *opnd |= ANYOF_SPACEL; |
ae5c130c GS |
1907 | else { |
1908 | for (Class = 0; Class < 256; Class++) | |
1909 | if (isSPACE(Class)) | |
1910 | ANYOF_SET(opnd, Class); | |
1911 | } | |
bbce6d69 | 1912 | } |
a0d0e21e LW |
1913 | lastclass = 1234; |
1914 | continue; | |
1915 | case 'S': | |
ae5c130c GS |
1916 | if (!SIZE_ONLY) { |
1917 | if (regflags & PMf_LOCALE) | |
bbce6d69 | 1918 | *opnd |= ANYOF_NSPACEL; |
ae5c130c GS |
1919 | else { |
1920 | for (Class = 0; Class < 256; Class++) | |
1921 | if (!isSPACE(Class)) | |
1922 | ANYOF_SET(opnd, Class); | |
1923 | } | |
bbce6d69 | 1924 | } |
a0d0e21e LW |
1925 | lastclass = 1234; |
1926 | continue; | |
1927 | case 'd': | |
ae5c130c GS |
1928 | if (!SIZE_ONLY) { |
1929 | for (Class = '0'; Class <= '9'; Class++) | |
1930 | ANYOF_SET(opnd, Class); | |
1931 | } | |
a0d0e21e LW |
1932 | lastclass = 1234; |
1933 | continue; | |
1934 | case 'D': | |
ae5c130c GS |
1935 | if (!SIZE_ONLY) { |
1936 | for (Class = 0; Class < '0'; Class++) | |
1937 | ANYOF_SET(opnd, Class); | |
1938 | for (Class = '9' + 1; Class < 256; Class++) | |
1939 | ANYOF_SET(opnd, Class); | |
1940 | } | |
a0d0e21e LW |
1941 | lastclass = 1234; |
1942 | continue; | |
1943 | case 'n': | |
8ac85365 | 1944 | Class = '\n'; |
a0d0e21e LW |
1945 | break; |
1946 | case 'r': | |
8ac85365 | 1947 | Class = '\r'; |
a0d0e21e LW |
1948 | break; |
1949 | case 't': | |
8ac85365 | 1950 | Class = '\t'; |
a0d0e21e LW |
1951 | break; |
1952 | case 'f': | |
8ac85365 | 1953 | Class = '\f'; |
a0d0e21e LW |
1954 | break; |
1955 | case 'b': | |
8ac85365 | 1956 | Class = '\b'; |
a0d0e21e LW |
1957 | break; |
1958 | case 'e': | |
8ac85365 | 1959 | Class = '\033'; |
a0d0e21e LW |
1960 | break; |
1961 | case 'a': | |
8ac85365 | 1962 | Class = '\007'; |
a0d0e21e LW |
1963 | break; |
1964 | case 'x': | |
77d41b28 GS |
1965 | Class = scan_hex(regcomp_parse, 2, &numlen); |
1966 | regcomp_parse += numlen; | |
a0d0e21e LW |
1967 | break; |
1968 | case 'c': | |
77d41b28 | 1969 | Class = UCHARAT(regcomp_parse++); |
8ac85365 | 1970 | Class = toCTRL(Class); |
a0d0e21e LW |
1971 | break; |
1972 | case '0': case '1': case '2': case '3': case '4': | |
1973 | case '5': case '6': case '7': case '8': case '9': | |
77d41b28 GS |
1974 | Class = scan_oct(--regcomp_parse, 3, &numlen); |
1975 | regcomp_parse += numlen; | |
a0d0e21e LW |
1976 | break; |
1977 | } | |
1978 | } | |
1979 | if (range) { | |
8ac85365 | 1980 | if (lastclass > Class) |
a0d0e21e LW |
1981 | FAIL("invalid [] range in regexp"); |
1982 | range = 0; | |
1983 | } | |
1984 | else { | |
8ac85365 | 1985 | lastclass = Class; |
77d41b28 GS |
1986 | if (*regcomp_parse == '-' && regcomp_parse+1 < regxend && |
1987 | regcomp_parse[1] != ']') { | |
1988 | regcomp_parse++; | |
a0d0e21e LW |
1989 | range = 1; |
1990 | continue; /* do it next time */ | |
1991 | } | |
a687059c | 1992 | } |
ae5c130c GS |
1993 | if (!SIZE_ONLY) { |
1994 | for ( ; lastclass <= Class; lastclass++) | |
1995 | ANYOF_SET(opnd, lastclass); | |
1996 | } | |
8ac85365 | 1997 | lastclass = Class; |
a0d0e21e | 1998 | } |
77d41b28 | 1999 | if (*regcomp_parse != ']') |
a0d0e21e LW |
2000 | FAIL("unmatched [] in regexp"); |
2001 | nextchar(); | |
ae5c130c GS |
2002 | /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */ |
2003 | if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) { | |
2004 | for (Class = 0; Class < 256; ++Class) { | |
2005 | if (ANYOF_TEST(opnd, Class)) { | |
2006 | I32 cf = fold[Class]; | |
2007 | ANYOF_SET(opnd, cf); | |
2008 | } | |
2009 | } | |
2010 | *opnd &= ~ANYOF_FOLD; | |
2011 | } | |
2012 | /* optimize inverted simple patterns (e.g. [^a-z]) */ | |
2013 | if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) { | |
2014 | for (Class = 0; Class < 32; ++Class) | |
2015 | opnd[1 + Class] ^= 0xFF; | |
2016 | *opnd = 0; | |
2017 | } | |
a0d0e21e LW |
2018 | return ret; |
2019 | } | |
2020 | ||
76e3520e | 2021 | STATIC char* |
8ac85365 | 2022 | nextchar(void) |
a0d0e21e | 2023 | { |
77d41b28 | 2024 | char* retval = regcomp_parse++; |
a0d0e21e | 2025 | |
4633a7c4 | 2026 | for (;;) { |
77d41b28 GS |
2027 | if (*regcomp_parse == '(' && regcomp_parse[1] == '?' && |
2028 | regcomp_parse[2] == '#') { | |
2029 | while (*regcomp_parse && *regcomp_parse != ')') | |
2030 | regcomp_parse++; | |
2031 | regcomp_parse++; | |
4633a7c4 LW |
2032 | continue; |
2033 | } | |
2034 | if (regflags & PMf_EXTENDED) { | |
77d41b28 GS |
2035 | if (isSPACE(*regcomp_parse)) { |
2036 | regcomp_parse++; | |
748a9306 LW |
2037 | continue; |
2038 | } | |
77d41b28 GS |
2039 | else if (*regcomp_parse == '#') { |
2040 | while (*regcomp_parse && *regcomp_parse != '\n') | |
2041 | regcomp_parse++; | |
2042 | regcomp_parse++; | |
748a9306 LW |
2043 | continue; |
2044 | } | |
748a9306 | 2045 | } |
4633a7c4 | 2046 | return retval; |
a0d0e21e | 2047 | } |
a687059c LW |
2048 | } |
2049 | ||
2050 | /* | |
c277df42 | 2051 | - reg_node - emit a node |
a0d0e21e | 2052 | */ |
76e3520e | 2053 | STATIC regnode * /* Location. */ |
c277df42 | 2054 | reg_node(U8 op) |
a687059c | 2055 | { |
c277df42 IZ |
2056 | register regnode *ret; |
2057 | register regnode *ptr; | |
a687059c | 2058 | |
a0d0e21e | 2059 | ret = regcode; |
c277df42 IZ |
2060 | if (SIZE_ONLY) { |
2061 | SIZE_ALIGN(regsize); | |
2062 | #ifdef REGALIGN_STRUCT | |
2063 | regsize += 1; | |
2064 | #else | |
a0d0e21e | 2065 | regsize += 3; |
c277df42 | 2066 | #endif |
a0d0e21e LW |
2067 | return(ret); |
2068 | } | |
a687059c | 2069 | |
c277df42 | 2070 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 2071 | ptr = ret; |
c277df42 | 2072 | FILL_ADVANCE_NODE(ptr, op); |
a0d0e21e | 2073 | regcode = ptr; |
a687059c | 2074 | |
a0d0e21e | 2075 | return(ret); |
a687059c LW |
2076 | } |
2077 | ||
2078 | /* | |
a0d0e21e LW |
2079 | - reganode - emit a node with an argument |
2080 | */ | |
76e3520e | 2081 | STATIC regnode * /* Location. */ |
c277df42 | 2082 | reganode(U8 op, U32 arg) |
fe14fcc3 | 2083 | { |
c277df42 IZ |
2084 | register regnode *ret; |
2085 | register regnode *ptr; | |
fe14fcc3 | 2086 | |
a0d0e21e | 2087 | ret = regcode; |
c277df42 IZ |
2088 | if (SIZE_ONLY) { |
2089 | SIZE_ALIGN(regsize); | |
fe14fcc3 | 2090 | #ifdef REGALIGN |
c277df42 IZ |
2091 | regsize += 2; |
2092 | #else | |
a0d0e21e | 2093 | regsize += 5; |
c277df42 | 2094 | #endif |
a0d0e21e LW |
2095 | return(ret); |
2096 | } | |
fe14fcc3 | 2097 | |
c277df42 | 2098 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 2099 | ptr = ret; |
c277df42 | 2100 | FILL_ADVANCE_NODE_ARG(ptr, op, arg); |
a0d0e21e | 2101 | regcode = ptr; |
fe14fcc3 | 2102 | |
a0d0e21e | 2103 | return(ret); |
fe14fcc3 LW |
2104 | } |
2105 | ||
2106 | /* | |
a0d0e21e LW |
2107 | - regc - emit (if appropriate) a byte of code |
2108 | */ | |
76e3520e | 2109 | STATIC void |
c277df42 | 2110 | regc(U8 b, char* s) |
a687059c | 2111 | { |
c277df42 IZ |
2112 | if (!SIZE_ONLY) |
2113 | *s = b; | |
a687059c LW |
2114 | } |
2115 | ||
2116 | /* | |
a0d0e21e LW |
2117 | - reginsert - insert an operator in front of already-emitted operand |
2118 | * | |
2119 | * Means relocating the operand. | |
2120 | */ | |
76e3520e | 2121 | STATIC void |
c277df42 | 2122 | reginsert(U8 op, regnode *opnd) |
a687059c | 2123 | { |
c277df42 IZ |
2124 | register regnode *src; |
2125 | register regnode *dst; | |
2126 | register regnode *place; | |
2127 | register int offset = regarglen[(U8)op]; | |
2128 | ||
2129 | /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */ | |
2130 | ||
2131 | if (SIZE_ONLY) { | |
2132 | regsize += NODE_STEP_REGNODE + offset; | |
a0d0e21e LW |
2133 | return; |
2134 | } | |
a687059c | 2135 | |
a0d0e21e | 2136 | src = regcode; |
c277df42 | 2137 | regcode += NODE_STEP_REGNODE + offset; |
a0d0e21e LW |
2138 | dst = regcode; |
2139 | while (src > opnd) | |
c277df42 | 2140 | StructCopy(--src, --dst, regnode); |
a0d0e21e LW |
2141 | |
2142 | place = opnd; /* Op node, where operand used to be. */ | |
c277df42 IZ |
2143 | src = NEXTOPER(place); |
2144 | FILL_ADVANCE_NODE(place, op); | |
2145 | Zero(src, offset, regnode); | |
2146 | #if defined(REGALIGN) && !defined(REGALIGN_STRUCT) | |
2147 | src[offset + 1] = '\177'; | |
2b69d0c2 | 2148 | #endif |
a687059c LW |
2149 | } |
2150 | ||
2151 | /* | |
c277df42 | 2152 | - regtail - set the next-pointer at the end of a node chain of p to val. |
a0d0e21e | 2153 | */ |
76e3520e | 2154 | STATIC void |
c277df42 | 2155 | regtail(regnode *p, regnode *val) |
a687059c | 2156 | { |
c277df42 IZ |
2157 | register regnode *scan; |
2158 | register regnode *temp; | |
a0d0e21e LW |
2159 | register I32 offset; |
2160 | ||
c277df42 | 2161 | if (SIZE_ONLY) |
a0d0e21e LW |
2162 | return; |
2163 | ||
2164 | /* Find last node. */ | |
2165 | scan = p; | |
2166 | for (;;) { | |
2167 | temp = regnext(scan); | |
2168 | if (temp == NULL) | |
2169 | break; | |
2170 | scan = temp; | |
2171 | } | |
a687059c LW |
2172 | |
2173 | #ifdef REGALIGN | |
c277df42 IZ |
2174 | # ifdef REGALIGN_STRUCT |
2175 | if (reg_off_by_arg[OP(scan)]) { | |
2176 | ARG_SET(scan, val - scan); | |
2177 | } else { | |
2178 | NEXT_OFF(scan) = val - scan; | |
2179 | } | |
2180 | # else | |
a0d0e21e | 2181 | offset = val - scan; |
c277df42 | 2182 | # ifndef lint |
a0d0e21e | 2183 | *(short*)(scan+1) = offset; |
c277df42 IZ |
2184 | # endif |
2185 | #endif | |
a687059c | 2186 | #else |
a0d0e21e LW |
2187 | if (OP(scan) == BACK) |
2188 | offset = scan - val; | |
2189 | else | |
2190 | offset = val - scan; | |
2191 | *(scan+1) = (offset>>8)&0377; | |
2192 | *(scan+2) = offset&0377; | |
a687059c LW |
2193 | #endif |
2194 | } | |
2195 | ||
2196 | /* | |
a0d0e21e LW |
2197 | - regoptail - regtail on operand of first argument; nop if operandless |
2198 | */ | |
76e3520e | 2199 | STATIC void |
c277df42 | 2200 | regoptail(regnode *p, regnode *val) |
a687059c | 2201 | { |
a0d0e21e | 2202 | /* "Operandless" and "op != BRANCH" are synonymous in practice. */ |
c277df42 IZ |
2203 | if (p == NULL || SIZE_ONLY) |
2204 | return; | |
2205 | if (regkind[(U8)OP(p)] == BRANCH) { | |
2206 | regtail(NEXTOPER(p), val); | |
2207 | } else if ( regkind[(U8)OP(p)] == BRANCHJ) { | |
2208 | regtail(NEXTOPER(NEXTOPER(p)), val); | |
2209 | } else | |
a0d0e21e | 2210 | return; |
a687059c LW |
2211 | } |
2212 | ||
2213 | /* | |
2214 | - regcurly - a little FSA that accepts {\d+,?\d*} | |
2215 | */ | |
79072805 | 2216 | STATIC I32 |
8ac85365 | 2217 | regcurly(register char *s) |
a687059c LW |
2218 | { |
2219 | if (*s++ != '{') | |
2220 | return FALSE; | |
f0fcb552 | 2221 | if (!isDIGIT(*s)) |
a687059c | 2222 | return FALSE; |
f0fcb552 | 2223 | while (isDIGIT(*s)) |
a687059c LW |
2224 | s++; |
2225 | if (*s == ',') | |
2226 | s++; | |
f0fcb552 | 2227 | while (isDIGIT(*s)) |
a687059c LW |
2228 | s++; |
2229 | if (*s != '}') | |
2230 | return FALSE; | |
2231 | return TRUE; | |
2232 | } | |
2233 | ||
a687059c | 2234 | |
76e3520e | 2235 | STATIC regnode * |
c277df42 IZ |
2236 | dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l) |
2237 | { | |
35ff7856 | 2238 | #ifdef DEBUGGING |
c277df42 IZ |
2239 | register char op = EXACT; /* Arbitrary non-END op. */ |
2240 | register regnode *next, *onode; | |
2241 | ||
2242 | while (op != END && (!last || node < last)) { | |
2243 | /* While that wasn't END last time... */ | |
2244 | ||
2245 | NODE_ALIGN(node); | |
2246 | op = OP(node); | |
2247 | if (op == CLOSE) | |
2248 | l--; | |
2249 | next = regnext(node); | |
2250 | /* Where, what. */ | |
2251 | if (OP(node) == OPTIMIZED) | |
2252 | goto after_print; | |
2253 | regprop(sv, node); | |
2254 | PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start, | |
2255 | 2*l + 1, "", SvPVX(sv)); | |
2256 | if (next == NULL) /* Next ptr. */ | |
2257 | PerlIO_printf(Perl_debug_log, "(0)"); | |
2258 | else | |
2259 | PerlIO_printf(Perl_debug_log, "(%d)", next - start); | |
2260 | (void)PerlIO_putc(Perl_debug_log, '\n'); | |
2261 | after_print: | |
2262 | if (regkind[(U8)op] == BRANCHJ) { | |
2263 | register regnode *nnode = (OP(next) == LONGJMP | |
2264 | ? regnext(next) | |
2265 | : next); | |
2266 | if (last && nnode > last) | |
2267 | nnode = last; | |
2268 | node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1); | |
2269 | } else if (regkind[(U8)op] == BRANCH) { | |
2270 | node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1); | |
2271 | } else if ( op == CURLY) { /* `next' might be very big: optimizer */ | |
2272 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, | |
2273 | NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1); | |
2274 | } else if (regkind[(U8)op] == CURLY && op != CURLYX) { | |
2275 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, | |
2276 | next, sv, l + 1); | |
2277 | } else if ( op == PLUS || op == STAR) { | |
2278 | node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1); | |
2279 | } else if (op == ANYOF) { | |
2280 | node = NEXTOPER(node); | |
2281 | node += ANY_SKIP; | |
2282 | } else if (regkind[(U8)op] == EXACT) { | |
2283 | /* Literal string, where present. */ | |
2284 | node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
2285 | node = NEXTOPER(node); | |
2286 | } else { | |
2287 | node = NEXTOPER(node); | |
2288 | node += regarglen[(U8)op]; | |
2289 | } | |
2290 | if (op == CURLYX || op == OPEN) | |
2291 | l++; | |
2292 | else if (op == WHILEM) | |
2293 | l--; | |
2294 | } | |
17c3b450 | 2295 | #endif /* DEBUGGING */ |
c277df42 IZ |
2296 | return node; |
2297 | } | |
2298 | ||
a687059c | 2299 | /* |
fd181c75 | 2300 | - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form |
a687059c LW |
2301 | */ |
2302 | void | |
8ac85365 | 2303 | regdump(regexp *r) |
a687059c | 2304 | { |
35ff7856 | 2305 | #ifdef DEBUGGING |
46fc3d4c | 2306 | SV *sv = sv_newmortal(); |
a687059c | 2307 | |
c277df42 | 2308 | (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0); |
a0d0e21e LW |
2309 | |
2310 | /* Header fields of interest. */ | |
c277df42 IZ |
2311 | if (r->anchored_substr) |
2312 | PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ", | |
2313 | colors[0], | |
2314 | SvPVX(r->anchored_substr), | |
2315 | colors[1], | |
2316 | SvTAIL(r->anchored_substr) ? "$" : "", | |
2317 | r->anchored_offset); | |
2318 | if (r->float_substr) | |
2319 | PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ", | |
2320 | colors[0], | |
2321 | SvPVX(r->float_substr), | |
2322 | colors[1], | |
2323 | SvTAIL(r->float_substr) ? "$" : "", | |
2324 | r->float_min_offset, r->float_max_offset); | |
2325 | if (r->check_substr) | |
2326 | PerlIO_printf(Perl_debug_log, | |
2327 | r->check_substr == r->float_substr | |
2328 | ? "(checking floating" : "(checking anchored"); | |
2329 | if (r->reganch & ROPT_NOSCAN) | |
2330 | PerlIO_printf(Perl_debug_log, " noscan"); | |
2331 | if (r->reganch & ROPT_CHECK_ALL) | |
2332 | PerlIO_printf(Perl_debug_log, " isall"); | |
2333 | if (r->check_substr) | |
2334 | PerlIO_printf(Perl_debug_log, ") "); | |
2335 | ||
46fc3d4c | 2336 | if (r->regstclass) { |
2337 | regprop(sv, r->regstclass); | |
2338 | PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv)); | |
2339 | } | |
774d564b | 2340 | if (r->reganch & ROPT_ANCH) { |
2341 | PerlIO_printf(Perl_debug_log, "anchored"); | |
2342 | if (r->reganch & ROPT_ANCH_BOL) | |
2343 | PerlIO_printf(Perl_debug_log, "(BOL)"); | |
c277df42 IZ |
2344 | if (r->reganch & ROPT_ANCH_MBOL) |
2345 | PerlIO_printf(Perl_debug_log, "(MBOL)"); | |
774d564b | 2346 | if (r->reganch & ROPT_ANCH_GPOS) |
2347 | PerlIO_printf(Perl_debug_log, "(GPOS)"); | |
2348 | PerlIO_putc(Perl_debug_log, ' '); | |
2349 | } | |
c277df42 IZ |
2350 | if (r->reganch & ROPT_GPOS_SEEN) |
2351 | PerlIO_printf(Perl_debug_log, "GPOS "); | |
a0d0e21e | 2352 | if (r->reganch & ROPT_SKIP) |
760ac839 | 2353 | PerlIO_printf(Perl_debug_log, "plus "); |
a0d0e21e | 2354 | if (r->reganch & ROPT_IMPLICIT) |
760ac839 | 2355 | PerlIO_printf(Perl_debug_log, "implicit "); |
760ac839 LW |
2356 | PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen); |
2357 | PerlIO_printf(Perl_debug_log, "\n"); | |
17c3b450 | 2358 | #endif /* DEBUGGING */ |
a687059c LW |
2359 | } |
2360 | ||
2361 | /* | |
a0d0e21e LW |
2362 | - regprop - printable representation of opcode |
2363 | */ | |
46fc3d4c | 2364 | void |
c277df42 | 2365 | regprop(SV *sv, regnode *o) |
a687059c | 2366 | { |
35ff7856 | 2367 | #ifdef DEBUGGING |
a0d0e21e LW |
2368 | register char *p = 0; |
2369 | ||
46fc3d4c | 2370 | sv_setpv(sv, ":"); |
11343788 | 2371 | switch (OP(o)) { |
a0d0e21e LW |
2372 | case BOL: |
2373 | p = "BOL"; | |
2374 | break; | |
2375 | case MBOL: | |
2376 | p = "MBOL"; | |
2377 | break; | |
2378 | case SBOL: | |
2379 | p = "SBOL"; | |
2380 | break; | |
2381 | case EOL: | |
2382 | p = "EOL"; | |
2383 | break; | |
2384 | case MEOL: | |
2385 | p = "MEOL"; | |
2386 | break; | |
2387 | case SEOL: | |
2388 | p = "SEOL"; | |
2389 | break; | |
2390 | case ANY: | |
2391 | p = "ANY"; | |
2392 | break; | |
2393 | case SANY: | |
2394 | p = "SANY"; | |
2395 | break; | |
2396 | case ANYOF: | |
2397 | p = "ANYOF"; | |
2398 | break; | |
2399 | case BRANCH: | |
2400 | p = "BRANCH"; | |
2401 | break; | |
bbce6d69 | 2402 | case EXACT: |
c277df42 | 2403 | sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 | 2404 | break; |
2405 | case EXACTF: | |
c277df42 | 2406 | sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 | 2407 | break; |
2408 | case EXACTFL: | |
c277df42 | 2409 | sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
a0d0e21e LW |
2410 | break; |
2411 | case NOTHING: | |
2412 | p = "NOTHING"; | |
2413 | break; | |
c277df42 IZ |
2414 | case TAIL: |
2415 | p = "TAIL"; | |
2416 | break; | |
a0d0e21e LW |
2417 | case BACK: |
2418 | p = "BACK"; | |
2419 | break; | |
2420 | case END: | |
2421 | p = "END"; | |
2422 | break; | |
a0d0e21e LW |
2423 | case BOUND: |
2424 | p = "BOUND"; | |
2425 | break; | |
bbce6d69 | 2426 | case BOUNDL: |
2427 | p = "BOUNDL"; | |
2428 | break; | |
a0d0e21e LW |
2429 | case NBOUND: |
2430 | p = "NBOUND"; | |
2431 | break; | |
bbce6d69 | 2432 | case NBOUNDL: |
2433 | p = "NBOUNDL"; | |
a0d0e21e LW |
2434 | break; |
2435 | case CURLY: | |
5dc0d613 | 2436 | sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e | 2437 | break; |
c277df42 IZ |
2438 | case CURLYM: |
2439 | #ifdef REGALIGN_STRUCT | |
2440 | sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); | |
2441 | #else | |
2442 | sv_catpvf(sv, "CURLYM {%d,%d}", ARG1(o), ARG2(o)); | |
2443 | #endif | |
2444 | break; | |
2445 | case CURLYN: | |
2446 | #ifdef REGALIGN_STRUCT | |
2447 | sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); | |
2448 | #else | |
2449 | sv_catpvf(sv, "CURLYN {%d,%d}", ARG1(o), ARG2(o)); | |
2450 | #endif | |
2451 | break; | |
a0d0e21e | 2452 | case CURLYX: |
5dc0d613 | 2453 | sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e LW |
2454 | break; |
2455 | case REF: | |
c277df42 | 2456 | sv_catpvf(sv, "REF%d", ARG(o)); |
a0d0e21e | 2457 | break; |
c8756f30 | 2458 | case REFF: |
c277df42 | 2459 | sv_catpvf(sv, "REFF%d", ARG(o)); |
c8756f30 AK |
2460 | break; |
2461 | case REFFL: | |
c277df42 | 2462 | sv_catpvf(sv, "REFFL%d", ARG(o)); |
c8756f30 | 2463 | break; |
a0d0e21e | 2464 | case OPEN: |
c277df42 | 2465 | sv_catpvf(sv, "OPEN%d", ARG(o)); |
a0d0e21e LW |
2466 | break; |
2467 | case CLOSE: | |
c277df42 | 2468 | sv_catpvf(sv, "CLOSE%d", ARG(o)); |
a0d0e21e LW |
2469 | p = NULL; |
2470 | break; | |
2471 | case STAR: | |
2472 | p = "STAR"; | |
2473 | break; | |
2474 | case PLUS: | |
2475 | p = "PLUS"; | |
2476 | break; | |
2477 | case MINMOD: | |
2478 | p = "MINMOD"; | |
2479 | break; | |
774d564b | 2480 | case GPOS: |
2481 | p = "GPOS"; | |
a0d0e21e LW |
2482 | break; |
2483 | case UNLESSM: | |
c277df42 IZ |
2484 | #ifdef REGALIGN_STRUCT |
2485 | sv_catpvf(sv, "UNLESSM[-%d]", o->flags); | |
2486 | #else | |
a0d0e21e | 2487 | p = "UNLESSM"; |
c277df42 | 2488 | #endif |
a0d0e21e LW |
2489 | break; |
2490 | case IFMATCH: | |
c277df42 IZ |
2491 | #ifdef REGALIGN_STRUCT |
2492 | sv_catpvf(sv, "IFMATCH[-%d]", o->flags); | |
2493 | #else | |
a0d0e21e | 2494 | p = "IFMATCH"; |
c277df42 | 2495 | #endif |
a0d0e21e LW |
2496 | break; |
2497 | case SUCCEED: | |
2498 | p = "SUCCEED"; | |
2499 | break; | |
2500 | case WHILEM: | |
2501 | p = "WHILEM"; | |
2502 | break; | |
bbce6d69 | 2503 | case DIGIT: |
2504 | p = "DIGIT"; | |
2505 | break; | |
2506 | case NDIGIT: | |
2507 | p = "NDIGIT"; | |
2508 | break; | |
2509 | case ALNUM: | |
2510 | p = "ALNUM"; | |
2511 | break; | |
2512 | case NALNUM: | |
2513 | p = "NALNUM"; | |
2514 | break; | |
2515 | case SPACE: | |
2516 | p = "SPACE"; | |
2517 | break; | |
2518 | case NSPACE: | |
2519 | p = "NSPACE"; | |
2520 | break; | |
2521 | case ALNUML: | |
2522 | p = "ALNUML"; | |
2523 | break; | |
2524 | case NALNUML: | |
2525 | p = "NALNUML"; | |
2526 | break; | |
2527 | case SPACEL: | |
2528 | p = "SPACEL"; | |
2529 | break; | |
2530 | case NSPACEL: | |
2531 | p = "NSPACEL"; | |
2532 | break; | |
c277df42 IZ |
2533 | case EVAL: |
2534 | p = "EVAL"; | |
2535 | break; | |
2536 | case LONGJMP: | |
2537 | p = "LONGJMP"; | |
2538 | break; | |
2539 | case BRANCHJ: | |
2540 | p = "BRANCHJ"; | |
2541 | break; | |
2542 | case IFTHEN: | |
2543 | p = "IFTHEN"; | |
2544 | break; | |
2545 | case GROUPP: | |
2546 | sv_catpvf(sv, "GROUPP%d", ARG(o)); | |
2547 | break; | |
2548 | case LOGICAL: | |
2549 | p = "LOGICAL"; | |
2550 | break; | |
2551 | case SUSPEND: | |
2552 | p = "SUSPEND"; | |
2553 | break; | |
2554 | case RENUM: | |
2555 | p = "RENUM"; | |
2556 | break; | |
2557 | case OPTIMIZED: | |
2558 | p = "OPTIMIZED"; | |
2559 | break; | |
a0d0e21e LW |
2560 | default: |
2561 | FAIL("corrupted regexp opcode"); | |
2562 | } | |
46fc3d4c | 2563 | if (p) |
2564 | sv_catpv(sv, p); | |
17c3b450 | 2565 | #endif /* DEBUGGING */ |
35ff7856 | 2566 | } |
a687059c | 2567 | |
2b69d0c2 | 2568 | void |
8ac85365 | 2569 | pregfree(struct regexp *r) |
a687059c | 2570 | { |
c277df42 | 2571 | if (!r || (--r->refcnt > 0)) |
a0d0e21e | 2572 | return; |
c277df42 | 2573 | if (r->precomp) |
a0d0e21e | 2574 | Safefree(r->precomp); |
c277df42 | 2575 | if (r->subbase) |
a0d0e21e | 2576 | Safefree(r->subbase); |
a193d654 GS |
2577 | if (r->substrs) { |
2578 | if (r->anchored_substr) | |
2579 | SvREFCNT_dec(r->anchored_substr); | |
2580 | if (r->float_substr) | |
2581 | SvREFCNT_dec(r->float_substr); | |
2779dcf1 | 2582 | Safefree(r->substrs); |
a193d654 | 2583 | } |
c277df42 IZ |
2584 | if (r->data) { |
2585 | int n = r->data->count; | |
2586 | while (--n >= 0) { | |
2587 | switch (r->data->what[n]) { | |
2588 | case 's': | |
2589 | SvREFCNT_dec((SV*)r->data->data[n]); | |
2590 | break; | |
2591 | case 'o': | |
2592 | op_free((OP_4tree*)r->data->data[n]); | |
2593 | break; | |
2594 | case 'n': | |
2595 | break; | |
2596 | default: | |
2597 | FAIL2("panic: regfree data code '%c'", r->data->what[n]); | |
2598 | } | |
2599 | } | |
2600 | Safefree(r->data->what); | |
2601 | Safefree(r->data); | |
a0d0e21e LW |
2602 | } |
2603 | Safefree(r->startp); | |
2604 | Safefree(r->endp); | |
2605 | Safefree(r); | |
a687059c | 2606 | } |
c277df42 IZ |
2607 | |
2608 | /* | |
2609 | - regnext - dig the "next" pointer out of a node | |
2610 | * | |
2611 | * [Note, when REGALIGN is defined there are two places in regmatch() | |
2612 | * that bypass this code for speed.] | |
2613 | */ | |
2614 | regnode * | |
2615 | regnext(register regnode *p) | |
2616 | { | |
2617 | register I32 offset; | |
2618 | ||
2619 | if (p == ®dummy) | |
2620 | return(NULL); | |
2621 | ||
2622 | offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p)); | |
2623 | if (offset == 0) | |
2624 | return(NULL); | |
2625 | ||
2626 | #ifdef REGALIGN | |
2627 | return(p+offset); | |
2628 | #else | |
2629 | if (OP(p) == BACK) | |
2630 | return(p-offset); | |
2631 | else | |
2632 | return(p+offset); | |
2633 | #endif | |
2634 | } | |
2635 | ||
01f988be | 2636 | STATIC void |
c277df42 | 2637 | re_croak2(const char* pat1,const char* pat2,...) |
c277df42 IZ |
2638 | { |
2639 | va_list args; | |
2640 | STRLEN l1 = strlen(pat1); | |
2641 | STRLEN l2 = strlen(pat2); | |
2642 | char buf[512]; | |
2643 | char *message; | |
2644 | ||
2645 | if (l1 > 510) | |
2646 | l1 = 510; | |
2647 | if (l1 + l2 > 510) | |
2648 | l2 = 510 - l1; | |
2649 | Copy(pat1, buf, l1 , char); | |
2650 | Copy(pat2, buf + l1, l2 , char); | |
2651 | buf[l1 + l2 + 1] = '\n'; | |
2652 | buf[l1 + l2 + 2] = '\0'; | |
c277df42 | 2653 | va_start(args, pat2); |
c277df42 IZ |
2654 | message = mess(buf, &args); |
2655 | va_end(args); | |
2656 | l1 = strlen(message); | |
2657 | if (l1 > 512) | |
2658 | l1 = 512; | |
2659 | Copy(message, buf, l1 , char); | |
2660 | buf[l1] = '\0'; /* Overwrite \n */ | |
2661 | croak("%s", buf); | |
2662 | } |