3 * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4 * 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Definitions etc. for regexp(3) routines.
14 * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
15 * not the System V one.
17 #ifndef PLUGGABLE_RE_EXTENSION
18 /* we don't want to include this stuff if we are inside of
19 an external regex engine based on the core one - like re 'debug'*/
23 typedef SSize_t regnode_offset;
32 /* this ensures that on alignment sensitive platforms
33 * this struct is aligned on 32 bit boundaries */
48 union regnode_head head;
51 typedef struct regnode regnode;
53 struct reg_substr_data;
60 struct reg_substr_datum {
61 SSize_t min_offset; /* min pos (in chars) that substr must appear */
62 SSize_t max_offset; /* max pos (in chars) that substr must appear */
63 SV *substr; /* non-utf8 variant */
64 SV *utf8_substr; /* utf8 variant */
65 SSize_t end_shift; /* how many fixed chars must end the string */
67 struct reg_substr_data {
68 U8 check_ix; /* index into data[] of check substr */
69 struct reg_substr_datum data[3]; /* Actual array */
73 # define SV_SAVED_COPY SV *saved_copy; /* If non-NULL, SV which is COW from original */
75 # define SV_SAVED_COPY
78 /* offsets within a string of a particular /(.)/ capture
79 * if you change this by adding new non-temporary fields
80 * then be sure to update Perl_rxres_save() in pp_ctl.c */
81 typedef struct regexp_paren_pair {
85 /* 'start_tmp' records a new opening position before the matching end
86 * has been found, so that the old start and end values are still
88 * "abc" =~ /(.(?{print "[$1]"}))+/
90 * This field is not part of the API. */
94 # if defined(PERL_IN_REGCOMP_ANY) || defined(PERL_IN_UTF8_C)
95 # define _invlist_union(a, b, output) _invlist_union_maybe_complement_2nd(a, b, FALSE, output)
96 # define _invlist_intersection(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, FALSE, output)
98 /* Subtracting b from a leaves in a everything that was there that isn't in b,
99 * that is the intersection of a with b's complement */
100 # define _invlist_subtract(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, TRUE, output)
103 /* record the position of a (?{...}) within a pattern */
105 struct reg_code_block {
112 /* array of reg_code_block's plus header info */
114 struct reg_code_blocks {
115 int refcnt; /* we may be pointed to from a regex and from the savestack */
116 int count; /* how many code block slots currently in use */
117 int size; /* how many slots allocated in code_block[] */
118 struct reg_code_block *cb; /* array of reg_code_block's */
123 =for apidoc AyT||regexp
124 The regexp/REGEXP struct, see L<perlreapi> for further documentation
125 on the individual fields. The struct is ordered so that the most
126 commonly used fields are placed at the start.
128 Any patch that adds items to this struct will need to include
129 changes to F<sv.c> (C<Perl_re_dup()>) and F<regcomp.c>
130 (C<pregfree()>). This involves freeing or cloning items in the
131 regexp's data array based on the data item's type.
135 /* NOTE: There is a copy of this struct in the documentation in perlreapi.pod
136 * if you make ANY changes please make sure they are ALSO made there.
138 typedef struct regexp {
139 /*----------------------------------------------------------------------
140 * Fields required for compatibility with SV types
144 /*----------------------------------------------------------------------
148 const struct regexp_engine* engine; /* what engine created this regexp? */
149 REGEXP *mother_re; /* what re is this a lightweight copy of? */
150 HV *paren_names; /* Optional hash of paren names */
152 /*----------------------------------------------------------------------
153 * Information about the match that the perl core uses to manage things
156 /* see comment in regcomp_internal.h about branch reset to understand
157 the distinction between physical and logical capture buffers */
158 U32 nparens; /* physical number of capture buffers */
159 U32 logical_nparens; /* logical_number of capture buffers */
160 I32 *logical_to_parno; /* map logical parno to first physcial */
161 I32 *parno_to_logical; /* map every physical parno to logical */
162 I32 *parno_to_logical_next; /* map every physical parno to the next
163 physical with the same logical id */
165 SSize_t maxlen; /* maximum possible number of chars in string to match */
166 SSize_t minlen; /* minimum possible number of chars in string to match */
167 SSize_t minlenret; /* minimum possible number of chars in $& */
168 STRLEN gofs; /* chars left of pos that we search from */
169 /* substring data about strings that must appear in
170 * the final match, used for optimisations */
172 struct reg_substr_data *substrs;
174 /* private engine specific data */
176 void *pprivate; /* Data private to the regex engine which
177 * created this object. */
179 U32 extflags; /* Flags used both externally and internally */
180 U32 intflags; /* Engine Specific Internal flags */
182 /*----------------------------------------------------------------------
183 * Data about the last/current match. These are modified during matching
186 U32 lastparen; /* highest close paren matched ($+) */
187 U32 lastcloseparen; /* last close paren matched ($^N) */
188 regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
189 char **recurse_locinput; /* used to detect infinite recursion, XXX: move to internal */
191 /*---------------------------------------------------------------------- */
193 /* offset from wrapped to the start of precomp */
194 PERL_BITFIELD32 pre_prefix:4;
196 /* original flags used to compile the pattern, may differ from
197 * extflags in various ways */
198 PERL_BITFIELD32 compflags:9;
200 /*---------------------------------------------------------------------- */
202 char *subbeg; /* saved or original string so \digit works forever. */
203 SV_SAVED_COPY /* If non-NULL, SV which is COW from original */
204 SSize_t sublen; /* Length of string pointed by subbeg */
205 SSize_t suboffset; /* byte offset of subbeg from logical start of str */
206 SSize_t subcoffset; /* suboffset equiv, but in chars (for @-/@+) */
208 /*----------------------------------------------------------------------
209 * More Operational fields
212 CV *qr_anoncv; /* the anon sub wrapped round qr/(?{..})/ */
216 #define RXp_PAREN_NAMES(rx) ((rx)->paren_names)
218 #define RXp_OFFS_START(rx,n) \
219 RXp_OFFSp(rx)[(n)].start
221 #define RXp_OFFS_END(rx,n) \
222 RXp_OFFSp(rx)[(n)].end
224 #define RXp_OFFS_VALID(rx,n) \
225 (RXp_OFFSp(rx)[(n)].end != -1 && RXp_OFFSp(rx)[(n)].start != -1 )
228 #define RX_OFFS_START(rx_sv,n) RXp_OFFS_START(ReANY(rx_sv),n)
229 #define RX_OFFS_END(rx_sv,n) RXp_OFFS_END(ReANY(rx_sv),n)
230 #define RX_OFFS_VALID(rx_sv,n) RXp_OFFS_VALID(ReANY(rx_sv),n)
234 =for apidoc mh||RX_OFFSp|const REGEXP * rx_sv
235 Returns the RX_OFFS structure as a pointer. This should NOT be used
236 directly, and the structure it returns may change in future versions of
237 perl. Use one of the other RX_OFFS_xxx macros instead, which should be
238 stable as Perl evolves. The 'p' suffix on the macro is meant to indicate
239 both that the macro is "private" to the internals, and that it returns
242 =for apidoc Amh||RX_OFFS_START|const REGEXP * rx_sv|U32 n
243 Returns the start offset of the C<n>'th capture buffer, when
244 C<n> is 0 it refers to the overall match as a whole. A value of
245 -1 is returned if the C<n>'th capture buffer did not match.
247 =for apidoc Amh||RXp_OFFS_START|const regexp * rx|U32 n
248 Same as RX_OFFS_START() but operates on a C<struct regexp *> instead of
251 =for apidoc Amh||RX_OFFS_END|const REGEXP * rx_sv|U32 n
252 Returns the end offset of the C<n>'th capture buffer, when C<n> is 0 it
253 refers to the overall match as a whole. A value of
254 -1 is returned if the C<n>'th capture buffer did not match.
256 =for apidoc Amh||RXp_OFFS_END|const regexp * rx|U32 n
257 Same as RX_OFFS_END() but operates on a C<struct regexp *> instead of
260 =for apidoc Amh||RX_OFFS_VALID|const REGEXP * rx_sv|U32 n
261 Returns true if the C<n>'th offset buffer matched, false otherwise, when
262 C<n> is 0 it refers to the overall match as a whole which should always
265 =for apidoc Amh||RXp_OFFS_VALID|const regexp * rx|U32 n
266 Same as RX_OFFS_VLAID() but operates on a C<struct regexp *> instead of
273 /* used for high speed searches */
274 typedef struct re_scream_pos_data_s
276 char **scream_olds; /* match pos */
277 SSize_t *scream_pos; /* Internal iterator of scream. */
278 } re_scream_pos_data;
280 /* regexp_engine structure. This is the dispatch table for regexes.
281 * Any regex engine implementation must be able to build one of these.
283 typedef struct regexp_engine {
284 REGEXP* (*comp) (pTHX_ SV * const pattern, U32 flags);
285 I32 (*exec) (pTHX_ REGEXP * const rx, char* stringarg, char* strend,
286 char* strbeg, SSize_t minend, SV* sv,
287 void* data, U32 flags);
288 char* (*intuit) (pTHX_
291 const char * const strbeg,
295 re_scream_pos_data *data);
296 SV* (*checkstr) (pTHX_ REGEXP * const rx);
297 void (*rxfree) (pTHX_ REGEXP * const rx);
298 void (*numbered_buff_FETCH) (pTHX_ REGEXP * const rx, const I32 paren,
300 void (*numbered_buff_STORE) (pTHX_ REGEXP * const rx, const I32 paren,
301 SV const * const value);
302 I32 (*numbered_buff_LENGTH) (pTHX_ REGEXP * const rx, const SV * const sv,
304 SV* (*named_buff) (pTHX_ REGEXP * const rx, SV * const key,
305 SV * const value, const U32 flags);
306 SV* (*named_buff_iter) (pTHX_ REGEXP * const rx, const SV * const lastkey,
308 SV* (*qr_package)(pTHX_ REGEXP * const rx);
310 void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
312 REGEXP* (*op_comp) (pTHX_ SV ** const patternp, int pat_count,
313 OP *expr, const struct regexp_engine* eng,
315 bool *is_bare_re, U32 orig_rx_flags, U32 pm_flags);
319 These are passed to the numbered capture variable callbacks as the
320 paren name. >= 1 is reserved for actual numbered captures, i.e. $1,
323 # define RX_BUFF_IDX_CARET_PREMATCH -5 /* ${^PREMATCH} */
324 # define RX_BUFF_IDX_CARET_POSTMATCH -4 /* ${^POSTMATCH} */
325 # define RX_BUFF_IDX_CARET_FULLMATCH -3 /* ${^MATCH} */
326 # define RX_BUFF_IDX_PREMATCH -2 /* $` */
327 # define RX_BUFF_IDX_POSTMATCH -1 /* $' */
328 # define RX_BUFF_IDX_FULLMATCH 0 /* $& */
331 Flags that are passed to the named_buff and named_buff_iter
332 callbacks above. Those routines are called from universal.c via the
333 Tie::Hash::NamedCapture interface for %+ and %- and the re::
334 functions in the same file.
337 /* The Tie::Hash::NamedCapture operation this is part of, if any */
338 # define RXapif_FETCH 0x0001
339 # define RXapif_STORE 0x0002
340 # define RXapif_DELETE 0x0004
341 # define RXapif_CLEAR 0x0008
342 # define RXapif_EXISTS 0x0010
343 # define RXapif_SCALAR 0x0020
344 # define RXapif_FIRSTKEY 0x0040
345 # define RXapif_NEXTKEY 0x0080
347 /* Whether %+ or %- is being operated on */
348 # define RXapif_ONE 0x0100 /* %+ */
349 # define RXapif_ALL 0x0200 /* %- */
351 /* Whether this is being called from a re:: function */
352 # define RXapif_REGNAME 0x0400
353 # define RXapif_REGNAMES 0x0800
354 # define RXapif_REGNAMES_COUNT 0x1000
357 =for apidoc Am|REGEXP *|SvRX|SV *sv
359 Convenience macro to get the REGEXP from a SV. This is approximately
360 equivalent to the following snippet:
365 sv = MUTABLE_SV(SvRV(sv));
366 if (SvTYPE(sv) == SVt_REGEXP)
369 C<NULL> will be returned if a REGEXP* is not found.
371 =for apidoc Am|bool|SvRXOK|SV* sv
373 Returns a boolean indicating whether the SV (or the one it references)
376 If you want to do something with the REGEXP* later use SvRX instead
382 # define SvRX(sv) (Perl_get_re_arg(aTHX_ sv))
383 # define SvRXOK(sv) cBOOL(Perl_get_re_arg(aTHX_ sv))
386 /* Flags stored in regexp->extflags
387 * These are used by code external to the regexp engine
389 * Note that the flags whose names start with RXf_PMf_ are defined in
390 * op_reg_common.h, being copied from the parallel flags of op_pmflags
392 * NOTE: if you modify any RXf flags you should run regen.pl or
393 * regen/regcomp.pl so that regnodes.h is updated with the changes.
397 # include "op_reg_common.h"
399 # define RXf_PMf_STD_PMMOD (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_FOLD|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_NOCAPTURE)
401 # define CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, x_count) \
402 case IGNORE_PAT_MOD: *(pmfl) |= RXf_PMf_FOLD; break; \
403 case MULTILINE_PAT_MOD: *(pmfl) |= RXf_PMf_MULTILINE; break; \
404 case SINGLE_PAT_MOD: *(pmfl) |= RXf_PMf_SINGLELINE; break; \
405 case XTENDED_PAT_MOD: if (x_count == 0) { \
406 *(pmfl) |= RXf_PMf_EXTENDED; \
407 *(pmfl) &= ~RXf_PMf_EXTENDED_MORE; \
410 *(pmfl) |= RXf_PMf_EXTENDED \
411 |RXf_PMf_EXTENDED_MORE; \
413 (x_count)++; break; \
414 case NOCAPTURE_PAT_MOD: *(pmfl) |= RXf_PMf_NOCAPTURE; break;
416 /* Note, includes charset ones, assumes 0 is the default for them */
417 # define STD_PMMOD_FLAGS_CLEAR(pmfl) \
418 *(pmfl) &= ~(RXf_PMf_FOLD|RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_CHARSET|RXf_PMf_NOCAPTURE)
420 /* chars and strings used as regex pattern modifiers
421 * Singular is a 'c'har, plural is a "string"
423 * NOTE, KEEPCOPY was originally 'k', but was changed to 'p' for preserve
424 * for compatibility reasons with Regexp::Common which highjacked (?k:...)
425 * for its own uses. So 'k' is out as well.
427 # define DEFAULT_PAT_MOD '^' /* Short for all the default modifiers */
428 # define EXEC_PAT_MOD 'e'
429 # define KEEPCOPY_PAT_MOD 'p'
430 # define NOCAPTURE_PAT_MOD 'n'
431 # define ONCE_PAT_MOD 'o'
432 # define GLOBAL_PAT_MOD 'g'
433 # define CONTINUE_PAT_MOD 'c'
434 # define MULTILINE_PAT_MOD 'm'
435 # define SINGLE_PAT_MOD 's'
436 # define IGNORE_PAT_MOD 'i'
437 # define XTENDED_PAT_MOD 'x'
438 # define NONDESTRUCT_PAT_MOD 'r'
439 # define LOCALE_PAT_MOD 'l'
440 # define UNICODE_PAT_MOD 'u'
441 # define DEPENDS_PAT_MOD 'd'
442 # define ASCII_RESTRICT_PAT_MOD 'a'
444 # define ONCE_PAT_MODS "o"
445 # define KEEPCOPY_PAT_MODS "p"
446 # define NOCAPTURE_PAT_MODS "n"
447 # define EXEC_PAT_MODS "e"
448 # define LOOP_PAT_MODS "gc"
449 # define NONDESTRUCT_PAT_MODS "r"
450 # define LOCALE_PAT_MODS "l"
451 # define UNICODE_PAT_MODS "u"
452 # define DEPENDS_PAT_MODS "d"
453 # define ASCII_RESTRICT_PAT_MODS "a"
454 # define ASCII_MORE_RESTRICT_PAT_MODS "aa"
456 /* This string is expected by regcomp.c to be ordered so that the first
457 * character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of extflags; the next
458 * character is bit +1, etc. */
459 # define STD_PAT_MODS "msixxn"
461 # define CHARSET_PAT_MODS ASCII_RESTRICT_PAT_MODS DEPENDS_PAT_MODS LOCALE_PAT_MODS UNICODE_PAT_MODS
463 /* This string is expected by XS_re_regexp_pattern() in universal.c to be ordered
464 * so that the first character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of
465 * extflags; the next character is in bit +1, etc. */
466 # define INT_PAT_MODS STD_PAT_MODS KEEPCOPY_PAT_MODS
468 # define EXT_PAT_MODS ONCE_PAT_MODS KEEPCOPY_PAT_MODS NOCAPTURE_PAT_MODS
469 # define QR_PAT_MODS STD_PAT_MODS EXT_PAT_MODS CHARSET_PAT_MODS
470 # define M_PAT_MODS QR_PAT_MODS LOOP_PAT_MODS
471 # define S_PAT_MODS M_PAT_MODS EXEC_PAT_MODS NONDESTRUCT_PAT_MODS
474 * NOTE: if you modify any RXf flags you should run regen.pl or
475 * regen/regcomp.pl so that regnodes.h is updated with the changes.
480 Set in Perl_pmruntime for a split. Will be used by regex engines to
481 check whether they should set RXf_SKIPWHITE
483 # define RXf_SPLIT RXf_PMf_SPLIT
485 /* Currently the regex flags occupy a single 32-bit word. Not all bits are
486 * currently used. The lower bits are shared with their corresponding PMf flag
487 * bits, up to but not including _RXf_PMf_SHIFT_NEXT. The unused bits
488 * immediately follow; finally the used RXf-only (unshared) bits, so that the
489 * highest bit in the word is used. This gathers all the unused bits as a pool
490 * in the middle, like so: 11111111111111110000001111111111
491 * where the '1's represent used bits, and the '0's unused. This design allows
492 * us to allocate off one end of the pool if we need to add a shared bit, and
493 * off the other end if we need a non-shared bit, without disturbing the other
494 * bits. This maximizes the likelihood of being able to change things without
495 * breaking binary compatibility.
497 * To add shared bits, do so in op_reg_common.h. This should change
498 * _RXf_PMf_SHIFT_NEXT so that things won't compile. Then come to regexp.h and
499 * op.h and adjust the constant adders in the definitions of RXf_BASE_SHIFT and
500 * Pmf_BASE_SHIFT down by the number of shared bits you added. That's it.
501 * Things should be binary compatible. But if either of these gets to having
502 * to subtract rather than add, leave at 0 and instead adjust all the entries
503 * that are in terms of it. But if the first one of those is already
504 * RXf_BASE_SHIFT+0, there are no bits left, and a redesign is in order.
506 * To remove unshared bits, just delete its entry. If you're where breaking
507 * binary compatibility is ok to do, you might want to adjust things to move
508 * the newly opened space so that it gets absorbed into the common pool.
510 * To add unshared bits, first use up any gaps in the middle. Otherwise,
511 * allocate off the low end until you get to RXf_BASE_SHIFT+0. If that isn't
512 * enough, move RXf_BASE_SHIFT down (if possible) and add the new bit at the
513 * other end instead; this preserves binary compatibility.
515 * For the regexp bits, PL_reg_extflags_name[] in regnodes.h has a comment
516 * giving which bits are used/unused */
518 # define RXf_BASE_SHIFT (_RXf_PMf_SHIFT_NEXT + 2)
520 /* What we have seen */
521 # define RXf_NO_INPLACE_SUBST (1U<<(RXf_BASE_SHIFT+2))
522 # define RXf_EVAL_SEEN (1U<<(RXf_BASE_SHIFT+3))
525 # define RXf_UNBOUNDED_QUANTIFIER_SEEN (1U<<(RXf_BASE_SHIFT+4))
526 # define RXf_CHECK_ALL (1U<<(RXf_BASE_SHIFT+5))
529 # define RXf_MATCH_UTF8 (1U<<(RXf_BASE_SHIFT+6)) /* $1 etc are utf8 */
532 # define RXf_USE_INTUIT_NOML (1U<<(RXf_BASE_SHIFT+7))
533 # define RXf_USE_INTUIT_ML (1U<<(RXf_BASE_SHIFT+8))
534 # define RXf_INTUIT_TAIL (1U<<(RXf_BASE_SHIFT+9))
535 # define RXf_USE_INTUIT (RXf_USE_INTUIT_NOML|RXf_USE_INTUIT_ML)
537 /* Do we have some sort of anchor? */
538 # define RXf_IS_ANCHORED (1U<<(RXf_BASE_SHIFT+10))
540 /* Copy and tainted info */
541 # define RXf_COPY_DONE (1U<<(RXf_BASE_SHIFT+11))
543 /* post-execution: $1 et al are tainted */
544 # define RXf_TAINTED_SEEN (1U<<(RXf_BASE_SHIFT+12))
545 /* this pattern was tainted during compilation */
546 # define RXf_TAINTED (1U<<(RXf_BASE_SHIFT+13))
548 /* Flags indicating special patterns */
549 # define RXf_START_ONLY (1U<<(RXf_BASE_SHIFT+14)) /* Pattern is /^/ */
550 # define RXf_SKIPWHITE (1U<<(RXf_BASE_SHIFT+15)) /* Pattern is for a */
552 # define RXf_WHITE (1U<<(RXf_BASE_SHIFT+16)) /* Pattern is /\s+/ */
553 # define RXf_NULL (1U<<(RXf_BASE_SHIFT+17)) /* Pattern is // */
555 /* See comments at the beginning of these defines about adding bits. The
556 * highest bit position should be used, so that if RXf_BASE_SHIFT gets
557 * increased, the #error below will be triggered so that you will be reminded
558 * to adjust things at the other end to keep the bit positions unchanged */
559 # if RXf_BASE_SHIFT+17 > 31
560 # error Too many RXf_PMf bits used. See comments at beginning of these for what to do
564 * NOTE: if you modify any RXf flags you should run regen.pl or
565 * regen/regcomp.pl so that regnodes.h is updated with the changes.
569 # ifdef NO_TAINT_SUPPORT
570 # define RX_ISTAINTED(rx_sv) 0
571 # define RXp_ISTAINTED(prog) 0
572 # define RX_TAINT_on(rx_sv) NOOP
573 # define RXp_MATCH_TAINTED(prog) 0
574 # define RX_MATCH_TAINTED(rx_sv) 0
575 # define RXp_MATCH_TAINTED_on(prog) NOOP
576 # define RX_MATCH_TAINTED_on(rx_sv) NOOP
577 # define RXp_MATCH_TAINTED_off(prog) NOOP
578 # define RX_MATCH_TAINTED_off(rx_sv) NOOP
580 # define RX_ISTAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED)
581 # define RXp_ISTAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED)
582 # define RX_TAINT_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED)
583 # define RXp_MATCH_TAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED_SEEN)
584 # define RX_MATCH_TAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED_SEEN)
585 # define RXp_MATCH_TAINTED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_TAINTED_SEEN)
586 # define RX_MATCH_TAINTED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED_SEEN)
587 # define RXp_MATCH_TAINTED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_TAINTED_SEEN)
588 # define RX_MATCH_TAINTED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_TAINTED_SEEN)
591 # define RXp_HAS_CUTGROUP(prog) ((prog)->intflags & PREGf_CUTGROUP_SEEN)
593 # define RX_MATCH_TAINTED_set(rx_sv, t) ((t) \
594 ? RX_MATCH_TAINTED_on(rx_sv) \
595 : RX_MATCH_TAINTED_off(rx_sv))
597 # define RXp_MATCH_COPIED(prog) (RXp_EXTFLAGS(prog) & RXf_COPY_DONE)
598 # define RX_MATCH_COPIED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_COPY_DONE)
599 # define RXp_MATCH_COPIED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_COPY_DONE)
600 # define RX_MATCH_COPIED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_COPY_DONE)
601 # define RXp_MATCH_COPIED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_COPY_DONE)
602 # define RX_MATCH_COPIED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_COPY_DONE)
603 # define RX_MATCH_COPIED_set(rx_sv,t) ((t) \
604 ? RX_MATCH_COPIED_on(rx_sv) \
605 : RX_MATCH_COPIED_off(rx_sv))
607 # define RXp_EXTFLAGS(rx) ((rx)->extflags)
608 # define RXp_COMPFLAGS(rx) ((rx)->compflags)
610 /* For source compatibility. We used to store these explicitly. */
611 # define RX_PRECOMP(rx_sv) (RX_WRAPPED(rx_sv) \
612 + ReANY(rx_sv)->pre_prefix)
613 # define RX_PRECOMP_const(rx_sv) (RX_WRAPPED_const(rx_sv) \
614 + ReANY(rx_sv)->pre_prefix)
615 /* FIXME? Are we hardcoding too much here and constraining plugin extension
616 writers? Specifically, the value 1 assumes that the wrapped version always
617 has exactly one character at the end, a ')'. Will that always be true? */
618 # define RX_PRELEN(rx_sv) (RX_WRAPLEN(rx_sv) \
619 - ReANY(rx_sv)->pre_prefix - 1)
621 # define RX_WRAPPED(rx_sv) SvPVX(rx_sv)
622 # define RX_WRAPPED_const(rx_sv) SvPVX_const(rx_sv)
623 # define RX_WRAPLEN(rx_sv) SvCUR(rx_sv)
624 # define RX_CHECK_SUBSTR(rx_sv) (ReANY(rx_sv)->check_substr)
625 # define RX_REFCNT(rx_sv) SvREFCNT(rx_sv)
626 # define RX_EXTFLAGS(rx_sv) RXp_EXTFLAGS(ReANY(rx_sv))
627 # define RX_COMPFLAGS(rx_sv) RXp_COMPFLAGS(ReANY(rx_sv))
628 # define RXp_ENGINE(prog) ((prog)->engine)
629 # define RX_ENGINE(rx_sv) (RXp_ENGINE(ReANY(rx_sv)))
630 # define RXp_SUBBEG(prog) ((prog)->subbeg)
631 # define RX_SUBBEG(rx_sv) (RXp_SUBBEG(ReANY(rx_sv)))
632 # define RXp_SUBOFFSET(prog) ((prog)->suboffset)
633 # define RX_SUBOFFSET(rx_sv) (RXp_SUBOFFSET(ReANY(rx_sv)))
634 # define RXp_SUBCOFFSET(prog) ((prog)->subcoffset)
635 # define RX_SUBCOFFSET(rx_sv) (RXp_SUBCOFFSET(ReANY(rx_sv)))
636 # define RXp_OFFSp(prog) ((prog)->offs)
637 # define RX_OFFSp(rx_sv) (RXp_OFFSp(ReANY(rx_sv)))
638 # define RXp_LOGICAL_NPARENS(prog) ((prog)->logical_nparens)
639 # define RX_LOGICAL_NPARENS(rx_sv) (RXp_LOGICAL_NPARENS(ReANY(rx_sv)))
640 # define RXp_LOGICAL_TO_PARNO(prog) ((prog)->logical_to_parno)
641 # define RX_LOGICAL_TO_PARNO(rx_sv) (RXp_LOGICAL_TO_PARNO(ReANY(rx_sv)))
642 # define RXp_PARNO_TO_LOGICAL(prog) ((prog)->parno_to_logical)
643 # define RX_PARNO_TO_LOGICAL(rx_sv) (RXp_PARNO_TO_LOGICAL(ReANY(rx_sv)))
644 # define RXp_PARNO_TO_LOGICAL_NEXT(prog) ((prog)->parno_to_logical_next)
645 # define RX_PARNO_TO_LOGICAL_NEXT(rx_sv) (RXp_PARNO_TO_LOGICAL_NEXT(ReANY(rx_sv)))
646 # define RXp_NPARENS(prog) ((prog)->nparens)
647 # define RX_NPARENS(rx_sv) (RXp_NPARENS(ReANY(rx_sv)))
648 # define RXp_SUBLEN(prog) ((prog)->sublen)
649 # define RX_SUBLEN(rx_sv) (RXp_SUBLEN(ReANY(rx_sv)))
650 # define RXp_MINLEN(prog) ((prog)->minlen)
651 # define RX_MINLEN(rx_sv) (RXp_MINLEN(ReANY(rx_sv)))
652 # define RXp_MINLENRET(prog) ((prog)->minlenret)
653 # define RX_MINLENRET(rx_sv) (RXp_MINLENRET(ReANY(rx_sv)))
654 # define RXp_GOFS(prog) ((prog)->gofs)
655 # define RX_GOFS(rx_sv) (RXp_GOFS(ReANY(rx_sv)))
656 # define RXp_LASTPAREN(prog) ((prog)->lastparen)
657 # define RX_LASTPAREN(rx_sv) (RXp_LASTPAREN(ReANY(rx_sv)))
658 # define RXp_LASTCLOSEPAREN(prog) ((prog)->lastcloseparen)
659 # define RX_LASTCLOSEPAREN(rx_sv) (RXp_LASTCLOSEPAREN(ReANY(rx_sv)))
660 # define RXp_SAVED_COPY(prog) ((prog)->saved_copy)
661 # define RX_SAVED_COPY(rx_sv) (RXp_SAVED_COPY(ReANY(rx_sv)))
662 # define RXp_SUBSTRS(prog) ((prog)->substrs)
663 # define RX_SUBSTRS(rx_sv) (RXp_SUBSTRS(ReANY(rx_sv)))
664 # define RXp_PPRIVATE(prog) ((prog)->pprivate)
665 # define RX_PPRIVATE(rx_sv) (RXp_PPRIVATE(ReANY(rx_sv)))
666 # define RXp_QR_ANONCV(prog) ((prog)->qr_anoncv)
667 # define RX_QR_ANONCV(rx_sv) (RXp_QR_ANONCV(ReANY(rx_sv)))
668 # define RXp_MOTHER_RE(prog) ((prog)->mother_re)
669 # define RX_MOTHER_RE(rx_sv) (RXp_MOTHER_RE(ReANY(rx_sv)))
670 # define RXp_PRE_PREFIX(prog) ((prog)->pre_prefix)
671 # define RX_PRE_PREFIX(rx_sv) (RXp_PRE_PREFIX(ReANY(rx_sv)))
673 /* last match was zero-length */
674 # define RXp_ZERO_LEN(prog) \
675 (RXp_OFFS_START(prog,0) + (SSize_t)RXp_GOFS(prog) \
676 == RXp_OFFS_END(prog,0))
677 # define RX_ZERO_LEN(rx_sv) (RXp_ZERO_LEN(ReANY(rx_sv)))
679 #endif /* PLUGGABLE_RE_EXTENSION */
681 /* Stuff that needs to be included in the pluggable extension goes below here */
683 #include "regexp_constants.h"
686 # define RXp_MATCH_COPY_FREE(prog) \
688 if (RXp_SAVED_COPY(prog)) { \
689 SV_CHECK_THINKFIRST_COW_DROP(RXp_SAVED_COPY(prog)); \
691 if (RXp_MATCH_COPIED(prog)) { \
692 Safefree(RXp_SUBBEG(prog)); \
693 RXp_MATCH_COPIED_off(prog); \
697 # define RXp_MATCH_COPY_FREE(prog) \
699 if (RXp_MATCH_COPIED(prog)) { \
700 Safefree(RXp_SUBBEG(prog)); \
701 RXp_MATCH_COPIED_off(prog); \
705 #define RX_MATCH_COPY_FREE(rx_sv) RXp_MATCH_COPY_FREE(ReANY(rx_sv))
707 #define RXp_MATCH_UTF8(prog) (RXp_EXTFLAGS(prog) & RXf_MATCH_UTF8)
708 #define RX_MATCH_UTF8(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_MATCH_UTF8)
709 #define RXp_MATCH_UTF8_on(prog) (RXp_EXTFLAGS(prog) |= RXf_MATCH_UTF8)
710 #define RX_MATCH_UTF8_on(rx_sv) (RXp_MATCH_UTF8_on(ReANY(rx_sv)))
711 #define RXp_MATCH_UTF8_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_MATCH_UTF8)
712 #define RX_MATCH_UTF8_off(rx_sv) (RXp_MATCH_UTF8_off(ReANY(rx_sv))
713 #define RXp_MATCH_UTF8_set(prog, t) ((t) \
714 ? RXp_MATCH_UTF8_on(prog) \
715 : RXp_MATCH_UTF8_off(prog))
716 #define RX_MATCH_UTF8_set(rx_sv, t) (RXp_MATCH_UTF8_set(ReANY(rx_sv), t))
718 /* Whether the pattern stored at RX_WRAPPED is in UTF-8 */
719 #define RX_UTF8(rx_sv) SvUTF8(rx_sv)
722 /* bits in flags arg of Perl_regexec_flags() */
724 #define REXEC_COPY_STR 0x01 /* Need to copy the string for captures. */
725 #define REXEC_CHECKED 0x02 /* re_intuit_start() already called. */
726 #define REXEC_SCREAM 0x04 /* currently unused. */
727 #define REXEC_IGNOREPOS 0x08 /* use stringarg, not pos(), for \G match */
728 #define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g:
729 no need to copy string again */
731 /* under REXEC_COPY_STR, it's ok for the
732 engine (modulo PL_sawamperand etc)
733 to skip copying: ... */
734 #define REXEC_COPY_SKIP_PRE 0x20 /* ...the $` part of the string, or */
735 #define REXEC_COPY_SKIP_POST 0x40 /* ...the $' part of the string */
736 #define REXEC_FAIL_ON_UNDERFLOW 0x80 /* fail the match if $& would start before
737 the start pos (so s/.\G// would fail
738 on second iteration */
740 #if defined(PERL_USE_GCC_BRACE_GROUPS)
741 # define ReREFCNT_inc(re) \
743 /* This is here to generate a casting warning if incorrect. */ \
744 REGEXP *const _rerefcnt_inc = (re); \
745 assert(SvTYPE(_rerefcnt_inc) == SVt_REGEXP); \
746 SvREFCNT_inc(_rerefcnt_inc); \
749 # define ReREFCNT_dec(re) \
751 /* This is here to generate a casting warning if incorrect. */ \
752 REGEXP *const _rerefcnt_dec = (re); \
753 SvREFCNT_dec(_rerefcnt_dec); \
756 # define ReREFCNT_dec(re) SvREFCNT_dec(re)
757 # define ReREFCNT_inc(re) ((REGEXP *) SvREFCNT_inc(re))
759 #define ReANY(re) Perl_ReANY((const REGEXP *)(re))
761 /* FIXME for plugins. */
763 #define FBMcf_TAIL_DOLLAR 1
764 #define FBMcf_TAIL_DOLLARM 2
765 #define FBMcf_TAIL_Z 4
766 #define FBMcf_TAIL_z 8
767 #define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
769 #define FBMrf_MULTILINE 1
771 struct regmatch_state;
772 struct regmatch_slab;
774 /* like regmatch_info_aux, but contains extra fields only needed if the
775 * pattern contains (?{}). If used, is snuck into the second slot in the
776 * regmatch_state stack at the start of execution */
780 PMOP *old_op; /* saved value of PL_op and ... */
781 REGEXP *old_op_val; /* ... saved value of PM_GETRE(PL_op) if any */
782 REGEXP *old_regcurpm_val; /* saved value of PM_GETRE(PL_reg_curpm) */
783 PMOP *curpm; /* saved PL_curpm */
785 SV *saved_copy; /* saved saved_copy field from rex */
787 char *subbeg; /* saved subbeg field from rex */
788 STRLEN sublen; /* saved sublen field from rex */
789 STRLEN suboffset; /* saved suboffset field from rex */
790 STRLEN subcoffset; /* saved subcoffset field from rex */
791 SV *sv; /* $_ during (?{}) */
792 MAGIC *pos_magic; /* pos() magic attached to $_ */
793 SSize_t pos; /* the original value of pos() in pos_magic */
794 U8 pos_flags; /* flags to be restored; currently only MGf_BYTES*/
795 } regmatch_info_aux_eval;
798 /* fields that logically live in regmatch_info, but which need cleaning
799 * up on croak(), and so are instead are snuck into the first slot in
800 * the regmatch_state stack at the start of execution */
803 regmatch_info_aux_eval *info_aux_eval;
804 struct regmatch_state *old_regmatch_state; /* saved PL_regmatch_state */
805 struct regmatch_slab *old_regmatch_slab; /* saved PL_regmatch_slab */
806 char *poscache; /* S-L cache of fail positions of WHILEMs */
811 =for apidoc Ay||regmatch_info
812 Some basic information about the current match that is created by
813 Perl_regexec_flags and then passed to regtry(), regmatch() etc.
814 It is allocated as a local var on the stack, so nothing should be
815 stored in it that needs preserving or clearing up on croak().
816 For that, see the aux_info and aux_info_eval members of the
817 regmatch_state union.
823 REGEXP *prog; /* the regex being executed */
824 const char * strbeg; /* real start of string */
825 char *strend; /* one byte beyond last char of match string */
826 char *till; /* matches shorter than this fail (see minlen arg) */
827 SV *sv; /* the SV string currently being matched */
828 char *ganch; /* position of \G anchor */
829 char *cutpoint; /* (*COMMIT) position (if any) */
830 regmatch_info_aux *info_aux; /* extra fields that need cleanup */
831 regmatch_info_aux_eval *info_aux_eval; /* extra saved state for (?{}) */
832 I32 poscache_maxiter; /* how many whilems todo before S-L cache kicks in */
833 I32 poscache_iter; /* current countdown from _maxiter to zero */
834 STRLEN poscache_size; /* size of regmatch_info_aux.poscache */
835 bool intuit; /* re_intuit_start() is the top-level caller */
836 bool is_utf8_pat; /* regex is utf8 */
837 bool is_utf8_target; /* string being matched is utf8 */
838 bool warned; /* we have issued a recursion warning; no need for more */
842 /* structures for holding and saving the state maintained by regmatch() */
844 #ifndef MAX_RECURSE_EVAL_NOCHANGE_DEPTH
845 # define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 10
848 /* The +1 is because everything matches itself, which isn't included in
849 * MAX_FOLD_FROMS; the +2 is based on the current Unicode standards needs, and
850 * is unlikely to change. An assertion should fail in regexec.c if it is too
851 * low. It is needed for certain edge cases involving multi-character folds
852 * when the first component also participates in a fold individually. */
853 #define MAX_MATCHES (MAX_FOLD_FROMS + 1 + 2)
855 struct next_matchable_info {
860 PERL_INT_FAST8_T count; /* Negative means not initialized */
861 PERL_UINT_FAST8_T min_length;
862 PERL_UINT_FAST8_T max_length;
863 PERL_UINT_FAST8_T initial_definitive;
864 PERL_UINT_FAST8_T initial_exact;
865 PERL_UINT_FAST8_T lengths[MAX_MATCHES];
867 /* The size is from trial and error, and could change with new Unicode
868 * standards, in which case there is an assertion that should start
869 * failing. This size could be calculated in one of the regen scripts
870 * dealing with Unicode, but khw thinks the likelihood of it changing is
871 * low enough that it isn't worth the effort. */
875 typedef I32 CHECKPOINT;
877 typedef struct regmatch_state {
878 int resume_state; /* where to jump to on return */
879 char *locinput; /* where to backtrack in string on failure */
881 U8 *sr0; /* position of start of script run, or NULL */
885 /* the 'info_aux' and 'info_aux_eval' union members are cuckoos in
886 * the nest. They aren't saved backtrack state; rather they
887 * represent one or two extra chunks of data that need allocating
888 * at the start of a match. These fields would logically live in
889 * the regmatch_info struct, except that is allocated on the
890 * C stack, and these fields are all things that require cleanup
891 * after a croak(), when the stack is lost.
892 * As a convenience, we just use the first 1 or 2 regmatch_state
893 * slots to store this info, as we will be allocating a slab of
894 * these anyway. Otherwise we'd have to malloc and then free them,
895 * or allocate them on the save stack (where they will get
896 * realloced if the save stack grows).
897 * info_aux contains the extra fields that are always needed;
898 * info_aux_eval contains extra fields that only needed if
899 * the pattern contains code blocks
900 * We split them into two separate structs to avoid increasing
901 * the size of the union.
904 regmatch_info_aux info_aux;
906 regmatch_info_aux_eval info_aux_eval;
908 /* this is a fake union member that matches the first element
909 * of each member that needs to store positive backtrack
912 struct regmatch_state *prev_yes_state;
916 /* NOTE: Regarding 'cp' and 'lastcp' in the following structs...
918 * In the majority of cases we use 'cp' for the "normal"
919 * checkpoint for paren saves, and 'lastcp' for the addtional
920 * paren saves that are done only under RE_PESSIMISTIC_PARENS.
922 * There may be a few cases where both are used always.
923 * Regardless they tend be used something like this:
925 * ST.cp = regcppush(rex, 0, maxopenparen);
926 * REGCP_SET(ST.lastcp);
928 * thus ST.cp holds the checkpoint from before we push parens,
929 * and ST.lastcp holds the checkpoint from afterwards.
932 /* branchlike members */
933 /* this is a fake union member that matches the first elements
934 * of each member that needs to behave like a branch */
936 /* this first element must match u.yes */
937 struct regmatch_state *prev_yes_state;
940 CHECKPOINT cp; /* see note above "struct branchlike" */
941 CHECKPOINT lastcp; /* see note above "struct branchlike" */
948 /* the first elements must match u.branchlike */
949 struct regmatch_state *prev_yes_state;
952 CHECKPOINT cp; /* see note above "struct branchlike" */
953 CHECKPOINT lastcp; /* see note above "struct branchlike" */
957 regnode *next_branch; /* next branch node */
961 /* the first elements must match u.branchlike */
962 struct regmatch_state *prev_yes_state;
965 CHECKPOINT cp; /* see note above "struct branchlike" */
966 CHECKPOINT lastcp; /* see note above "struct branchlike" */
970 U32 accepted; /* how many accepting states left */
971 bool longfold; /* saw a fold with a 1->n char mapping */
972 U16 *jump; /* positive offsets from me */
975 regnode *me; /* Which node am I - needed for jump tries*/
976 U8 *firstpos; /* pos in string of first trie match */
977 U32 firstchars; /* len in chars of firstpos from start */
978 U16 nextword; /* next word to try */
979 U16 topword; /* longest accepted word */
982 /* special types - these members are used to store state for special
983 regops like eval, if/then, lookaround and the markpoint state */
985 /* this first element must match u.yes */
986 struct regmatch_state *prev_yes_state;
987 struct regmatch_state *prev_curlyx;
988 struct regmatch_state *prev_eval;
990 CHECKPOINT cp; /* see note above "struct branchlike" */
991 CHECKPOINT lastcp; /* see note above "struct branchlike" */
992 U32 close_paren; /* which close bracket is our end (+1) */
993 regnode *B; /* the node following us */
994 char *prev_recurse_locinput;
998 /* this first element must match u.yes */
999 struct regmatch_state *prev_yes_state;
1001 I32 logical; /* saved copy of 'logical' var */
1002 U8 count; /* number of beginning positions */
1005 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
1006 char *prev_match_end;
1007 } ifmatch; /* and SUSPEND/UNLESSM */
1010 /* this first element must match u.yes */
1011 struct regmatch_state *prev_yes_state;
1012 struct regmatch_state *prev_mark;
1021 /* quantifiers - these members are used for storing state for
1022 the regops used to implement quantifiers */
1024 /* this first element must match u.yes */
1025 struct regmatch_state *prev_yes_state;
1026 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
1027 regnode *me; /* the CURLYX node */
1028 regnode *B; /* the B node in /A*B/ */
1029 CHECKPOINT cp; /* see note above "struct branchlike" */
1030 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1032 int parenfloor; /* how far back to strip paren data */
1034 /* these two are modified by WHILEM */
1035 int count; /* how many instances of A we've matched */
1036 char *lastloc; /* where previous A matched (0-len detect) */
1040 /* this first element must match u.yes */
1041 struct regmatch_state *prev_yes_state;
1042 struct regmatch_state *save_curlyx;
1043 CHECKPOINT cp; /* see note above "struct branchlike" */
1044 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1045 char *save_lastloc; /* previous curlyx.lastloc */
1051 /* this first element must match u.yes */
1052 struct regmatch_state *prev_yes_state;
1055 CHECKPOINT cp; /* see note above "struct branchlike" */
1056 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1057 I32 alen; /* length of first-matched A string */
1060 regnode *A, *B; /* the nodes corresponding to /A*B/ */
1061 regnode *me; /* the curlym node */
1062 struct next_matchable_info Binfo;
1069 CHECKPOINT cp; /* see note above "struct branchlike" */
1070 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1071 char *maxpos; /* highest possible point in string to match */
1072 char *oldloc; /* the previous locinput */
1074 int min, max; /* {m,n} */
1075 regnode *A, *B; /* the nodes corresponding to /A*B/ */
1076 struct next_matchable_info Binfo;
1077 } curly; /* and CURLYN/PLUS/STAR */
1082 } backref; /* REF and friends */
1088 /* how many regmatch_state structs to allocate as a single slab.
1089 * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
1090 * pointers, plus 1 for any mythical malloc overhead. */
1092 #define PERL_REGMATCH_SLAB_SLOTS \
1093 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
1095 typedef struct regmatch_slab {
1096 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
1097 struct regmatch_slab *prev, *next;
1101 #define REG_FETCH_ABSOLUTE 1
1104 * ex: set ts=8 sts=4 sw=4 et: