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