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