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