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); |
af819cba |
733 | DEBUG_r( |
734 | if (!PL_colorset) { |
735 | int i = 0; |
736 | char *s = PerlEnv_getenv("PERL_RE_COLORS"); |
737 | |
738 | if (s) { |
739 | PL_colors[0] = s = savepv(s); |
740 | while (++i < 6) { |
741 | s = strchr(s, '\t'); |
742 | if (s) { |
743 | *s = '\0'; |
744 | PL_colors[i] = ++s; |
745 | } |
746 | else |
747 | PL_colors[i] = ""; |
748 | } |
749 | } else { |
750 | while (i < 6) |
751 | PL_colors[i++] = ""; |
752 | } |
753 | PL_colorset = 1; |
754 | } |
755 | ); |
756 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n", |
757 | PL_colors[4],PL_colors[5],PL_colors[0], |
758 | xend - exp, PL_regprecomp, PL_colors[1])); |
3280af22 |
759 | PL_regflags = pm->op_pmflags; |
760 | PL_regsawback = 0; |
bbce6d69 |
761 | |
3280af22 |
762 | PL_regseen = 0; |
763 | PL_seen_zerolen = *exp == '^' ? -1 : 0; |
764 | PL_seen_evals = 0; |
765 | PL_extralen = 0; |
c277df42 |
766 | |
bbce6d69 |
767 | /* First pass: determine size, legality. */ |
3280af22 |
768 | PL_regcomp_parse = exp; |
769 | PL_regxend = xend; |
770 | PL_regnaughty = 0; |
771 | PL_regnpar = 1; |
772 | PL_regsize = 0L; |
773 | PL_regcode = &PL_regdummy; |
774 | regc((U8)MAGIC, (char*)PL_regcode); |
a0d0e21e |
775 | if (reg(0, &flags) == NULL) { |
3280af22 |
776 | Safefree(PL_regprecomp); |
777 | PL_regprecomp = Nullch; |
a0d0e21e |
778 | return(NULL); |
779 | } |
3280af22 |
780 | DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize)); |
c277df42 |
781 | |
c277df42 |
782 | /* Small enough for pointer-storage convention? |
783 | If extralen==0, this means that we will not need long jumps. */ |
3280af22 |
784 | if (PL_regsize >= 0x10000L && PL_extralen) |
785 | PL_regsize += PL_extralen; |
c277df42 |
786 | else |
3280af22 |
787 | PL_extralen = 0; |
a0d0e21e |
788 | |
bbce6d69 |
789 | /* Allocate space and initialize. */ |
3280af22 |
790 | Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode), |
c277df42 |
791 | char, regexp); |
a0d0e21e |
792 | if (r == NULL) |
793 | FAIL("regexp out of space"); |
c277df42 |
794 | r->refcnt = 1; |
bbce6d69 |
795 | r->prelen = xend - exp; |
3280af22 |
796 | r->precomp = PL_regprecomp; |
a0d0e21e |
797 | r->subbeg = r->subbase = NULL; |
3280af22 |
798 | r->nparens = PL_regnpar - 1; /* set early to validate backrefs */ |
799 | PL_regcomp_rx = r; |
bbce6d69 |
800 | |
801 | /* Second pass: emit code. */ |
3280af22 |
802 | PL_regcomp_parse = exp; |
803 | PL_regxend = xend; |
804 | PL_regnaughty = 0; |
805 | PL_regnpar = 1; |
806 | PL_regcode = r->program; |
2cd61cdb |
807 | /* Store the count of eval-groups for security checks: */ |
3280af22 |
808 | PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals); |
809 | regc((U8)MAGIC, (char*) PL_regcode++); |
c277df42 |
810 | r->data = 0; |
a0d0e21e |
811 | if (reg(0, &flags) == NULL) |
812 | return(NULL); |
813 | |
814 | /* Dig out information for optimizations. */ |
8782bef2 |
815 | r->reganch = pm->op_pmflags & PMf_COMPILETIME; |
3280af22 |
816 | pm->op_pmflags = PL_regflags; |
c277df42 |
817 | r->regstclass = NULL; |
3280af22 |
818 | r->naughty = PL_regnaughty >= 10; /* Probably an expensive pattern. */ |
c277df42 |
819 | scan = r->program + 1; /* First BRANCH. */ |
2779dcf1 |
820 | |
821 | /* XXXX To minimize changes to RE engine we always allocate |
822 | 3-units-long substrs field. */ |
823 | Newz(1004, r->substrs, 1, struct reg_substr_data); |
824 | |
c277df42 |
825 | if (OP(scan) != BRANCH) { /* Only one top-level choice. */ |
826 | scan_data_t data; |
827 | I32 fake; |
c5254dd6 |
828 | STRLEN longest_float_length, longest_fixed_length; |
a0d0e21e |
829 | |
c277df42 |
830 | StructCopy(&zero_scan_data, &data, scan_data_t); |
a0d0e21e |
831 | first = scan; |
c277df42 |
832 | /* Skip introductions and multiplicators >= 1. */ |
a0d0e21e |
833 | while ((OP(first) == OPEN && (sawopen = 1)) || |
834 | (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) || |
835 | (OP(first) == PLUS) || |
836 | (OP(first) == MINMOD) || |
837 | (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) { |
838 | if (OP(first) == PLUS) |
839 | sawplus = 1; |
840 | else |
841 | first += regarglen[(U8)OP(first)]; |
842 | first = NEXTOPER(first); |
a687059c |
843 | } |
844 | |
a0d0e21e |
845 | /* Starting-point info. */ |
846 | again: |
c277df42 |
847 | if (OP(first) == EXACT); /* Empty, get anchored substr later. */ |
a0d0e21e |
848 | else if (strchr(simple+2,OP(first))) |
849 | r->regstclass = first; |
bbce6d69 |
850 | else if (regkind[(U8)OP(first)] == BOUND || |
851 | regkind[(U8)OP(first)] == NBOUND) |
a0d0e21e |
852 | r->regstclass = first; |
853 | else if (regkind[(U8)OP(first)] == BOL) { |
c277df42 |
854 | r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL); |
a0d0e21e |
855 | first = NEXTOPER(first); |
774d564b |
856 | goto again; |
857 | } |
858 | else if (OP(first) == GPOS) { |
859 | r->reganch |= ROPT_ANCH_GPOS; |
860 | first = NEXTOPER(first); |
861 | goto again; |
a0d0e21e |
862 | } |
863 | else if ((OP(first) == STAR && |
864 | regkind[(U8)OP(NEXTOPER(first))] == ANY) && |
865 | !(r->reganch & ROPT_ANCH) ) |
866 | { |
867 | /* turn .* into ^.* with an implied $*=1 */ |
774d564b |
868 | r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT; |
a0d0e21e |
869 | first = NEXTOPER(first); |
774d564b |
870 | goto again; |
a0d0e21e |
871 | } |
3280af22 |
872 | if (sawplus && (!sawopen || !PL_regsawback)) |
a0d0e21e |
873 | r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */ |
874 | |
c277df42 |
875 | /* Scan is after the zeroth branch, first is atomic matcher. */ |
876 | DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n", |
877 | first - scan + 1)); |
a0d0e21e |
878 | /* |
879 | * If there's something expensive in the r.e., find the |
880 | * longest literal string that must appear and make it the |
881 | * regmust. Resolve ties in favor of later strings, since |
882 | * the regstart check works with the beginning of the r.e. |
883 | * and avoiding duplication strengthens checking. Not a |
884 | * strong reason, but sufficient in the absence of others. |
885 | * [Now we resolve ties in favor of the earlier string if |
c277df42 |
886 | * it happens that c_offset_min has been invalidated, since the |
a0d0e21e |
887 | * earlier string may buy us something the later one won't.] |
888 | */ |
a0d0e21e |
889 | minlen = 0; |
a687059c |
890 | |
c277df42 |
891 | data.longest_fixed = newSVpv("",0); |
892 | data.longest_float = newSVpv("",0); |
893 | data.last_found = newSVpv("",0); |
894 | data.longest = &(data.longest_fixed); |
895 | first = scan; |
896 | |
3280af22 |
897 | minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */ |
c277df42 |
898 | &data, SCF_DO_SUBSTR); |
3280af22 |
899 | if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed) |
c277df42 |
900 | && data.last_start_min == 0 && data.last_end > 0 |
3280af22 |
901 | && !PL_seen_zerolen |
902 | && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS))) |
c277df42 |
903 | r->reganch |= ROPT_CHECK_ALL; |
904 | scan_commit(&data); |
905 | SvREFCNT_dec(data.last_found); |
906 | |
c5254dd6 |
907 | longest_float_length = SvCUR(data.longest_float); |
908 | if (longest_float_length |
c277df42 |
909 | || (data.flags & SF_FL_BEFORE_EOL |
910 | && (!(data.flags & SF_FL_BEFORE_MEOL) |
3280af22 |
911 | || (PL_regflags & PMf_MULTILINE)))) { |
c277df42 |
912 | if (SvCUR(data.longest_fixed) |
913 | && data.offset_fixed == data.offset_float_min) |
914 | goto remove; /* Like in (a)+. */ |
915 | |
916 | r->float_substr = data.longest_float; |
917 | r->float_min_offset = data.offset_float_min; |
918 | r->float_max_offset = data.offset_float_max; |
2779dcf1 |
919 | fbm_compile(r->float_substr, 0); |
c277df42 |
920 | BmUSEFUL(r->float_substr) = 100; |
921 | if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
922 | && (!(data.flags & SF_FL_BEFORE_MEOL) |
3280af22 |
923 | || (PL_regflags & PMf_MULTILINE))) |
c277df42 |
924 | SvTAIL_on(r->float_substr); |
925 | } else { |
926 | remove: |
927 | r->float_substr = Nullsv; |
928 | SvREFCNT_dec(data.longest_float); |
c5254dd6 |
929 | longest_float_length = 0; |
a0d0e21e |
930 | } |
c277df42 |
931 | |
c5254dd6 |
932 | longest_fixed_length = SvCUR(data.longest_fixed); |
933 | if (longest_fixed_length |
c277df42 |
934 | || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
935 | && (!(data.flags & SF_FIX_BEFORE_MEOL) |
3280af22 |
936 | || (PL_regflags & PMf_MULTILINE)))) { |
c277df42 |
937 | r->anchored_substr = data.longest_fixed; |
938 | r->anchored_offset = data.offset_fixed; |
2779dcf1 |
939 | fbm_compile(r->anchored_substr, 0); |
c277df42 |
940 | BmUSEFUL(r->anchored_substr) = 100; |
941 | if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
942 | && (!(data.flags & SF_FIX_BEFORE_MEOL) |
3280af22 |
943 | || (PL_regflags & PMf_MULTILINE))) |
c277df42 |
944 | SvTAIL_on(r->anchored_substr); |
945 | } else { |
946 | r->anchored_substr = Nullsv; |
947 | SvREFCNT_dec(data.longest_fixed); |
c5254dd6 |
948 | longest_fixed_length = 0; |
a0d0e21e |
949 | } |
c277df42 |
950 | |
951 | /* A temporary algorithm prefers floated substr to fixed one to dig more info. */ |
c5254dd6 |
952 | if (longest_fixed_length > longest_float_length) { |
c277df42 |
953 | r->check_substr = r->anchored_substr; |
954 | r->check_offset_min = r->check_offset_max = r->anchored_offset; |
955 | if (r->reganch & ROPT_ANCH_SINGLE) |
956 | r->reganch |= ROPT_NOSCAN; |
957 | } else { |
958 | r->check_substr = r->float_substr; |
959 | r->check_offset_min = data.offset_float_min; |
960 | r->check_offset_max = data.offset_float_max; |
a0d0e21e |
961 | } |
c277df42 |
962 | } else { |
963 | /* Several toplevels. Best we can is to set minlen. */ |
964 | I32 fake; |
965 | |
966 | DEBUG_r(PerlIO_printf(Perl_debug_log, "\n")); |
967 | scan = r->program + 1; |
3280af22 |
968 | minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0); |
c277df42 |
969 | r->check_substr = r->anchored_substr = r->float_substr = Nullsv; |
a0d0e21e |
970 | } |
971 | |
a0d0e21e |
972 | r->minlen = minlen; |
3280af22 |
973 | if (PL_regseen & REG_SEEN_GPOS) |
c277df42 |
974 | r->reganch |= ROPT_GPOS_SEEN; |
3280af22 |
975 | if (PL_regseen & REG_SEEN_LOOKBEHIND) |
c277df42 |
976 | r->reganch |= ROPT_LOOKBEHIND_SEEN; |
3280af22 |
977 | if (PL_regseen & REG_SEEN_EVAL) |
ce862d02 |
978 | r->reganch |= ROPT_EVAL_SEEN; |
3280af22 |
979 | Newz(1002, r->startp, PL_regnpar, char*); |
980 | Newz(1002, r->endp, PL_regnpar, char*); |
a0d0e21e |
981 | DEBUG_r(regdump(r)); |
982 | return(r); |
a687059c |
983 | } |
984 | |
985 | /* |
986 | - reg - regular expression, i.e. main body or parenthesized thing |
987 | * |
988 | * Caller must absorb opening parenthesis. |
989 | * |
990 | * Combining parenthesis handling with the base level of regular expression |
991 | * is a trifle forced, but the need to tie the tails of the branches to what |
992 | * follows makes it hard to avoid. |
993 | */ |
76e3520e |
994 | STATIC regnode * |
8ac85365 |
995 | reg(I32 paren, I32 *flagp) |
c277df42 |
996 | /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */ |
a687059c |
997 | { |
5c0ca799 |
998 | dTHR; |
c277df42 |
999 | register regnode *ret; /* Will be the head of the group. */ |
1000 | register regnode *br; |
1001 | register regnode *lastbr; |
1002 | register regnode *ender = 0; |
a0d0e21e |
1003 | register I32 parno = 0; |
3280af22 |
1004 | I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0; |
c277df42 |
1005 | char c; |
a0d0e21e |
1006 | |
821b33a5 |
1007 | *flagp = 0; /* Tentatively. */ |
a0d0e21e |
1008 | |
1009 | /* Make an OPEN node, if parenthesized. */ |
1010 | if (paren) { |
3280af22 |
1011 | if (*PL_regcomp_parse == '?') { |
ca9dfc88 |
1012 | U16 posflags = 0, negflags = 0; |
1013 | U16 *flagsp = &posflags; |
1014 | |
3280af22 |
1015 | PL_regcomp_parse++; |
1016 | paren = *PL_regcomp_parse++; |
c277df42 |
1017 | ret = NULL; /* For look-ahead/behind. */ |
a0d0e21e |
1018 | switch (paren) { |
c277df42 |
1019 | case '<': |
3280af22 |
1020 | PL_regseen |= REG_SEEN_LOOKBEHIND; |
1021 | if (*PL_regcomp_parse == '!') |
c277df42 |
1022 | paren = ','; |
3280af22 |
1023 | if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!') |
c277df42 |
1024 | goto unknown; |
3280af22 |
1025 | PL_regcomp_parse++; |
a0d0e21e |
1026 | case '=': |
1027 | case '!': |
3280af22 |
1028 | PL_seen_zerolen++; |
c277df42 |
1029 | case ':': |
1030 | case '>': |
a0d0e21e |
1031 | break; |
1032 | case '$': |
1033 | case '@': |
c277df42 |
1034 | FAIL2("Sequence (?%c...) not implemented", (int)paren); |
a0d0e21e |
1035 | break; |
1036 | case '#': |
3280af22 |
1037 | while (*PL_regcomp_parse && *PL_regcomp_parse != ')') |
1038 | PL_regcomp_parse++; |
1039 | if (*PL_regcomp_parse != ')') |
c277df42 |
1040 | FAIL("Sequence (?#... not terminated"); |
a0d0e21e |
1041 | nextchar(); |
1042 | *flagp = TRYAGAIN; |
1043 | return NULL; |
c277df42 |
1044 | case '{': |
1045 | { |
1046 | dTHR; |
1047 | I32 count = 1, n = 0; |
1048 | char c; |
3280af22 |
1049 | char *s = PL_regcomp_parse; |
c277df42 |
1050 | SV *sv; |
1051 | OP_4tree *sop, *rop; |
1052 | |
3280af22 |
1053 | PL_seen_zerolen++; |
1054 | PL_regseen |= REG_SEEN_EVAL; |
1055 | while (count && (c = *PL_regcomp_parse)) { |
1056 | if (c == '\\' && PL_regcomp_parse[1]) |
1057 | PL_regcomp_parse++; |
c277df42 |
1058 | else if (c == '{') |
1059 | count++; |
1060 | else if (c == '}') |
1061 | count--; |
3280af22 |
1062 | PL_regcomp_parse++; |
c277df42 |
1063 | } |
3280af22 |
1064 | if (*PL_regcomp_parse != ')') |
c277df42 |
1065 | FAIL("Sequence (?{...}) not terminated or not {}-balanced"); |
1066 | if (!SIZE_ONLY) { |
1067 | AV *av; |
1068 | |
3280af22 |
1069 | if (PL_regcomp_parse - 1 - s) |
1070 | sv = newSVpv(s, PL_regcomp_parse - 1 - s); |
c277df42 |
1071 | else |
1072 | sv = newSVpv("", 0); |
1073 | |
1074 | rop = sv_compile_2op(sv, &sop, "re", &av); |
1075 | |
1076 | n = add_data(3, "nso"); |
3280af22 |
1077 | PL_regcomp_rx->data->data[n] = (void*)rop; |
1078 | PL_regcomp_rx->data->data[n+1] = (void*)av; |
1079 | PL_regcomp_rx->data->data[n+2] = (void*)sop; |
c277df42 |
1080 | SvREFCNT_dec(sv); |
cc6b7395 |
1081 | } else { /* First pass */ |
3280af22 |
1082 | if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &PL_compiling) |
2cd61cdb |
1083 | /* No compiled RE interpolated, has runtime |
1084 | components ===> unsafe. */ |
1085 | FAIL("Eval-group not allowed at runtime, use re 'eval'"); |
3280af22 |
1086 | if (PL_tainted) |
cc6b7395 |
1087 | FAIL("Eval-group in insecure regular expression"); |
c277df42 |
1088 | } |
1089 | |
1090 | nextchar(); |
c277df42 |
1091 | return reganode(EVAL, n); |
1092 | } |
1093 | case '(': |
1094 | { |
3280af22 |
1095 | if (PL_regcomp_parse[0] == '?') { |
1096 | if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!' |
1097 | || PL_regcomp_parse[1] == '<' |
1098 | || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */ |
c277df42 |
1099 | I32 flag; |
1100 | |
1101 | ret = reg_node(LOGICAL); |
1102 | regtail(ret, reg(1, &flag)); |
1103 | goto insert_if; |
1104 | } |
3280af22 |
1105 | } else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) { |
1106 | parno = atoi(PL_regcomp_parse++); |
c277df42 |
1107 | |
3280af22 |
1108 | while (isDIGIT(*PL_regcomp_parse)) |
1109 | PL_regcomp_parse++; |
c277df42 |
1110 | ret = reganode(GROUPP, parno); |
1111 | if ((c = *nextchar()) != ')') |
1112 | FAIL2("Switch (?(number%c not recognized", c); |
1113 | insert_if: |
1114 | regtail(ret, reganode(IFTHEN, 0)); |
1115 | br = regbranch(&flags, 1); |
1116 | if (br == NULL) |
1117 | br = reganode(LONGJMP, 0); |
1118 | else |
1119 | regtail(br, reganode(LONGJMP, 0)); |
1120 | c = *nextchar(); |
1121 | if (c == '|') { |
1122 | lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */ |
1123 | regbranch(&flags, 1); |
1124 | regtail(ret, lastbr); |
1125 | c = *nextchar(); |
1126 | } else |
1127 | lastbr = NULL; |
1128 | if (c != ')') |
1129 | FAIL("Switch (?(condition)... contains too many branches"); |
1130 | ender = reg_node(TAIL); |
1131 | regtail(br, ender); |
1132 | if (lastbr) { |
1133 | regtail(lastbr, ender); |
1134 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); |
1135 | } else |
1136 | regtail(ret, ender); |
1137 | return ret; |
1138 | } else { |
3280af22 |
1139 | FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse); |
c277df42 |
1140 | } |
1141 | } |
1b1626e4 |
1142 | case 0: |
c277df42 |
1143 | FAIL("Sequence (? incomplete"); |
1b1626e4 |
1144 | break; |
a0d0e21e |
1145 | default: |
3280af22 |
1146 | --PL_regcomp_parse; |
ca9dfc88 |
1147 | parse_flags: |
3280af22 |
1148 | while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) { |
1149 | if (*PL_regcomp_parse != 'o') |
1150 | pmflag(flagsp, *PL_regcomp_parse); |
1151 | ++PL_regcomp_parse; |
ca9dfc88 |
1152 | } |
3280af22 |
1153 | if (*PL_regcomp_parse == '-') { |
ca9dfc88 |
1154 | flagsp = &negflags; |
3280af22 |
1155 | ++PL_regcomp_parse; |
ca9dfc88 |
1156 | goto parse_flags; |
48c036b1 |
1157 | } |
3280af22 |
1158 | PL_regflags |= posflags; |
1159 | PL_regflags &= ~negflags; |
1160 | if (*PL_regcomp_parse == ':') { |
1161 | PL_regcomp_parse++; |
ca9dfc88 |
1162 | paren = ':'; |
1163 | break; |
1164 | } |
c277df42 |
1165 | unknown: |
3280af22 |
1166 | if (*PL_regcomp_parse != ')') |
1167 | FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse); |
a0d0e21e |
1168 | nextchar(); |
1169 | *flagp = TRYAGAIN; |
1170 | return NULL; |
1171 | } |
1172 | } |
1173 | else { |
3280af22 |
1174 | parno = PL_regnpar; |
1175 | PL_regnpar++; |
a0d0e21e |
1176 | ret = reganode(OPEN, parno); |
c277df42 |
1177 | open = 1; |
a0d0e21e |
1178 | } |
1179 | } else |
1180 | ret = NULL; |
1181 | |
1182 | /* Pick up the branches, linking them together. */ |
c277df42 |
1183 | br = regbranch(&flags, 1); |
a0d0e21e |
1184 | if (br == NULL) |
1185 | return(NULL); |
3280af22 |
1186 | if (*PL_regcomp_parse == '|') { |
1187 | if (!SIZE_ONLY && PL_extralen) { |
c277df42 |
1188 | reginsert(BRANCHJ, br); |
1189 | } else |
1190 | reginsert(BRANCH, br); |
1191 | have_branch = 1; |
1192 | if (SIZE_ONLY) |
3280af22 |
1193 | PL_extralen += 1; /* For BRANCHJ-BRANCH. */ |
c277df42 |
1194 | } else if (paren == ':') { |
1195 | *flagp |= flags&SIMPLE; |
1196 | } |
1197 | if (open) { /* Starts with OPEN. */ |
1198 | regtail(ret, br); /* OPEN -> first. */ |
1199 | } else if (paren != '?') /* Not Conditional */ |
a0d0e21e |
1200 | ret = br; |
821b33a5 |
1201 | if (flags&HASWIDTH) |
1202 | *flagp |= HASWIDTH; |
a0d0e21e |
1203 | *flagp |= flags&SPSTART; |
c277df42 |
1204 | lastbr = br; |
3280af22 |
1205 | while (*PL_regcomp_parse == '|') { |
1206 | if (!SIZE_ONLY && PL_extralen) { |
c277df42 |
1207 | ender = reganode(LONGJMP,0); |
1208 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */ |
1209 | } |
1210 | if (SIZE_ONLY) |
3280af22 |
1211 | PL_extralen += 2; /* Account for LONGJMP. */ |
a0d0e21e |
1212 | nextchar(); |
c277df42 |
1213 | br = regbranch(&flags, 0); |
a687059c |
1214 | if (br == NULL) |
a0d0e21e |
1215 | return(NULL); |
c277df42 |
1216 | regtail(lastbr, br); /* BRANCH -> BRANCH. */ |
1217 | lastbr = br; |
821b33a5 |
1218 | if (flags&HASWIDTH) |
1219 | *flagp |= HASWIDTH; |
a687059c |
1220 | *flagp |= flags&SPSTART; |
a0d0e21e |
1221 | } |
1222 | |
c277df42 |
1223 | if (have_branch || paren != ':') { |
1224 | /* Make a closing node, and hook it on the end. */ |
1225 | switch (paren) { |
1226 | case ':': |
1227 | ender = reg_node(TAIL); |
1228 | break; |
1229 | case 1: |
1230 | ender = reganode(CLOSE, parno); |
1231 | break; |
1232 | case '<': |
c277df42 |
1233 | case ',': |
1234 | case '=': |
1235 | case '!': |
c277df42 |
1236 | *flagp &= ~HASWIDTH; |
821b33a5 |
1237 | /* FALL THROUGH */ |
1238 | case '>': |
1239 | ender = reg_node(SUCCEED); |
c277df42 |
1240 | break; |
1241 | case 0: |
1242 | ender = reg_node(END); |
1243 | break; |
1244 | } |
1245 | regtail(lastbr, ender); |
a0d0e21e |
1246 | |
c277df42 |
1247 | if (have_branch) { |
1248 | /* Hook the tails of the branches to the closing node. */ |
1249 | for (br = ret; br != NULL; br = regnext(br)) { |
1250 | regoptail(br, ender); |
1251 | } |
1252 | } |
a0d0e21e |
1253 | } |
c277df42 |
1254 | |
1255 | { |
1256 | char *p; |
1257 | static char parens[] = "=!<,>"; |
1258 | |
1259 | if (paren && (p = strchr(parens, paren))) { |
1260 | int node = ((p - parens) % 2) ? UNLESSM : IFMATCH; |
1261 | int flag = (p - parens) > 1; |
1262 | |
1263 | if (paren == '>') |
1264 | node = SUSPEND, flag = 0; |
1265 | reginsert(node,ret); |
c277df42 |
1266 | ret->flags = flag; |
c277df42 |
1267 | regtail(ret, reg_node(TAIL)); |
1268 | } |
a0d0e21e |
1269 | } |
1270 | |
1271 | /* Check for proper termination. */ |
3280af22 |
1272 | if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) { |
a0d0e21e |
1273 | FAIL("unmatched () in regexp"); |
3280af22 |
1274 | } else if (!paren && PL_regcomp_parse < PL_regxend) { |
1275 | if (*PL_regcomp_parse == ')') { |
a0d0e21e |
1276 | FAIL("unmatched () in regexp"); |
1277 | } else |
1278 | FAIL("junk on end of regexp"); /* "Can't happen". */ |
1279 | /* NOTREACHED */ |
1280 | } |
c277df42 |
1281 | if (paren != 0) { |
3280af22 |
1282 | PL_regflags = oregflags; |
c277df42 |
1283 | } |
a687059c |
1284 | |
a0d0e21e |
1285 | return(ret); |
a687059c |
1286 | } |
1287 | |
1288 | /* |
1289 | - regbranch - one alternative of an | operator |
1290 | * |
1291 | * Implements the concatenation operator. |
1292 | */ |
76e3520e |
1293 | STATIC regnode * |
c277df42 |
1294 | regbranch(I32 *flagp, I32 first) |
a687059c |
1295 | { |
5c0ca799 |
1296 | dTHR; |
c277df42 |
1297 | register regnode *ret; |
1298 | register regnode *chain = NULL; |
1299 | register regnode *latest; |
1300 | I32 flags = 0, c = 0; |
a0d0e21e |
1301 | |
c277df42 |
1302 | if (first) |
1303 | ret = NULL; |
1304 | else { |
3280af22 |
1305 | if (!SIZE_ONLY && PL_extralen) |
c277df42 |
1306 | ret = reganode(BRANCHJ,0); |
1307 | else |
1308 | ret = reg_node(BRANCH); |
1309 | } |
1310 | |
1311 | if (!first && SIZE_ONLY) |
3280af22 |
1312 | PL_extralen += 1; /* BRANCHJ */ |
c277df42 |
1313 | |
1314 | *flagp = WORST; /* Tentatively. */ |
a0d0e21e |
1315 | |
3280af22 |
1316 | PL_regcomp_parse--; |
a0d0e21e |
1317 | nextchar(); |
3280af22 |
1318 | while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') { |
a0d0e21e |
1319 | flags &= ~TRYAGAIN; |
1320 | latest = regpiece(&flags); |
1321 | if (latest == NULL) { |
1322 | if (flags & TRYAGAIN) |
1323 | continue; |
1324 | return(NULL); |
c277df42 |
1325 | } else if (ret == NULL) |
1326 | ret = latest; |
a0d0e21e |
1327 | *flagp |= flags&HASWIDTH; |
c277df42 |
1328 | if (chain == NULL) /* First piece. */ |
a0d0e21e |
1329 | *flagp |= flags&SPSTART; |
1330 | else { |
3280af22 |
1331 | PL_regnaughty++; |
a0d0e21e |
1332 | regtail(chain, latest); |
a687059c |
1333 | } |
a0d0e21e |
1334 | chain = latest; |
c277df42 |
1335 | c++; |
1336 | } |
1337 | if (chain == NULL) { /* Loop ran zero times. */ |
1338 | chain = reg_node(NOTHING); |
1339 | if (ret == NULL) |
1340 | ret = chain; |
1341 | } |
1342 | if (c == 1) { |
1343 | *flagp |= flags&SIMPLE; |
a0d0e21e |
1344 | } |
a687059c |
1345 | |
a0d0e21e |
1346 | return(ret); |
a687059c |
1347 | } |
1348 | |
1349 | /* |
1350 | - regpiece - something followed by possible [*+?] |
1351 | * |
1352 | * Note that the branching code sequences used for ? and the general cases |
1353 | * of * and + are somewhat optimized: they use the same NOTHING node as |
1354 | * both the endmarker for their branch list and the body of the last branch. |
1355 | * It might seem that this node could be dispensed with entirely, but the |
1356 | * endmarker role is not redundant. |
1357 | */ |
76e3520e |
1358 | STATIC regnode * |
8ac85365 |
1359 | regpiece(I32 *flagp) |
a687059c |
1360 | { |
5c0ca799 |
1361 | dTHR; |
c277df42 |
1362 | register regnode *ret; |
a0d0e21e |
1363 | register char op; |
1364 | register char *next; |
1365 | I32 flags; |
3280af22 |
1366 | char *origparse = PL_regcomp_parse; |
a0d0e21e |
1367 | char *maxpos; |
1368 | I32 min; |
c277df42 |
1369 | I32 max = REG_INFTY; |
a0d0e21e |
1370 | |
1371 | ret = regatom(&flags); |
1372 | if (ret == NULL) { |
1373 | if (flags & TRYAGAIN) |
1374 | *flagp |= TRYAGAIN; |
1375 | return(NULL); |
1376 | } |
1377 | |
3280af22 |
1378 | op = *PL_regcomp_parse; |
a0d0e21e |
1379 | |
3280af22 |
1380 | if (op == '{' && regcurly(PL_regcomp_parse)) { |
1381 | next = PL_regcomp_parse + 1; |
a0d0e21e |
1382 | maxpos = Nullch; |
1383 | while (isDIGIT(*next) || *next == ',') { |
1384 | if (*next == ',') { |
1385 | if (maxpos) |
1386 | break; |
1387 | else |
1388 | maxpos = next; |
a687059c |
1389 | } |
a0d0e21e |
1390 | next++; |
1391 | } |
1392 | if (*next == '}') { /* got one */ |
1393 | if (!maxpos) |
1394 | maxpos = next; |
3280af22 |
1395 | PL_regcomp_parse++; |
1396 | min = atoi(PL_regcomp_parse); |
a0d0e21e |
1397 | if (*maxpos == ',') |
1398 | maxpos++; |
1399 | else |
3280af22 |
1400 | maxpos = PL_regcomp_parse; |
a0d0e21e |
1401 | max = atoi(maxpos); |
1402 | if (!max && *maxpos != '0') |
c277df42 |
1403 | max = REG_INFTY; /* meaning "infinity" */ |
1404 | else if (max >= REG_INFTY) |
1405 | FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1); |
3280af22 |
1406 | PL_regcomp_parse = next; |
a0d0e21e |
1407 | nextchar(); |
1408 | |
1409 | do_curly: |
1410 | if ((flags&SIMPLE)) { |
3280af22 |
1411 | PL_regnaughty += 2 + PL_regnaughty / 2; |
a0d0e21e |
1412 | reginsert(CURLY, ret); |
1413 | } |
1414 | else { |
3280af22 |
1415 | PL_regnaughty += 4 + PL_regnaughty; /* compound interest */ |
c277df42 |
1416 | regtail(ret, reg_node(WHILEM)); |
3280af22 |
1417 | if (!SIZE_ONLY && PL_extralen) { |
c277df42 |
1418 | reginsert(LONGJMP,ret); |
1419 | reginsert(NOTHING,ret); |
1420 | NEXT_OFF(ret) = 3; /* Go over LONGJMP. */ |
1421 | } |
a0d0e21e |
1422 | reginsert(CURLYX,ret); |
3280af22 |
1423 | if (!SIZE_ONLY && PL_extralen) |
c277df42 |
1424 | NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */ |
1425 | regtail(ret, reg_node(NOTHING)); |
1426 | if (SIZE_ONLY) |
3280af22 |
1427 | PL_extralen += 3; |
a0d0e21e |
1428 | } |
c277df42 |
1429 | ret->flags = 0; |
a0d0e21e |
1430 | |
1431 | if (min > 0) |
821b33a5 |
1432 | *flagp = WORST; |
1433 | if (max > 0) |
1434 | *flagp |= HASWIDTH; |
a0d0e21e |
1435 | if (max && max < min) |
c277df42 |
1436 | FAIL("Can't do {n,m} with n > m"); |
1437 | if (!SIZE_ONLY) { |
1438 | ARG1_SET(ret, min); |
1439 | ARG2_SET(ret, max); |
a687059c |
1440 | } |
a687059c |
1441 | |
a0d0e21e |
1442 | goto nest_check; |
a687059c |
1443 | } |
a0d0e21e |
1444 | } |
a687059c |
1445 | |
a0d0e21e |
1446 | if (!ISMULT1(op)) { |
1447 | *flagp = flags; |
a687059c |
1448 | return(ret); |
a0d0e21e |
1449 | } |
bb20fd44 |
1450 | |
c277df42 |
1451 | #if 0 /* Now runtime fix should be reliable. */ |
bb20fd44 |
1452 | if (!(flags&HASWIDTH) && op != '?') |
c277df42 |
1453 | FAIL("regexp *+ operand could be empty"); |
1454 | #endif |
bb20fd44 |
1455 | |
a0d0e21e |
1456 | nextchar(); |
1457 | |
821b33a5 |
1458 | *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH); |
a0d0e21e |
1459 | |
1460 | if (op == '*' && (flags&SIMPLE)) { |
1461 | reginsert(STAR, ret); |
c277df42 |
1462 | ret->flags = 0; |
3280af22 |
1463 | PL_regnaughty += 4; |
a0d0e21e |
1464 | } |
1465 | else if (op == '*') { |
1466 | min = 0; |
1467 | goto do_curly; |
1468 | } else if (op == '+' && (flags&SIMPLE)) { |
1469 | reginsert(PLUS, ret); |
c277df42 |
1470 | ret->flags = 0; |
3280af22 |
1471 | PL_regnaughty += 3; |
a0d0e21e |
1472 | } |
1473 | else if (op == '+') { |
1474 | min = 1; |
1475 | goto do_curly; |
1476 | } else if (op == '?') { |
1477 | min = 0; max = 1; |
1478 | goto do_curly; |
1479 | } |
1480 | nest_check: |
3280af22 |
1481 | if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) { |
a0d0e21e |
1482 | warn("%.*s matches null string many times", |
3280af22 |
1483 | PL_regcomp_parse - origparse, origparse); |
a0d0e21e |
1484 | } |
1485 | |
3280af22 |
1486 | if (*PL_regcomp_parse == '?') { |
a0d0e21e |
1487 | nextchar(); |
1488 | reginsert(MINMOD, ret); |
c277df42 |
1489 | regtail(ret, ret + NODE_STEP_REGNODE); |
a0d0e21e |
1490 | } |
3280af22 |
1491 | if (ISMULT2(PL_regcomp_parse)) |
a0d0e21e |
1492 | FAIL("nested *?+ in regexp"); |
1493 | |
1494 | return(ret); |
a687059c |
1495 | } |
1496 | |
1497 | /* |
1498 | - regatom - the lowest level |
1499 | * |
1500 | * Optimization: gobbles an entire sequence of ordinary characters so that |
1501 | * it can turn them into a single node, which is smaller to store and |
1502 | * faster to run. Backslashed characters are exceptions, each becoming a |
1503 | * separate node; the code is simpler that way and it's not worth fixing. |
1504 | * |
1505 | * [Yes, it is worth fixing, some scripts can run twice the speed.] |
1506 | */ |
76e3520e |
1507 | STATIC regnode * |
8ac85365 |
1508 | regatom(I32 *flagp) |
a687059c |
1509 | { |
5c0ca799 |
1510 | dTHR; |
c277df42 |
1511 | register regnode *ret = 0; |
a0d0e21e |
1512 | I32 flags; |
1513 | |
1514 | *flagp = WORST; /* Tentatively. */ |
1515 | |
1516 | tryagain: |
3280af22 |
1517 | switch (*PL_regcomp_parse) { |
a0d0e21e |
1518 | case '^': |
3280af22 |
1519 | PL_seen_zerolen++; |
a0d0e21e |
1520 | nextchar(); |
3280af22 |
1521 | if (PL_regflags & PMf_MULTILINE) |
c277df42 |
1522 | ret = reg_node(MBOL); |
3280af22 |
1523 | else if (PL_regflags & PMf_SINGLELINE) |
c277df42 |
1524 | ret = reg_node(SBOL); |
a0d0e21e |
1525 | else |
c277df42 |
1526 | ret = reg_node(BOL); |
a0d0e21e |
1527 | break; |
1528 | case '$': |
3280af22 |
1529 | if (PL_regcomp_parse[1]) |
1530 | PL_seen_zerolen++; |
a0d0e21e |
1531 | nextchar(); |
3280af22 |
1532 | if (PL_regflags & PMf_MULTILINE) |
c277df42 |
1533 | ret = reg_node(MEOL); |
3280af22 |
1534 | else if (PL_regflags & PMf_SINGLELINE) |
c277df42 |
1535 | ret = reg_node(SEOL); |
a0d0e21e |
1536 | else |
c277df42 |
1537 | ret = reg_node(EOL); |
a0d0e21e |
1538 | break; |
1539 | case '.': |
1540 | nextchar(); |
3280af22 |
1541 | if (PL_regflags & PMf_SINGLELINE) |
c277df42 |
1542 | ret = reg_node(SANY); |
a0d0e21e |
1543 | else |
c277df42 |
1544 | ret = reg_node(ANY); |
3280af22 |
1545 | PL_regnaughty++; |
a0d0e21e |
1546 | *flagp |= HASWIDTH|SIMPLE; |
1547 | break; |
1548 | case '[': |
3280af22 |
1549 | PL_regcomp_parse++; |
a0d0e21e |
1550 | ret = regclass(); |
1551 | *flagp |= HASWIDTH|SIMPLE; |
1552 | break; |
1553 | case '(': |
1554 | nextchar(); |
1555 | ret = reg(1, &flags); |
1556 | if (ret == NULL) { |
1557 | if (flags & TRYAGAIN) |
1558 | goto tryagain; |
1559 | return(NULL); |
1560 | } |
c277df42 |
1561 | *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE); |
a0d0e21e |
1562 | break; |
1563 | case '|': |
1564 | case ')': |
1565 | if (flags & TRYAGAIN) { |
1566 | *flagp |= TRYAGAIN; |
1567 | return NULL; |
1568 | } |
3280af22 |
1569 | FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse); |
a0d0e21e |
1570 | /* Supposed to be caught earlier. */ |
1571 | break; |
85afd4ae |
1572 | case '{': |
3280af22 |
1573 | if (!regcurly(PL_regcomp_parse)) { |
1574 | PL_regcomp_parse++; |
85afd4ae |
1575 | goto defchar; |
1576 | } |
1577 | /* FALL THROUGH */ |
a0d0e21e |
1578 | case '?': |
1579 | case '+': |
1580 | case '*': |
3115e423 |
1581 | FAIL("?+*{} follows nothing in regexp"); |
a0d0e21e |
1582 | break; |
1583 | case '\\': |
3280af22 |
1584 | switch (*++PL_regcomp_parse) { |
a0d0e21e |
1585 | case 'A': |
3280af22 |
1586 | PL_seen_zerolen++; |
c277df42 |
1587 | ret = reg_node(SBOL); |
a0d0e21e |
1588 | *flagp |= SIMPLE; |
1589 | nextchar(); |
1590 | break; |
1591 | case 'G': |
c277df42 |
1592 | ret = reg_node(GPOS); |
3280af22 |
1593 | PL_regseen |= REG_SEEN_GPOS; |
a0d0e21e |
1594 | *flagp |= SIMPLE; |
1595 | nextchar(); |
1596 | break; |
1597 | case 'Z': |
c277df42 |
1598 | ret = reg_node(SEOL); |
a0d0e21e |
1599 | *flagp |= SIMPLE; |
1600 | nextchar(); |
1601 | break; |
b85d18e9 |
1602 | case 'z': |
1603 | ret = reg_node(EOS); |
1604 | *flagp |= SIMPLE; |
3280af22 |
1605 | PL_seen_zerolen++; /* Do not optimize RE away */ |
b85d18e9 |
1606 | nextchar(); |
1607 | break; |
a0d0e21e |
1608 | case 'w': |
3280af22 |
1609 | ret = reg_node((PL_regflags & PMf_LOCALE) ? ALNUML : ALNUM); |
a0d0e21e |
1610 | *flagp |= HASWIDTH|SIMPLE; |
1611 | nextchar(); |
1612 | break; |
1613 | case 'W': |
3280af22 |
1614 | ret = reg_node((PL_regflags & PMf_LOCALE) ? NALNUML : NALNUM); |
a0d0e21e |
1615 | *flagp |= HASWIDTH|SIMPLE; |
1616 | nextchar(); |
1617 | break; |
1618 | case 'b': |
3280af22 |
1619 | PL_seen_zerolen++; |
1620 | ret = reg_node((PL_regflags & PMf_LOCALE) ? BOUNDL : BOUND); |
a0d0e21e |
1621 | *flagp |= SIMPLE; |
1622 | nextchar(); |
1623 | break; |
1624 | case 'B': |
3280af22 |
1625 | PL_seen_zerolen++; |
1626 | ret = reg_node((PL_regflags & PMf_LOCALE) ? NBOUNDL : NBOUND); |
a0d0e21e |
1627 | *flagp |= SIMPLE; |
1628 | nextchar(); |
1629 | break; |
1630 | case 's': |
3280af22 |
1631 | ret = reg_node((PL_regflags & PMf_LOCALE) ? SPACEL : SPACE); |
a0d0e21e |
1632 | *flagp |= HASWIDTH|SIMPLE; |
1633 | nextchar(); |
1634 | break; |
1635 | case 'S': |
3280af22 |
1636 | ret = reg_node((PL_regflags & PMf_LOCALE) ? NSPACEL : NSPACE); |
a0d0e21e |
1637 | *flagp |= HASWIDTH|SIMPLE; |
1638 | nextchar(); |
1639 | break; |
1640 | case 'd': |
c277df42 |
1641 | ret = reg_node(DIGIT); |
a0d0e21e |
1642 | *flagp |= HASWIDTH|SIMPLE; |
1643 | nextchar(); |
1644 | break; |
1645 | case 'D': |
c277df42 |
1646 | ret = reg_node(NDIGIT); |
a0d0e21e |
1647 | *flagp |= HASWIDTH|SIMPLE; |
1648 | nextchar(); |
1649 | break; |
1650 | case 'n': |
1651 | case 'r': |
1652 | case 't': |
1653 | case 'f': |
1654 | case 'e': |
1655 | case 'a': |
1656 | case 'x': |
1657 | case 'c': |
1658 | case '0': |
1659 | goto defchar; |
1660 | case '1': case '2': case '3': case '4': |
1661 | case '5': case '6': case '7': case '8': case '9': |
1662 | { |
3280af22 |
1663 | I32 num = atoi(PL_regcomp_parse); |
a0d0e21e |
1664 | |
3280af22 |
1665 | if (num > 9 && num >= PL_regnpar) |
a0d0e21e |
1666 | goto defchar; |
1667 | else { |
3280af22 |
1668 | if (!SIZE_ONLY && num > PL_regcomp_rx->nparens) |
ef64f398 |
1669 | FAIL("reference to nonexistent group"); |
3280af22 |
1670 | PL_regsawback = 1; |
1671 | ret = reganode((PL_regflags & PMf_FOLD) |
1672 | ? ((PL_regflags & PMf_LOCALE) ? REFFL : REFF) |
c8756f30 |
1673 | : REF, num); |
a0d0e21e |
1674 | *flagp |= HASWIDTH; |
3280af22 |
1675 | while (isDIGIT(*PL_regcomp_parse)) |
1676 | PL_regcomp_parse++; |
1677 | PL_regcomp_parse--; |
a0d0e21e |
1678 | nextchar(); |
1679 | } |
1680 | } |
1681 | break; |
1682 | case '\0': |
3280af22 |
1683 | if (PL_regcomp_parse >= PL_regxend) |
a0d0e21e |
1684 | FAIL("trailing \\ in regexp"); |
1685 | /* FALL THROUGH */ |
1686 | default: |
1687 | goto defchar; |
1688 | } |
1689 | break; |
4633a7c4 |
1690 | |
1691 | case '#': |
3280af22 |
1692 | if (PL_regflags & PMf_EXTENDED) { |
1693 | while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++; |
1694 | if (PL_regcomp_parse < PL_regxend) |
4633a7c4 |
1695 | goto tryagain; |
1696 | } |
1697 | /* FALL THROUGH */ |
1698 | |
a0d0e21e |
1699 | default: { |
1700 | register I32 len; |
c277df42 |
1701 | register U8 ender; |
a0d0e21e |
1702 | register char *p; |
c277df42 |
1703 | char *oldp, *s; |
a0d0e21e |
1704 | I32 numlen; |
1705 | |
3280af22 |
1706 | PL_regcomp_parse++; |
a0d0e21e |
1707 | |
1708 | defchar: |
3280af22 |
1709 | ret = reg_node((PL_regflags & PMf_FOLD) |
1710 | ? ((PL_regflags & PMf_LOCALE) ? EXACTFL : EXACTF) |
bbce6d69 |
1711 | : EXACT); |
161b471a |
1712 | s = (char *) OPERAND(ret); |
c277df42 |
1713 | regc(0, s++); /* save spot for len */ |
3280af22 |
1714 | for (len = 0, p = PL_regcomp_parse - 1; |
1715 | len < 127 && p < PL_regxend; |
a0d0e21e |
1716 | len++) |
1717 | { |
1718 | oldp = p; |
5b5a24f7 |
1719 | |
3280af22 |
1720 | if (PL_regflags & PMf_EXTENDED) |
1721 | p = regwhite(p, PL_regxend); |
a0d0e21e |
1722 | switch (*p) { |
1723 | case '^': |
1724 | case '$': |
1725 | case '.': |
1726 | case '[': |
1727 | case '(': |
1728 | case ')': |
1729 | case '|': |
1730 | goto loopdone; |
1731 | case '\\': |
1732 | switch (*++p) { |
1733 | case 'A': |
1734 | case 'G': |
1735 | case 'Z': |
b85d18e9 |
1736 | case 'z': |
a0d0e21e |
1737 | case 'w': |
1738 | case 'W': |
1739 | case 'b': |
1740 | case 'B': |
1741 | case 's': |
1742 | case 'S': |
1743 | case 'd': |
1744 | case 'D': |
1745 | --p; |
1746 | goto loopdone; |
1747 | case 'n': |
1748 | ender = '\n'; |
1749 | p++; |
a687059c |
1750 | break; |
a0d0e21e |
1751 | case 'r': |
1752 | ender = '\r'; |
1753 | p++; |
a687059c |
1754 | break; |
a0d0e21e |
1755 | case 't': |
1756 | ender = '\t'; |
1757 | p++; |
a687059c |
1758 | break; |
a0d0e21e |
1759 | case 'f': |
1760 | ender = '\f'; |
1761 | p++; |
a687059c |
1762 | break; |
a0d0e21e |
1763 | case 'e': |
1764 | ender = '\033'; |
1765 | p++; |
a687059c |
1766 | break; |
a0d0e21e |
1767 | case 'a': |
1768 | ender = '\007'; |
1769 | p++; |
a687059c |
1770 | break; |
a0d0e21e |
1771 | case 'x': |
1772 | ender = scan_hex(++p, 2, &numlen); |
1773 | p += numlen; |
a687059c |
1774 | break; |
a0d0e21e |
1775 | case 'c': |
1776 | p++; |
bbce6d69 |
1777 | ender = UCHARAT(p++); |
1778 | ender = toCTRL(ender); |
a687059c |
1779 | break; |
a0d0e21e |
1780 | case '0': case '1': case '2': case '3':case '4': |
1781 | case '5': case '6': case '7': case '8':case '9': |
1782 | if (*p == '0' || |
3280af22 |
1783 | (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) { |
a0d0e21e |
1784 | ender = scan_oct(p, 3, &numlen); |
1785 | p += numlen; |
1786 | } |
1787 | else { |
1788 | --p; |
1789 | goto loopdone; |
a687059c |
1790 | } |
1791 | break; |
a0d0e21e |
1792 | case '\0': |
3280af22 |
1793 | if (p >= PL_regxend) |
a687059c |
1794 | FAIL("trailing \\ in regexp"); |
1795 | /* FALL THROUGH */ |
a0d0e21e |
1796 | default: |
1797 | ender = *p++; |
1798 | break; |
1799 | } |
1800 | break; |
a687059c |
1801 | default: |
a0d0e21e |
1802 | ender = *p++; |
1803 | break; |
a687059c |
1804 | } |
3280af22 |
1805 | if (PL_regflags & PMf_EXTENDED) |
1806 | p = regwhite(p, PL_regxend); |
a0d0e21e |
1807 | if (ISMULT2(p)) { /* Back off on ?+*. */ |
1808 | if (len) |
1809 | p = oldp; |
1810 | else { |
1811 | len++; |
c277df42 |
1812 | regc(ender, s++); |
a0d0e21e |
1813 | } |
1814 | break; |
a687059c |
1815 | } |
c277df42 |
1816 | regc(ender, s++); |
a0d0e21e |
1817 | } |
1818 | loopdone: |
3280af22 |
1819 | PL_regcomp_parse = p - 1; |
a0d0e21e |
1820 | nextchar(); |
1821 | if (len < 0) |
1822 | FAIL("internal disaster in regexp"); |
1823 | if (len > 0) |
1824 | *flagp |= HASWIDTH; |
1825 | if (len == 1) |
1826 | *flagp |= SIMPLE; |
c277df42 |
1827 | if (!SIZE_ONLY) |
a0d0e21e |
1828 | *OPERAND(ret) = len; |
c277df42 |
1829 | regc('\0', s++); |
1830 | if (SIZE_ONLY) { |
3280af22 |
1831 | PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
c277df42 |
1832 | } else { |
3280af22 |
1833 | PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
c277df42 |
1834 | } |
a687059c |
1835 | } |
a0d0e21e |
1836 | break; |
1837 | } |
a687059c |
1838 | |
a0d0e21e |
1839 | return(ret); |
a687059c |
1840 | } |
1841 | |
873ef191 |
1842 | STATIC char * |
8ac85365 |
1843 | regwhite(char *p, char *e) |
5b5a24f7 |
1844 | { |
1845 | while (p < e) { |
1846 | if (isSPACE(*p)) |
1847 | ++p; |
1848 | else if (*p == '#') { |
1849 | do { |
1850 | p++; |
1851 | } while (p < e && *p != '\n'); |
1852 | } |
1853 | else |
1854 | break; |
1855 | } |
1856 | return p; |
1857 | } |
1858 | |
76e3520e |
1859 | STATIC regnode * |
8ac85365 |
1860 | regclass(void) |
a687059c |
1861 | { |
5c0ca799 |
1862 | dTHR; |
c277df42 |
1863 | register char *opnd, *s; |
8ac85365 |
1864 | register I32 Class; |
a0d0e21e |
1865 | register I32 lastclass = 1234; |
1866 | register I32 range = 0; |
c277df42 |
1867 | register regnode *ret; |
a0d0e21e |
1868 | register I32 def; |
1869 | I32 numlen; |
1870 | |
3280af22 |
1871 | s = opnd = (char *) OPERAND(PL_regcode); |
c277df42 |
1872 | ret = reg_node(ANYOF); |
8ac85365 |
1873 | for (Class = 0; Class < 33; Class++) |
c277df42 |
1874 | regc(0, s++); |
3280af22 |
1875 | if (*PL_regcomp_parse == '^') { /* Complement of range. */ |
1876 | PL_regnaughty++; |
1877 | PL_regcomp_parse++; |
c277df42 |
1878 | if (!SIZE_ONLY) |
bbce6d69 |
1879 | *opnd |= ANYOF_INVERT; |
1880 | } |
c277df42 |
1881 | if (!SIZE_ONLY) { |
3280af22 |
1882 | PL_regcode += ANY_SKIP; |
1883 | if (PL_regflags & PMf_FOLD) |
bbce6d69 |
1884 | *opnd |= ANYOF_FOLD; |
3280af22 |
1885 | if (PL_regflags & PMf_LOCALE) |
bbce6d69 |
1886 | *opnd |= ANYOF_LOCALE; |
c277df42 |
1887 | } else { |
3280af22 |
1888 | PL_regsize += ANY_SKIP; |
a0d0e21e |
1889 | } |
3280af22 |
1890 | if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-') |
a0d0e21e |
1891 | goto skipcond; /* allow 1st char to be ] or - */ |
3280af22 |
1892 | while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') { |
a0d0e21e |
1893 | skipcond: |
3280af22 |
1894 | Class = UCHARAT(PL_regcomp_parse++); |
1895 | if (Class == '[' && PL_regcomp_parse + 1 < PL_regxend && |
4599a1de |
1896 | /* I smell either [: or [= or [. -- POSIX has been here, right? */ |
3280af22 |
1897 | (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) { |
1898 | char posixccc = *PL_regcomp_parse; |
1899 | char* posixccs = PL_regcomp_parse++; |
4599a1de |
1900 | |
3280af22 |
1901 | while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc) |
1902 | PL_regcomp_parse++; |
1903 | if (PL_regcomp_parse == PL_regxend) |
4599a1de |
1904 | /* Grandfather lone [:, [=, [. */ |
3280af22 |
1905 | PL_regcomp_parse = posixccs; |
4599a1de |
1906 | else { |
3280af22 |
1907 | PL_regcomp_parse++; /* skip over the posixccc */ |
1908 | if (*PL_regcomp_parse == ']') { |
4599a1de |
1909 | /* Not Implemented Yet. |
1910 | * (POSIX Extended Character Classes, that is) |
1911 | * The text between e.g. [: and :] would start |
77d41b28 |
1912 | * at posixccs + 1 and stop at regcomp_parse - 2. */ |
3280af22 |
1913 | if (PL_dowarn && !SIZE_ONLY) |
4599a1de |
1914 | warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc); |
3280af22 |
1915 | PL_regcomp_parse++; /* skip over the ending ] */ |
4599a1de |
1916 | } |
1917 | } |
1918 | } |
8ac85365 |
1919 | if (Class == '\\') { |
3280af22 |
1920 | Class = UCHARAT(PL_regcomp_parse++); |
8ac85365 |
1921 | switch (Class) { |
a0d0e21e |
1922 | case 'w': |
ae5c130c |
1923 | if (!SIZE_ONLY) { |
3280af22 |
1924 | if (PL_regflags & PMf_LOCALE) |
bbce6d69 |
1925 | *opnd |= ANYOF_ALNUML; |
ae5c130c |
1926 | else { |
1927 | for (Class = 0; Class < 256; Class++) |
1928 | if (isALNUM(Class)) |
1929 | ANYOF_SET(opnd, Class); |
1930 | } |
bbce6d69 |
1931 | } |
a0d0e21e |
1932 | lastclass = 1234; |
1933 | continue; |
1934 | case 'W': |
ae5c130c |
1935 | if (!SIZE_ONLY) { |
3280af22 |
1936 | if (PL_regflags & PMf_LOCALE) |
bbce6d69 |
1937 | *opnd |= ANYOF_NALNUML; |
ae5c130c |
1938 | else { |
1939 | for (Class = 0; Class < 256; Class++) |
1940 | if (!isALNUM(Class)) |
1941 | ANYOF_SET(opnd, Class); |
1942 | } |
bbce6d69 |
1943 | } |
a0d0e21e |
1944 | lastclass = 1234; |
1945 | continue; |
1946 | case 's': |
ae5c130c |
1947 | if (!SIZE_ONLY) { |
3280af22 |
1948 | if (PL_regflags & PMf_LOCALE) |
bbce6d69 |
1949 | *opnd |= ANYOF_SPACEL; |
ae5c130c |
1950 | else { |
1951 | for (Class = 0; Class < 256; Class++) |
1952 | if (isSPACE(Class)) |
1953 | ANYOF_SET(opnd, Class); |
1954 | } |
bbce6d69 |
1955 | } |
a0d0e21e |
1956 | lastclass = 1234; |
1957 | continue; |
1958 | case 'S': |
ae5c130c |
1959 | if (!SIZE_ONLY) { |
3280af22 |
1960 | if (PL_regflags & PMf_LOCALE) |
bbce6d69 |
1961 | *opnd |= ANYOF_NSPACEL; |
ae5c130c |
1962 | else { |
1963 | for (Class = 0; Class < 256; Class++) |
1964 | if (!isSPACE(Class)) |
1965 | ANYOF_SET(opnd, Class); |
1966 | } |
bbce6d69 |
1967 | } |
a0d0e21e |
1968 | lastclass = 1234; |
1969 | continue; |
1970 | case 'd': |
ae5c130c |
1971 | if (!SIZE_ONLY) { |
1972 | for (Class = '0'; Class <= '9'; Class++) |
1973 | ANYOF_SET(opnd, Class); |
1974 | } |
a0d0e21e |
1975 | lastclass = 1234; |
1976 | continue; |
1977 | case 'D': |
ae5c130c |
1978 | if (!SIZE_ONLY) { |
1979 | for (Class = 0; Class < '0'; Class++) |
1980 | ANYOF_SET(opnd, Class); |
1981 | for (Class = '9' + 1; Class < 256; Class++) |
1982 | ANYOF_SET(opnd, Class); |
1983 | } |
a0d0e21e |
1984 | lastclass = 1234; |
1985 | continue; |
1986 | case 'n': |
8ac85365 |
1987 | Class = '\n'; |
a0d0e21e |
1988 | break; |
1989 | case 'r': |
8ac85365 |
1990 | Class = '\r'; |
a0d0e21e |
1991 | break; |
1992 | case 't': |
8ac85365 |
1993 | Class = '\t'; |
a0d0e21e |
1994 | break; |
1995 | case 'f': |
8ac85365 |
1996 | Class = '\f'; |
a0d0e21e |
1997 | break; |
1998 | case 'b': |
8ac85365 |
1999 | Class = '\b'; |
a0d0e21e |
2000 | break; |
2001 | case 'e': |
8ac85365 |
2002 | Class = '\033'; |
a0d0e21e |
2003 | break; |
2004 | case 'a': |
8ac85365 |
2005 | Class = '\007'; |
a0d0e21e |
2006 | break; |
2007 | case 'x': |
3280af22 |
2008 | Class = scan_hex(PL_regcomp_parse, 2, &numlen); |
2009 | PL_regcomp_parse += numlen; |
a0d0e21e |
2010 | break; |
2011 | case 'c': |
3280af22 |
2012 | Class = UCHARAT(PL_regcomp_parse++); |
8ac85365 |
2013 | Class = toCTRL(Class); |
a0d0e21e |
2014 | break; |
2015 | case '0': case '1': case '2': case '3': case '4': |
2016 | case '5': case '6': case '7': case '8': case '9': |
3280af22 |
2017 | Class = scan_oct(--PL_regcomp_parse, 3, &numlen); |
2018 | PL_regcomp_parse += numlen; |
a0d0e21e |
2019 | break; |
2020 | } |
2021 | } |
2022 | if (range) { |
8ac85365 |
2023 | if (lastclass > Class) |
a0d0e21e |
2024 | FAIL("invalid [] range in regexp"); |
2025 | range = 0; |
2026 | } |
2027 | else { |
8ac85365 |
2028 | lastclass = Class; |
3280af22 |
2029 | if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend && |
2030 | PL_regcomp_parse[1] != ']') { |
2031 | PL_regcomp_parse++; |
a0d0e21e |
2032 | range = 1; |
2033 | continue; /* do it next time */ |
2034 | } |
a687059c |
2035 | } |
ae5c130c |
2036 | if (!SIZE_ONLY) { |
2037 | for ( ; lastclass <= Class; lastclass++) |
2038 | ANYOF_SET(opnd, lastclass); |
2039 | } |
8ac85365 |
2040 | lastclass = Class; |
a0d0e21e |
2041 | } |
3280af22 |
2042 | if (*PL_regcomp_parse != ']') |
a0d0e21e |
2043 | FAIL("unmatched [] in regexp"); |
2044 | nextchar(); |
ae5c130c |
2045 | /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */ |
2046 | if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) { |
2047 | for (Class = 0; Class < 256; ++Class) { |
2048 | if (ANYOF_TEST(opnd, Class)) { |
2049 | I32 cf = fold[Class]; |
2050 | ANYOF_SET(opnd, cf); |
2051 | } |
2052 | } |
2053 | *opnd &= ~ANYOF_FOLD; |
2054 | } |
2055 | /* optimize inverted simple patterns (e.g. [^a-z]) */ |
2056 | if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) { |
2057 | for (Class = 0; Class < 32; ++Class) |
2058 | opnd[1 + Class] ^= 0xFF; |
2059 | *opnd = 0; |
2060 | } |
a0d0e21e |
2061 | return ret; |
2062 | } |
2063 | |
76e3520e |
2064 | STATIC char* |
8ac85365 |
2065 | nextchar(void) |
a0d0e21e |
2066 | { |
5c0ca799 |
2067 | dTHR; |
3280af22 |
2068 | char* retval = PL_regcomp_parse++; |
a0d0e21e |
2069 | |
4633a7c4 |
2070 | for (;;) { |
3280af22 |
2071 | if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' && |
2072 | PL_regcomp_parse[2] == '#') { |
2073 | while (*PL_regcomp_parse && *PL_regcomp_parse != ')') |
2074 | PL_regcomp_parse++; |
2075 | PL_regcomp_parse++; |
4633a7c4 |
2076 | continue; |
2077 | } |
3280af22 |
2078 | if (PL_regflags & PMf_EXTENDED) { |
2079 | if (isSPACE(*PL_regcomp_parse)) { |
2080 | PL_regcomp_parse++; |
748a9306 |
2081 | continue; |
2082 | } |
3280af22 |
2083 | else if (*PL_regcomp_parse == '#') { |
2084 | while (*PL_regcomp_parse && *PL_regcomp_parse != '\n') |
2085 | PL_regcomp_parse++; |
2086 | PL_regcomp_parse++; |
748a9306 |
2087 | continue; |
2088 | } |
748a9306 |
2089 | } |
4633a7c4 |
2090 | return retval; |
a0d0e21e |
2091 | } |
a687059c |
2092 | } |
2093 | |
2094 | /* |
c277df42 |
2095 | - reg_node - emit a node |
a0d0e21e |
2096 | */ |
76e3520e |
2097 | STATIC regnode * /* Location. */ |
c277df42 |
2098 | reg_node(U8 op) |
a687059c |
2099 | { |
5c0ca799 |
2100 | dTHR; |
c277df42 |
2101 | register regnode *ret; |
2102 | register regnode *ptr; |
a687059c |
2103 | |
3280af22 |
2104 | ret = PL_regcode; |
c277df42 |
2105 | if (SIZE_ONLY) { |
6b88bc9c |
2106 | SIZE_ALIGN(PL_regsize); |
3280af22 |
2107 | PL_regsize += 1; |
a0d0e21e |
2108 | return(ret); |
2109 | } |
a687059c |
2110 | |
c277df42 |
2111 | NODE_ALIGN_FILL(ret); |
a0d0e21e |
2112 | ptr = ret; |
c277df42 |
2113 | FILL_ADVANCE_NODE(ptr, op); |
3280af22 |
2114 | PL_regcode = ptr; |
a687059c |
2115 | |
a0d0e21e |
2116 | return(ret); |
a687059c |
2117 | } |
2118 | |
2119 | /* |
a0d0e21e |
2120 | - reganode - emit a node with an argument |
2121 | */ |
76e3520e |
2122 | STATIC regnode * /* Location. */ |
c277df42 |
2123 | reganode(U8 op, U32 arg) |
fe14fcc3 |
2124 | { |
5c0ca799 |
2125 | dTHR; |
c277df42 |
2126 | register regnode *ret; |
2127 | register regnode *ptr; |
fe14fcc3 |
2128 | |
3280af22 |
2129 | ret = PL_regcode; |
c277df42 |
2130 | if (SIZE_ONLY) { |
6b88bc9c |
2131 | SIZE_ALIGN(PL_regsize); |
3280af22 |
2132 | PL_regsize += 2; |
a0d0e21e |
2133 | return(ret); |
2134 | } |
fe14fcc3 |
2135 | |
c277df42 |
2136 | NODE_ALIGN_FILL(ret); |
a0d0e21e |
2137 | ptr = ret; |
c277df42 |
2138 | FILL_ADVANCE_NODE_ARG(ptr, op, arg); |
3280af22 |
2139 | PL_regcode = ptr; |
fe14fcc3 |
2140 | |
a0d0e21e |
2141 | return(ret); |
fe14fcc3 |
2142 | } |
2143 | |
2144 | /* |
a0d0e21e |
2145 | - regc - emit (if appropriate) a byte of code |
2146 | */ |
76e3520e |
2147 | STATIC void |
c277df42 |
2148 | regc(U8 b, char* s) |
a687059c |
2149 | { |
5c0ca799 |
2150 | dTHR; |
c277df42 |
2151 | if (!SIZE_ONLY) |
2152 | *s = b; |
a687059c |
2153 | } |
2154 | |
2155 | /* |
a0d0e21e |
2156 | - reginsert - insert an operator in front of already-emitted operand |
2157 | * |
2158 | * Means relocating the operand. |
2159 | */ |
76e3520e |
2160 | STATIC void |
c277df42 |
2161 | reginsert(U8 op, regnode *opnd) |
a687059c |
2162 | { |
5c0ca799 |
2163 | dTHR; |
c277df42 |
2164 | register regnode *src; |
2165 | register regnode *dst; |
2166 | register regnode *place; |
2167 | register int offset = regarglen[(U8)op]; |
2168 | |
2169 | /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */ |
2170 | |
2171 | if (SIZE_ONLY) { |
3280af22 |
2172 | PL_regsize += NODE_STEP_REGNODE + offset; |
a0d0e21e |
2173 | return; |
2174 | } |
a687059c |
2175 | |
3280af22 |
2176 | src = PL_regcode; |
2177 | PL_regcode += NODE_STEP_REGNODE + offset; |
2178 | dst = PL_regcode; |
a0d0e21e |
2179 | while (src > opnd) |
c277df42 |
2180 | StructCopy(--src, --dst, regnode); |
a0d0e21e |
2181 | |
2182 | place = opnd; /* Op node, where operand used to be. */ |
c277df42 |
2183 | src = NEXTOPER(place); |
2184 | FILL_ADVANCE_NODE(place, op); |
2185 | Zero(src, offset, regnode); |
a687059c |
2186 | } |
2187 | |
2188 | /* |
c277df42 |
2189 | - regtail - set the next-pointer at the end of a node chain of p to val. |
a0d0e21e |
2190 | */ |
76e3520e |
2191 | STATIC void |
c277df42 |
2192 | regtail(regnode *p, regnode *val) |
a687059c |
2193 | { |
5c0ca799 |
2194 | dTHR; |
c277df42 |
2195 | register regnode *scan; |
2196 | register regnode *temp; |
a0d0e21e |
2197 | register I32 offset; |
2198 | |
c277df42 |
2199 | if (SIZE_ONLY) |
a0d0e21e |
2200 | return; |
2201 | |
2202 | /* Find last node. */ |
2203 | scan = p; |
2204 | for (;;) { |
2205 | temp = regnext(scan); |
2206 | if (temp == NULL) |
2207 | break; |
2208 | scan = temp; |
2209 | } |
a687059c |
2210 | |
c277df42 |
2211 | if (reg_off_by_arg[OP(scan)]) { |
2212 | ARG_SET(scan, val - scan); |
2213 | } else { |
2214 | NEXT_OFF(scan) = val - scan; |
2215 | } |
a687059c |
2216 | } |
2217 | |
2218 | /* |
a0d0e21e |
2219 | - regoptail - regtail on operand of first argument; nop if operandless |
2220 | */ |
76e3520e |
2221 | STATIC void |
c277df42 |
2222 | regoptail(regnode *p, regnode *val) |
a687059c |
2223 | { |
5c0ca799 |
2224 | dTHR; |
a0d0e21e |
2225 | /* "Operandless" and "op != BRANCH" are synonymous in practice. */ |
c277df42 |
2226 | if (p == NULL || SIZE_ONLY) |
2227 | return; |
2228 | if (regkind[(U8)OP(p)] == BRANCH) { |
2229 | regtail(NEXTOPER(p), val); |
2230 | } else if ( regkind[(U8)OP(p)] == BRANCHJ) { |
2231 | regtail(NEXTOPER(NEXTOPER(p)), val); |
2232 | } else |
a0d0e21e |
2233 | return; |
a687059c |
2234 | } |
2235 | |
2236 | /* |
2237 | - regcurly - a little FSA that accepts {\d+,?\d*} |
2238 | */ |
79072805 |
2239 | STATIC I32 |
8ac85365 |
2240 | regcurly(register char *s) |
a687059c |
2241 | { |
2242 | if (*s++ != '{') |
2243 | return FALSE; |
f0fcb552 |
2244 | if (!isDIGIT(*s)) |
a687059c |
2245 | return FALSE; |
f0fcb552 |
2246 | while (isDIGIT(*s)) |
a687059c |
2247 | s++; |
2248 | if (*s == ',') |
2249 | s++; |
f0fcb552 |
2250 | while (isDIGIT(*s)) |
a687059c |
2251 | s++; |
2252 | if (*s != '}') |
2253 | return FALSE; |
2254 | return TRUE; |
2255 | } |
2256 | |
a687059c |
2257 | |
76e3520e |
2258 | STATIC regnode * |
c277df42 |
2259 | dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l) |
2260 | { |
35ff7856 |
2261 | #ifdef DEBUGGING |
c277df42 |
2262 | register char op = EXACT; /* Arbitrary non-END op. */ |
2263 | register regnode *next, *onode; |
2264 | |
2265 | while (op != END && (!last || node < last)) { |
2266 | /* While that wasn't END last time... */ |
2267 | |
2268 | NODE_ALIGN(node); |
2269 | op = OP(node); |
2270 | if (op == CLOSE) |
2271 | l--; |
2272 | next = regnext(node); |
2273 | /* Where, what. */ |
2274 | if (OP(node) == OPTIMIZED) |
2275 | goto after_print; |
2276 | regprop(sv, node); |
54dc92de |
2277 | PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start, |
c277df42 |
2278 | 2*l + 1, "", SvPVX(sv)); |
2279 | if (next == NULL) /* Next ptr. */ |
2280 | PerlIO_printf(Perl_debug_log, "(0)"); |
2281 | else |
2282 | PerlIO_printf(Perl_debug_log, "(%d)", next - start); |
2283 | (void)PerlIO_putc(Perl_debug_log, '\n'); |
2284 | after_print: |
2285 | if (regkind[(U8)op] == BRANCHJ) { |
2286 | register regnode *nnode = (OP(next) == LONGJMP |
2287 | ? regnext(next) |
2288 | : next); |
2289 | if (last && nnode > last) |
2290 | nnode = last; |
2291 | node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1); |
2292 | } else if (regkind[(U8)op] == BRANCH) { |
2293 | node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1); |
2294 | } else if ( op == CURLY) { /* `next' might be very big: optimizer */ |
2295 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, |
2296 | NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1); |
2297 | } else if (regkind[(U8)op] == CURLY && op != CURLYX) { |
2298 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, |
2299 | next, sv, l + 1); |
2300 | } else if ( op == PLUS || op == STAR) { |
2301 | node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1); |
2302 | } else if (op == ANYOF) { |
2303 | node = NEXTOPER(node); |
2304 | node += ANY_SKIP; |
2305 | } else if (regkind[(U8)op] == EXACT) { |
2306 | /* Literal string, where present. */ |
2307 | node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
2308 | node = NEXTOPER(node); |
2309 | } else { |
2310 | node = NEXTOPER(node); |
2311 | node += regarglen[(U8)op]; |
2312 | } |
2313 | if (op == CURLYX || op == OPEN) |
2314 | l++; |
2315 | else if (op == WHILEM) |
2316 | l--; |
2317 | } |
17c3b450 |
2318 | #endif /* DEBUGGING */ |
c277df42 |
2319 | return node; |
2320 | } |
2321 | |
a687059c |
2322 | /* |
fd181c75 |
2323 | - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form |
a687059c |
2324 | */ |
2325 | void |
8ac85365 |
2326 | regdump(regexp *r) |
a687059c |
2327 | { |
35ff7856 |
2328 | #ifdef DEBUGGING |
5c0ca799 |
2329 | dTHR; |
46fc3d4c |
2330 | SV *sv = sv_newmortal(); |
a687059c |
2331 | |
c277df42 |
2332 | (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0); |
a0d0e21e |
2333 | |
2334 | /* Header fields of interest. */ |
c277df42 |
2335 | if (r->anchored_substr) |
2336 | PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ", |
3280af22 |
2337 | PL_colors[0], |
c277df42 |
2338 | SvPVX(r->anchored_substr), |
3280af22 |
2339 | PL_colors[1], |
c277df42 |
2340 | SvTAIL(r->anchored_substr) ? "$" : "", |
2341 | r->anchored_offset); |
2342 | if (r->float_substr) |
2343 | PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ", |
3280af22 |
2344 | PL_colors[0], |
c277df42 |
2345 | SvPVX(r->float_substr), |
3280af22 |
2346 | PL_colors[1], |
c277df42 |
2347 | SvTAIL(r->float_substr) ? "$" : "", |
2348 | r->float_min_offset, r->float_max_offset); |
2349 | if (r->check_substr) |
2350 | PerlIO_printf(Perl_debug_log, |
2351 | r->check_substr == r->float_substr |
2352 | ? "(checking floating" : "(checking anchored"); |
2353 | if (r->reganch & ROPT_NOSCAN) |
2354 | PerlIO_printf(Perl_debug_log, " noscan"); |
2355 | if (r->reganch & ROPT_CHECK_ALL) |
2356 | PerlIO_printf(Perl_debug_log, " isall"); |
2357 | if (r->check_substr) |
2358 | PerlIO_printf(Perl_debug_log, ") "); |
2359 | |
46fc3d4c |
2360 | if (r->regstclass) { |
2361 | regprop(sv, r->regstclass); |
2362 | PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv)); |
2363 | } |
774d564b |
2364 | if (r->reganch & ROPT_ANCH) { |
2365 | PerlIO_printf(Perl_debug_log, "anchored"); |
2366 | if (r->reganch & ROPT_ANCH_BOL) |
2367 | PerlIO_printf(Perl_debug_log, "(BOL)"); |
c277df42 |
2368 | if (r->reganch & ROPT_ANCH_MBOL) |
2369 | PerlIO_printf(Perl_debug_log, "(MBOL)"); |
774d564b |
2370 | if (r->reganch & ROPT_ANCH_GPOS) |
2371 | PerlIO_printf(Perl_debug_log, "(GPOS)"); |
2372 | PerlIO_putc(Perl_debug_log, ' '); |
2373 | } |
c277df42 |
2374 | if (r->reganch & ROPT_GPOS_SEEN) |
2375 | PerlIO_printf(Perl_debug_log, "GPOS "); |
a0d0e21e |
2376 | if (r->reganch & ROPT_SKIP) |
760ac839 |
2377 | PerlIO_printf(Perl_debug_log, "plus "); |
a0d0e21e |
2378 | if (r->reganch & ROPT_IMPLICIT) |
760ac839 |
2379 | PerlIO_printf(Perl_debug_log, "implicit "); |
760ac839 |
2380 | PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen); |
ce862d02 |
2381 | if (r->reganch & ROPT_EVAL_SEEN) |
2382 | PerlIO_printf(Perl_debug_log, "with eval "); |
760ac839 |
2383 | PerlIO_printf(Perl_debug_log, "\n"); |
17c3b450 |
2384 | #endif /* DEBUGGING */ |
a687059c |
2385 | } |
2386 | |
2387 | /* |
a0d0e21e |
2388 | - regprop - printable representation of opcode |
2389 | */ |
46fc3d4c |
2390 | void |
c277df42 |
2391 | regprop(SV *sv, regnode *o) |
a687059c |
2392 | { |
35ff7856 |
2393 | #ifdef DEBUGGING |
5c0ca799 |
2394 | dTHR; |
a0d0e21e |
2395 | register char *p = 0; |
2396 | |
54dc92de |
2397 | sv_setpvn(sv, "", 0); |
11343788 |
2398 | switch (OP(o)) { |
a0d0e21e |
2399 | case BOL: |
2400 | p = "BOL"; |
2401 | break; |
2402 | case MBOL: |
2403 | p = "MBOL"; |
2404 | break; |
2405 | case SBOL: |
2406 | p = "SBOL"; |
2407 | break; |
2408 | case EOL: |
2409 | p = "EOL"; |
2410 | break; |
b85d18e9 |
2411 | case EOS: |
2412 | p = "EOS"; |
2413 | break; |
a0d0e21e |
2414 | case MEOL: |
2415 | p = "MEOL"; |
2416 | break; |
2417 | case SEOL: |
2418 | p = "SEOL"; |
2419 | break; |
2420 | case ANY: |
2421 | p = "ANY"; |
2422 | break; |
2423 | case SANY: |
2424 | p = "SANY"; |
2425 | break; |
2426 | case ANYOF: |
2427 | p = "ANYOF"; |
2428 | break; |
2429 | case BRANCH: |
2430 | p = "BRANCH"; |
2431 | break; |
bbce6d69 |
2432 | case EXACT: |
3280af22 |
2433 | sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]); |
bbce6d69 |
2434 | break; |
2435 | case EXACTF: |
3280af22 |
2436 | sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]); |
bbce6d69 |
2437 | break; |
2438 | case EXACTFL: |
3280af22 |
2439 | sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]); |
a0d0e21e |
2440 | break; |
2441 | case NOTHING: |
2442 | p = "NOTHING"; |
2443 | break; |
c277df42 |
2444 | case TAIL: |
2445 | p = "TAIL"; |
2446 | break; |
a0d0e21e |
2447 | case BACK: |
2448 | p = "BACK"; |
2449 | break; |
2450 | case END: |
2451 | p = "END"; |
2452 | break; |
a0d0e21e |
2453 | case BOUND: |
2454 | p = "BOUND"; |
2455 | break; |
bbce6d69 |
2456 | case BOUNDL: |
2457 | p = "BOUNDL"; |
2458 | break; |
a0d0e21e |
2459 | case NBOUND: |
2460 | p = "NBOUND"; |
2461 | break; |
bbce6d69 |
2462 | case NBOUNDL: |
2463 | p = "NBOUNDL"; |
a0d0e21e |
2464 | break; |
2465 | case CURLY: |
5dc0d613 |
2466 | sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e |
2467 | break; |
c277df42 |
2468 | case CURLYM: |
c277df42 |
2469 | sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); |
c277df42 |
2470 | break; |
2471 | case CURLYN: |
c277df42 |
2472 | sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); |
c277df42 |
2473 | break; |
a0d0e21e |
2474 | case CURLYX: |
5dc0d613 |
2475 | sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e |
2476 | break; |
2477 | case REF: |
c277df42 |
2478 | sv_catpvf(sv, "REF%d", ARG(o)); |
a0d0e21e |
2479 | break; |
c8756f30 |
2480 | case REFF: |
c277df42 |
2481 | sv_catpvf(sv, "REFF%d", ARG(o)); |
c8756f30 |
2482 | break; |
2483 | case REFFL: |
c277df42 |
2484 | sv_catpvf(sv, "REFFL%d", ARG(o)); |
c8756f30 |
2485 | break; |
a0d0e21e |
2486 | case OPEN: |
c277df42 |
2487 | sv_catpvf(sv, "OPEN%d", ARG(o)); |
a0d0e21e |
2488 | break; |
2489 | case CLOSE: |
c277df42 |
2490 | sv_catpvf(sv, "CLOSE%d", ARG(o)); |
a0d0e21e |
2491 | p = NULL; |
2492 | break; |
2493 | case STAR: |
2494 | p = "STAR"; |
2495 | break; |
2496 | case PLUS: |
2497 | p = "PLUS"; |
2498 | break; |
2499 | case MINMOD: |
2500 | p = "MINMOD"; |
2501 | break; |
774d564b |
2502 | case GPOS: |
2503 | p = "GPOS"; |
a0d0e21e |
2504 | break; |
2505 | case UNLESSM: |
c277df42 |
2506 | sv_catpvf(sv, "UNLESSM[-%d]", o->flags); |
a0d0e21e |
2507 | break; |
2508 | case IFMATCH: |
c277df42 |
2509 | sv_catpvf(sv, "IFMATCH[-%d]", o->flags); |
a0d0e21e |
2510 | break; |
2511 | case SUCCEED: |
2512 | p = "SUCCEED"; |
2513 | break; |
2514 | case WHILEM: |
2515 | p = "WHILEM"; |
2516 | break; |
bbce6d69 |
2517 | case DIGIT: |
2518 | p = "DIGIT"; |
2519 | break; |
2520 | case NDIGIT: |
2521 | p = "NDIGIT"; |
2522 | break; |
2523 | case ALNUM: |
2524 | p = "ALNUM"; |
2525 | break; |
2526 | case NALNUM: |
2527 | p = "NALNUM"; |
2528 | break; |
2529 | case SPACE: |
2530 | p = "SPACE"; |
2531 | break; |
2532 | case NSPACE: |
2533 | p = "NSPACE"; |
2534 | break; |
2535 | case ALNUML: |
2536 | p = "ALNUML"; |
2537 | break; |
2538 | case NALNUML: |
2539 | p = "NALNUML"; |
2540 | break; |
2541 | case SPACEL: |
2542 | p = "SPACEL"; |
2543 | break; |
2544 | case NSPACEL: |
2545 | p = "NSPACEL"; |
2546 | break; |
c277df42 |
2547 | case EVAL: |
2548 | p = "EVAL"; |
2549 | break; |
2550 | case LONGJMP: |
2551 | p = "LONGJMP"; |
2552 | break; |
2553 | case BRANCHJ: |
2554 | p = "BRANCHJ"; |
2555 | break; |
2556 | case IFTHEN: |
2557 | p = "IFTHEN"; |
2558 | break; |
2559 | case GROUPP: |
2560 | sv_catpvf(sv, "GROUPP%d", ARG(o)); |
2561 | break; |
2562 | case LOGICAL: |
2563 | p = "LOGICAL"; |
2564 | break; |
2565 | case SUSPEND: |
2566 | p = "SUSPEND"; |
2567 | break; |
2568 | case RENUM: |
2569 | p = "RENUM"; |
2570 | break; |
2571 | case OPTIMIZED: |
2572 | p = "OPTIMIZED"; |
2573 | break; |
a0d0e21e |
2574 | default: |
2575 | FAIL("corrupted regexp opcode"); |
2576 | } |
46fc3d4c |
2577 | if (p) |
2578 | sv_catpv(sv, p); |
17c3b450 |
2579 | #endif /* DEBUGGING */ |
35ff7856 |
2580 | } |
a687059c |
2581 | |
2b69d0c2 |
2582 | void |
8ac85365 |
2583 | pregfree(struct regexp *r) |
a687059c |
2584 | { |
5c0ca799 |
2585 | dTHR; |
c277df42 |
2586 | if (!r || (--r->refcnt > 0)) |
a0d0e21e |
2587 | return; |
c277df42 |
2588 | if (r->precomp) |
a0d0e21e |
2589 | Safefree(r->precomp); |
c277df42 |
2590 | if (r->subbase) |
a0d0e21e |
2591 | Safefree(r->subbase); |
a193d654 |
2592 | if (r->substrs) { |
2593 | if (r->anchored_substr) |
2594 | SvREFCNT_dec(r->anchored_substr); |
2595 | if (r->float_substr) |
2596 | SvREFCNT_dec(r->float_substr); |
2779dcf1 |
2597 | Safefree(r->substrs); |
a193d654 |
2598 | } |
c277df42 |
2599 | if (r->data) { |
2600 | int n = r->data->count; |
2601 | while (--n >= 0) { |
2602 | switch (r->data->what[n]) { |
2603 | case 's': |
2604 | SvREFCNT_dec((SV*)r->data->data[n]); |
2605 | break; |
2606 | case 'o': |
2607 | op_free((OP_4tree*)r->data->data[n]); |
2608 | break; |
2609 | case 'n': |
2610 | break; |
2611 | default: |
2612 | FAIL2("panic: regfree data code '%c'", r->data->what[n]); |
2613 | } |
2614 | } |
2615 | Safefree(r->data->what); |
2616 | Safefree(r->data); |
a0d0e21e |
2617 | } |
2618 | Safefree(r->startp); |
2619 | Safefree(r->endp); |
2620 | Safefree(r); |
a687059c |
2621 | } |
c277df42 |
2622 | |
2623 | /* |
2624 | - regnext - dig the "next" pointer out of a node |
2625 | * |
2626 | * [Note, when REGALIGN is defined there are two places in regmatch() |
2627 | * that bypass this code for speed.] |
2628 | */ |
2629 | regnode * |
2630 | regnext(register regnode *p) |
2631 | { |
5c0ca799 |
2632 | dTHR; |
c277df42 |
2633 | register I32 offset; |
2634 | |
3280af22 |
2635 | if (p == &PL_regdummy) |
c277df42 |
2636 | return(NULL); |
2637 | |
2638 | offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p)); |
2639 | if (offset == 0) |
2640 | return(NULL); |
2641 | |
c277df42 |
2642 | return(p+offset); |
c277df42 |
2643 | } |
2644 | |
01f988be |
2645 | STATIC void |
c277df42 |
2646 | re_croak2(const char* pat1,const char* pat2,...) |
c277df42 |
2647 | { |
2648 | va_list args; |
2649 | STRLEN l1 = strlen(pat1); |
2650 | STRLEN l2 = strlen(pat2); |
2651 | char buf[512]; |
2652 | char *message; |
2653 | |
2654 | if (l1 > 510) |
2655 | l1 = 510; |
2656 | if (l1 + l2 > 510) |
2657 | l2 = 510 - l1; |
2658 | Copy(pat1, buf, l1 , char); |
2659 | Copy(pat2, buf + l1, l2 , char); |
3b818b81 |
2660 | buf[l1 + l2] = '\n'; |
2661 | buf[l1 + l2 + 1] = '\0'; |
c277df42 |
2662 | va_start(args, pat2); |
c277df42 |
2663 | message = mess(buf, &args); |
2664 | va_end(args); |
2665 | l1 = strlen(message); |
2666 | if (l1 > 512) |
2667 | l1 = 512; |
2668 | Copy(message, buf, l1 , char); |
2669 | buf[l1] = '\0'; /* Overwrite \n */ |
2670 | croak("%s", buf); |
2671 | } |