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