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