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