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